30d9ad48fde48734a66a53a6c7e040d25285c9b7
[htsworkflow.git] / htsworkflow / frontend / reports / reports.py
1 from htsworkflow.frontend.experiments.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 def getBgColor(reads_cnt,exp_type):
7   # 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
8   bgcolor = '#ff3300'  # Red is the color for minimum read counts                                                                                                                            
9   rc_thr = [12000000,5000000,3000000] # Default for ChIP-Seq and Methyl-Seq
10   if exp_type == 'RNA-seq':
11     rc_thr = [20000000,10000000,6000000]
12
13   if reads_cnt > rc_thr[0]:
14     bgcolor = '#66ff66'  # Green                                                                                                                                                                                                                                               
15   else:
16     if reads_cnt > rc_thr[1]:
17       bgcolor ='#00ccff'  # Blue                                                                                                                                                                                                                                               
18     else:
19        if reads_cnt > rc_thr[2]:
20          bgcolor ='#ffcc33'  # Orange                                                                                                                                                                                                                                          
21   #tstr = '<div style="background-color:'+bgcolor+';color:black">'
22   #tstr += res[0].__str__()+' Lanes, '+rc+' M Reads'
23   #tstr += '</div>'
24
25   return bgcolor
26
27 def report1(request):
28   EXP = 'ChIP-seq'
29
30   if request.GET.has_key('aflid'):
31     AFL_Id = request.GET['aflid']
32     try:
33       AFL = Affiliation.objects.get(id=AFL_Id).name
34       AFL_CNT = Affiliation.objects.get(id=AFL_Id).contact
35     except ObjectDoesNotExist:
36       return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL_ID+"'")
37   else:
38     AFL = 'ENCODE_Tier1'
39     AFL_CNT = ''
40     try:
41       AFL_Id = Affiliation.objects.get(name=AFL,contact=AFL_CNT).id.__str__()
42     except ObjectDoesNotExist:
43       return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL+"'")
44   
45   TFall = Library.objects.values('antibody').order_by('antibody').distinct()
46   CLLall = Library.objects.values('cell_line').order_by('cell_line').distinct()
47
48   TFs = TFall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT)
49   CLLs = CLLall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT)
50
51   # Check Replicate numbers
52   Reps = 1
53   RepRecs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT).order_by('-replicate')
54   if len(RepRecs) > 0: Reps = RepRecs[0].replicate
55   
56   ########
57   str = ''
58   str += '<span style="margin-right:20px"><a target=_self href="/admin" target=_self">Main Page</a></span>'
59   ##str += '<span style="margin-right:20px">Max Replicates: '+MaxRep.replicate.__str__()+'</span>'
60   str += '<span>Select another <b>'+EXP+'</b> Report:</span>  <select>'
61   for af in Affiliation.objects.distinct():
62     str += '<option value='+af.id.__str__()
63     if AFL_Id == af.id.__str__():
64       str += ' selected'
65     str += ' onclick="window.location=\'/reports/report?aflid='+af.id.__str__()+'\'; return false;">'+af.name+' '+af.contact+'</option>'  
66   str += '</select>'
67
68   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:#ff3300"> < 3 M</span></span>' 
69
70   str += '<span style="margin-left:20px">'
71   str += '<u>Switch to:</u> '+AFL+' '+AFL_CNT+' <a target=_self href="/reports/report_RM?exp=RNA-seq&aflid='+AFL_Id+'"><b>RNA-Seq</b> Report</a>'
72   str += '  | '
73   str += '<a target=_self href="/reports/report_RM?exp=Methyl-seq&aflid='+AFL_Id+'"><b>Methyl-Seq</b> Report</a>'
74
75   bgc = '#ffffff' 
76   pbgc = '#f7f7f7'
77   str += '<br/><br/><table border=1 cellspacing="2">'
78   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>'
79   str += '<tr><th style="text-align:right">CELL LINE</th>'
80   for H in CLLs: 
81     str += '<th colspan='+Reps.__str__()+' style="text-align:center;background-color:'+bgc+'">'+Cellline.objects.get(id=H['cell_line']).cellline_name+'</th>'
82     tbgc = bgc
83     bgc = pbgc
84     pbgc = tbgc 
85   str += '</tr><tr><th style="text-align:left">TF</th>'
86   bgc = '#ffffff'
87   pbgc = '#f7f7f7'
88   for H in CLLs:
89     for r in range(1,Reps+1):
90       str += '<th style="text-align:center;background-color:'+bgc+'">Rep. '+r.__str__()+'</th>'
91     tbgc = bgc
92     bgc = pbgc
93     pbgc = tbgc
94   str += '</tr>'
95   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>'
96   bgc = '#ffffff'
97   pbgc = '#f7f7f7'
98   for H in CLLs:
99     for r in range(1,Reps+1):
100       repReads = Library.objects.filter(experiment_type='INPUT_RXLCh',affiliations__name=AFL,affiliations__contact=AFL_CNT,cell_line=H['cell_line'].__str__(),replicate=r)
101       str += "<td align=center style='background-color:"+bgc+"'>"
102       if len(repReads) == 0:
103         str += 'No Libraries'
104       else:
105         cnt = 0
106         for R1 in repReads:
107           rres = R1.aligned_m_reads()
108           # Check data sanlty                                                                                                                                           
109           if rres[2] != 'OK':
110             str += '<div style="border:solid red 2px">'+rres[2]
111           else:
112             cnt = rres[1]
113             if cnt > 0:
114               str += "<div style='background-color:"+getBgColor(cnt,EXP)+";font-size:140%'>"
115               str += "%1.2f" % (cnt/1000000.0)+" M"
116             else:  str += "<div style='background-color:#ff3300;width:100%;font-size:140%'>0 Reads"
117           str += "<div style='font-size:70%'>"+R1.library_id+", "+R1.condition.nickname+"</div>"
118           str += "</div>"
119       str += '</td>'
120     tbgc = bgc
121     bgc = pbgc
122     pbgc = tbgc
123   str += '</tr>' 
124
125   for T in TFs:
126     str += '<tr>'
127     try:
128       if T['antibody']:
129         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>'
130     except Antibody.DoesNotExist:
131       str += '<td>n/a</td>'
132
133     bgc = '#ffffff'
134     pbgc = '#f7f7f7'
135     for H in CLLs:
136       for r in range(1,Reps+1):
137         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)
138         str += "<td align=center style='background-color:"+bgc+"'>"
139         if len(repReads) == 0: 
140           str += 'No Libraries'
141         else:
142           cnt = 0
143           for R1 in repReads:
144             rres = R1.aligned_m_reads()
145             # Check data sanlty
146             if rres[2] != 'OK':
147               str += '<div style="border:solid red 2px">'+rres[2]
148             else:
149               cnt = rres[1]
150               if cnt > 0:
151                 str += "<div style='background-color:"+getBgColor(cnt,EXP)+";font-size:140%'>"
152                 str += "%1.2f" % (cnt/1000000.0)+" M"
153               else:  str += "<div style='background-color:#ff3300;width:100%;font-size:140%'>0 Reads"
154             str += "<div style='font-size:70%'>"+R1.library_id+", "+R1.condition.nickname+"</div>"
155             str += "</div>"
156         str += '</td>'
157       tbgc = bgc
158       bgc = pbgc
159       pbgc = tbgc
160     str += '</tr>'
161   str += '</table>'
162
163   return render_to_response('reports/report.html',{'main': str})
164
165
166 def report_RM(request): #for RNA-Seq and Methyl-Seq
167   EXP = 'RNA-seq'  
168
169   if request.GET.has_key('exp'):
170     EXP = request.GET['exp'] # Methyl-seq
171
172   if request.GET.has_key('aflid'):
173     AFL_Id = request.GET['aflid']
174     try:
175       AFL = Affiliation.objects.get(id=AFL_Id).name
176       AFL_CNT = Affiliation.objects.get(id=AFL_Id).contact
177     except ObjectDoesNotExist:
178       return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL_ID+"'")
179   else:
180     AFL = 'ENCODE_Tier1'
181     AFL_CNT = ''
182     try:
183       AFL_Id = Affiliation.objects.get(name=AFL,contact=AFL_CNT).id.__str__()
184     except ObjectDoesNotExist:
185       return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL+"'")
186
187   CLLall = Library.objects.values('cell_line').order_by('cell_line').distinct()
188   CLLs = CLLall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT)
189
190   ########
191   # Check Replicate numbers
192   Reps = 1
193   RepRecs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT).order_by('-replicate')
194   if len(RepRecs) > 0: Reps = RepRecs[0].replicate
195                                                                                                                                                                               
196   str = ''
197   str += '<span style="margin-right:20px"><a  target=_self href="/admin" target=_self">Main Page</a></span>'
198   str += '<span>Select another <b>'+EXP+'</b> Report:</span> <select>'
199   for af in Affiliation.objects.distinct():
200     str += '<option value='+af.id.__str__()
201     if AFL_Id == af.id.__str__():
202       str += ' selected'
203     str += ' onclick="window.location=\'/reports/report_RM?exp='+EXP+'&aflid='+af.id.__str__()+'\'; return false;">'+af.name+' '+af.contact+'</option>'
204   str += '</select>'
205
206   if EXP == 'RNA-seq':
207     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:#ff3300"> < 6 M</span></span>'
208     str += '<span style="margin-left:20px">'
209     str += '<u>Switch to:</u> '+AFL+' '+AFL_CNT+' <a target=_self href="/reports/report?exp=RNA-seq&aflid='+AFL_Id+'"><b>ChIP-Seq</b> Report</a>'
210     str += '  | '
211     str += '<a target=_self href="/reports/report_RM?exp=Methyl-seq&aflid='+AFL_Id+'"><b>Methyl-Seq</b> Report</a>'
212   else:
213     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:#ff3300"> < 3 M</span></span>'
214     str += '<span style="margin-left:20px">'
215     str += '<u>Switch to:</u> '+AFL+' '+AFL_CNT+' <a target=_self href="/reports/report?exp=RNA-seq&aflid='+AFL_Id+'"><b>ChIP-Seq</b> Report</a>'
216     str += '  | '
217     str += '<a target=_self href="/reports/report_RM?exp=RNA-seq&aflid='+AFL_Id+'"><b>RNA-Seq</b> Report</a>'
218
219   str += '<br/><br/><table border=1 cellspacing="2">'
220   str += '<tr><th colspan='+(Reps*len(CLLs)).__str__()+' style="text-align:center">'+AFL+' '+AFL_CNT+'  <span style="font-size:140%">'+EXP+'</span></th></tr>'
221   str += '<tr>'
222   bgc = '#ffffff'
223   pbgc = '#f7f7f7'
224   for H in CLLs:
225     str += '<th colspan='+Reps.__str__()+' style="text-align:center;background-color:'+bgc+'">'+Cellline.objects.get(id=H['cell_line']).cellline_name+'</th>'
226     tbgc = bgc
227     bgc = pbgc
228     pbgc = tbgc
229   str += '</tr><tr>'
230   bgc = '#ffffff'
231   pbgc = '#f7f7f7'
232   for H in CLLs:
233     for r in range(1,Reps+1):
234       str += '<th style="text-align:center;background-color:'+bgc+'">Rep. '+r.__str__()+'</th>'
235     tbgc = bgc
236     bgc = pbgc
237     pbgc = tbgc
238   str += '</tr>'
239
240   str += '<tr>' 
241   bgc = '#ffffff'
242   pbgc = '#f7f7f7'
243   for H in CLLs:
244     for r in range(1,Reps+1):
245       repReads = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT,cell_line=H['cell_line'],replicate=r)                                                                                                                    
246       str += "<td align=center valign=top style='background-color:"+bgc+"'>"
247       if len(repReads) == 0:
248         str += 'No Libraries'
249       else:
250         cnt = 0
251         for R1 in repReads:
252           rres = R1.aligned_m_reads()
253           # Check data sanlty   
254           if rres[2] != 'OK':
255             str += '<div style="border:solid red 2px">'+rres[2]
256           else:
257             cnt = rres[1]
258             if cnt > 0:
259               str += "<div style='background-color:"+getBgColor(cnt,EXP)+";border:solid #cccccc 1px;font-size:140%'>"
260               str += "%1.2f" % (cnt/1000000.0)+" M"
261             else:  str += "<div style='background-color:#ff3300;border:solid #cccccc 1px;width:100%;font-size:140%'>0 Reads"
262           str += "<div style='font-size:80%'><a title='View Record' target=_self href=/admin/fctracker/library/?q="+R1.library_id+">"+R1.library_id+"</a>, "+R1.condition.nickname+", "+R1.library_species.common_name+"</div>"
263           str += "<div style='font-size:70%'>\""+R1.library_name+"\"</div"
264           str += "</div>"
265       str += '</td>'
266     tbgc = bgc
267     bgc = pbgc
268     pbgc = tbgc
269   str += '</tr>'
270   str += '</table>'
271
272   return render_to_response('reports/report.html',{'main': str})
273
274 def getNotRanFCs(request):
275   FCall = FlowCell.objects.order_by('-run_date').distinct()
276   str = '<table><tr><th>FlowCell</th><th>Lanes</th><th>Creation Date</th></tr>'
277   for f in FCall:
278     try:
279       t = DataRun.objects.get(fcid=f.id)
280     except ObjectDoesNotExist:
281       str += '<tr><td>'+f.flowcell_id+'</td><td>'+f.Lanes()+'</td><td>'+f.run_date.__str__()+'</td></tr>'
282   str += "</table>"
283   return render_to_response('reports/report.html',{'main':str})
284  
285 def test_Libs(request):
286   str = ''
287   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>'
288   allLibs = Library.objects.all()
289   #allLibs = Library.objects.filter(antibody__isnull=False)
290   for L in allLibs:
291     str += '<tr>'
292     str += '<td>'+L.library_id+'</td><td>'+L.library_name+'</td>'   
293     str += '<td>'
294     str += L.experiment_type+'_'
295     if L.cell_line.cellline_name != 'Unknown':
296       str += L.cell_line.cellline_name+'_'
297
298     try:
299       if L.antibody is not None:
300         str += L.antibody.nickname + '_'
301     except Antibody.DoesNotExist:
302       pass
303   
304     str += 'Rep'+L.replicate.__str__()
305     str += '</td></tr>' 
306
307   str += '</table>'
308   return HttpResponse(str)