1 # some core functions of the exp tracker module
2 from django.http import HttpResponse
3 from datetime import datetime
6 from gaworkflow.frontend.exp_track.models import DataRun
7 from gaworkflow.frontend.fctracker.models import FlowCell, Library
8 from django.core.exceptions import ObjectDoesNotExist
9 from django.core.mail import send_mail, mail_admins
11 def updStatus(request):
15 UpdatedStatus = 'unknown'
21 if request.has_key('fcid'):
22 fcid = request['fcid']
24 return HttpResponse('missing fcid')
26 if request.has_key('runf'):
27 runfolder = request['runf']
29 return HttpResponse('missing runf')
31 if request.has_key('user'):
32 user = request['user']
34 return HttpResponse('access denied')
36 #TO DO: pswd decode etc..
37 #if request.has_key('pswd'):
38 # pswd = request['pswd']
40 # return HttpResponse('access denied')
42 ClIP = request.META['REMOTE_ADDR']
44 #Check permission to update
45 if (user == 'rami' and (ClIP == '171.65.76.167' or ClIP == '171.65.76.51' or ClIP == '171.65.76.194' or (ClIP == '74.51.115.100' or ClIP == '64.89.97.100'))): ## or ClIP == '127.0.0.1')):
49 if request.has_key('updst'):
50 UpdatedStatus = request['updst']
52 return HttpResponse('missing status')
54 # Update Data Run status in DB
55 # Try get rec. If not found return 'entry not found + <fcid><runfolder>', if found try update and return updated
57 rec = DataRun.objects.get(run_folder=runfolder)
58 rec.run_status = UpdatedStatus
60 #if there's a message update that too
61 mytimestamp = datetime.now().__str__()
62 mytimestamp = re.sub(pattern=":[^:]*$",repl="",string=mytimestamp)
63 if request.has_key('msg'):
64 rec.run_note += ", "+request['msg']+" ("+mytimestamp+")"
66 if UpdatedStatus == '1':
67 rec.run_note = "Started ("+mytimestamp+")"
70 output = "updated to:'"+DataRun.RUN_STATUS_CHOICES[int(UpdatedStatus)][1].__str__()+"'"
71 except ObjectDoesNotExist:
72 output = "entry not found: "+fcid+", "+runfolder
74 output ="access denied.. u: "+user+", ClIP: "+ClIP
76 #Notify researcher by email
78 #send_mail('Exp Tracker', 'Data Run Status '+output, 'rrauch@stanford.edu', ['rrrami@gmail.com'], fail_silently=False)
79 #mail_admins("test subject", "testing , testing", fail_silently=False)
80 # gives error: (49, "Can't assign requested address")
81 return HttpResponse(output)
83 def generateConfile(fcid):
84 cnfgfile = 'READ_LENGTH 25\n'
85 cnfgfile += 'ANALYSIS eland\n'
86 cnfgfile += 'GENOME_FILE all_chr.fa\n'
87 cnfgfile += 'ELAND_MULTIPLE_INSTANCES 8\n'
88 genome_dir = 'GENOME_DIR /Volumes/Genomes/'
89 eland_genome = 'ELAND_GENOME /Volumes/Genomes/'
92 rec = FlowCell.objects.get(flowcell_id=fcid)
94 cnfgfile += '1:'+genome_dir+rec.lane_1_library.library_species.use_genome_build+'\n'
95 cnfgfile += '1:'+eland_genome+rec.lane_1_library.library_species.use_genome_build+'\n'
97 cnfgfile += '2:'+genome_dir+rec.lane_2_library.library_species.use_genome_build+'\n'
98 cnfgfile += '2:'+eland_genome+rec.lane_2_library.library_species.use_genome_build+'\n'
100 cnfgfile += '3:'+genome_dir+rec.lane_3_library.library_species.use_genome_build+'\n'
101 cnfgfile += '3:'+eland_genome+rec.lane_3_library.library_species.use_genome_build+'\n'
103 cnfgfile += '4:'+genome_dir+rec.lane_4_library.library_species.use_genome_build+'\n'
104 cnfgfile += '4:'+eland_genome+rec.lane_4_library.library_species.use_genome_build+'\n'
106 cnfgfile += '5:'+genome_dir+rec.lane_5_library.library_species.use_genome_build+'\n'
107 cnfgfile += '5:'+eland_genome+rec.lane_5_library.library_species.use_genome_build+'\n'
109 cnfgfile += '6:'+genome_dir+rec.lane_6_library.library_species.use_genome_build+'\n'
110 cnfgfile += '6:'+eland_genome+rec.lane_6_library.library_species.use_genome_build+'\n'
112 cnfgfile += '7:'+genome_dir+rec.lane_7_library.library_species.use_genome_build+'\n'
113 cnfgfile += '7:'+eland_genome+rec.lane_7_library.library_species.use_genome_build+'\n'
115 cnfgfile += '8:'+genome_dir+rec.lane_8_library.library_species.use_genome_build+'\n'
116 cnfgfile += '8:'+eland_genome+rec.lane_8_library.library_species.use_genome_build
118 except ObjectDoesNotExist:
119 cnfgfile = 'Entry not found for runfolder = '+runfolder
123 def getConfile(request):
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(r"READ_LENGTH.+$")
135 match_str = re.compile('^READ_LENGTH.+')
136 if not match_str.search(cnfgfile):
137 cnfgfile = generateConfile(fcid)
138 if match_str.search(cnfgfile):
139 rec = DataRun.objects.get(run_folder=runfolder) #,flowcell_id=fcid)
140 rec.config_params = cnfgfile
143 cnfgfile = 'Failed generating config params for RunFolder = '+runfolder +', Flowcell id = '+ fcid+ ' Config Text:\n'+cnfgfile
145 except ObjectDoesNotExist:
146 cnfgfile = 'Entry not found for RunFolder = '+runfolder
148 return HttpResponse(cnfgfile)
150 def getLaneLibs(request):
153 if request.has_key('fcid'):
154 fcid = request['fcid']
156 rec = FlowCell.objects.get(flowcell_id=fcid)
158 year = datetime.today().year.__str__()
159 year = replace(year,'20','')
160 month = datetime.today().month
161 if month < 10: month = "0"+month.__str__()
162 else: month = month.__str__()
163 day = datetime.today().day
164 if day < 10: day = "0"+day.__str__()
165 else: day = day.__str__()
166 mydate = year+month+day
167 outputfile = '<?xml version="1.0" ?>'
168 outputfile += '\n<SolexaResult Date="'+mydate+'" Flowcell="'+fcid+'">'
169 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+'"/>'
170 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+'"/>'
171 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+'"/>'
172 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+'"/>'
173 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+'"/>'
174 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+'"/>'
175 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+'"/>'
176 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+'"/>'
177 outputfile += '\n</SolexaResult>'
178 except ObjectDoesNotExist:
179 outputfile = 'Flowcell entry not found for: '+fcid
180 else: outputfile = 'Missing input: flowcell id'
182 return HttpResponse(outputfile)