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
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]
15 if reads_cnt > rc_thr[0]:
16 bgcolor = '#66ff66' # Green
18 if reads_cnt > rc_thr[1]:
19 bgcolor ='#00ccff' # Blue
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'
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})
36 if request.has_key('aflid'):
37 AFL_Id = request['aflid']
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+"'")
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+"'")
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>')
54 TFall = Library.objects.values('antibody').order_by('antibody').distinct()
55 CLLall = Library.objects.values('cell_line').order_by('cell_line').distinct()
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)
60 # Check Replicate numbers
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
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;">'
71 if request.user.username == 'caltech':
72 Affil_List = Affiliation.objects.filter(name__contains='ENCODE').distinct()
74 Affil_List = Affiliation.objects.distinct()
77 str += '<option value='+af.id.__str__()
78 if AFL_Id == af.id.__str__():
80 str += '>'+af.name+' '+af.contact+'</option>'
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>'
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>'
88 str += '<a target=_self href="/htsw_reports/report_RM?exp=Methyl-seq&aflid='+AFL_Id+'"><b>Methyl-Seq</b> Report</a>'
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>'
96 str += '<th colspan='+Reps.__str__()+' style="text-align:center;background-color:'+bgc+'">'+Cellline.objects.get(id=H['cell_line']).cellline_name+'</th>'
100 str += '</tr><tr><th style="text-align:left">TF</th>'
104 for r in range(1,Reps+1):
105 str += '<th style="text-align:center;background-color:'+bgc+'">Rep. '+r.__str__()+'</th>'
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>'
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'
122 rres = R1.aligned_m_reads()
125 str += '<div style="border:solid red 2px">'+rres[2]
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>"
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>'
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'
162 rres = R1.aligned_m_reads()
165 str += '<div style="border:solid red 2px">'+rres[2]
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>"
184 return render_to_response('htsw_reports/report.html',{'main': str})
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})
194 if request.has_key('exp'):
195 EXP = request['exp'] # Methyl-seq
197 if request.has_key('aflid'):
198 AFL_Id = request['aflid']
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+"'")
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+"'")
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>')
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)
219 # Check Replicate numbers
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
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;">'
228 if request.user.username == 'caltech':
229 Affil_List = Affiliation.objects.filter(name__contains='ENCODE').distinct()
231 Affil_List = Affiliation.objects.distinct()
233 for af in Affil_List:
234 str += '<option value='+af.id.__str__()
235 if AFL_Id == af.id.__str__():
237 str += '>'+af.name+' '+af.contact+'</option>'
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>'
245 str += '<a target=_self href="/htsw_reports/report_RM?exp=Methyl-seq&aflid='+AFL_Id+'"><b>Methyl-Seq</b> Report</a>'
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>'
251 str += '<a target=_self href="/htsw_reports/report_RM?exp=RNA-seq&aflid='+AFL_Id+'"><b>RNA-Seq</b> Report</a>'
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>'
259 str += '<th colspan='+Reps.__str__()+' style="text-align:center;background-color:'+bgc+'">'+Cellline.objects.get(id=H['cell_line']).cellline_name+'</th>'
267 for r in range(1,Reps+1):
268 str += '<th style="text-align:center;background-color:'+bgc+'">Rep. '+r.__str__()+'</th>'
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'
286 rres = R1.aligned_m_reads()
289 str += '<div style="border:solid red 2px">'+rres[2]
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>"
309 return render_to_response('htsw_reports/report.html',{'main': str})
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})
316 FCall = FlowCell.objects.order_by('-run_date').distinct()
317 str = '<table><tr><th>FlowCell</th><th>Lanes</th><th>Creation Date</th></tr>'
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>'
324 return render_to_response('htsw_reports/report.html',{'main':str})
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})
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)
337 str += '<td>'+L.library_id+'</td><td>'+L.library_name+'</td>'
339 str += L.experiment_type+'_'
340 if L.cell_line.cellline_name != 'Unknown':
341 str += L.cell_line.cellline_name+'_'
344 if L.antibody is not None:
345 str += L.antibody.nickname + '_'
346 except Antibody.DoesNotExist:
349 str += 'Rep'+L.replicate.__str__()
353 return HttpResponse(str)