1 from django.http import HttpResponse
2 from django.shortcuts import render_to_response
3 from elandifier.eland_config import forms
4 from elandifier import settings
5 from elandifier.fctracker import models
9 # Create your views here.
12 #def contact(request):
13 # if request.method == 'POST':
14 # form = ContactForm(request.POST)
16 # # Do form processing here...
17 # return HttpResponseRedirect('/url/on_success/')
19 # form = ContactForm()
24 def _saveConfigFile(form):
26 Given a valid form, save eland config to file based on flowcell number.
28 assert form.is_valid()
30 clean_data = form.cleaned_data
31 flowcell = clean_data['flow_cell_number'].replace('/','_').replace('..', '__')
33 file_path = os.path.join(settings.UPLOADTO_CONFIG_FILE, flowcell)
35 f = open(file_path, 'w')
36 cfg = generateElandConfig(form)
43 Save info to the database.
45 clean_data = form.cleaned_data
47 fc_id = clean_data['flow_cell_number']
50 fc = models.FlowCell.objects.get(flowcell_id=fc_id)
51 except models.FlowCell.DoesNotExist:
52 fc = models.FlowCell()
54 fc.flowcell_id = fc_id
55 fc.run_date = clean_data['run_date']
58 fc.lane1_sample = clean_data['lane1_description']
59 species_name = clean_data['lane1_species']
61 specie = models.Specie.objects.get(scientific_name=species_name)
62 except models.Specie.DoesNotExist:
63 specie = models.Specie(scientific_name=species_name)
65 fc.lane1_species = specie
68 fc.lane2_sample = clean_data['lane2_description']
69 species_name = clean_data['lane2_species']
71 specie = models.Specie.objects.get(scientific_name=species_name)
72 except models.Specie.DoesNotExist:
73 specie = models.Specie(scientific_name=species_name)
75 fc.lane2_species = specie
78 fc.lane3_sample = clean_data['lane3_description']
79 species_name = clean_data['lane3_species']
81 specie = models.Specie.objects.get(scientific_name=species_name)
82 except models.Specie.DoesNotExist:
83 specie = models.Specie(scientific_name=species_name)
85 fc.lane3_species = specie
88 fc.lane4_sample = clean_data['lane4_description']
89 species_name = clean_data['lane4_species']
91 specie = models.Specie.objects.get(scientific_name=species_name)
92 except models.Specie.DoesNotExist:
93 specie = models.Specie(scientific_name=species_name)
95 fc.lane4_species = specie
98 fc.lane5_sample = clean_data['lane5_description']
99 species_name = clean_data['lane5_species']
101 specie = models.Specie.objects.get(scientific_name=species_name)
102 except models.Specie.DoesNotExist:
103 specie = models.Specie(scientific_name=species_name)
105 fc.lane5_species = specie
108 fc.lane6_sample = clean_data['lane6_description']
109 species_name = clean_data['lane6_species']
111 specie = models.Specie.objects.get(scientific_name=species_name)
112 except models.Specie.DoesNotExist:
113 specie = models.Specie(scientific_name=species_name)
115 fc.lane6_species = specie
118 fc.lane7_sample = clean_data['lane7_description']
119 species_name = clean_data['lane7_species']
121 specie = models.Specie.objects.get(scientific_name=species_name)
122 except models.Specie.DoesNotExist:
123 specie = models.Specie(scientific_name=species_name)
125 fc.lane7_species = specie
128 fc.lane8_sample = clean_data['lane8_description']
129 species_name = clean_data['lane8_species']
131 specie = models.Specie.objects.get(scientific_name=species_name)
132 except models.Specie.DoesNotExist:
133 specie = models.Specie(scientific_name=species_name)
135 fc.lane8_species = specie
137 fc.notes = clean_data['notes']
144 def generateElandConfig(form):
147 form = form.cleaned_data
149 BASE_DIR = '/data-store01/compbio/genomes'
151 data.append("# FLOWCELL: %s" % (form['flow_cell_number']))
154 notes = form['notes'].replace('\r\n', '\n').replace('\r', '\n')
155 notes = notes.replace('\n', '\n# ')
156 data.append("# NOTES:")
157 data.append("# %s\n#" % (notes))
159 #Convert all newline conventions to unix style
160 l1d = form['lane1_description'].replace('\r\n', '\n').replace('\r', '\n')
161 l2d = form['lane2_description'].replace('\r\n', '\n').replace('\r', '\n')
162 l3d = form['lane3_description'].replace('\r\n', '\n').replace('\r', '\n')
163 l4d = form['lane4_description'].replace('\r\n', '\n').replace('\r', '\n')
164 l5d = form['lane5_description'].replace('\r\n', '\n').replace('\r', '\n')
165 l6d = form['lane6_description'].replace('\r\n', '\n').replace('\r', '\n')
166 l7d = form['lane7_description'].replace('\r\n', '\n').replace('\r', '\n')
167 l8d = form['lane8_description'].replace('\r\n', '\n').replace('\r', '\n')
169 # Turn new lines into indented commented newlines
170 l1d = l1d.replace('\n', '\n# ')
171 l2d = l2d.replace('\n', '\n# ')
172 l3d = l3d.replace('\n', '\n# ')
173 l4d = l4d.replace('\n', '\n# ')
174 l5d = l5d.replace('\n', '\n# ')
175 l6d = l6d.replace('\n', '\n# ')
176 l7d = l7d.replace('\n', '\n# ')
177 l8d = l8d.replace('\n', '\n# ')
179 data.append("# Lane1: %s" % (l1d))
180 data.append("# Lane2: %s" % (l2d))
181 data.append("# Lane3: %s" % (l3d))
182 data.append("# Lane4: %s" % (l4d))
183 data.append("# Lane5: %s" % (l5d))
184 data.append("# Lane6: %s" % (l6d))
185 data.append("# Lane7: %s" % (l7d))
186 data.append("# Lane8: %s" % (l8d))
188 #data.append("GENOME_DIR %s" % (BASE_DIR))
189 #data.append("CONTAM_DIR %s" % (BASE_DIR))
190 read_length = form['read_length']
191 data.append("READ_LENGTH %d" % (read_length))
192 #data.append("ELAND_REPEAT")
193 data.append("ELAND_MULTIPLE_INSTANCES 8")
195 #Construct genome dictionary to figure out what lanes to put
196 # in the config file.
198 l1s = form['lane1_species']
199 genome_dict.setdefault(l1s, []).append('1')
200 l2s = form['lane2_species']
201 genome_dict.setdefault(l2s, []).append('2')
202 l3s = form['lane3_species']
203 genome_dict.setdefault(l3s, []).append('3')
204 l4s = form['lane4_species']
205 genome_dict.setdefault(l4s, []).append('4')
206 l5s = form['lane5_species']
207 genome_dict.setdefault(l5s, []).append('5')
208 l6s = form['lane6_species']
209 genome_dict.setdefault(l6s, []).append('6')
210 l7s = form['lane7_species']
211 genome_dict.setdefault(l7s, []).append('7')
212 l8s = form['lane8_species']
213 genome_dict.setdefault(l8s, []).append('8')
215 genome_list = genome_dict.keys()
218 #Loop through and create entries for each species.
219 for genome in genome_list:
220 lanes = ''.join(genome_dict[genome])
221 data.append('%s:ANALYSIS eland' % (lanes))
222 data.append('%s:READ_LENGTH %s' % (lanes, read_length))
223 data.append('%s:ELAND_GENOME %s' % (lanes, os.path.join(BASE_DIR, genome)))
224 data.append('%s:USE_BASES %s' % (lanes, 'Y'*int(read_length)))
226 data.append('SEQUENCE_FORMAT --scarf')
228 return '\n'.join(data)
230 def config(request, flowcell=None):
232 Returns eland config file for a given flowcell number,
233 or returns a list of available flowcell numbers.
236 # Provide INDEX of available Flowcell config files.
238 #Find all FC* config files and report an index html file
239 fc_list = [ os.path.split(file_path)[1] for file_path in glob.glob(os.path.join(settings.UPLOADTO_CONFIG_FILE, 'FC*')) ]
240 #Convert FC* list to html links
241 fc_html = [ '<a href="/elandifier/config/%s/">%s</a>' % (fc_name, fc_name) for fc_name in fc_list ]
242 return HttpResponse('<br />'.join(fc_html))
244 #FIXME: Should validate flowcell input before using.
245 file_path = os.path.join(settings.UPLOADTO_CONFIG_FILE, flowcell)
247 if not os.path.isfile(file_path):
248 return HttpResponse("Hmm, config file for %s does not seem to exist. Maybe I don't exist either?" % (flowcell))
250 f = open(file_path, 'r')
254 return HttpResponse(cfg, mimetype="text/plain")
261 Return a form for filling out information about the flowcell
263 if request.method == 'POST':
264 form = forms.ConfigForm(request.POST, error_class=forms.DivErrorList)
266 #cfg = generateElandConfig(form)
267 _saveConfigFile(form)
269 return HttpResponse("Eland Config Saved!", mimetype="text/plain")
271 return render_to_response('config_form.html', {'form': form })
274 fm = forms.ConfigForm(error_class=forms.DivErrorList)
275 return render_to_response('config_form.html', {'form': fm })