added grey highlight for bad libraries (by tag text)
[htsworkflow.git] / htswfrontend / htswfrontend / htsw_reports / reports.py
1 from htswfrontend.exp_track.models import *
2 from django.http import HttpResponse
3 from django.core.exceptions import ObjectDoesNotExist
4 from django.shortcuts import render_to_response, get_object_or_404
5
6 badLibBgColor = '#cccccc'
7 def getBgColor(reads_cnt,exp_type,isBadLib):
8   if isBadLib: return badLibBgColor
9   # Color Scheme: green is more than 12M, blue is more than 5M, orange is more than 3M and red is less. For RNAseq, all those thresholds are ~ double
10   bgcolor = '#ffcdd0'  # Red is the color for minimum read counts                                                                                                                            
11   rc_thr = [12000000,5000000,3000000] # Default for ChIP-Seq and Methyl-Seq
12   if exp_type == 'RNA-seq':
13     rc_thr = [20000000,10000000,6000000]
14
15   if reads_cnt > rc_thr[0]:
16     bgcolor = '#66ff66'  # Green                                                                                                                                                                                                                                               
17   else:
18     if reads_cnt > rc_thr[1]:
19       bgcolor ='#00ccff'  # Blue                                                                                                                                                                                                                                               
20     else:
21        if reads_cnt > rc_thr[2]:
22          bgcolor ='#ffcc33'  # Orange                                                                                                                                                                                                                                          
23   #tstr = '<div style="background-color:'+bgcolor+';color:black">'
24   #tstr += res[0].__str__()+' Lanes, '+rc+' M Reads'
25   #tstr += '</div>'
26
27   return bgcolor
28
29 def report1(request):
30   if not request.user.is_authenticated():
31     str= '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">Please log in first</a></span>'
32     return render_to_response('htsw_reports/report.html',{'main':str})
33
34   EXP = 'ChIP-seq'
35
36   if request.has_key('aflid'):
37     AFL_Id = request['aflid']
38     try:
39       AFL = Affiliation.objects.get(id=AFL_Id).name
40       AFL_CNT = Affiliation.objects.get(id=AFL_Id).contact
41     except ObjectDoesNotExist:
42       return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL_Id+"'")
43   else:
44     AFL = 'ENCODE_Tier1'
45     AFL_CNT = ''
46     try:
47       AFL_Id = Affiliation.objects.get(name=AFL,contact=AFL_CNT).id.__str__()
48     except ObjectDoesNotExist:
49       return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL+"'")
50
51   if request.user.username == 'caltech':
52     if AFL.find('ENCODE') < 0:  return HttpResponse('Record Not Found <div style="margin-right:20px"><a target=_self href="/admin" target=_self>HOME</a></div>')
53   
54   TFall = Library.objects.values('antibody').order_by('antibody').distinct()
55   CLLall = Library.objects.values('cell_line').order_by('cell_line').distinct()
56
57   TFs = TFall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT)
58   CLLs = CLLall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT)
59
60   # Check Replicate numbers
61   Reps = 1
62   RepRecs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT).order_by('-replicate')
63   if len(RepRecs) > 0: Reps = RepRecs[0].replicate
64   
65   ########
66   str = ''
67   str += '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">HOME</a></span>'
68   ##str += '<span style="margin-right:20px">Max Replicates: '+MaxRep.replicate.__str__()+'</span>'
69   str += '<span>Select another <b>'+EXP+'</b> Report:</span>  <select onchange="window.location=\'/htsw_reports/report?aflid=\'+this.options[selectedIndex].value; return false;">'
70
71   if request.user.username == 'caltech':
72     Affil_List = Affiliation.objects.filter(name__contains='ENCODE').distinct()
73   else:
74     Affil_List = Affiliation.objects.distinct()
75     
76   for af in Affil_List:
77     str += '<option value='+af.id.__str__()
78     if AFL_Id == af.id.__str__():
79       str += ' selected'
80     str += '>'+af.name+' '+af.contact+'</option>'
81   str += '</select>'
82
83   str += '<span style="margin-left:20px;padding:1px;border:solid #cccccc 1px">color scheme: <span style="margin-left:5px;background-color:#66ff66"> > 12 M</span><span style="margin-left:5px;background-color:#00ccff"> >  5 M</span><span style="margin-left:5px;background-color:#ffcc33"> > 3 M</span><span style="margin-left:5px;background-color:#ffcdd0"> < 3 M</span><span style="margin-left:5px;background-color:'+badLibBgColor+'">Bad Library</span></span>'
84
85   str += '<span style="margin-left:20px">'
86   str += '<u>Switch to:</u> '+AFL+' '+AFL_CNT+' <a target=_self href="/htsw_reports/report_RM?exp=RNA-seq&aflid='+AFL_Id+'"><b>RNA-Seq</b> Report</a>'
87   str += '  | '
88   str += '<a target=_self href="/htsw_reports/report_RM?exp=Methyl-seq&aflid='+AFL_Id+'"><b>Methyl-Seq</b> Report</a>'
89
90   bgc = '#ffffff' 
91   pbgc = '#f7f7f7'
92   str += '<br/><br/><table border=1>'
93   str += '<tr><th style="text-align:right">PROJECT</th><th colspan='+(Reps*len(CLLs)).__str__()+' style="text-align:center">'+AFL+' '+AFL_CNT+' <span style="font-size:140%">'+EXP+'</span></th></tr>'
94   str += '<tr><th style="text-align:right">CELL LINE</th>'
95   for H in CLLs: 
96     str += '<th colspan='+Reps.__str__()+' style="text-align:center;background-color:'+bgc+'">'+Cellline.objects.get(id=H['cell_line']).cellline_name+'</th>'
97     tbgc = bgc
98     bgc = pbgc
99     pbgc = tbgc 
100   str += '</tr><tr><th style="text-align:left">TF</th>'
101   bgc = '#ffffff'
102   pbgc = '#f7f7f7'
103   for H in CLLs:
104     for r in range(1,Reps+1):
105       str += '<th style="text-align:center;background-color:'+bgc+'">Rep. '+r.__str__()+'</th>'
106     tbgc = bgc
107     bgc = pbgc
108     pbgc = tbgc
109   str += '</tr>'
110   str += '<tr><td align=right><a title="View Libraries Records" target=_self href=/admin/fctracker/library/?affiliations__id__exact='+AFL_Id+'&experiment_type__exact=INPUT_RXLCh>Total Chromatin</a></td>'
111   bgc = '#ffffff'
112   pbgc = '#f7f7f7'
113   for H in CLLs:
114     for r in range(1,Reps+1):
115       repReads = Library.objects.filter(experiment_type='INPUT_RXLCh',affiliations__name=AFL,affiliations__contact=AFL_CNT,cell_line=H['cell_line'].__str__(),replicate=r)
116       str += "<td align=center style='background-color:"+bgc+"'>"
117       if len(repReads) == 0:
118         str += 'No Libraries'
119       else:
120         cnt = 0
121         for R1 in repReads:
122           rres = R1.aligned_m_reads()
123           # Check data sanlty                                                                                                                                           
124           if rres[2] != 'OK':
125             str += '<div style="border:solid red 2px">'+rres[2]
126           else:
127             cnt = rres[1]
128             if cnt > 0:
129               str += "<div style='background-color:"+getBgColor(cnt,EXP,(R1.libtags().find('Bad library')!=-1))+";font-size:140%'>"
130               str += "%1.2f" % (cnt/1000000.0)+" M"
131             else:  str += "<div style='background-color:#ffcdd0;width:100%;font-size:140%'>0 Reads"
132           str += "<div style='font-size:70%'><a title='View Library Record' target=_self style='font-weight:bold;color:black' href=/admin/fctracker/library/?q="+R1.library_id+">"+R1.library_id+"</a>, "+R1.condition.nickname
133           if R1.libtags().find('Data submitted to DCC') != -1:
134             str += "<div style='border:solid #666666 1px;'>data submitted</div>"
135           str += "</div>"
136           str += "</div>"
137       str += '</td>'
138     tbgc = bgc
139     bgc = pbgc
140     pbgc = tbgc
141   str += '</tr>' 
142
143   for T in TFs:
144     str += '<tr>'
145     try:
146       if T['antibody']:
147         str += '<td><a title="View Libraries Records" target=_self href=/admin/fctracker/library/?affiliations__id__exact='+AFL_Id+'&antibody__id__exact='+T['antibody'].__str__()+'>'+Antibody.objects.get(id=T['antibody']).nickname+'</a></td>'
148     except Antibody.DoesNotExist:
149       str += '<td>n/a</td>'
150
151     bgc = '#ffffff'
152     pbgc = '#f7f7f7'
153     for H in CLLs:
154       for r in range(1,Reps+1):
155         repReads = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT,cell_line=H['cell_line'].__str__(),antibody=T['antibody'].__str__(),replicate=r)
156         str += "<td align=center style='background-color:"+bgc+"'>"
157         if len(repReads) == 0: 
158           str += 'No Libraries'
159         else:
160           cnt = 0
161           for R1 in repReads:
162             rres = R1.aligned_m_reads()
163             # Check data sanity
164             if rres[2] != 'OK':
165               str += '<div style="border:solid red 2px">'+rres[2]
166             else:
167               cnt = rres[1]
168               if cnt > 0:
169                 str += "<div style='background-color:"+getBgColor(cnt,EXP,(R1.libtags().find('Bad library')!=-1))+";font-size:140%;margin:2px'>"
170                 str += "%1.2f" % (cnt/1000000.0)+" M"
171               else:  str += "<div style='background-color:#ffcdd0;width:100%;font-size:140%'>0 Reads"
172             str += "<div style='font-size:70%'><a title='View Library Record' target=_self style='font-weight:bold;color:black' href=/admin/fctracker/library/?q="+R1.library_id+">"+R1.library_id+"</a>, "+R1.condition.nickname
173             if R1.libtags().find('Data submitted to DCC') != -1:
174               str += "<div style='border:solid #666666 1px;'>data submitted</div>"
175             str += "</div>"
176             str += "</div>"
177         str += '</td>'
178       tbgc = bgc
179       bgc = pbgc
180       pbgc = tbgc
181     str += '</tr>'
182   str += '</table>'
183
184   return render_to_response('htsw_reports/report.html',{'main': str})
185
186
187 def report_RM(request): #for RNA-Seq and Methyl-Seq
188   if not request.user.is_authenticated():
189     str= '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">Please log in first</a></span>'
190     return render_to_response('htsw_reports/report.html',{'main':str})
191
192   EXP = 'RNA-seq'  
193
194   if request.has_key('exp'):
195     EXP = request['exp'] # Methyl-seq
196
197   if request.has_key('aflid'):
198     AFL_Id = request['aflid']
199     try:
200       AFL = Affiliation.objects.get(id=AFL_Id).name
201       AFL_CNT = Affiliation.objects.get(id=AFL_Id).contact
202     except ObjectDoesNotExist:
203       return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL_Id+"'")
204   else:
205     AFL = 'ENCODE_Tier1'
206     AFL_CNT = ''
207     try:
208       AFL_Id = Affiliation.objects.get(name=AFL,contact=AFL_CNT).id.__str__()
209     except ObjectDoesNotExist:
210       return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL+"'")
211
212   if request.user.username == 'caltech':
213     if AFL.find('ENCODE') < 0:  return HttpResponse('Record Not Found <div style="margin-right:20px"><a target=_self href="/admin" target=_self>HOME</a></div>')
214
215   CLLall = Library.objects.values('cell_line').order_by('cell_line').distinct()
216   CLLs = CLLall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT)
217
218   ########
219   # Check Replicate numbers
220   Reps = 1
221   RepRecs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT).order_by('-replicate')
222   if len(RepRecs) > 0: Reps = RepRecs[0].replicate
223                                                                                                                                                                               
224   str = ''
225   str += '<span style="margin-right:20px"><a  target=_self href="/admin" target=_self" style="font-size:140%">HOME</a></span>'
226   str += '<span>Select another <b>'+EXP+'</b> Report:</span> <select onchange="window.location=\'/htsw_reports/report_RM?exp='+EXP+'&aflid=\'+this.options[selectedIndex].value; return false;">'
227
228   if request.user.username == 'caltech':
229     Affil_List = Affiliation.objects.filter(name__contains='ENCODE').distinct()
230   else:
231     Affil_List = Affiliation.objects.distinct()
232
233   for af in Affil_List:
234     str += '<option value='+af.id.__str__()
235     if AFL_Id == af.id.__str__():
236       str += ' selected'
237     str += '>'+af.name+' '+af.contact+'</option>'
238   str += '</select>'
239
240   if EXP == 'RNA-seq':
241     str += '<span style="margin-left:20px;padding:1px;border:solid #cccccc 1px">color scheme: <span style="margin-left:5px;background-color:#66ff66"> > 20 M</span><span style="margin-left:5px;background-color:#00ccff"> >  10 M</span><span style="margin-left:5px;background-color:#ffcc33"> > 6 M</span><span style="margin-left:5px;background-color:#ffcdd0"> < 6 M</span><span style="margin-left:5px;background-color:'+badLibBgColor+'">Bad Library</span></span>'
242     str += '<span style="margin-left:20px">'
243     str += '<u>Switch to:</u> '+AFL+' '+AFL_CNT+' <a target=_self href="/htsw_reports/report?exp=RNA-seq&aflid='+AFL_Id+'"><b>ChIP-Seq</b> Report</a>'
244     str += '  | '
245     str += '<a target=_self href="/htsw_reports/report_RM?exp=Methyl-seq&aflid='+AFL_Id+'"><b>Methyl-Seq</b> Report</a>'
246   else:
247     str += '<span style="margin-left:20px;padding:1px;border:solid #cccccc 1px">color scheme: <span style="margin-left:5px;background-color:#66ff66"> > 12 M</span><span style="margin-left:5px;background-color:#00ccff"> >  5 M</span><span style="margin-left:5px;background-color:#ffcc33"> > 3 M</span><span style="margin-left:5px;background-color:#ffcdd0"> < 3 M</span><span style="margin-left:5px;background-color:'+badLibBgColor+'">Bad Library</span></span>'
248     str += '<span style="margin-left:20px">'
249     str += '<u>Switch to:</u> '+AFL+' '+AFL_CNT+' <a target=_self href="/htsw_reports/report?exp=RNA-seq&aflid='+AFL_Id+'"><b>ChIP-Seq</b> Report</a>'
250     str += '  | '
251     str += '<a target=_self href="/htsw_reports/report_RM?exp=RNA-seq&aflid='+AFL_Id+'"><b>RNA-Seq</b> Report</a>'
252
253   str += '<br/><br/><table border=1>'
254   str += '<tr><th colspan='+(Reps*len(CLLs)).__str__()+' style="text-align:center">'+AFL+' '+AFL_CNT+'  <span style="font-size:140%">'+EXP+'</span></th></tr>'
255   str += '<tr>'
256   bgc = '#ffffff'
257   pbgc = '#f7f7f7'
258   for H in CLLs:
259     str += '<th colspan='+Reps.__str__()+' style="text-align:center;background-color:'+bgc+'">'+Cellline.objects.get(id=H['cell_line']).cellline_name+'</th>'
260     tbgc = bgc
261     bgc = pbgc
262     pbgc = tbgc
263   str += '</tr><tr>'
264   bgc = '#ffffff'
265   pbgc = '#f7f7f7'
266   for H in CLLs:
267     for r in range(1,Reps+1):
268       str += '<th style="text-align:center;background-color:'+bgc+'">Rep. '+r.__str__()+'</th>'
269     tbgc = bgc
270     bgc = pbgc
271     pbgc = tbgc
272   str += '</tr>'
273
274   str += '<tr>' 
275   bgc = '#ffffff'
276   pbgc = '#f7f7f7'
277   for H in CLLs:
278     for r in range(1,Reps+1):
279       repReads = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT,cell_line=H['cell_line'],replicate=r)                                                                                                                    
280       str += "<td align=center valign=top style='background-color:"+bgc+"'>"
281       if len(repReads) == 0:
282         str += 'No Libraries'
283       else:
284         cnt = 0
285         for R1 in repReads:
286           rres = R1.aligned_m_reads()
287           # Check data sanlty   
288           if rres[2] != 'OK':
289             str += '<div style="border:solid red 2px">'+rres[2]
290           else:
291             cnt = rres[1]
292             if cnt > 0:
293               str += "<div style='background-color:"+getBgColor(cnt,EXP,(R1.libtags().find('Bad library')!=-1))+";border:solid #cccccc 1px;font-size:140%'>"
294               str += "%1.2f" % (cnt/1000000.0)+" M"
295             else:  str += "<div style='background-color:#ffcdd0;border:solid #cccccc 1px;width:100%;font-size:140%'>0 Reads"
296           str += "<div style='font-size:80%'><a title='View Record' style='color:black' target=_self href=/admin/fctracker/library/?q="+R1.library_id+">"+R1.library_id+"</a>, "+R1.condition.nickname+", "+R1.library_species.common_name+"</div>"
297           str += "<div style='font-size:70%'>\""+R1.library_name+"\""
298           if R1.libtags().find('Data submitted to DCC') != -1:
299             str += "<div style='width:75%;border:solid #666666 1px;'>data submitted</div>"
300           str += "</div>"
301           str += "</div>"
302       str += '</td>'
303     tbgc = bgc
304     bgc = pbgc
305     pbgc = tbgc
306   str += '</tr>'
307   str += '</table>'
308
309   return render_to_response('htsw_reports/report.html',{'main': str})
310
311 def getNotRanFCs(request):
312   if not request.user.is_authenticated():
313     str= '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">Please log in first</a></span>'
314     return render_to_response('htsw_reports/report.html',{'main':str})
315
316   FCall = FlowCell.objects.order_by('-run_date').distinct()
317   str = '<table><tr><th>FlowCell</th><th>Lanes</th><th>Creation Date</th></tr>'
318   for f in FCall:
319     try:
320       t = DataRun.objects.get(fcid=f.id)
321     except ObjectDoesNotExist:
322       str += '<tr><td>'+f.flowcell_id+'</td><td>'+f.Lanes()+'</td><td>'+f.run_date.__str__()+'</td></tr>'
323   str += "</table>"
324   return render_to_response('htsw_reports/report.html',{'main':str})
325  
326 def test_Libs(request):
327   if not request.user.is_authenticated():
328     str= '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">Please log in first</a></span>'
329     return render_to_response('htsw_reports/report.html',{'main':str})
330
331   str = ''
332   str += '<table border=1><tr><th>Lib ID</th><th>Current Libaray Name (Free Text)</th><th>Auto-composed Libaray Name (antibody + celline + libid + species + [replicate])</th></tr>'
333   allLibs = Library.objects.all()
334   #allLibs = Library.objects.filter(antibody__isnull=False)
335   for L in allLibs:
336     str += '<tr>'
337     str += '<td>'+L.library_id+'</td><td>'+L.library_name+'</td>'   
338     str += '<td>'
339     str += L.experiment_type+'_'
340     if L.cell_line.cellline_name != 'Unknown':
341       str += L.cell_line.cellline_name+'_'
342
343     try:
344       if L.antibody is not None:
345         str += L.antibody.nickname + '_'
346     except Antibody.DoesNotExist:
347       pass
348   
349     str += 'Rep'+L.replicate.__str__()
350     str += '</td></tr>' 
351
352   str += '</table>'
353   return HttpResponse(str)