From 69a1cf656c9d6cc248e8ebd5ac0794a009bafb40 Mon Sep 17 00:00:00 2001 From: Rami Rauch Date: Fri, 20 Feb 2009 22:18:08 +0000 Subject: [PATCH] added grey highlight for bad libraries (by tag text) --- .../htswfrontend/htsw_reports/q_reports.py | 481 ++++++++++++++++++ 1 file changed, 481 insertions(+) create mode 100755 htswfrontend/htswfrontend/htsw_reports/q_reports.py diff --git a/htswfrontend/htswfrontend/htsw_reports/q_reports.py b/htswfrontend/htswfrontend/htsw_reports/q_reports.py new file mode 100755 index 0000000..dc06352 --- /dev/null +++ b/htswfrontend/htswfrontend/htsw_reports/q_reports.py @@ -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 = '
' + #tstr += res[0].__str__()+' Lanes, '+rc+' M Reads' + #tstr += '
' + + return bgcolor + +def q_report1(request): + if not request.user.is_authenticated(): + str = 'Please log in first' + 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
HOME
') + + ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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 += '
HOME

Time Report Generator for ChIP-Seq

' + QReportText += '
' + QReportText += '

' + QReportText += EXP+' ' + QReportText += '     Start Date: ' + QReportText += '' # class="vDateField" />' # id="id_run_start_time_date"/>' + QReportText += '    End Date: ' + #QReportText += 'Today | ' + #QReportText += 'Calendar' + #QReportText += 'End Date: Today | Calendar' + QReportText += '

' + + QReportText +='
' + 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 += '

For '+AFL+' '+AFL_CNT+''+' between '+s_year+'-'+arMonth[s_month]+'-'+s_day+' and '+e_year+'-'+arMonth[e_month]+'-'+e_day+end_date_text+'

' + QReportText +='
' + FCText = '

The following Flowcells were sequenced:

' + FCText += '' + 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')] += '
'+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 += '' + + FCText += '
Flowcell IdRan On
'+DR.fcid.flowcell_id+''+DR.run_start_time.__str__()+'
' + + ## 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 +='
' + QReportText += '

' + if RelevantLibs: + QReportText += 'The following '+len(RelevantLibs).__str__()+' ' + else: + QReportText += 'No ' + QReportText += EXP+' Libraries were sequenced:

' + QReportText += '
(bad libraries marked in grey)
' + QReportText += '' + for rlib in RelevantLibs: + bgColor = '' + if rlib.libtags().find('Bad library')!=-1: bgColor='#cccccc' + QReportText += '' + if rlib.antibody is not None: QReportText += '' + else: + if rlib.experiment_type == 'INPUT_RXLCh': QReportText += '' + else: QReportText += '' + + QReportText += '' + QReportText += '' + QReportText += '' + QReportText += '' + QReportText += '' + + QReportText += '
Library IdAntibodyCell lineConditionReplicateLanes ran (during this period)
'+rlib.library_id+''+rlib.antibody.nickname+'[INPUT_RXLCh]none'+rlib.cell_line.nickname+''+rlib.condition.nickname+'Rep'+rlib.replicate.__str__()+''+LIB_LANE_COUNT[rlib.library_id].__str__()+' lanes' + QReportText += 'details..' + QReportText += '
' + QReportText +='
' + QReportText +='
'+FCText+'
' + + QReportText += '
' + + 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 += 'HOME' + ##str += 'Max Replicates: '+MaxRep.replicate.__str__()+'' + str += 'Select another '+EXP+' Report: ' + + str += 'color scheme: > 12 M > 5 M > 3 M < 3 M' + + str += '' + str += 'Switch to: '+AFL+' '+AFL_CNT+' RNA-Seq Report' + str += ' | ' + str += 'Methyl-Seq Report' + str += '(db: '+settings.DB_NAME+')' + + str += QReportText + + bgc = '#ffffff' + pbgc = '#f7f7f7' + str += '' + str += '' + str += '' + for H in CLLs: + str += '' + tbgc = bgc + bgc = pbgc + pbgc = tbgc + str += '' + bgc = '#ffffff' + pbgc = '#f7f7f7' + for H in CLLs: + for r in range(1,Reps+1): + str += '' + tbgc = bgc + bgc = pbgc + pbgc = tbgc + str += '' + str += '' + 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 += "' + tbgc = bgc + bgc = pbgc + pbgc = tbgc + str += '' + + for T in TFs: + str += '' + try: + if T['antibody']: + str += '' + except Antibody.DoesNotExist: + str += '' + + 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 += "' + tbgc = bgc + bgc = pbgc + pbgc = tbgc + str += '' + str += '
PROJECT'+AFL+' '+AFL_CNT+' '+EXP+'
CELL LINE'+Cellline.objects.get(id=H['cell_line']).cellline_name+'
TFRep. '+r.__str__()+'
Total Chromatin" + 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 += '
'+rres[2] + else: + cnt = rres[1] + if cnt > 0: + str += "
" + str += "%1.2f" % (cnt/1000000.0)+" M" + else: str += "
0 Reads" + str += "
"+R1.library_id+" ("+rres[0].__str__()+" lanes), "+R1.condition.nickname + if R1.libtags().find('Data submitted to DCC') != -1: + str += "
data submitted
" + str += "
" + str += "
" + str += '
'+Antibody.objects.get(id=T['antibody']).nickname+'n/a" + 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 += '
'+rres[2] + else: + cnt = rres[1] + if cnt > 0: + str += "
" + str += "%1.2f" % (cnt/1000000.0)+" M" + else: str += "
0 Reads" + str += "
"+R1.library_id+" ("+rres[0].__str__()+" lanes), "+R1.condition.nickname + if R1.libtags().find('Data submitted to DCC') != -1: + str += "
data submitted
" + str += "
" + str += "
" + str += '
' + + 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= 'Please log in first' + 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 += 'HOME' + str += 'Select another '+EXP+' Report: ' + + if EXP == 'RNA-seq': + str += 'color scheme: > 20 M > 10 M > 6 M < 6 M' + str += '' + str += 'Switch to: '+AFL+' '+AFL_CNT+' ChIP-Seq Report' + str += ' | ' + str += 'Methyl-Seq Report' + else: + str += 'color scheme: > 12 M > 5 M > 3 M < 3 M' + str += '' + str += 'Switch to: '+AFL+' '+AFL_CNT+' ChIP-Seq Report' + str += ' | ' + str += 'RNA-Seq Report' + + str += '

