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 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]
13 if reads_cnt > rc_thr[0]:
14 bgcolor = '#66ff66' # Green
16 if reads_cnt > rc_thr[1]:
17 bgcolor ='#00ccff' # Blue
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'
30 if request.has_key('aflid'):
31 AFL_Id = request['aflid']
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+"'")
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+"'")
45 TFall = Library.objects.values('antibody').order_by('antibody').distinct()
46 CLLall = Library.objects.values('cell_line').order_by('cell_line').distinct()
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)
51 # Check Replicate numbers
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
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__():
65 str += ' onclick="window.location=\'/htsw_reports/report?aflid='+af.id.__str__()+'\'; return false;">'+af.name+' '+af.contact+'</option>'
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>'
70 str += '<span style="margin-left:20px">'
71 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>'
73 str += '<a target=_self href="/htsw_reports/report_RM?exp=Methyl-seq&aflid='+AFL_Id+'"><b>Methyl-Seq</b> Report</a>'
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>'
81 str += '<th colspan='+Reps.__str__()+' style="text-align:center;background-color:'+bgc+'">'+Cellline.objects.get(id=H['cell_line']).cellline_name+'</th>'
85 str += '</tr><tr><th style="text-align:left">TF</th>'
89 for r in range(1,Reps+1):
90 str += '<th style="text-align:center;background-color:'+bgc+'">Rep. '+r.__str__()+'</th>'
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>'
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'
107 cnt = R1.aligned_m_reads()[1]
109 str += "<div style='background-color:"+getBgColor(cnt,EXP)+";font-size:140%'>"
110 str += "%1.2f" % (cnt/1000000.0)+" M"
111 else: str += "<div style='background-color:#ff3300;width:100%;font-size:140%'>0 Reads"
112 str += "<div style='font-size:70%'>"+R1.library_id+", "+R1.condition.nickname+"</div>"
124 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>'
125 except Antibody.DoesNotExist:
126 str += '<td>n/a</td>'
131 for r in range(1,Reps+1):
132 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)
133 str += "<td align=center style='background-color:"+bgc+"'>"
134 if len(repReads) == 0:
135 str += 'No Libraries'
139 cnt = R1.aligned_m_reads()[1]
141 str += "<div style='background-color:"+getBgColor(cnt,EXP)+";font-size:140%'>"
142 str += "%1.2f" % (cnt/1000000.0)+" M"
143 else: str += "<div style='background-color:#ff3300;width:100%;font-size:140%'>0 Reads"
144 str += "<div style='font-size:70%'>"+R1.library_id+", "+R1.condition.nickname+"</div>"
153 return render_to_response('htsw_reports/report.html',{'main': str})
156 def report_RM(request): #for RNA-Seq and Methyl-Seq
159 if request.has_key('exp'):
160 EXP = request['exp'] # Methyl-seq
162 if request.has_key('aflid'):
163 AFL_Id = request['aflid']
165 AFL = Affiliation.objects.get(id=AFL_Id).name
166 AFL_CNT = Affiliation.objects.get(id=AFL_Id).contact
167 except ObjectDoesNotExist:
168 return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL_ID+"'")
173 AFL_Id = Affiliation.objects.get(name=AFL,contact=AFL_CNT).id.__str__()
174 except ObjectDoesNotExist:
175 return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL+"'")
177 CLLall = Library.objects.values('cell_line').order_by('cell_line').distinct()
178 CLLs = CLLall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT)
181 # Check Replicate numbers
183 RepRecs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT).order_by('-replicate')
184 if len(RepRecs) > 0: Reps = RepRecs[0].replicate
187 str += '<span style="margin-right:20px"><a target=_self href="/admin" target=_self">Main Page</a></span>'
188 str += '<span>Select another <b>'+EXP+'</b> Report:</span> <select>'
189 for af in Affiliation.objects.distinct():
190 str += '<option value='+af.id.__str__()
191 if AFL_Id == af.id.__str__():
193 str += ' onclick="window.location=\'/htsw_reports/report_RM?exp='+EXP+'&aflid='+af.id.__str__()+'\'; return false;">'+af.name+' '+af.contact+'</option>'
197 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>'
198 str += '<span style="margin-left:20px">'
199 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>'
201 str += '<a target=_self href="/htsw_reports/report_RM?exp=Methyl-seq&aflid='+AFL_Id+'"><b>Methyl-Seq</b> Report</a>'
203 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>'
204 str += '<span style="margin-left:20px">'
205 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>'
207 str += '<a target=_self href="/htsw_reports/report_RM?exp=RNA-seq&aflid='+AFL_Id+'"><b>RNA-Seq</b> Report</a>'
209 str += '<br/><br/><table border=1 cellspacing="2">'
210 str += '<tr><th colspan='+(Reps*len(CLLs)).__str__()+' style="text-align:center">'+AFL+' '+AFL_CNT+' <span style="font-size:140%">'+EXP+'</span></th></tr>'
215 str += '<th colspan='+Reps.__str__()+' style="text-align:center;background-color:'+bgc+'">'+Cellline.objects.get(id=H['cell_line']).cellline_name+'</th>'
223 for r in range(1,Reps+1):
224 str += '<th style="text-align:center;background-color:'+bgc+'">Rep. '+r.__str__()+'</th>'
234 for r in range(1,Reps+1):
235 repReads = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT,cell_line=H['cell_line'],replicate=r)
236 str += "<td align=center valign=top style='background-color:"+bgc+"'>"
237 if len(repReads) == 0:
238 str += 'No Libraries'
242 cnt = R1.aligned_m_reads()[1]
244 str += "<div style='background-color:"+getBgColor(cnt,EXP)+";border:solid #cccccc 1px;font-size:140%'>"
245 str += "%1.2f" % (cnt/1000000.0)+" M"
246 else: str += "<div style='background-color:#ff3300;border:solid #cccccc 1px;width:100%;font-size:140%'>0 Reads"
247 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>"
248 str += "<div style='font-size:70%'>\""+R1.library_name+"\"</div"
257 return render_to_response('htsw_reports/report.html',{'main': str})
259 def getNotRanFCs(request):
260 FCall = FlowCell.objects.order_by('-run_date').distinct()
261 str = '<table><tr><th>FlowCell</th><th>Lanes</th><th>Creation Date</th></tr>'
264 t = DataRun.objects.get(fcid=f.id)
265 except ObjectDoesNotExist:
266 str += '<tr><td>'+f.flowcell_id+'</td><td>'+f.Lanes()+'</td><td>'+f.run_date.__str__()+'</td></tr>'
268 return render_to_response('htsw_reports/report.html',{'main':str})
270 def test_Libs(request):
272 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>'
273 allLibs = Library.objects.all()
274 #allLibs = Library.objects.filter(antibody__isnull=False)
277 str += '<td>'+L.library_id+'</td><td>'+L.library_name+'</td>'
279 str += L.experiment_type+'_'
280 if L.cell_line.cellline_name != 'Unknown':
281 str += L.cell_line.cellline_name+'_'
284 if L.antibody is not None:
285 str += L.antibody.nickname + '_'
286 except Antibody.DoesNotExist:
289 str += 'Rep'+L.replicate.__str__()
293 return HttpResponse(str)