1 from django.http import HttpResponse
2 from django.shortcuts import render_to_response
3 from django.core.exceptions import ObjectDoesNotExist
5 from htsworkflow.frontend.eland_config import forms
6 from htsworkflow.frontend import settings
7 from htsworkflow.frontend.fctracker import models
11 # Create your views here.
14 def _validate_input(data):
15 #if data.find('..') == -1 or data.find('/') == -1 or data.find('\\') == -1:
16 return data.replace('..', '').replace('/', '_').replace('\\', '_')
18 #def contact(request):
19 # if request.method == 'POST':
20 # form = ContactForm(request.POST)
22 # # Do form processing here...
23 # return HttpResponseRedirect('/url/on_success/')
25 # form = ContactForm()
30 #def _saveConfigFile(form):
32 # Given a valid form, save eland config to file based on flowcell number.
34 # assert form.is_valid()
36 # clean_data = form.cleaned_data
37 # flowcell = clean_data['flow_cell_number'].replace('/','_').replace('..', '__')
39 # file_path = os.path.join(settings.UPLOADTO_CONFIG_FILE, flowcell)
41 # f = open(file_path, 'w')
42 # cfg = generateElandConfig(form)
49 # Save info to the database.
51 # clean_data = form.cleaned_data
53 # fc_id = clean_data['flow_cell_number']
56 # fc = models.FlowCell.objects.get(flowcell_id=fc_id)
57 # except models.FlowCell.DoesNotExist:
58 # fc = models.FlowCell()
60 # fc.flowcell_id = fc_id
61 # fc.run_date = clean_data['run_date']
64 # fc.lane1_sample = clean_data['lane1_description']
65 # species_name = clean_data['lane1_species']
67 # specie = models.Specie.objects.get(scientific_name=species_name)
68 # except models.Specie.DoesNotExist:
69 # specie = models.Specie(scientific_name=species_name)
71 # fc.lane1_species = specie
74 # fc.lane2_sample = clean_data['lane2_description']
75 # species_name = clean_data['lane2_species']
77 # specie = models.Specie.objects.get(scientific_name=species_name)
78 # except models.Specie.DoesNotExist:
79 # specie = models.Specie(scientific_name=species_name)
81 # fc.lane2_species = specie
84 # fc.lane3_sample = clean_data['lane3_description']
85 # species_name = clean_data['lane3_species']
87 # specie = models.Specie.objects.get(scientific_name=species_name)
88 # except models.Specie.DoesNotExist:
89 # specie = models.Specie(scientific_name=species_name)
91 # fc.lane3_species = specie
94 # fc.lane4_sample = clean_data['lane4_description']
95 # species_name = clean_data['lane4_species']
97 # specie = models.Specie.objects.get(scientific_name=species_name)
98 # except models.Specie.DoesNotExist:
99 # specie = models.Specie(scientific_name=species_name)
101 # fc.lane4_species = specie
104 # fc.lane5_sample = clean_data['lane5_description']
105 # species_name = clean_data['lane5_species']
107 # specie = models.Specie.objects.get(scientific_name=species_name)
108 # except models.Specie.DoesNotExist:
109 # specie = models.Specie(scientific_name=species_name)
111 # fc.lane5_species = specie
114 # fc.lane6_sample = clean_data['lane6_description']
115 # species_name = clean_data['lane6_species']
117 # specie = models.Specie.objects.get(scientific_name=species_name)
118 # except models.Specie.DoesNotExist:
119 # specie = models.Specie(scientific_name=species_name)
121 # fc.lane6_species = specie
124 # fc.lane7_sample = clean_data['lane7_description']
125 # species_name = clean_data['lane7_species']
127 # specie = models.Specie.objects.get(scientific_name=species_name)
128 # except models.Specie.DoesNotExist:
129 # specie = models.Specie(scientific_name=species_name)
131 # fc.lane7_species = specie
134 # fc.lane8_sample = clean_data['lane8_description']
135 # species_name = clean_data['lane8_species']
137 # specie = models.Specie.objects.get(scientific_name=species_name)
138 # except models.Specie.DoesNotExist:
139 # specie = models.Specie(scientific_name=species_name)
141 # fc.lane8_species = specie
143 # fc.notes = clean_data['notes']
150 #def generateElandConfig(form):
153 # form = form.cleaned_data
155 # BASE_DIR = '/data-store01/compbio/genomes'
157 # data.append("# FLOWCELL: %s" % (form['flow_cell_number']))
160 # notes = form['notes'].replace('\r\n', '\n').replace('\r', '\n')
161 # notes = notes.replace('\n', '\n# ')
162 # data.append("# NOTES:")
163 # data.append("# %s\n#" % (notes))
165 # #Convert all newline conventions to unix style
166 # l1d = form['lane1_description'].replace('\r\n', '\n').replace('\r', '\n')
167 # l2d = form['lane2_description'].replace('\r\n', '\n').replace('\r', '\n')
168 # l3d = form['lane3_description'].replace('\r\n', '\n').replace('\r', '\n')
169 # l4d = form['lane4_description'].replace('\r\n', '\n').replace('\r', '\n')
170 # l5d = form['lane5_description'].replace('\r\n', '\n').replace('\r', '\n')
171 # l6d = form['lane6_description'].replace('\r\n', '\n').replace('\r', '\n')
172 # l7d = form['lane7_description'].replace('\r\n', '\n').replace('\r', '\n')
173 # l8d = form['lane8_description'].replace('\r\n', '\n').replace('\r', '\n')
175 # # Turn new lines into indented commented newlines
176 # l1d = l1d.replace('\n', '\n# ')
177 # l2d = l2d.replace('\n', '\n# ')
178 # l3d = l3d.replace('\n', '\n# ')
179 # l4d = l4d.replace('\n', '\n# ')
180 # l5d = l5d.replace('\n', '\n# ')
181 # l6d = l6d.replace('\n', '\n# ')
182 # l7d = l7d.replace('\n', '\n# ')
183 # l8d = l8d.replace('\n', '\n# ')
185 # data.append("# Lane1: %s" % (l1d))
186 # data.append("# Lane2: %s" % (l2d))
187 # data.append("# Lane3: %s" % (l3d))
188 # data.append("# Lane4: %s" % (l4d))
189 # data.append("# Lane5: %s" % (l5d))
190 # data.append("# Lane6: %s" % (l6d))
191 # data.append("# Lane7: %s" % (l7d))
192 # data.append("# Lane8: %s" % (l8d))
194 # #data.append("GENOME_DIR %s" % (BASE_DIR))
195 # #data.append("CONTAM_DIR %s" % (BASE_DIR))
196 # read_length = form['read_length']
197 # data.append("READ_LENGTH %d" % (read_length))
198 # #data.append("ELAND_REPEAT")
199 # data.append("ELAND_MULTIPLE_INSTANCES 8")
201 # #Construct genome dictionary to figure out what lanes to put
202 # # in the config file.
204 # l1s = form['lane1_species']
205 # genome_dict.setdefault(l1s, []).append('1')
206 # l2s = form['lane2_species']
207 # genome_dict.setdefault(l2s, []).append('2')
208 # l3s = form['lane3_species']
209 # genome_dict.setdefault(l3s, []).append('3')
210 # l4s = form['lane4_species']
211 # genome_dict.setdefault(l4s, []).append('4')
212 # l5s = form['lane5_species']
213 # genome_dict.setdefault(l5s, []).append('5')
214 # l6s = form['lane6_species']
215 # genome_dict.setdefault(l6s, []).append('6')
216 # l7s = form['lane7_species']
217 # genome_dict.setdefault(l7s, []).append('7')
218 # l8s = form['lane8_species']
219 # genome_dict.setdefault(l8s, []).append('8')
221 # genome_list = genome_dict.keys()
224 # #Loop through and create entries for each species.
225 # for genome in genome_list:
226 # lanes = ''.join(genome_dict[genome])
227 # data.append('%s:ANALYSIS eland' % (lanes))
228 # data.append('%s:READ_LENGTH %s' % (lanes, read_length))
229 # data.append('%s:ELAND_GENOME %s' % (lanes, os.path.join(BASE_DIR, genome)))
230 # data.append('%s:USE_BASES %s' % (lanes, 'Y'*int(read_length)))
232 # data.append('SEQUENCE_FORMAT --scarf')
234 # return '\n'.join(data)
237 def getElandConfig(flowcell, regenerate=False):
239 file_path = os.path.join(settings.UPLOADTO_CONFIG_FILE, flowcell)
241 #If we are regenerating the config file, skip
242 # reading of existing file. If the file doesn't
243 # exist, try to generate it form the DB.
244 if not regenerate and os.path.isfile(file_path):
245 f = open(file_path, 'r')
251 fcObj = models.FlowCell.objects.get(flowcell_id__iexact=flowcell)
252 except ObjectDoesNotExist:
257 #form = form.cleaned_data
259 BASE_DIR = '/data-store01/compbio/genomes'
261 data.append("# FLOWCELL: %s" % (fcObj.flowcell_id))
264 notes = fcObj.notes.replace('\r\n', '\n').replace('\r', '\n')
265 notes = notes.replace('\n', '\n# ')
266 data.append("# NOTES:")
267 data.append("# %s\n#" % (notes))
269 #Convert all newline conventions to unix style
270 l1d = str(fcObj.lane_1_library.library_id) + '|' \
271 + fcObj.lane_1_library.library_name.replace('\r\n', '\n').replace('\r', '\n')
272 l2d = str(fcObj.lane_2_library.library_id) + '|' \
273 + fcObj.lane_2_library.library_name.replace('\r\n', '\n').replace('\r', '\n')
274 l3d = str(fcObj.lane_3_library.library_id) + '|' \
275 + fcObj.lane_3_library.library_name.replace('\r\n', '\n').replace('\r', '\n')
276 l4d = str(fcObj.lane_4_library.library_id) + '|' \
277 + fcObj.lane_4_library.library_name.replace('\r\n', '\n').replace('\r', '\n')
279 l5d = str(fcObj.lane_5_library.library_id) + '|' \
280 + fcObj.lane_5_library.library_name.replace('\r\n', '\n').replace('\r', '\n')
281 l6d = str(fcObj.lane_6_library.library_id) + '|' \
282 + fcObj.lane_6_library.library_name.replace('\r\n', '\n').replace('\r', '\n')
283 l7d = str(fcObj.lane_7_library.library_id) + '|' \
284 + fcObj.lane_7_library.library_name.replace('\r\n', '\n').replace('\r', '\n')
285 l8d = str(fcObj.lane_8_library.library_id) + '|' \
286 + fcObj.lane_8_library.library_name.replace('\r\n', '\n').replace('\r', '\n')
288 # Turn new lines into indented commented newlines
289 l1d = l1d.replace('\n', '\n# ')
290 l2d = l2d.replace('\n', '\n# ')
291 l3d = l3d.replace('\n', '\n# ')
292 l4d = l4d.replace('\n', '\n# ')
293 l5d = l5d.replace('\n', '\n# ')
294 l6d = l6d.replace('\n', '\n# ')
295 l7d = l7d.replace('\n', '\n# ')
296 l8d = l8d.replace('\n', '\n# ')
298 data.append("# Lane1: %s" % (l1d))
299 data.append("# Lane2: %s" % (l2d))
300 data.append("# Lane3: %s" % (l3d))
301 data.append("# Lane4: %s" % (l4d))
302 data.append("# Lane5: %s" % (l5d))
303 data.append("# Lane6: %s" % (l6d))
304 data.append("# Lane7: %s" % (l7d))
305 data.append("# Lane8: %s" % (l8d))
307 #data.append("GENOME_DIR %s" % (BASE_DIR))
308 #data.append("CONTAM_DIR %s" % (BASE_DIR))
309 read_length = fcObj.read_length
310 data.append("READ_LENGTH %d" % (read_length))
311 #data.append("ELAND_REPEAT")
312 data.append("ELAND_MULTIPLE_INSTANCES 8")
314 #Construct genome dictionary to figure out what lanes to put
315 # in the config file.
318 #l1s = form['lane1_species']
319 l1s = fcObj.lane_1_library.library_species.scientific_name #+ '|' + \
320 #fcObj.lane_1_library.library_species.use_genome_build
321 genome_dict.setdefault(l1s, []).append('1')
322 l2s = fcObj.lane_2_library.library_species.scientific_name #+ '|' + \
323 #fcObj.lane_2_library.library_species.use_genome_build
324 genome_dict.setdefault(l2s, []).append('2')
325 l3s = fcObj.lane_3_library.library_species.scientific_name #+ '|' + \
326 #fcObj.lane_3_library.library_species.use_genome_build
327 genome_dict.setdefault(l3s, []).append('3')
328 l4s = fcObj.lane_4_library.library_species.scientific_name #+ '|' + \
329 #fcObj.lane_4_library.library_species.use_genome_build
330 genome_dict.setdefault(l4s, []).append('4')
331 l5s = fcObj.lane_5_library.library_species.scientific_name #+ '|' + \
332 #fcObj.lane_5_library.library_species.use_genome_build
333 genome_dict.setdefault(l5s, []).append('5')
334 l6s = fcObj.lane_6_library.library_species.scientific_name #+ '|' + \
335 #fcObj.lane_6_library.library_species.use_genome_build
336 genome_dict.setdefault(l6s, []).append('6')
337 l7s = fcObj.lane_7_library.library_species.scientific_name #+ '|' + \
338 #fcObj.lane_7_library.library_species.use_genome_build
339 genome_dict.setdefault(l7s, []).append('7')
340 l8s = fcObj.lane_8_library.library_species.scientific_name #+ '|' + \
341 #fcObj.lane_8_library.library_species.use_genome_build
342 genome_dict.setdefault(l8s, []).append('8')
344 genome_list = genome_dict.keys()
347 #Loop through and create entries for each species.
348 for genome in genome_list:
349 lanes = ''.join(genome_dict[genome])
350 data.append('%s:ANALYSIS eland' % (lanes))
351 data.append('%s:READ_LENGTH %s' % (lanes, read_length))
352 data.append('%s:ELAND_GENOME %s' % (lanes, '%%(%s)s' % (genome)))
353 data.append('%s:USE_BASES %s' % (lanes, 'Y'*int(read_length)))
355 data.append('SEQUENCE_FORMAT --scarf')
357 data = '\n'.join(data)
359 f = open(file_path, 'w')
367 def config(request, flowcell=None):
369 Returns eland config file for a given flowcell number,
370 or returns a list of available flowcell numbers.
373 # Provide INDEX of available Flowcell config files.
375 #Find all FC* config files and report an index html file
376 #fc_list = [ os.path.split(file_path)[1] for file_path in glob.glob(os.path.join(settings.UPLOADTO_CONFIG_FILE, 'FC*')) ]
377 fc_list = [ fc.flowcell_id for fc in models.FlowCell.objects.all() ]
379 #Convert FC* list to html links
380 fc_html = [ '<a href="/eland_config/%s/">%s</a>' % (fc_name, fc_name) for fc_name in fc_list ]
382 return HttpResponse('<br />'.join(fc_html))
384 #FIXME: Should validate flowcell input before using.
385 flowcell = _validate_input(flowcell)
386 cfg = getElandConfig(flowcell, regenerate=True)
389 return HttpResponse("Hmm, config file for %s does not seem to exist." % (flowcell))
392 return HttpResponse(cfg, mimetype="text/plain")
399 # Return a form for filling out information about the flowcell
401 # if request.method == 'POST':
402 # form = forms.ConfigForm(request.POST, error_class=forms.DivErrorList)
403 # if form.is_valid():
404 # #cfg = generateElandConfig(form)
405 # _saveConfigFile(form)
407 # return HttpResponse("Eland Config Saved!", mimetype="text/plain")
409 # return render_to_response('config_form.html', {'form': form })
412 # fm = forms.ConfigForm(error_class=forms.DivErrorList)
413 # return render_to_response('config_form.html', {'form': fm })