1 # Create your views here.
2 from htsworkflow.frontend.experiments.models import FlowCell
3 from htsworkflow.frontend.samples.changelist import ChangeList
4 from htsworkflow.frontend.samples.models import Library
5 from htsworkflow.frontend.samples.results import get_flowcell_result_dict, parse_flowcell_id
6 from htsworkflow.pipelines.runfolder import load_pipeline_run_xml
7 from htsworkflow.pipelines import runfolder
8 from htsworkflow.frontend import settings
9 from htsworkflow.util import makebed
10 from htsworkflow.util import opener
12 from django.http import HttpResponse
13 from django.template import RequestContext
14 from django.template.loader import get_template
20 LANE_LIST = [1,2,3,4,5,6,7,8]
22 def create_library_context(cl):
24 Create a list of libraries that includes how many lanes were run
27 #for lib in library_items.object_list:
28 for lib in cl.result_list:
30 summary['library_id'] = lib.library_id
31 summary['library_name'] = lib.library_name
32 summary['species_name' ] = lib.library_species.scientific_name
34 for lane_id in LANE_LIST:
35 lane = getattr(lib, 'lane_%d_library' % (lane_id,))
36 lanes_run += len( lane.all() )
37 summary['lanes_run'] = lanes_run
38 records.append(summary)
39 cl.result_count = unicode(cl.paginator._count) + u" libraries"
40 return {'library_list': records }
44 fcl = ChangeList(request, Library,
45 list_filter=['library_species','affiliations'],
46 search_fields=['library_id', 'library_name'],
48 queryset=Library.objects.filter(hidden__exact=0)
51 context = { 'cl': fcl, 'title': 'Library Index'}
52 context.update(create_library_context(fcl))
53 t = get_template('samples/library_index.html')
54 c = RequestContext(request, context)
55 return HttpResponse( t.render(c) )
57 def library_to_flowcells(request, lib_id):
59 Display information about all the flowcells a library has been run on.
61 t = get_template("samples/library_detail.html")
64 lib = Library.objects.get(library_id=lib_id)
66 return HttpResponse("Library %s does not exist" % (lib_id))
70 output.append('<b>Library ID:</b> %s' % (lib.library_id))
71 output.append('<b>Name:</b> %s' % (lib.library_name))
72 output.append('<b>Species:</b> %s' % (lib.library_species.scientific_name))
75 output.append('<b>FLOWCELL - LANE:</b>')
77 output.extend([ '%s - Lane 1 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 1)) for fc in lib.lane_1_library.all() ])
78 output.extend([ '%s - Lane 2 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 2)) for fc in lib.lane_2_library.all() ])
79 output.extend([ '%s - Lane 3 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 3)) for fc in lib.lane_3_library.all() ])
80 output.extend([ '%s - Lane 4 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 4)) for fc in lib.lane_4_library.all() ])
81 output.extend([ '%s - Lane 5 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 5)) for fc in lib.lane_5_library.all() ])
82 output.extend([ '%s - Lane 6 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 6)) for fc in lib.lane_6_library.all() ])
83 output.extend([ '%s - Lane 7 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 7)) for fc in lib.lane_7_library.all() ])
84 output.extend([ '%s - Lane 8 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 8)) for fc in lib.lane_8_library.all() ])
86 record_count = lib.lane_1_library.count() + \
87 lib.lane_2_library.count() + \
88 lib.lane_3_library.count() + \
89 lib.lane_4_library.count() + \
90 lib.lane_5_library.count() + \
91 lib.lane_6_library.count() + \
92 lib.lane_7_library.count() + \
93 lib.lane_8_library.count()
96 flowcell_list.extend([ (fc.flowcell_id, 1) for fc in lib.lane_1_library.all() ])
97 flowcell_list.extend([ (fc.flowcell_id, 2) for fc in lib.lane_2_library.all() ])
98 flowcell_list.extend([ (fc.flowcell_id, 3) for fc in lib.lane_3_library.all() ])
99 flowcell_list.extend([ (fc.flowcell_id, 4) for fc in lib.lane_4_library.all() ])
100 flowcell_list.extend([ (fc.flowcell_id, 5) for fc in lib.lane_5_library.all() ])
101 flowcell_list.extend([ (fc.flowcell_id, 6) for fc in lib.lane_6_library.all() ])
102 flowcell_list.extend([ (fc.flowcell_id, 7) for fc in lib.lane_7_library.all() ])
103 flowcell_list.extend([ (fc.flowcell_id, 8) for fc in lib.lane_8_library.all() ])
106 lane_summary_list = []
107 for fc, lane in flowcell_list:
108 lane_summary, err_list = _summary_stats(fc, lane)
110 lane_summary_list.extend(lane_summary)
115 output.append('<br />')
116 output.append(t.render(RequestContext(request, {'lane_summary_list': lane_summary_list})))
117 output.append('<br />')
119 if record_count == 0:
120 output.append("None Found")
122 return HttpResponse('<br />\n'.join(output))
125 def summaryhtm_fc_cnm(request, fc_id, cnm):
127 returns a Summary.htm file if it exists.
129 fc_id, status = parse_flowcell_id(fc_id)
130 d = get_flowcell_result_dict(fc_id)
133 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
136 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
138 summary_filepath = d[cnm]['summary']
140 if summary_filepath is None:
141 return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
143 f = open(summary_filepath, 'r')
145 return HttpResponse(f)
148 def result_fc_cnm_eland_lane(request, fc_id, cnm, lane):
150 returns an eland_file upon calling.
152 fc_id, status = parse_flowcell_id(fc_id)
153 d = get_flowcell_result_dict(fc_id)
156 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
159 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
161 erd = d[cnm]['eland_results']
165 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
169 f = opener.autoopen(filepath, 'r')
171 return HttpResponse(f, mimetype="application/x-elandresult")
174 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
176 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
178 return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
181 def bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=False):
183 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
185 fc_id, status = parse_flowcell_id(fc_id)
186 d = get_flowcell_result_dict(fc_id)
189 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
192 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
194 erd = d[cnm]['eland_results']
198 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
203 fi = opener.autoopen(filepath, 'r')
206 name, description = makebed.make_description( fc_id, lane )
208 bedgen = makebed.make_bed_from_eland_generator(fi, name, description)
211 return HttpResponse(bedgen)
213 return HttpResponse(bedgen, mimetype="application/x-bedfile")
216 def _summary_stats(flowcell_id, lane_id):
218 Return the summary statistics for a given flowcell, lane, and end.
220 fc_id, status = parse_flowcell_id(flowcell_id)
221 fc_result_dict = get_flowcell_result_dict(fc_id)
226 if fc_result_dict is None:
227 err_list.append('Results for Flowcell %s not found.' % (fc_id))
228 return (summary_list, err_list)
230 for cycle_width in fc_result_dict:
231 xmlpath = fc_result_dict[cycle_width]['run_xml']
234 err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cycle_width))
238 run = load_pipeline_run_xml(xmlpath)
239 gerald_summary = run.gerald.summary.lane_results
240 for end in range(len(gerald_summary)):
241 eland_summary = run.gerald.eland_results.results[end][lane_id]
242 # add information to lane_summary
243 eland_summary.flowcell_id = flowcell_id
244 eland_summary.clusters = gerald_summary[end][lane_id].cluster
245 eland_summary.cycle_width = cycle_width
246 eland_summary.summarized_reads = runfolder.summarize_mapped_reads(eland_summary.genome_map, eland_summary.mapped_reads)
248 # grab some more information out of the flowcell db
249 flowcell = FlowCell.objects.get(flowcell_id=fc_id)
250 pm_field = 'lane_%d_pM' % (lane_id)
251 eland_summary.successful_pm = getattr(flowcell, pm_field)
253 summary_list.append(eland_summary)
256 summary_list.append("Summary report needs to be updated.")
257 logging.error("Exception: " + str(e))
259 return (summary_list, err_list)
261 def _summary_stats_old(flowcell_id, lane):
263 return a dictionary of summary stats for a given flowcell_id & lane.
265 fc_id, status = parse_flowcell_id(flowcell_id)
266 fc_result_dict = get_flowcell_result_dict(fc_id)
272 if fc_result_dict is None:
273 err_list.append('Results for Flowcell %s not found.' % (fc_id))
274 return (dict_list, err_list, summary_list)
276 for cnm in fc_result_dict:
278 xmlpath = fc_result_dict[cnm]['run_xml']
281 err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cnm))
284 tree = ElementTree.parse(xmlpath).getroot()
285 results = runfolder.PipelineRun(pathname='', xml=tree)
287 lane_report = runfolder.summarize_lane(results.gerald, lane)
288 summary_list.append(os.linesep.join(lane_report))
290 summary_list.append("Summary report needs to be updated.")
291 logging.error("Exception: " + str(e))
293 print "----------------------------------"
294 print "-- DOES NOT SUPPORT PAIRED END ---"
295 print "----------------------------------"
296 lane_results = results.gerald.summary[0][lane]
301 d['average_alignment_score'] = lrs.average_alignment_score
302 d['average_first_cycle_intensity'] = lrs.average_first_cycle_intensity
303 d['cluster'] = lrs.cluster
305 d['flowcell'] = flowcell_id
307 d['percent_error_rate'] = lrs.percent_error_rate
308 d['percent_intensity_after_20_cycles'] = lrs.percent_intensity_after_20_cycles
309 d['percent_pass_filter_align'] = lrs.percent_pass_filter_align
310 d['percent_pass_filter_clusters'] = lrs.percent_pass_filter_clusters
312 #FIXME: function finished, but need to take advantage of
313 # may need to take in a list of lanes so we only have to
314 # load the xml file once per flowcell rather than once
318 return (dict_list, err_list, summary_list)
323 def _files(flowcell_id, lane):
325 Sets up available files for download
327 flowcell_id, id = parse_flowcell_id(flowcell_id)
328 d = get_flowcell_result_dict(flowcell_id)
335 # c_name == 'CN-M' (i.e. C1-33)
338 if d[c_name]['summary'] is not None:
339 output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' \
340 % (flowcell_id, c_name, c_name))
342 erd = d[c_name]['eland_results']
345 output.append('<a href="/results/%s/%s/eland_result/%s">eland_result(%s)</a>' % (flowcell_id, c_name, lane, c_name))
346 output.append('<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>' % (flowcell_id, c_name, lane, c_name))
351 return '(' + '|'.join(output) + ')'