added grey highlight for bad libraries (by tag text)
[htsworkflow.git] / htswfrontend / htswfrontend / htsw_reports / q_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 arMonth = {"01":"Jan","02":"Feb","03":"Mar","04":"Apr","05":"May","06":"Jun","07":"Jul","08":"Aug","09":"Sep","10":"Oct","11":"Nov","12":"Dec"}
7 def getBgColor(reads_cnt,exp_type):
8   # 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
9   bgcolor = '#ffcdd0'  # Red is the color for minimum read counts                                                                                                                            
10   rc_thr = [12000000,5000000,3000000] # Default for ChIP-Seq and Methyl-Seq
11   if exp_type == 'RNA-seq':
12     rc_thr = [20000000,10000000,6000000]
13
14   if reads_cnt > rc_thr[0]:
15     bgcolor = '#66ff66'  # Green                                                                                                                                                                                                                                               
16   else:
17     if reads_cnt > rc_thr[1]:
18       bgcolor ='#00ccff'  # Blue                                                                                                                                                                                                                                               
19     else:
20        if reads_cnt > rc_thr[2]:
21          bgcolor ='#ffcc33'  # Orange                                                                                                                                                                                                                                          
22   #tstr = '<div style="background-color:'+bgcolor+';color:black">'
23   #tstr += res[0].__str__()+' Lanes, '+rc+' M Reads'
24   #tstr += '</div>'
25
26   return bgcolor
27
28 def q_report1(request):
29   if not request.user.is_authenticated():
30     str = '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">Please log in first</a></span>'
31     return render_to_response('htsw_reports/report.html',{'main':str}) 
32
33   EXP = 'ChIP-seq'
34
35   if request.has_key('aflid'):
36     AFL_Id = request['aflid']
37     try:
38       AFL = Affiliation.objects.get(id=AFL_Id).name
39       AFL_CNT = Affiliation.objects.get(id=AFL_Id).contact
40     except ObjectDoesNotExist:
41       return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL_Id+"'")
42   else:
43     AFL = 'ENCODE_Tier1'
44     AFL_CNT = ''
45     try:
46       AFL_Id = Affiliation.objects.get(name=AFL,contact=AFL_CNT).id.__str__()
47     except ObjectDoesNotExist:
48       return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL+"'")
49
50   if request.user.username == 'caltech':
51     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>')
52
53   ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q REPORT PART ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54   start_date = datetime(2009,01,01).date().__str__()
55   end_date = datetime(2011,01,01)
56   end_date_text = ''
57
58   if request.has_key('start_date'):
59     start_date = request['start_date']
60
61   if request.has_key('end_date'):
62     end_date = request['end_date']
63   else: #end_date = today
64     end_date = end_date.now().date().__str__()
65     end_date_text = ' (Today)'
66
67   DRuns = []
68   if start_date and end_date:
69     start_date = datetime(int(start_date.split('-')[0]),int(start_date.split('-')[1]),int(start_date.split('-')[2]),0,0,0)
70     end_date = datetime(int(end_date.split('-')[0]),int(end_date.split('-')[1]),int(end_date.split('-')[2]),0,0,0)
71     DRuns = DataRun.objects.filter(run_start_time__range=(start_date, end_date))
72     
73   QReportText = ''
74   QReportText += '<div><span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">HOME</a></span><h2>Time Report Generator for ChIP-Seq</h2></div>'
75   QReportText += '<form action="" method="get" id="qreport_form">'
76   QReportText += '<p>'
77   QReportText += EXP+' <select onchange="window.location=\'/htsw_reports/q_report?start_date='+start_date.date().__str__()+'&end_date='+end_date.date().__str__()+'&aflid=\'+this.options[selectedIndex].value; return false;">'
78
79   if request.user.username == 'caltech':
80     Affil_List = Affiliation.objects.filter(name__contains='ENCODE').distinct()
81   else:
82     Affil_List = Affiliation.objects.distinct()
83
84   for af in Affil_List:
85     QReportText += '<option value='+af.id.__str__()
86     if AFL_Id == af.id.__str__():
87       QReportText += ' selected'
88     QReportText += '>'+af.name+' '+af.contact+'</option>'
89   QReportText += '</select>'
90   QReportText += '&nbsp;&nbsp;&nbsp;&nbsp; Start Date: '
91   QReportText += '<input type="text" maxlength="10" value="'+start_date.date().__str__()+'" size="10" name="start_date" />'  # class="vDateField" />' # id="id_run_start_time_date"/>'
92   QReportText += '&nbsp;&nbsp;&nbsp;&nbsp;End Date: <input type="text" maxlength="10" value="'+end_date.date().__str__()+'" size="10" name="end_date" />' 
93   #QReportText += '<span><a href="javascript:DateTimeShortcuts.handleCalendarQuickLink(0, 0);">Today</a> | <a href="javascript:DateTimeShortcuts.openCalendar(0);" id="calendarlink0">'
94   #QReportText += '<img src="http://myerslab.stanford.edu/media/img/admin/icon_calendar.gif" alt="Calendar"/></a></span>'
95   #QReportText += 'End Date: <input type="text" maxlength="10" value="" size="10" name="end_date" class="vDateField required" id="end_date"/><span><a href="javascript:DateTimeShortcuts.handleCalendarQuickLink(0, 0);">Today</a> | <a href="javascript:DateTimeShortcuts.openCalendar(0);" id="calendarlink0"><img src="http://myerslab.stanford.edu/media/img/admin/icon_calendar.gif" alt="Calendar"/></a></span>'
96   QReportText += '<span style="margin-left:10px"><input type="submit" value="Run Report" name="runreport"  /></span></p>'
97
98   QReportText +='<div style="border:solid #cccccc 1px;padding:2px;background-color:#FFFFCC;margin-top:5px">'
99   s_year = start_date.date().__str__().split("-")[0]
100   s_month = start_date.date().__str__().split("-")[1]
101   s_day = start_date.date().__str__().split("-")[2]
102   e_year = end_date.date().__str__().split("-")[0]
103   e_month = end_date.date().__str__().split("-")[1]
104   e_day = end_date.date().__str__().split("-")[2]
105   QReportText += '<h2>For <u>'+AFL+'</u> <u>'+AFL_CNT+'</u>'+' between <u>'+s_year+'-'+arMonth[s_month]+'-'+s_day+'</u> and <u>'+e_year+'-'+arMonth[e_month]+'-'+e_day+end_date_text+'</u></h2>'
106   QReportText +='<div style="float:left">'
107   FCText = '<h4>The following Flowcells were sequenced:</h4>' 
108   FCText += '<table border=1><tr><th>Flowcell Id</th><th>Ran On</th></tr>'
109   libidlist = []
110   LIB_LANE_COUNT = {}
111   LIB_FC_SUMMARY = {}
112   for DR in  DRuns:
113    libidlist.append(DR.fcid.lane_1_library.library_id)
114    #if LIB_LANE_COUNT.has_key(DR.fcid.lane_1_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_1_library.library_id] += 1
115    #else: LIB_LANE_COUNT[DR.fcid.lane_1_library.library_id] = 1
116    libidlist.append(DR.fcid.lane_2_library.library_id)
117    #if LIB_LANE_COUNT.has_key(DR.fcid.lane_2_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_2_library.library_id] += 1
118    #else: LIB_LANE_COUNT[DR.fcid.lane_2_library.library_id] = 1
119    libidlist.append(DR.fcid.lane_3_library.library_id)
120    #if LIB_LANE_COUNT.has_key(DR.fcid.lane_3_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_3_library.library_id] += 1
121    #else: LIB_LANE_COUNT[DR.fcid.lane_3_library.library_id] = 1
122    libidlist.append(DR.fcid.lane_4_library.library_id)
123    #if LIB_LANE_COUNT.has_key(DR.fcid.lane_4_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_4_library.library_id] += 1
124    #else: LIB_LANE_COUNT[DR.fcid.lane_4_library.library_id] = 1
125    libidlist.append(DR.fcid.lane_5_library.library_id)
126    #if LIB_LANE_COUNT.has_key(DR.fcid.lane_5_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_5_library.library_id] += 1
127    #else: LIB_LANE_COUNT[DR.fcid.lane_5_library.library_id] = 1
128    libidlist.append(DR.fcid.lane_6_library.library_id)
129    #if LIB_LANE_COUNT.has_key(DR.fcid.lane_6_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_6_library.library_id] += 1
130    #else: LIB_LANE_COUNT[DR.fcid.lane_6_library.library_id] = 1
131    libidlist.append(DR.fcid.lane_7_library.library_id)
132    #if LIB_LANE_COUNT.has_key(DR.fcid.lane_7_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_7_library.library_id] += 1
133    #else: LIB_LANE_COUNT[DR.fcid.lane_7_library.library_id] = 1
134    libidlist.append(DR.fcid.lane_8_library.library_id)
135    #if LIB_LANE_COUNT.has_key(DR.fcid.lane_8_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_8_library.library_id] += 1
136    #else: LIB_LANE_COUNT[DR.fcid.lane_8_library.library_id] = 1
137    for i in range(1,9):
138      if LIB_LANE_COUNT.has_key(eval('DR.fcid.lane_'+i.__str__()+'_library.library_id')): LIB_LANE_COUNT[eval('DR.fcid.lane_'+i.__str__()+'_library.library_id')] += 1
139      else: LIB_LANE_COUNT[eval('DR.fcid.lane_'+i.__str__()+'_library.library_id')] = 1
140      if LIB_FC_SUMMARY.has_key(eval('DR.fcid.lane_'+i.__str__()+'_library.library_id')): LIB_FC_SUMMARY[eval('DR.fcid.lane_'+i.__str__()+'_library.library_id')] += '<br/>'+DR.fcid.flowcell_id+' (lane '+i.__str__()+')'
141      else: LIB_FC_SUMMARY[eval('DR.fcid.lane_'+i.__str__()+'_library.library_id')] = DR.fcid.flowcell_id+' (lane '+i.__str__()+')' 
142
143    FCText += '<tr><td>'+DR.fcid.flowcell_id+'</td><td>'+DR.run_start_time.__str__()+'</td></tr>'
144
145   FCText += '</table>' 
146
147   ## RelevantLibs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT,library_id__in=libidlist).order_by('antibody','cell_line')
148
149   if EXP == 'ChIP-seq':
150     RelevantLibs = Library.objects.filter(experiment_type__in=[EXP,'INPUT_RXLCh'],affiliations__name=AFL,affiliations__contact=AFL_CNT,library_id__in=libidlist).order_by('antibody','cell_line')
151   else:
152     RelevantLibs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT,library_id__in=libidlist).order_by('antibody','cell_line')
153
154   rliblist = []
155
156   QReportText +='<div style="float:left">'
157   QReportText += '<h4>'
158   if RelevantLibs:
159     QReportText += 'The following <u>'+len(RelevantLibs).__str__()+'</u> '
160   else:
161     QReportText += 'No ' 
162   QReportText += EXP+' Libraries  were sequenced:</h4>'
163   QReportText += '<div style="width:200px;background-color:#cccccc;font-size:90%">(bad libraries marked in grey)</div>'
164   QReportText += '<table border=1><tr><th>Library Id</th><th>Antibody</th><th>Cell line</th><th>Condition</th><th>Replicate</th><th>Lanes ran (during this period)</th></tr>'
165   for rlib in RelevantLibs:
166     bgColor = ''
167     if rlib.libtags().find('Bad library')!=-1: bgColor='#cccccc'
168     QReportText += '<tr style="background-color:'+bgColor+'"><td><a title="View Library Record" target=_self href=/admin/fctracker/library/?q='+rlib.library_id+'>'+rlib.library_id+'</a></td>'
169     if rlib.antibody is not None: QReportText += '<td>'+rlib.antibody.nickname+'</td>'
170     else: 
171       if rlib.experiment_type == 'INPUT_RXLCh':  QReportText += '<td>[INPUT_RXLCh]</td>'
172       else: QReportText += '<td>none</td>'
173     
174     QReportText += '<td>'+rlib.cell_line.nickname+'</td>'
175     QReportText += '<td>'+rlib.condition.nickname+'</td>'
176     QReportText += '<td>Rep'+rlib.replicate.__str__()+'</td>'
177     QReportText += '<td><a  title="View Data Runs Record" target=_self href=/admin/exp_track/datarun/?q='+rlib.library_id+'&run_start_time__gte='+start_date.date().__str__()+'&run_start_time__lte='+end_date.date().__str__()+'>'+LIB_LANE_COUNT[rlib.library_id].__str__()+' lanes</a>'
178     QReportText += '<span style="margin-left:40px;cursor:pointer;color:#5B80B2" onClick="el=document.getElementById(\'d_'+rlib.library_id+'\');if(el){el.style.display=(el.style.display==\'none\'?\'block\':\'none\');}">details..</span><div id=d_'+rlib.library_id+'  style="display:none;border:solid 1px #cccccc;padding:3px;width:150px;max-hight:200px;overflow:auto">'+LIB_FC_SUMMARY[rlib.library_id]+'</div>'
179     QReportText += '</td>'
180     QReportText += '</tr>'
181
182   QReportText += '</table>'
183   QReportText +='</div>'
184   QReportText +='<div style="float:left;margin-left:100px">'+FCText+'</div>'
185
186   QReportText += '</div>'
187  
188   return render_to_response('htsw_reports/report.html',{'main': QReportText})
189   ###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END OF Q REPORT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190
191   TFall = Library.objects.values('antibody').order_by('antibody').distinct()
192   CLLall = Library.objects.values('cell_line').order_by('cell_line').distinct()
193
194   TFs = TFall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT,library_id__in=libidlist)
195   CLLs = CLLall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT)
196
197   # Check Replicate numbers
198   Reps = 1
199   RepRecs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT).order_by('-replicate')
200   if len(RepRecs) > 0: Reps = RepRecs[0].replicate
201   
202   ########
203   str = ''
204   str += '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">HOME</a></span>'
205   ##str += '<span style="margin-right:20px">Max Replicates: '+MaxRep.replicate.__str__()+'</span>'
206   str += '<span>Select another <b>'+EXP+'</b> Report:</span>  <select onchange="window.location=\'/htsw_reports/report?aflid=\'+this.options[selectedIndex].value; return false;">'
207   for af in Affiliation.objects.distinct():
208     str += '<option value='+af.id.__str__()
209     if AFL_Id == af.id.__str__():
210       str += ' selected'
211     str += '>'+af.name+' '+af.contact+'</option>'
212   str += '</select>'
213
214   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>' 
215
216   str += '<span style="margin-left:20px">'
217   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>'
218   str += '  | '
219   str += '<a target=_self href="/htsw_reports/report_RM?exp=Methyl-seq&aflid='+AFL_Id+'"><b>Methyl-Seq</b> Report</a>'
220   str += '<span style="margin-left:20px">(db: '+settings.DB_NAME+')</span>'
221
222   str += QReportText
223
224   bgc = '#ffffff' 
225   pbgc = '#f7f7f7'
226   str += '<table border=1 style="margin-top:5px">'
227   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>'
228   str += '<tr><th style="text-align:right">CELL LINE</th>'
229   for H in CLLs: 
230     str += '<th colspan='+Reps.__str__()+' style="text-align:center;background-color:'+bgc+'">'+Cellline.objects.get(id=H['cell_line']).cellline_name+'</th>'
231     tbgc = bgc
232     bgc = pbgc
233     pbgc = tbgc 
234   str += '</tr><tr><th style="text-align:left">TF</th>'
235   bgc = '#ffffff'
236   pbgc = '#f7f7f7'
237   for H in CLLs:
238     for r in range(1,Reps+1):
239       str += '<th style="text-align:center;background-color:'+bgc+'">Rep. '+r.__str__()+'</th>'
240     tbgc = bgc
241     bgc = pbgc
242     pbgc = tbgc
243   str += '</tr>'
244   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>'
245   bgc = '#ffffff'
246   pbgc = '#f7f7f7'
247   for H in CLLs:
248     for r in range(1,Reps+1):
249       repReads = Library.objects.filter(experiment_type='INPUT_RXLCh',affiliations__name=AFL,affiliations__contact=AFL_CNT,cell_line=H['cell_line'].__str__(),replicate=r,library_id__in=libidlist)
250
251       ### TO DO: Replace the above query with: Get DataRuns by StartRunDate BETWEEN FROM_DATE and TO_DATE, Get DataRun.Flowcell.Library records. 
252
253       str += "<td align=center style='background-color:"+bgc+"'>"
254       if len(repReads) == 0:
255         str += 'No Libraries'
256       else:
257         cnt = 0
258         for R1 in repReads:
259           rres = R1.aligned_m_reads()
260           # Check data sanlty                                                                                                                                           
261           if rres[2] != 'OK':
262             str += '<div style="border:solid red 2px">'+rres[2]
263           else:
264             cnt = rres[1]
265             if cnt > 0:
266               str += "<div style='background-color:"+getBgColor(cnt,EXP)+";font-size:140%'>"
267               str += "%1.2f" % (cnt/1000000.0)+" M"
268             else:  str += "<div style='background-color:#ffcdd0;width:100%;font-size:140%'>0 Reads"
269           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+" ("+rres[0].__str__()+" lanes)</a>, "+R1.condition.nickname
270           if R1.libtags().find('Data submitted to DCC') != -1:
271             str += "<div style='border:solid #666666 1px;'>data submitted</div>"
272           str += "</div>"
273           str += "</div>"
274       str += '</td>'
275     tbgc = bgc
276     bgc = pbgc
277     pbgc = tbgc
278   str += '</tr>' 
279
280   for T in TFs:
281     str += '<tr>'
282     try:
283       if T['antibody']:
284         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>'
285     except Antibody.DoesNotExist:
286       str += '<td>n/a</td>'
287
288     bgc = '#ffffff'
289     pbgc = '#f7f7f7'
290     for H in CLLs:
291       for r in range(1,Reps+1):
292         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,library_id__in=libidlist)
293         str += "<td align=center style='background-color:"+bgc+"'>"
294         if len(repReads) == 0: 
295           str += 'No Libraries'
296         else:
297           cnt = 0
298           for R1 in repReads:
299             rres = R1.aligned_m_reads()
300             # Check data sanity
301             if rres[2] != 'OK':
302               str += '<div style="border:solid red 2px">'+rres[2]
303             else:
304               cnt = rres[1]
305               if cnt > 0:
306                 str += "<div style='background-color:"+getBgColor(cnt,EXP)+";font-size:140%;margin:2px'>"
307                 str += "%1.2f" % (cnt/1000000.0)+" M"
308               else:  str += "<div style='background-color:#ffcdd0;width:100%;font-size:140%'>0 Reads"
309             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+" ("+rres[0].__str__()+" lanes)</a>, "+R1.condition.nickname
310             if R1.libtags().find('Data submitted to DCC') != -1:
311               str += "<div style='border:solid #666666 1px;'>data submitted</div>"
312             str += "</div>"
313             str += "</div>"
314         str += '</td>'
315       tbgc = bgc
316       bgc = pbgc
317       pbgc = tbgc
318     str += '</tr>'
319   str += '</table>'
320
321   return render_to_response('htsw_reports/report.html',{'main': str})
322
323
324 def report_RM(request): #for RNA-Seq and Methyl-Seq
325   if not request.user.is_authenticated():
326     str= '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">Please log in first</a></span>'
327     return render_to_response('htsw_reports/report.html',{'main':str})
328
329   EXP = 'RNA-seq'  
330
331   if request.has_key('exp'):
332     EXP = request['exp'] # Methyl-seq
333
334   if request.has_key('aflid'):
335     AFL_Id = request['aflid']
336     try:
337       AFL = Affiliation.objects.get(id=AFL_Id).name
338       AFL_CNT = Affiliation.objects.get(id=AFL_Id).contact
339     except ObjectDoesNotExist:
340       return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL_Id+"'")
341   else:
342     AFL = 'ENCODE_Tier1'
343     AFL_CNT = ''
344     try:
345       AFL_Id = Affiliation.objects.get(name=AFL,contact=AFL_CNT).id.__str__()
346     except ObjectDoesNotExist:
347       return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL+"'")
348
349   CLLall = Library.objects.values('cell_line').order_by('cell_line').distinct()
350   CLLs = CLLall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT)
351
352   ########
353   # Check Replicate numbers
354   Reps = 1
355   RepRecs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT).order_by('-replicate')
356   if len(RepRecs) > 0: Reps = RepRecs[0].replicate
357                                                                                                                                                                               
358   str = ''
359   str += '<span style="margin-right:20px"><a  target=_self href="/admin" target=_self" style="font-size:140%">HOME</a></span>'
360   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;">'
361   for af in Affiliation.objects.distinct():
362     str += '<option value='+af.id.__str__()
363     if AFL_Id == af.id.__str__():
364       str += ' selected'
365     str += '>'+af.name+' '+af.contact+'</option>'
366   str += '</select>'
367
368   if EXP == 'RNA-seq':
369     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>'
370     str += '<span style="margin-left:20px">'
371     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>'
372     str += '  | '
373     str += '<a target=_self href="/htsw_reports/report_RM?exp=Methyl-seq&aflid='+AFL_Id+'"><b>Methyl-Seq</b> Report</a>'
374   else:
375     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>'
376     str += '<span style="margin-left:20px">'
377     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>'
378     str += '  | '
379     str += '<a target=_self href="/htsw_reports/report_RM?exp=RNA-seq&aflid='+AFL_Id+'"><b>RNA-Seq</b> Report</a>'
380
381   str += '<br/><br/><table border=1>'
382   str += '<tr><th colspan='+(Reps*len(CLLs)).__str__()+' style="text-align:center">'+AFL+' '+AFL_CNT+'  <span style="font-size:140%">'+EXP+'</span></th></tr>'
383   str += '<tr>'
384   bgc = '#ffffff'
385   pbgc = '#f7f7f7'
386   for H in CLLs:
387     str += '<th colspan='+Reps.__str__()+' style="text-align:center;background-color:'+bgc+'">'+Cellline.objects.get(id=H['cell_line']).cellline_name+'</th>'
388     tbgc = bgc
389     bgc = pbgc
390     pbgc = tbgc
391   str += '</tr><tr>'
392   bgc = '#ffffff'
393   pbgc = '#f7f7f7'
394   for H in CLLs:
395     for r in range(1,Reps+1):
396       str += '<th style="text-align:center;background-color:'+bgc+'">Rep. '+r.__str__()+'</th>'
397     tbgc = bgc
398     bgc = pbgc
399     pbgc = tbgc
400   str += '</tr>'
401
402   str += '<tr>' 
403   bgc = '#ffffff'
404   pbgc = '#f7f7f7'
405   for H in CLLs:
406     for r in range(1,Reps+1):
407       repReads = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT,cell_line=H['cell_line'],replicate=r)                                                                                                                    
408       str += "<td align=center valign=top style='background-color:"+bgc+"'>"
409       if len(repReads) == 0:
410         str += 'No Libraries'
411       else:
412         cnt = 0
413         for R1 in repReads:
414           rres = R1.aligned_m_reads()
415           # Check data sanlty   
416           if rres[2] != 'OK':
417             str += '<div style="border:solid red 2px">'+rres[2]
418           else:
419             cnt = rres[1]
420             if cnt > 0:
421               str += "<div style='background-color:"+getBgColor(cnt,EXP)+";border:solid #cccccc 1px;font-size:140%'>"
422               str += "%1.2f" % (cnt/1000000.0)+" M"
423             else:  str += "<div style='background-color:#ffcdd0;border:solid #cccccc 1px;width:100%;font-size:140%'>0 Reads"
424           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+" ("+rres[0].__str__()+" lanes)</a>, "+R1.condition.nickname+", "+R1.library_species.common_name+"</div>"
425           str += "<div style='font-size:70%'>\""+R1.library_name+"\""
426           if R1.libtags().find('Data submitted to DCC') != -1:
427             str += "<div style='width:75%;border:solid #666666 1px;'>data submitted</div>"
428           str += "</div>"
429           str += "</div>"
430       str += '</td>'
431     tbgc = bgc
432     bgc = pbgc
433     pbgc = tbgc
434   str += '</tr>'
435   str += '</table>'
436
437   return render_to_response('htsw_reports/report.html',{'main': str})
438
439 def getNotRanFCs(request):
440   if not request.user.is_authenticated():
441     str= '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">Please log in first</a></span>'
442     return render_to_response('htsw_reports/report.html',{'main':str})
443
444   FCall = FlowCell.objects.order_by('-run_date').distinct()
445   str = '<table><tr><th>FlowCell</th><th>Lanes</th><th>Creation Date</th></tr>'
446   for f in FCall:
447     try:
448       t = DataRun.objects.get(fcid=f.id)
449     except ObjectDoesNotExist:
450       str += '<tr><td>'+f.flowcell_id+'</td><td>'+f.Lanes()+'</td><td>'+f.run_date.__str__()+'</td></tr>'
451   str += "</table>"
452   return render_to_response('htsw_reports/report.html',{'main':str})
453  
454 def test_Libs(request):
455   if not request.user.is_authenticated():
456     str= '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">Please log in first</a></span>'
457     return render_to_response('htsw_reports/report.html',{'main':str})
458
459   str = ''
460   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>'
461   allLibs = Library.objects.all()
462   #allLibs = Library.objects.filter(antibody__isnull=False)
463   for L in allLibs:
464     str += '<tr>'
465     str += '<td>'+L.library_id+'</td><td>'+L.library_name+'</td>'   
466     str += '<td>'
467     str += L.experiment_type+'_'
468     if L.cell_line.cellline_name != 'Unknown':
469       str += L.cell_line.cellline_name+'_'
470
471     try:
472       if L.antibody is not None:
473         str += L.antibody.nickname + '_'
474     except Antibody.DoesNotExist:
475       pass
476   
477     str += 'Rep'+L.replicate.__str__()
478     str += '</td></tr>' 
479
480   str += '</table>'
481   return HttpResponse(str)