' + str += '' + str += '' + bgc = '#ffffff' + pbgc = '#f7f7f7' + for H in CLLs: + str += '' + tbgc = bgc + bgc = pbgc + pbgc = tbgc + str += '' + bgc = '#ffffff' + pbgc = '#f7f7f7' + for H in CLLs: + for r in range(1,Reps+1): + str += '' + tbgc = bgc + bgc = pbgc + pbgc = tbgc + str += '' + + str += '' + 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 += "' + tbgc = bgc + bgc = pbgc + pbgc = tbgc + str += '' + str += '
'+AFL+' '+AFL_CNT+' '+EXP+'
'+Cellline.objects.get(id=H['cell_line']).cellline_name+'
Rep. '+r.__str__()+'
" + 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 += '
'+rres[2] + else: + cnt = rres[1] + if cnt > 0: + str += "
" + str += "%1.2f" % (cnt/1000000.0)+" M" + else: str += "
0 Reads" + str += "
"+R1.library_id+" ("+rres[0].__str__()+" lanes), "+R1.condition.nickname+", "+R1.library_species.common_name+"
" + str += "
\""+R1.library_name+"\"" + if R1.libtags().find('Data submitted to DCC') != -1: + str += "
data submitted
" + str += "
" + str += "
" + str += '
' + + return render_to_response('htsw_reports/report.html',{'main': str}) + +def getNotRanFCs(request): + if not request.user.is_authenticated(): + str= 'Please log in first' + return render_to_response('htsw_reports/report.html',{'main':str}) + + FCall = FlowCell.objects.order_by('-run_date').distinct() + str = '' + for f in FCall: + try: + t = DataRun.objects.get(fcid=f.id) + except ObjectDoesNotExist: + str += '' + str += "
FlowCellLanesCreation Date
'+f.flowcell_id+''+f.Lanes()+''+f.run_date.__str__()+'
" + return render_to_response('htsw_reports/report.html',{'main':str}) + +def test_Libs(request): + if not request.user.is_authenticated(): + str= 'Please log in first' + return render_to_response('htsw_reports/report.html',{'main':str}) + + str = '' + str += '' + allLibs = Library.objects.all() + #allLibs = Library.objects.filter(antibody__isnull=False) + for L in allLibs: + str += '' + str += '' + str += '' + + str += '
Lib IDCurrent Libaray Name (Free Text)Auto-composed Libaray Name (antibody + celline + libid + species + [replicate])
'+L.library_id+''+L.library_name+'' + 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 += '
' + return HttpResponse(str) -- 2.30.2