1 # some core functions of the exp tracker module
2 from django.http import HttpResponse
3 from datetime import datetime
6 from htswfrontend import settings
7 from htswfrontend.exp_track.models import FlowCell, DataRun
8 from htswfrontend.fctracker.models import Library
9 from django.core.exceptions import ObjectDoesNotExist
10 from django.core.mail import send_mail, mail_admins
12 def updStatus(request):
15 UpdatedStatus = 'unknown'
18 ClIP = request.META['REMOTE_ADDR']
21 #Check access permission
22 if (settings.ALLOWED_IPS.has_key(ClIP)): granted = True
23 if not granted: return HttpResponse("access denied.")
26 # ~~~~~~Parameters for the job ~~~~
27 if request.has_key('fcid'):
28 fcid = request['fcid']
30 return HttpResponse('missing fcid')
32 if request.has_key('runf'):
33 runfolder = request['runf']
35 return HttpResponse('missing runf')
38 if request.has_key('updst'):
39 UpdatedStatus = request['updst']
41 return HttpResponse('missing status')
43 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45 # Update Data Run status in DB
46 # Try get rec. If not found return 'entry not found + <fcid><runfolder>', if found try update and return updated
48 rec = DataRun.objects.get(run_folder=runfolder)
49 rec.run_status = UpdatedStatus
51 #if there's a message update that too
52 mytimestamp = datetime.now().__str__()
53 mytimestamp = re.sub(pattern=":[^:]*$",repl="",string=mytimestamp)
54 if request.has_key('msg'):
55 rec.run_note += ", "+request['msg']+" ("+mytimestamp+")"
57 if UpdatedStatus == '1':
58 rec.run_note = "Started ("+mytimestamp+")"
59 rec.run_start_time = datetime.now()
62 output = "Hello "+settings.ALLOWED_IPS[ClIP]+". Updated " + runfolder + " status code to "+UpdatedStatus+" ("+DataRun.RUN_STATUS_CHOICES[int(UpdatedStatus)][1].__str__()+")"
63 if request.has_key('msg'): output += " and added message text: '"+request['msg']+" ("+mytimestamp+")'"
64 except ObjectDoesNotExist:
65 output = "entry not found: "+fcid+", "+runfolder
68 #Notify researcher by email
70 #send_mail('Exp Tracker', 'Data Run Status '+output, 'rrauch@stanford.edu', ['rrrami@gmail.com'], fail_silently=False)
71 #mail_admins("test subject", "testing , testing", fail_silently=False)
72 # gives error: (49, "Can't assign requested address")
73 return HttpResponse(output)
75 def generateConfile(request,fcid):
77 rec = FlowCell.objects.get(flowcell_id=fcid)
78 cnfgfile = 'READ_LENGTH 36\n' # 25
80 cnfgfile += 'ANALYSIS eland_pair\n'
82 cnfgfile += 'ANALYSIS eland_extended\n'
83 cnfgfile += 'GENOME_FILE all_chr.fa\n'
84 cnfgfile += 'ELAND_MULTIPLE_INSTANCES 8\n'
85 genome_dir = 'GENOME_DIR /mnt/encodedisk6/genomes/' # /Volumes/Genomes/
86 eland_genome = 'ELAND_GENOME /mnt/encodedisk6/genomes/' # /Volumes/Genomes/
88 cnfgfile += '1:'+genome_dir+rec.lane_1_library.library_species.use_genome_build+'\n'
89 cnfgfile += '1:'+eland_genome+rec.lane_1_library.library_species.use_genome_build+'\n'
91 cnfgfile += '2:'+genome_dir+rec.lane_2_library.library_species.use_genome_build+'\n'
92 cnfgfile += '2:'+eland_genome+rec.lane_2_library.library_species.use_genome_build+'\n'
94 cnfgfile += '3:'+genome_dir+rec.lane_3_library.library_species.use_genome_build+'\n'
95 cnfgfile += '3:'+eland_genome+rec.lane_3_library.library_species.use_genome_build+'\n'
97 cnfgfile += '4:'+genome_dir+rec.lane_4_library.library_species.use_genome_build+'\n'
98 cnfgfile += '4:'+eland_genome+rec.lane_4_library.library_species.use_genome_build+'\n'
100 cnfgfile += '5:'+genome_dir+rec.lane_5_library.library_species.use_genome_build+'\n'
101 cnfgfile += '5:'+eland_genome+rec.lane_5_library.library_species.use_genome_build+'\n'
103 cnfgfile += '6:'+genome_dir+rec.lane_6_library.library_species.use_genome_build+'\n'
104 cnfgfile += '6:'+eland_genome+rec.lane_6_library.library_species.use_genome_build+'\n'
106 cnfgfile += '7:'+genome_dir+rec.lane_7_library.library_species.use_genome_build+'\n'
107 cnfgfile += '7:'+eland_genome+rec.lane_7_library.library_species.use_genome_build+'\n'
109 cnfgfile += '8:'+genome_dir+rec.lane_8_library.library_species.use_genome_build+'\n'
110 cnfgfile += '8:'+eland_genome+rec.lane_8_library.library_species.use_genome_build
112 except ObjectDoesNotExist:
113 cnfgfile = 'Entry not found for fcid = '+fcid
117 def OLD_getConfile(request):
119 ClIP = request.META['REMOTE_ADDR']
120 if (settings.ALLOWED_IPS.has_key(ClIP)): granted = True
122 if not granted: return HttpResponse("access denied. IP: "+ClIP)
126 runfolder = 'unknown'
127 if request.has_key('fcid'):
128 fcid = request['fcid']
129 if request.has_key('runf'):
130 runfolder = request['runf']
132 rec = DataRun.objects.get(run_folder=runfolder) #,flowcell_id=fcid)
133 cnfgfile = rec.config_params
134 match_str = re.compile('^READ_LENGTH.+')
135 if not match_str.search(cnfgfile):
136 cnfgfile = generateConfile(request,fcid)
137 if match_str.search(cnfgfile):
138 rec = DataRun.objects.get(run_folder=runfolder) #,flowcell_id=fcid)
139 rec.config_params = cnfgfile
142 cnfgfile = 'Failed generating config params for RunFolder = '+runfolder +', Flowcell id = '+ fcid+ ' Config Text:\n'+cnfgfile
144 except ObjectDoesNotExist:
145 cnfgfile = 'Entry not found for RunFolder = '+runfolder
147 return HttpResponse(cnfgfile)
150 def getConfParams(request):
152 ClIP = request.META['REMOTE_ADDR']
153 if (settings.ALLOWED_IPS.has_key(ClIP)): granted = True
154 if not granted: return HttpResponse("access denied. IP: "+ClIP)
158 if request.has_key('fcid'):
159 fcid = request['fcid']
161 rec = FlowCell.objects.get(flowcell_id=fcid)
162 paramstext = 'Lane:'+ rec.lane_1_library.library_id +':'+rec.lane_1_library.library_species.use_genome_build
163 paramstext += '\nLane:'+rec.lane_2_library.library_id +':'+rec.lane_2_library.library_species.use_genome_build
164 paramstext += '\nLane:'+rec.lane_3_library.library_id +':'+rec.lane_3_library.library_species.use_genome_build
165 paramstext += '\nLane:'+rec.lane_4_library.library_id +':'+rec.lane_4_library.library_species.use_genome_build
166 paramstext += '\nLane:'+rec.lane_5_library.library_id +':'+rec.lane_5_library.library_species.use_genome_build
167 paramstext += '\nLane:'+rec.lane_6_library.library_id +':'+rec.lane_6_library.library_species.use_genome_build
168 paramstext += '\nLane:'+rec.lane_7_library.library_id +':'+rec.lane_7_library.library_species.use_genome_build
169 paramstext += '\nLane:'+rec.lane_8_library.library_id +':'+rec.lane_8_library.library_species.use_genome_build
170 if rec.is_paired_end: paramstext += '\nIsPairedEnd::1'
171 else: paramstext += '\nIsPairedEnd::0'
173 except ObjectDoesNotExist:
174 paramstext = 'Flowcell entry not found for: '+fcid
176 paramstext = 'missing input - flowcell id'
178 return HttpResponse(paramstext)
181 def getLaneLibs(request):
183 ClIP = request.META['REMOTE_ADDR']
184 if (settings.ALLOWED_IPS.has_key(ClIP)): granted = True
186 if not granted: return HttpResponse("access denied.")
190 if request.has_key('fcid'):
191 fcid = request['fcid']
193 rec = FlowCell.objects.get(flowcell_id=fcid)
195 year = datetime.today().year.__str__()
196 year = replace(year,'20','')
197 month = datetime.today().month
198 if month < 10: month = "0"+month.__str__()
199 else: month = month.__str__()
200 day = datetime.today().day
201 if day < 10: day = "0"+day.__str__()
202 else: day = day.__str__()
203 mydate = year+month+day
204 outputfile = '<?xml version="1.0" ?>'
205 outputfile += '\n<SolexaResult Date="'+mydate+'" Flowcell="'+fcid+'" Client="'+settings.ALLOWED_IPS[ClIP]+'">'
206 outputfile += '\n<Lane Index="1" Name="'+rec.lane_1_library.library_name+'" Library="'+rec.lane_1_library.library_id+'" Genome="'+rec.lane_1_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
207 outputfile += '\n<Lane Index="2" Name="'+rec.lane_2_library.library_name+'" Library="'+rec.lane_2_library.library_id+'" Genome="'+rec.lane_2_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
208 outputfile += '\n<Lane Index="3" Name="'+rec.lane_3_library.library_name+'" Library="'+rec.lane_3_library.library_id+'" Genome="'+rec.lane_3_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
209 outputfile += '\n<Lane Index="4" Name="'+rec.lane_4_library.library_name+'" Library="'+rec.lane_4_library.library_id+'" Genome="'+rec.lane_4_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
210 outputfile += '\n<Lane Index="5" Name="'+rec.lane_5_library.library_name+'" Library="'+rec.lane_5_library.library_id+'" Genome="'+rec.lane_5_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
211 outputfile += '\n<Lane Index="6" Name="'+rec.lane_6_library.library_name+'" Library="'+rec.lane_6_library.library_id+'" Genome="'+rec.lane_6_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
212 outputfile += '\n<Lane Index="7" Name="'+rec.lane_7_library.library_name+'" Library="'+rec.lane_7_library.library_id+'" Genome="'+rec.lane_7_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
213 outputfile += '\n<Lane Index="8" Name="'+rec.lane_8_library.library_name+'" Library="'+rec.lane_8_library.library_id+'" Genome="'+rec.lane_8_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
214 outputfile += '\n</SolexaResult>'
215 except ObjectDoesNotExist:
216 outputfile = 'Flowcell entry not found for: '+fcid
217 else: outputfile = 'Missing input: flowcell id'
219 return HttpResponse(outputfile)