1 # Create your views here.
2 from htsworkflow.frontend.samples.models import Library
3 from htsworkflow.frontend.samples.results import get_flowcell_result_dict, parse_flowcell_id
4 from htsworkflow.pipelines.runfolder import load_pipeline_run_xml
5 from htsworkflow.pipelines import runfolder
6 from htsworkflow.frontend import settings
7 from htsworkflow.util import makebed
8 from htsworkflow.util import opener
10 from django.http import HttpResponse
11 from django.template.loader import get_template
12 from django.template import Context
18 LANE_LIST = [1,2,3,4,5,6,7,8]
20 def create_library_list():
22 Create a list of libraries that includes how many lanes were run
25 for lib in Library.objects.all():
27 summary['library_id'] = lib.library_id
28 summary['library_name'] = lib.library_name
29 summary['species_name' ] = lib.library_species.scientific_name
31 for lane_id in LANE_LIST:
32 lane = getattr(lib, 'lane_%d_library' % (lane_id,))
33 lanes_run += len( lane.all() )
34 summary['lanes_run'] = lanes_run
35 library_list.append(summary)
39 library_list = create_library_list()
40 t = get_template('samples/library_index.html')
41 c = Context({'library_list': library_list })
42 return HttpResponse( t.render(c) )
44 def library_to_flowcells(request, lib_id):
46 Display information about all the flowcells a library has been run on.
48 t = get_template("samples/library_detail.html")
51 lib = Library.objects.get(library_id=lib_id)
53 return HttpResponse("Library %s does not exist" % (lib_id))
57 output.append('<b>Library ID:</b> %s' % (lib.library_id))
58 output.append('<b>Name:</b> %s' % (lib.library_name))
59 output.append('<b>Species:</b> %s' % (lib.library_species.scientific_name))
62 output.append('<b>FLOWCELL - LANE:</b>')
64 output.extend([ '%s - Lane 1 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 1)) for fc in lib.lane_1_library.all() ])
65 output.extend([ '%s - Lane 2 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 2)) for fc in lib.lane_2_library.all() ])
66 output.extend([ '%s - Lane 3 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 3)) for fc in lib.lane_3_library.all() ])
67 output.extend([ '%s - Lane 4 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 4)) for fc in lib.lane_4_library.all() ])
68 output.extend([ '%s - Lane 5 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 5)) for fc in lib.lane_5_library.all() ])
69 output.extend([ '%s - Lane 6 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 6)) for fc in lib.lane_6_library.all() ])
70 output.extend([ '%s - Lane 7 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 7)) for fc in lib.lane_7_library.all() ])
71 output.extend([ '%s - Lane 8 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 8)) for fc in lib.lane_8_library.all() ])
73 record_count = lib.lane_1_library.count() + \
74 lib.lane_2_library.count() + \
75 lib.lane_3_library.count() + \
76 lib.lane_4_library.count() + \
77 lib.lane_5_library.count() + \
78 lib.lane_6_library.count() + \
79 lib.lane_7_library.count() + \
80 lib.lane_8_library.count()
83 flowcell_list.extend([ (fc.flowcell_id, 1) for fc in lib.lane_1_library.all() ])
84 flowcell_list.extend([ (fc.flowcell_id, 2) for fc in lib.lane_2_library.all() ])
85 flowcell_list.extend([ (fc.flowcell_id, 3) for fc in lib.lane_3_library.all() ])
86 flowcell_list.extend([ (fc.flowcell_id, 4) for fc in lib.lane_4_library.all() ])
87 flowcell_list.extend([ (fc.flowcell_id, 5) for fc in lib.lane_5_library.all() ])
88 flowcell_list.extend([ (fc.flowcell_id, 6) for fc in lib.lane_6_library.all() ])
89 flowcell_list.extend([ (fc.flowcell_id, 7) for fc in lib.lane_7_library.all() ])
90 flowcell_list.extend([ (fc.flowcell_id, 8) for fc in lib.lane_8_library.all() ])
93 lane_summary_list = []
94 for fc, lane in flowcell_list:
95 lane_summary, err_list = _summary_stats(fc, lane)
97 lane_summary_list.extend(lane_summary)
102 output.append('<br />')
103 output.append(t.render(Context({'lane_summary_list': lane_summary_list})))
104 output.append('<br />')
106 if record_count == 0:
107 output.append("None Found")
109 return HttpResponse('<br />\n'.join(output))
112 def summaryhtm_fc_cnm(request, fc_id, cnm):
114 returns a Summary.htm file if it exists.
116 fc_id, status = parse_flowcell_id(fc_id)
117 d = get_flowcell_result_dict(fc_id)
120 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
123 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
125 summary_filepath = d[cnm]['summary']
127 if summary_filepath is None:
128 return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
130 f = open(summary_filepath, 'r')
132 return HttpResponse(f)
135 def result_fc_cnm_eland_lane(request, fc_id, cnm, lane):
137 returns an eland_file upon calling.
139 fc_id, status = parse_flowcell_id(fc_id)
140 d = get_flowcell_result_dict(fc_id)
143 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
146 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
148 erd = d[cnm]['eland_results']
152 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
156 f = opener.autoopen(filepath, 'r')
158 return HttpResponse(f, mimetype="application/x-elandresult")
161 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
163 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
165 return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
168 def bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=False):
170 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
172 fc_id, status = parse_flowcell_id(fc_id)
173 d = get_flowcell_result_dict(fc_id)
176 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
179 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
181 erd = d[cnm]['eland_results']
185 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
190 fi = opener.autoopen(filepath, 'r')
193 name, description = makebed.make_description( fc_id, lane )
195 bedgen = makebed.make_bed_from_eland_generator(fi, name, description)
198 return HttpResponse(bedgen)
200 return HttpResponse(bedgen, mimetype="application/x-bedfile")
203 def _summary_stats(flowcell_id, lane_id):
205 Return the summary statistics for a given flowcell, lane, and end.
207 fc_id, status = parse_flowcell_id(flowcell_id)
208 fc_result_dict = get_flowcell_result_dict(fc_id)
213 if fc_result_dict is None:
214 err_list.append('Results for Flowcell %s not found.' % (fc_id))
215 return (summary_list, err_list)
217 for cycle_width in fc_result_dict:
218 xmlpath = fc_result_dict[cycle_width]['run_xml']
221 err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cycle_width))
225 run = load_pipeline_run_xml(xmlpath)
226 gerald_summary = run.gerald.summary.lane_results
227 for end in range(len(gerald_summary)):
228 eland_summary = run.gerald.eland_results.results[end][lane_id]
229 # add information to lane_summary
230 eland_summary.flowcell_id = flowcell_id
231 eland_summary.clusters = gerald_summary[end][lane_id].cluster
232 eland_summary.cycle_width = cycle_width
233 eland_summary.summarized_reads = runfolder.summarize_mapped_reads(eland_summary.mapped_reads)
234 summary_list.append(eland_summary)
237 summary_list.append("Summary report needs to be updated.")
238 logging.error("Exception: " + str(e))
240 return (summary_list, err_list)
242 def _summary_stats_old(flowcell_id, lane):
244 return a dictionary of summary stats for a given flowcell_id & lane.
246 fc_id, status = parse_flowcell_id(flowcell_id)
247 fc_result_dict = get_flowcell_result_dict(fc_id)
253 if fc_result_dict is None:
254 err_list.append('Results for Flowcell %s not found.' % (fc_id))
255 return (dict_list, err_list, summary_list)
257 for cnm in fc_result_dict:
259 xmlpath = fc_result_dict[cnm]['run_xml']
262 err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cnm))
265 tree = ElementTree.parse(xmlpath).getroot()
266 results = runfolder.PipelineRun(pathname='', xml=tree)
268 lane_report = runfolder.summarize_lane(results.gerald, lane)
269 summary_list.append(os.linesep.join(lane_report))
271 summary_list.append("Summary report needs to be updated.")
272 logging.error("Exception: " + str(e))
274 print "----------------------------------"
275 print "-- DOES NOT SUPPORT PAIRED END ---"
276 print "----------------------------------"
277 lane_results = results.gerald.summary[0][lane]
282 d['average_alignment_score'] = lrs.average_alignment_score
283 d['average_first_cycle_intensity'] = lrs.average_first_cycle_intensity
284 d['cluster'] = lrs.cluster
286 d['flowcell'] = flowcell_id
288 d['percent_error_rate'] = lrs.percent_error_rate
289 d['percent_intensity_after_20_cycles'] = lrs.percent_intensity_after_20_cycles
290 d['percent_pass_filter_align'] = lrs.percent_pass_filter_align
291 d['percent_pass_filter_clusters'] = lrs.percent_pass_filter_clusters
293 #FIXME: function finished, but need to take advantage of
294 # may need to take in a list of lanes so we only have to
295 # load the xml file once per flowcell rather than once
299 return (dict_list, err_list, summary_list)
304 def _files(flowcell_id, lane):
306 Sets up available files for download
308 flowcell_id, id = parse_flowcell_id(flowcell_id)
309 d = get_flowcell_result_dict(flowcell_id)
316 # c_name == 'CN-M' (i.e. C1-33)
319 if d[c_name]['summary'] is not None:
320 output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' \
321 % (flowcell_id, c_name, c_name))
323 erd = d[c_name]['eland_results']
326 output.append('<a href="/results/%s/%s/eland_result/%s">eland_result(%s)</a>' % (flowcell_id, c_name, lane, c_name))
327 output.append('<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>' % (flowcell_id, c_name, lane, c_name))
332 return '(' + '|'.join(output) + ')'