38daaab4f6f143ada5f717445ae06f3321049042
[htsworkflow.git] / gaworkflow / frontend / exp_track / exptrack.py
1 # some core functions of the exp tracker module
2 from django.http import HttpResponse
3 from datetime import datetime
4 from string import *
5 import re
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
10
11 def updStatus(request):
12     output=''
13     user = 'none'
14     pswd = ''
15     UpdatedStatus = 'unknown'
16     ClIP = 'unknown'
17     granted = False
18     fcid = 'none'
19     runfolder = 'unknown'
20     
21     if request.has_key('fcid'):
22       fcid = request['fcid']
23     else:
24       return HttpResponse('missing fcid')
25     
26     if request.has_key('runf'):
27       runfolder = request['runf']
28     else:
29       return HttpResponse('missing runf')
30     
31     if request.has_key('user'):  
32       user = request['user']
33     else:
34       return HttpResponse('access denied')
35
36     #TO DO: pswd decode etc..
37     #if request.has_key('pswd'):
38     #  pswd = request['pswd']
39     #else: 
40     #  return HttpResponse('access denied')
41
42     ClIP = request.META['REMOTE_ADDR']
43     
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')):
46       granted = True
47     
48     if granted:
49       if request.has_key('updst'):
50         UpdatedStatus = request['updst']
51       else:
52         return HttpResponse('missing status')
53  
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 
56       try:
57         rec = DataRun.objects.get(run_folder=runfolder)
58         rec.run_status = UpdatedStatus
59
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+")"
65         else :
66           if UpdatedStatus == '1':
67             rec.run_note = "Started ("+mytimestamp+")"
68
69         rec.save()
70         output = "updated to:'"+DataRun.RUN_STATUS_CHOICES[int(UpdatedStatus)][1].__str__()+"'"
71       except ObjectDoesNotExist:
72         output = "entry not found: "+fcid+", "+runfolder
73     else: 
74       output ="access denied.. u: "+user+", ClIP: "+ClIP
75
76     #Notify researcher by email
77     # Doesn't work
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)
82
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/'
90     
91     try:                                                                                                                                              
92       rec = FlowCell.objects.get(flowcell_id=fcid)
93       
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'
96
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'
99  
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'
102
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'
105       
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'
108
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'
111
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'
114
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
117
118     except ObjectDoesNotExist:
119       cnfgfile = 'Entry not found for fcid  = '+fcid
120
121     return cnfgfile
122
123 def getConfile(request):
124     fcid = 'none'
125     cnfgfile = ''
126     runfolder = 'unknown'
127     if request.has_key('fcid'):
128       fcid = request['fcid']
129       if request.has_key('runf'):
130         runfolder = request['runf']
131         try:
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
141               rec.save()
142             else:
143               cnfgfile = 'Failed generating config params for RunFolder = '+runfolder +', Flowcell id = '+ fcid+ ' Config Text:\n'+cnfgfile  
144             
145         except ObjectDoesNotExist:
146           cnfgfile = 'Entry not found for RunFolder = '+runfolder
147
148     return HttpResponse(cnfgfile)
149
150 def getLaneLibs(request):
151     fcid = 'none'
152     outputfile = ''
153     if request.has_key('fcid'):
154       fcid = request['fcid']                                                                                                      
155       try:                                
156         rec = FlowCell.objects.get(flowcell_id=fcid)
157         #Ex: 071211
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'
181
182     return HttpResponse(outputfile)