Update hudson alpha's eland config file generator to work with the
[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 os
6 import re
7 from htsworkflow.frontend import settings
8 from htsworkflow.frontend.experiments.models import FlowCell, DataRun
9 from htsworkflow.frontend.samples.models import Library
10 from django.core.exceptions import ObjectDoesNotExist
11 from django.core.mail import send_mail, mail_admins
12
13 def updStatus(request):
14     output=''
15     user = 'none'
16     pswd = ''
17     UpdatedStatus = 'unknown'
18     fcid = 'none'
19     runfolder = 'unknown'
20     ClIP = request.META['REMOTE_ADDR']
21     granted = False    
22
23     if request.has_key('user'):
24       user = request['user']
25
26     #Check access permission 
27     if (user == 'rami' and settings.ALLOWED_IPS.has_key(ClIP)):  granted = True
28     if not granted: return HttpResponse("access denied.")
29
30
31     # ~~~~~~Parameters for the job ~~~~
32     if request.has_key('fcid'):
33       fcid = request['fcid']
34     else:
35       return HttpResponse('missing fcid')
36     
37     if request.has_key('runf'):
38       runfolder = request['runf']
39     else:
40       return HttpResponse('missing runf')
41
42     
43     if request.has_key('updst'):
44       UpdatedStatus = request['updst']
45     else:
46       return HttpResponse('missing status')
47     
48     # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
49
50     # Update Data Run status in DB
51     # Try get rec. If not found return 'entry not found + <fcid><runfolder>', if found try update and return updated 
52     try:
53       rec = DataRun.objects.get(run_folder=runfolder)
54       rec.run_status = UpdatedStatus
55
56       #if there's a message update that too
57       mytimestamp = datetime.now().__str__()
58       mytimestamp = re.sub(pattern=":[^:]*$",repl="",string=mytimestamp)
59       if request.has_key('msg'):
60         rec.run_note += ", "+request['msg']+" ("+mytimestamp+")"
61       else :
62         if UpdatedStatus == '1':
63           rec.run_note = "Started ("+mytimestamp+")"
64
65       rec.save()
66       output = "Hello "+settings.ALLOWED_IPS[ClIP]+". Updated to:'"+DataRun.RUN_STATUS_CHOICES[int(UpdatedStatus)][1].__str__()+"'"
67     except ObjectDoesNotExist:
68       output = "entry not found: "+fcid+", "+runfolder
69
70
71     #Notify researcher by email
72     # Doesn't work
73     #send_mail('Exp Tracker', 'Data Run Status '+output, 'rrauch@stanford.edu', ['rrrami@gmail.com'], fail_silently=False)
74     #mail_admins("test subject", "testing , testing", fail_silently=False)
75     # gives error: (49, "Can't assign requested address")
76     return HttpResponse(output)
77
78 def generateConfile(request,fcid):
79     #granted = False
80     #ClIP = request.META['REMOTE_ADDR']
81     #if (settings.ALLOWED_IPS.has_key(ClIP)):  granted = True
82
83     #if not granted: return HttpResponse("access denied.")
84
85     config = ['READ_LENGTH 25']
86     config += ['ANALYSIS eland']
87     config += ['GENOME_FILE all_chr.fa']
88     config += ['ELAND_MULTIPLE_INSTANCES 8']
89     genome_dir = 'GENOME_DIR /Volumes/Genomes/'
90     eland_genome = 'ELAND_GENOME /Volumes/Genomes/'
91     
92     try:                                                                                                                                              
93       fc = FlowCell.objects.get(flowcell_id=fcid)
94       for lane in fc.lane_set.all():
95           print dir(lane.library.library_species)
96           config += [ str(lane.lane_number) +":" + \
97                       genome_dir + lane.library.library_species.scientific_name ]
98           config += [ str(lane.lane_number) +":" + \
99                       eland_genome + lane.library.library_species.scientific_name ]
100       
101     except ObjectDoesNotExist:
102       config = 'Entry not found for fcid  = '+fcid
103
104     return os.linesep.join(config)
105
106 def getConfile(req):
107     granted = False
108     ClIP = req.META['REMOTE_ADDR']
109     if (settings.ALLOWED_IPS.has_key(ClIP)):  granted = True
110
111     if not granted: return HttpResponse("access denied. IP: "+ClIP)
112
113     fcid = 'none'
114     cnfgfile = 'Nothing found'
115     runfolder = 'unknown'
116     request = req.REQUEST
117     print request, dir(request)
118     print request['fcid'], request.has_key('fcid')
119     print request['runf']
120     if request.has_key('fcid'):
121       fcid = request['fcid']
122       if request.has_key('runf'):
123         runfolder = request['runf']
124         try:
125           rec = DataRun.objects.get(run_folder=runfolder) #,flowcell_id=fcid)
126           cnfgfile = rec.config_params
127           #match_str = re.compile(r"READ_LENGTH.+$")
128           match_str = re.compile('^READ_LENGTH.+')
129           if not match_str.search(cnfgfile):
130             cnfgfile = generateConfile(request,fcid)
131             if match_str.search(cnfgfile):
132               rec = DataRun.objects.get(run_folder=runfolder) #,flowcell_id=fcid)
133               rec.config_params = cnfgfile
134               rec.save()
135             else:
136               cnfgfile = 'Failed generating config params for RunFolder = '+runfolder +', Flowcell id = '+ fcid+ ' Config Text:\n'+cnfgfile  
137             
138         except ObjectDoesNotExist:
139           cnfgfile = 'Entry not found for RunFolder = '+runfolder
140
141     return HttpResponse(cnfgfile, mimetype='text/plain')
142
143 def getLaneLibs(req):
144     granted = False
145     ClIP = req.META['REMOTE_ADDR']
146     if (settings.ALLOWED_IPS.has_key(ClIP)):  granted = True
147
148     if not granted: return HttpResponse("access denied.")
149
150     request = req.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+'" Client="'+settings.ALLOWED_IPS[ClIP]+'">'
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+'" PrimerName="" PrimerSeq=""/>'
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+'" PrimerName="" PrimerSeq=""/>'
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+'" PrimerName="" PrimerSeq=""/>'
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+'" PrimerName="" PrimerSeq=""/>'
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+'" PrimerName="" PrimerSeq=""/>'
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+'" PrimerName="" PrimerSeq=""/>'
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+'" PrimerName="" PrimerSeq=""/>'
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+'" PrimerName="" PrimerSeq=""/>'
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, mimetype='text/plain')