added grey highlight for bad libraries (by tag text)
authorRami Rauch <rrauch@stanford.edu>
Fri, 20 Feb 2009 22:18:08 +0000 (22:18 +0000)
committerRami Rauch <rrauch@stanford.edu>
Fri, 20 Feb 2009 22:18:08 +0000 (22:18 +0000)
htswfrontend/htswfrontend/htsw_reports/q_reports.py [new file with mode: 0755]

diff --git a/htswfrontend/htswfrontend/htsw_reports/q_reports.py b/htswfrontend/htswfrontend/htsw_reports/q_reports.py
new file mode 100755 (executable)
index 0000000..dc06352
--- /dev/null
@@ -0,0 +1,481 @@
+from htswfrontend.exp_track.models import *
+from django.http import HttpResponse
+from django.core.exceptions import ObjectDoesNotExist
+from django.shortcuts import render_to_response, get_object_or_404
+
+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"}
+def getBgColor(reads_cnt,exp_type):
+  # 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
+  bgcolor = '#ffcdd0'  # Red is the color for minimum read counts                                                                                                                            
+  rc_thr = [12000000,5000000,3000000] # Default for ChIP-Seq and Methyl-Seq
+  if exp_type == 'RNA-seq':
+    rc_thr = [20000000,10000000,6000000]
+
+  if reads_cnt > rc_thr[0]:
+    bgcolor = '#66ff66'  # Green                                                                                                                                                                                                                                               
+  else:
+    if reads_cnt > rc_thr[1]:
+      bgcolor ='#00ccff'  # Blue                                                                                                                                                                                                                                               
+    else:
+       if reads_cnt > rc_thr[2]:
+         bgcolor ='#ffcc33'  # Orange                                                                                                                                                                                                                                          
+  #tstr = '<div style="background-color:'+bgcolor+';color:black">'
+  #tstr += res[0].__str__()+' Lanes, '+rc+' M Reads'
+  #tstr += '</div>'
+
+  return bgcolor
+
+def q_report1(request):
+  if not request.user.is_authenticated():
+    str = '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">Please log in first</a></span>'
+    return render_to_response('htsw_reports/report.html',{'main':str}) 
+
+  EXP = 'ChIP-seq'
+
+  if request.has_key('aflid'):
+    AFL_Id = request['aflid']
+    try:
+      AFL = Affiliation.objects.get(id=AFL_Id).name
+      AFL_CNT = Affiliation.objects.get(id=AFL_Id).contact
+    except ObjectDoesNotExist:
+      return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL_Id+"'")
+  else:
+    AFL = 'ENCODE_Tier1'
+    AFL_CNT = ''
+    try:
+      AFL_Id = Affiliation.objects.get(name=AFL,contact=AFL_CNT).id.__str__()
+    except ObjectDoesNotExist:
+      return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL+"'")
+
+  if request.user.username == 'caltech':
+    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>')
+
+  ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q REPORT PART ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  start_date = datetime(2009,01,01).date().__str__()
+  end_date = datetime(2011,01,01)
+  end_date_text = ''
+
+  if request.has_key('start_date'):
+    start_date = request['start_date']
+
+  if request.has_key('end_date'):
+    end_date = request['end_date']
+  else: #end_date = today
+    end_date = end_date.now().date().__str__()
+    end_date_text = ' (Today)'
+
+  DRuns = []
+  if start_date and end_date:
+    start_date = datetime(int(start_date.split('-')[0]),int(start_date.split('-')[1]),int(start_date.split('-')[2]),0,0,0)
+    end_date = datetime(int(end_date.split('-')[0]),int(end_date.split('-')[1]),int(end_date.split('-')[2]),0,0,0)
+    DRuns = DataRun.objects.filter(run_start_time__range=(start_date, end_date))
+    
+  QReportText = ''
+  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>'
+  QReportText += '<form action="" method="get" id="qreport_form">'
+  QReportText += '<p>'
+  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;">'
+
+  if request.user.username == 'caltech':
+    Affil_List = Affiliation.objects.filter(name__contains='ENCODE').distinct()
+  else:
+    Affil_List = Affiliation.objects.distinct()
+
+  for af in Affil_List:
+    QReportText += '<option value='+af.id.__str__()
+    if AFL_Id == af.id.__str__():
+      QReportText += ' selected'
+    QReportText += '>'+af.name+' '+af.contact+'</option>'
+  QReportText += '</select>'
+  QReportText += '&nbsp;&nbsp;&nbsp;&nbsp; Start Date: '
+  QReportText += '<input type="text" maxlength="10" value="'+start_date.date().__str__()+'" size="10" name="start_date" />'  # class="vDateField" />' # id="id_run_start_time_date"/>'
+  QReportText += '&nbsp;&nbsp;&nbsp;&nbsp;End Date: <input type="text" maxlength="10" value="'+end_date.date().__str__()+'" size="10" name="end_date" />' 
+  #QReportText += '<span><a href="javascript:DateTimeShortcuts.handleCalendarQuickLink(0, 0);">Today</a> | <a href="javascript:DateTimeShortcuts.openCalendar(0);" id="calendarlink0">'
+  #QReportText += '<img src="http://myerslab.stanford.edu/media/img/admin/icon_calendar.gif" alt="Calendar"/></a></span>'
+  #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>'
+  QReportText += '<span style="margin-left:10px"><input type="submit" value="Run Report" name="runreport"  /></span></p>'
+
+  QReportText +='<div style="border:solid #cccccc 1px;padding:2px;background-color:#FFFFCC;margin-top:5px">'
+  s_year = start_date.date().__str__().split("-")[0]
+  s_month = start_date.date().__str__().split("-")[1]
+  s_day = start_date.date().__str__().split("-")[2]
+  e_year = end_date.date().__str__().split("-")[0]
+  e_month = end_date.date().__str__().split("-")[1]
+  e_day = end_date.date().__str__().split("-")[2]
+  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>'
+  QReportText +='<div style="float:left">'
+  FCText = '<h4>The following Flowcells were sequenced:</h4>' 
+  FCText += '<table border=1><tr><th>Flowcell Id</th><th>Ran On</th></tr>'
+  libidlist = []
+  LIB_LANE_COUNT = {}
+  LIB_FC_SUMMARY = {}
+  for DR in  DRuns:
+   libidlist.append(DR.fcid.lane_1_library.library_id)
+   #if LIB_LANE_COUNT.has_key(DR.fcid.lane_1_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_1_library.library_id] += 1
+   #else: LIB_LANE_COUNT[DR.fcid.lane_1_library.library_id] = 1
+   libidlist.append(DR.fcid.lane_2_library.library_id)
+   #if LIB_LANE_COUNT.has_key(DR.fcid.lane_2_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_2_library.library_id] += 1
+   #else: LIB_LANE_COUNT[DR.fcid.lane_2_library.library_id] = 1
+   libidlist.append(DR.fcid.lane_3_library.library_id)
+   #if LIB_LANE_COUNT.has_key(DR.fcid.lane_3_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_3_library.library_id] += 1
+   #else: LIB_LANE_COUNT[DR.fcid.lane_3_library.library_id] = 1
+   libidlist.append(DR.fcid.lane_4_library.library_id)
+   #if LIB_LANE_COUNT.has_key(DR.fcid.lane_4_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_4_library.library_id] += 1
+   #else: LIB_LANE_COUNT[DR.fcid.lane_4_library.library_id] = 1
+   libidlist.append(DR.fcid.lane_5_library.library_id)
+   #if LIB_LANE_COUNT.has_key(DR.fcid.lane_5_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_5_library.library_id] += 1
+   #else: LIB_LANE_COUNT[DR.fcid.lane_5_library.library_id] = 1
+   libidlist.append(DR.fcid.lane_6_library.library_id)
+   #if LIB_LANE_COUNT.has_key(DR.fcid.lane_6_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_6_library.library_id] += 1
+   #else: LIB_LANE_COUNT[DR.fcid.lane_6_library.library_id] = 1
+   libidlist.append(DR.fcid.lane_7_library.library_id)
+   #if LIB_LANE_COUNT.has_key(DR.fcid.lane_7_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_7_library.library_id] += 1
+   #else: LIB_LANE_COUNT[DR.fcid.lane_7_library.library_id] = 1
+   libidlist.append(DR.fcid.lane_8_library.library_id)
+   #if LIB_LANE_COUNT.has_key(DR.fcid.lane_8_library.library_id): LIB_LANE_COUNT[DR.fcid.lane_8_library.library_id] += 1
+   #else: LIB_LANE_COUNT[DR.fcid.lane_8_library.library_id] = 1
+   for i in range(1,9):
+     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
+     else: LIB_LANE_COUNT[eval('DR.fcid.lane_'+i.__str__()+'_library.library_id')] = 1
+     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__()+')'
+     else: LIB_FC_SUMMARY[eval('DR.fcid.lane_'+i.__str__()+'_library.library_id')] = DR.fcid.flowcell_id+' (lane '+i.__str__()+')' 
+
+   FCText += '<tr><td>'+DR.fcid.flowcell_id+'</td><td>'+DR.run_start_time.__str__()+'</td></tr>'
+
+  FCText += '</table>' 
+
+  ## RelevantLibs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT,library_id__in=libidlist).order_by('antibody','cell_line')
+
+  if EXP == 'ChIP-seq':
+    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')
+  else:
+    RelevantLibs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT,library_id__in=libidlist).order_by('antibody','cell_line')
+
+  rliblist = []
+
+  QReportText +='<div style="float:left">'
+  QReportText += '<h4>'
+  if RelevantLibs:
+    QReportText += 'The following <u>'+len(RelevantLibs).__str__()+'</u> '
+  else:
+    QReportText += 'No ' 
+  QReportText += EXP+' Libraries  were sequenced:</h4>'
+  QReportText += '<div style="width:200px;background-color:#cccccc;font-size:90%">(bad libraries marked in grey)</div>'
+  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>'
+  for rlib in RelevantLibs:
+    bgColor = ''
+    if rlib.libtags().find('Bad library')!=-1: bgColor='#cccccc'
+    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>'
+    if rlib.antibody is not None: QReportText += '<td>'+rlib.antibody.nickname+'</td>'
+    else: 
+      if rlib.experiment_type == 'INPUT_RXLCh':  QReportText += '<td>[INPUT_RXLCh]</td>'
+      else: QReportText += '<td>none</td>'
+    
+    QReportText += '<td>'+rlib.cell_line.nickname+'</td>'
+    QReportText += '<td>'+rlib.condition.nickname+'</td>'
+    QReportText += '<td>Rep'+rlib.replicate.__str__()+'</td>'
+    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>'
+    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>'
+    QReportText += '</td>'
+    QReportText += '</tr>'
+
+  QReportText += '</table>'
+  QReportText +='</div>'
+  QReportText +='<div style="float:left;margin-left:100px">'+FCText+'</div>'
+
+  QReportText += '</div>'
+  return render_to_response('htsw_reports/report.html',{'main': QReportText})
+  ###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END OF Q REPORT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+  TFall = Library.objects.values('antibody').order_by('antibody').distinct()
+  CLLall = Library.objects.values('cell_line').order_by('cell_line').distinct()
+
+  TFs = TFall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT,library_id__in=libidlist)
+  CLLs = CLLall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT)
+
+  # Check Replicate numbers
+  Reps = 1
+  RepRecs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT).order_by('-replicate')
+  if len(RepRecs) > 0: Reps = RepRecs[0].replicate
+  
+  ########
+  str = ''
+  str += '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">HOME</a></span>'
+  ##str += '<span style="margin-right:20px">Max Replicates: '+MaxRep.replicate.__str__()+'</span>'
+  str += '<span>Select another <b>'+EXP+'</b> Report:</span>  <select onchange="window.location=\'/htsw_reports/report?aflid=\'+this.options[selectedIndex].value; return false;">'
+  for af in Affiliation.objects.distinct():
+    str += '<option value='+af.id.__str__()
+    if AFL_Id == af.id.__str__():
+      str += ' selected'
+    str += '>'+af.name+' '+af.contact+'</option>'
+  str += '</select>'
+
+  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>' 
+
+  str += '<span style="margin-left:20px">'
+  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>'
+  str += '  | '
+  str += '<a target=_self href="/htsw_reports/report_RM?exp=Methyl-seq&aflid='+AFL_Id+'"><b>Methyl-Seq</b> Report</a>'
+  str += '<span style="margin-left:20px">(db: '+settings.DB_NAME+')</span>'
+
+  str += QReportText
+
+  bgc = '#ffffff' 
+  pbgc = '#f7f7f7'
+  str += '<table border=1 style="margin-top:5px">'
+  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>'
+  str += '<tr><th style="text-align:right">CELL LINE</th>'
+  for H in CLLs: 
+    str += '<th colspan='+Reps.__str__()+' style="text-align:center;background-color:'+bgc+'">'+Cellline.objects.get(id=H['cell_line']).cellline_name+'</th>'
+    tbgc = bgc
+    bgc = pbgc
+    pbgc = tbgc 
+  str += '</tr><tr><th style="text-align:left">TF</th>'
+  bgc = '#ffffff'
+  pbgc = '#f7f7f7'
+  for H in CLLs:
+    for r in range(1,Reps+1):
+      str += '<th style="text-align:center;background-color:'+bgc+'">Rep. '+r.__str__()+'</th>'
+    tbgc = bgc
+    bgc = pbgc
+    pbgc = tbgc
+  str += '</tr>'
+  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>'
+  bgc = '#ffffff'
+  pbgc = '#f7f7f7'
+  for H in CLLs:
+    for r in range(1,Reps+1):
+      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)
+
+      ### TO DO: Replace the above query with: Get DataRuns by StartRunDate BETWEEN FROM_DATE and TO_DATE, Get DataRun.Flowcell.Library records. 
+
+      str += "<td align=center style='background-color:"+bgc+"'>"
+      if len(repReads) == 0:
+        str += 'No Libraries'
+      else:
+        cnt = 0
+        for R1 in repReads:
+          rres = R1.aligned_m_reads()
+          # Check data sanlty                                                                                                                                           
+          if rres[2] != 'OK':
+            str += '<div style="border:solid red 2px">'+rres[2]
+          else:
+            cnt = rres[1]
+            if cnt > 0:
+              str += "<div style='background-color:"+getBgColor(cnt,EXP)+";font-size:140%'>"
+              str += "%1.2f" % (cnt/1000000.0)+" M"
+            else:  str += "<div style='background-color:#ffcdd0;width:100%;font-size:140%'>0 Reads"
+          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
+          if R1.libtags().find('Data submitted to DCC') != -1:
+            str += "<div style='border:solid #666666 1px;'>data submitted</div>"
+          str += "</div>"
+          str += "</div>"
+      str += '</td>'
+    tbgc = bgc
+    bgc = pbgc
+    pbgc = tbgc
+  str += '</tr>' 
+
+  for T in TFs:
+    str += '<tr>'
+    try:
+      if T['antibody']:
+        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>'
+    except Antibody.DoesNotExist:
+      str += '<td>n/a</td>'
+
+    bgc = '#ffffff'
+    pbgc = '#f7f7f7'
+    for H in CLLs:
+      for r in range(1,Reps+1):
+        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)
+        str += "<td align=center style='background-color:"+bgc+"'>"
+        if len(repReads) == 0: 
+          str += 'No Libraries'
+        else:
+          cnt = 0
+          for R1 in repReads:
+            rres = R1.aligned_m_reads()
+            # Check data sanity
+            if rres[2] != 'OK':
+              str += '<div style="border:solid red 2px">'+rres[2]
+            else:
+              cnt = rres[1]
+              if cnt > 0:
+                str += "<div style='background-color:"+getBgColor(cnt,EXP)+";font-size:140%;margin:2px'>"
+                str += "%1.2f" % (cnt/1000000.0)+" M"
+              else:  str += "<div style='background-color:#ffcdd0;width:100%;font-size:140%'>0 Reads"
+            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
+            if R1.libtags().find('Data submitted to DCC') != -1:
+              str += "<div style='border:solid #666666 1px;'>data submitted</div>"
+            str += "</div>"
+            str += "</div>"
+        str += '</td>'
+      tbgc = bgc
+      bgc = pbgc
+      pbgc = tbgc
+    str += '</tr>'
+  str += '</table>'
+
+  return render_to_response('htsw_reports/report.html',{'main': str})
+
+
+def report_RM(request): #for RNA-Seq and Methyl-Seq
+  if not request.user.is_authenticated():
+    str= '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">Please log in first</a></span>'
+    return render_to_response('htsw_reports/report.html',{'main':str})
+
+  EXP = 'RNA-seq'  
+
+  if request.has_key('exp'):
+    EXP = request['exp'] # Methyl-seq
+
+  if request.has_key('aflid'):
+    AFL_Id = request['aflid']
+    try:
+      AFL = Affiliation.objects.get(id=AFL_Id).name
+      AFL_CNT = Affiliation.objects.get(id=AFL_Id).contact
+    except ObjectDoesNotExist:
+      return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL_Id+"'")
+  else:
+    AFL = 'ENCODE_Tier1'
+    AFL_CNT = ''
+    try:
+      AFL_Id = Affiliation.objects.get(name=AFL,contact=AFL_CNT).id.__str__()
+    except ObjectDoesNotExist:
+      return HttpResponse("ERROR: Affiliation Record Not Found for: '"+AFL+"'")
+
+  CLLall = Library.objects.values('cell_line').order_by('cell_line').distinct()
+  CLLs = CLLall.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT)
+
+  ########
+  # Check Replicate numbers
+  Reps = 1
+  RepRecs = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT).order_by('-replicate')
+  if len(RepRecs) > 0: Reps = RepRecs[0].replicate
+                                                                                                                                                                              
+  str = ''
+  str += '<span style="margin-right:20px"><a  target=_self href="/admin" target=_self" style="font-size:140%">HOME</a></span>'
+  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;">'
+  for af in Affiliation.objects.distinct():
+    str += '<option value='+af.id.__str__()
+    if AFL_Id == af.id.__str__():
+      str += ' selected'
+    str += '>'+af.name+' '+af.contact+'</option>'
+  str += '</select>'
+
+  if EXP == 'RNA-seq':
+    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>'
+    str += '<span style="margin-left:20px">'
+    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>'
+    str += '  | '
+    str += '<a target=_self href="/htsw_reports/report_RM?exp=Methyl-seq&aflid='+AFL_Id+'"><b>Methyl-Seq</b> Report</a>'
+  else:
+    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>'
+    str += '<span style="margin-left:20px">'
+    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>'
+    str += '  | '
+    str += '<a target=_self href="/htsw_reports/report_RM?exp=RNA-seq&aflid='+AFL_Id+'"><b>RNA-Seq</b> Report</a>'
+
+  str += '<br/><br/><table border=1>'
+  str += '<tr><th colspan='+(Reps*len(CLLs)).__str__()+' style="text-align:center">'+AFL+' '+AFL_CNT+'  <span style="font-size:140%">'+EXP+'</span></th></tr>'
+  str += '<tr>'
+  bgc = '#ffffff'
+  pbgc = '#f7f7f7'
+  for H in CLLs:
+    str += '<th colspan='+Reps.__str__()+' style="text-align:center;background-color:'+bgc+'">'+Cellline.objects.get(id=H['cell_line']).cellline_name+'</th>'
+    tbgc = bgc
+    bgc = pbgc
+    pbgc = tbgc
+  str += '</tr><tr>'
+  bgc = '#ffffff'
+  pbgc = '#f7f7f7'
+  for H in CLLs:
+    for r in range(1,Reps+1):
+      str += '<th style="text-align:center;background-color:'+bgc+'">Rep. '+r.__str__()+'</th>'
+    tbgc = bgc
+    bgc = pbgc
+    pbgc = tbgc
+  str += '</tr>'
+
+  str += '<tr>' 
+  bgc = '#ffffff'
+  pbgc = '#f7f7f7'
+  for H in CLLs:
+    for r in range(1,Reps+1):
+      repReads = Library.objects.filter(experiment_type=EXP,affiliations__name=AFL,affiliations__contact=AFL_CNT,cell_line=H['cell_line'],replicate=r)                                                                                                                    
+      str += "<td align=center valign=top style='background-color:"+bgc+"'>"
+      if len(repReads) == 0:
+        str += 'No Libraries'
+      else:
+        cnt = 0
+        for R1 in repReads:
+          rres = R1.aligned_m_reads()
+          # Check data sanlty   
+          if rres[2] != 'OK':
+            str += '<div style="border:solid red 2px">'+rres[2]
+          else:
+            cnt = rres[1]
+            if cnt > 0:
+              str += "<div style='background-color:"+getBgColor(cnt,EXP)+";border:solid #cccccc 1px;font-size:140%'>"
+              str += "%1.2f" % (cnt/1000000.0)+" M"
+            else:  str += "<div style='background-color:#ffcdd0;border:solid #cccccc 1px;width:100%;font-size:140%'>0 Reads"
+          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>"
+          str += "<div style='font-size:70%'>\""+R1.library_name+"\""
+          if R1.libtags().find('Data submitted to DCC') != -1:
+            str += "<div style='width:75%;border:solid #666666 1px;'>data submitted</div>"
+          str += "</div>"
+          str += "</div>"
+      str += '</td>'
+    tbgc = bgc
+    bgc = pbgc
+    pbgc = tbgc
+  str += '</tr>'
+  str += '</table>'
+
+  return render_to_response('htsw_reports/report.html',{'main': str})
+
+def getNotRanFCs(request):
+  if not request.user.is_authenticated():
+    str= '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">Please log in first</a></span>'
+    return render_to_response('htsw_reports/report.html',{'main':str})
+
+  FCall = FlowCell.objects.order_by('-run_date').distinct()
+  str = '<table><tr><th>FlowCell</th><th>Lanes</th><th>Creation Date</th></tr>'
+  for f in FCall:
+    try:
+      t = DataRun.objects.get(fcid=f.id)
+    except ObjectDoesNotExist:
+      str += '<tr><td>'+f.flowcell_id+'</td><td>'+f.Lanes()+'</td><td>'+f.run_date.__str__()+'</td></tr>'
+  str += "</table>"
+  return render_to_response('htsw_reports/report.html',{'main':str})
+def test_Libs(request):
+  if not request.user.is_authenticated():
+    str= '<span style="margin-right:20px"><a target=_self href="/admin" target=_self style="font-size:140%">Please log in first</a></span>'
+    return render_to_response('htsw_reports/report.html',{'main':str})
+
+  str = ''
+  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>'
+  allLibs = Library.objects.all()
+  #allLibs = Library.objects.filter(antibody__isnull=False)
+  for L in allLibs:
+    str += '<tr>'
+    str += '<td>'+L.library_id+'</td><td>'+L.library_name+'</td>'   
+    str += '<td>'
+    str += L.experiment_type+'_'
+    if L.cell_line.cellline_name != 'Unknown':
+      str += L.cell_line.cellline_name+'_'
+
+    try:
+      if L.antibody is not None:
+        str += L.antibody.nickname + '_'
+    except Antibody.DoesNotExist:
+      pass
+  
+    str += 'Rep'+L.replicate.__str__()
+    str += '</td></tr>' 
+
+  str += '</table>'
+  return HttpResponse(str)