ca224a95753fe1f9723dd8bbf45a5d81e5c5ab0e
[htsworkflow.git] / htsworkflow / frontend / experiments / experiments.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 htsworkflow.frontend import settings
7 from htsworkflow.frontend.experiments.models import FlowCell, DataRun
8 from htsworkflow.frontend.samples.models import Library
9 from django.core.exceptions import ObjectDoesNotExist
10 from django.core.mail import send_mail, mail_admins
11
12 def updStatus(request):
13     output=''
14     user = 'none'
15     pswd = ''
16     UpdatedStatus = 'unknown'
17     fcid = 'none'
18     runfolder = 'unknown'
19     ClIP = request.META['REMOTE_ADDR']
20     granted = False    
21
22     if request.has_key('user'):
23       user = request['user']
24
25     #Check access permission 
26     if (user == 'rami' and settings.ALLOWED_IPS.has_key(ClIP)):  granted = True
27     if not granted: return HttpResponse("access denied.")
28
29
30     # ~~~~~~Parameters for the job ~~~~
31     if request.has_key('fcid'):
32       fcid = request['fcid']
33     else:
34       return HttpResponse('missing fcid')
35     
36     if request.has_key('runf'):
37       runfolder = request['runf']
38     else:
39       return HttpResponse('missing runf')
40
41     
42     if request.has_key('updst'):
43       UpdatedStatus = request['updst']
44     else:
45       return HttpResponse('missing status')
46     
47     # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
48
49     # Update Data Run status in DB
50     # Try get rec. If not found return 'entry not found + <fcid><runfolder>', if found try update and return updated 
51     try:
52       rec = DataRun.objects.get(run_folder=runfolder)
53       rec.run_status = UpdatedStatus
54
55       #if there's a message update that too
56       mytimestamp = datetime.now().__str__()
57       mytimestamp = re.sub(pattern=":[^:]*$",repl="",string=mytimestamp)
58       if request.has_key('msg'):
59         rec.run_note += ", "+request['msg']+" ("+mytimestamp+")"
60       else :
61         if UpdatedStatus == '1':
62           rec.run_note = "Started ("+mytimestamp+")"
63
64       rec.save()
65       output = "Hello "+settings.ALLOWED_IPS[ClIP]+". Updated to:'"+DataRun.RUN_STATUS_CHOICES[int(UpdatedStatus)][1].__str__()+"'"
66     except ObjectDoesNotExist:
67       output = "entry not found: "+fcid+", "+runfolder
68
69
70     #Notify researcher by email
71     # Doesn't work
72     #send_mail('Exp Tracker', 'Data Run Status '+output, 'rrauch@stanford.edu', ['rrrami@gmail.com'], fail_silently=False)
73     #mail_admins("test subject", "testing , testing", fail_silently=False)
74     # gives error: (49, "Can't assign requested address")
75     return HttpResponse(output)
76
77 def generateConfile(request,fcid):
78     #granted = False
79     #ClIP = request.META['REMOTE_ADDR']
80     #if (settings.ALLOWED_IPS.has_key(ClIP)):  granted = True
81
82     #if not granted: return HttpResponse("access denied.")
83
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(req):
124     granted = False
125     ClIP = req.META['REMOTE_ADDR']
126     if (settings.ALLOWED_IPS.has_key(ClIP)):  granted = True
127
128     if not granted: return HttpResponse("access denied. IP: "+ClIP)
129
130     fcid = 'none'
131     cnfgfile = 'Nothing found'
132     runfolder = 'unknown'
133     request = req.REQUEST
134     print request, dir(request)
135     print request['fcid'], request.has_key('fcid')
136     print request['runf']
137     if request.has_key('fcid'):
138       fcid = request['fcid']
139       if request.has_key('runf'):
140         runfolder = request['runf']
141         try:
142           rec = DataRun.objects.get(run_folder=runfolder) #,flowcell_id=fcid)
143           cnfgfile = rec.config_params
144           #match_str = re.compile(r"READ_LENGTH.+$")
145           match_str = re.compile('^READ_LENGTH.+')
146           if not match_str.search(cnfgfile):
147             cnfgfile = generateConfile(request,fcid)
148             if match_str.search(cnfgfile):
149               rec = DataRun.objects.get(run_folder=runfolder) #,flowcell_id=fcid)
150               rec.config_params = cnfgfile
151               rec.save()
152             else:
153               cnfgfile = 'Failed generating config params for RunFolder = '+runfolder +', Flowcell id = '+ fcid+ ' Config Text:\n'+cnfgfile  
154             
155         except ObjectDoesNotExist:
156           cnfgfile = 'Entry not found for RunFolder = '+runfolder
157
158     return HttpResponse(cnfgfile, mimetype='text/plain')
159
160 def getLaneLibs(req):
161     granted = False
162     ClIP = req.META['REMOTE_ADDR']
163     if (settings.ALLOWED_IPS.has_key(ClIP)):  granted = True
164
165     if not granted: return HttpResponse("access denied.")
166
167     request = req.REQUEST
168     fcid = 'none'
169     outputfile = ''
170     if request.has_key('fcid'):
171       fcid = request['fcid']
172       try:                                
173         rec = FlowCell.objects.get(flowcell_id=fcid)
174         #Ex: 071211
175         year = datetime.today().year.__str__()
176         year = replace(year,'20','')
177         month = datetime.today().month
178         if month < 10: month = "0"+month.__str__()
179         else: month = month.__str__() 
180         day = datetime.today().day
181         if day < 10: day = "0"+day.__str__()
182         else: day = day.__str__()
183         mydate = year+month+day
184         outputfile = '<?xml version="1.0" ?>'
185         outputfile += '\n<SolexaResult Date="'+mydate+'" Flowcell="'+fcid+'" Client="'+settings.ALLOWED_IPS[ClIP]+'">'
186         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=""/>'
187         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=""/>'
188         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=""/>'
189         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=""/>'
190         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=""/>'
191         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=""/>'
192         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=""/>'
193         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=""/>'
194         outputfile += '\n</SolexaResult>'
195       except ObjectDoesNotExist:
196         outputfile = 'Flowcell entry not found for: '+fcid
197     else: outputfile = 'Missing input: flowcell id'
198
199     return HttpResponse(outputfile, mimetype='text/plain')