1 # Create your views here.
2 from htsworkflow.frontend.samples.changelist import ChangeList
3 from htsworkflow.frontend.samples.models import Library
4 from htsworkflow.frontend.samples.results import get_flowcell_result_dict, parse_flowcell_id
5 from htsworkflow.pipelines.runfolder import load_pipeline_run_xml
6 from htsworkflow.pipelines import runfolder
7 from htsworkflow.frontend import settings
8 from htsworkflow.util import makebed
9 from htsworkflow.util import opener
11 from django.http import HttpResponse
12 from django.template import RequestContext
13 from django.template.loader import get_template
19 LANE_LIST = [1,2,3,4,5,6,7,8]
21 def create_library_context(cl):
23 Create a list of libraries that includes how many lanes were run
26 #for lib in library_items.object_list:
27 for lib in cl.result_list:
29 summary['library_id'] = lib.library_id
30 summary['library_name'] = lib.library_name
31 summary['species_name' ] = lib.library_species.scientific_name
33 for lane_id in LANE_LIST:
34 lane = getattr(lib, 'lane_%d_library' % (lane_id,))
35 lanes_run += len( lane.all() )
36 summary['lanes_run'] = lanes_run
37 records.append(summary)
38 cl.result_count = unicode(cl.paginator._count) + u" libraries"
39 return {'library_list': records }
43 fcl = ChangeList(request, Library,
44 list_filter=['library_species','affiliations'],
45 search_fields=['library_id', 'library_name'],
47 queryset=Library.objects.filter(hidden__exact=0)
50 context = { 'cl': fcl}
51 context.update(create_library_context(fcl))
52 t = get_template('samples/library_index.html')
53 c = RequestContext(request, context)
54 return HttpResponse( t.render(c) )
56 def library_to_flowcells(request, lib_id):
58 Display information about all the flowcells a library has been run on.
60 t = get_template("samples/library_detail.html")
63 lib = Library.objects.get(library_id=lib_id)
65 return HttpResponse("Library %s does not exist" % (lib_id))
69 output.append('<b>Library ID:</b> %s' % (lib.library_id))
70 output.append('<b>Name:</b> %s' % (lib.library_name))
71 output.append('<b>Species:</b> %s' % (lib.library_species.scientific_name))
74 output.append('<b>FLOWCELL - LANE:</b>')
76 output.extend([ '%s - Lane 1 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 1)) for fc in lib.lane_1_library.all() ])
77 output.extend([ '%s - Lane 2 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 2)) for fc in lib.lane_2_library.all() ])
78 output.extend([ '%s - Lane 3 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 3)) for fc in lib.lane_3_library.all() ])
79 output.extend([ '%s - Lane 4 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 4)) for fc in lib.lane_4_library.all() ])
80 output.extend([ '%s - Lane 5 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 5)) for fc in lib.lane_5_library.all() ])
81 output.extend([ '%s - Lane 6 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 6)) for fc in lib.lane_6_library.all() ])
82 output.extend([ '%s - Lane 7 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 7)) for fc in lib.lane_7_library.all() ])
83 output.extend([ '%s - Lane 8 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 8)) for fc in lib.lane_8_library.all() ])
85 record_count = lib.lane_1_library.count() + \
86 lib.lane_2_library.count() + \
87 lib.lane_3_library.count() + \
88 lib.lane_4_library.count() + \
89 lib.lane_5_library.count() + \
90 lib.lane_6_library.count() + \
91 lib.lane_7_library.count() + \
92 lib.lane_8_library.count()
95 flowcell_list.extend([ (fc.flowcell_id, 1) for fc in lib.lane_1_library.all() ])
96 flowcell_list.extend([ (fc.flowcell_id, 2) for fc in lib.lane_2_library.all() ])
97 flowcell_list.extend([ (fc.flowcell_id, 3) for fc in lib.lane_3_library.all() ])
98 flowcell_list.extend([ (fc.flowcell_id, 4) for fc in lib.lane_4_library.all() ])
99 flowcell_list.extend([ (fc.flowcell_id, 5) for fc in lib.lane_5_library.all() ])
100 flowcell_list.extend([ (fc.flowcell_id, 6) for fc in lib.lane_6_library.all() ])
101 flowcell_list.extend([ (fc.flowcell_id, 7) for fc in lib.lane_7_library.all() ])
102 flowcell_list.extend([ (fc.flowcell_id, 8) for fc in lib.lane_8_library.all() ])
105 lane_summary_list = []
106 for fc, lane in flowcell_list:
107 lane_summary, err_list = _summary_stats(fc, lane)
109 lane_summary_list.extend(lane_summary)
114 output.append('<br />')
115 output.append(t.render(RequestContext(request, {'lane_summary_list': lane_summary_list})))
116 output.append('<br />')
118 if record_count == 0:
119 output.append("None Found")
121 return HttpResponse('<br />\n'.join(output))
124 def summaryhtm_fc_cnm(request, fc_id, cnm):
126 returns a Summary.htm file if it exists.
128 fc_id, status = parse_flowcell_id(fc_id)
129 d = get_flowcell_result_dict(fc_id)
132 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
135 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
137 summary_filepath = d[cnm]['summary']
139 if summary_filepath is None:
140 return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
142 f = open(summary_filepath, 'r')
144 return HttpResponse(f)
147 def result_fc_cnm_eland_lane(request, fc_id, cnm, lane):
149 returns an eland_file upon calling.
151 fc_id, status = parse_flowcell_id(fc_id)
152 d = get_flowcell_result_dict(fc_id)
155 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
158 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
160 erd = d[cnm]['eland_results']
164 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
168 f = opener.autoopen(filepath, 'r')
170 return HttpResponse(f, mimetype="application/x-elandresult")
173 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
175 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
177 return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
180 def bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=False):
182 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
184 fc_id, status = parse_flowcell_id(fc_id)
185 d = get_flowcell_result_dict(fc_id)
188 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
191 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
193 erd = d[cnm]['eland_results']
197 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
202 fi = opener.autoopen(filepath, 'r')
205 name, description = makebed.make_description( fc_id, lane )
207 bedgen = makebed.make_bed_from_eland_generator(fi, name, description)
210 return HttpResponse(bedgen)
212 return HttpResponse(bedgen, mimetype="application/x-bedfile")
215 def _summary_stats(flowcell_id, lane_id):
217 Return the summary statistics for a given flowcell, lane, and end.
219 fc_id, status = parse_flowcell_id(flowcell_id)
220 fc_result_dict = get_flowcell_result_dict(fc_id)
225 if fc_result_dict is None:
226 err_list.append('Results for Flowcell %s not found.' % (fc_id))
227 return (summary_list, err_list)
229 for cycle_width in fc_result_dict:
230 xmlpath = fc_result_dict[cycle_width]['run_xml']
233 err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cycle_width))
237 run = load_pipeline_run_xml(xmlpath)
238 gerald_summary = run.gerald.summary.lane_results
239 for end in range(len(gerald_summary)):
240 eland_summary = run.gerald.eland_results.results[end][lane_id]
241 # add information to lane_summary
242 eland_summary.flowcell_id = flowcell_id
243 eland_summary.clusters = gerald_summary[end][lane_id].cluster
244 eland_summary.cycle_width = cycle_width
245 eland_summary.summarized_reads = runfolder.summarize_mapped_reads(eland_summary.mapped_reads)
246 summary_list.append(eland_summary)
249 summary_list.append("Summary report needs to be updated.")
250 logging.error("Exception: " + str(e))
252 return (summary_list, err_list)
254 def _summary_stats_old(flowcell_id, lane):
256 return a dictionary of summary stats for a given flowcell_id & lane.
258 fc_id, status = parse_flowcell_id(flowcell_id)
259 fc_result_dict = get_flowcell_result_dict(fc_id)
265 if fc_result_dict is None:
266 err_list.append('Results for Flowcell %s not found.' % (fc_id))
267 return (dict_list, err_list, summary_list)
269 for cnm in fc_result_dict:
271 xmlpath = fc_result_dict[cnm]['run_xml']
274 err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cnm))
277 tree = ElementTree.parse(xmlpath).getroot()
278 results = runfolder.PipelineRun(pathname='', xml=tree)
280 lane_report = runfolder.summarize_lane(results.gerald, lane)
281 summary_list.append(os.linesep.join(lane_report))
283 summary_list.append("Summary report needs to be updated.")
284 logging.error("Exception: " + str(e))
286 print "----------------------------------"
287 print "-- DOES NOT SUPPORT PAIRED END ---"
288 print "----------------------------------"
289 lane_results = results.gerald.summary[0][lane]
294 d['average_alignment_score'] = lrs.average_alignment_score
295 d['average_first_cycle_intensity'] = lrs.average_first_cycle_intensity
296 d['cluster'] = lrs.cluster
298 d['flowcell'] = flowcell_id
300 d['percent_error_rate'] = lrs.percent_error_rate
301 d['percent_intensity_after_20_cycles'] = lrs.percent_intensity_after_20_cycles
302 d['percent_pass_filter_align'] = lrs.percent_pass_filter_align
303 d['percent_pass_filter_clusters'] = lrs.percent_pass_filter_clusters
305 #FIXME: function finished, but need to take advantage of
306 # may need to take in a list of lanes so we only have to
307 # load the xml file once per flowcell rather than once
311 return (dict_list, err_list, summary_list)
316 def _files(flowcell_id, lane):
318 Sets up available files for download
320 flowcell_id, id = parse_flowcell_id(flowcell_id)
321 d = get_flowcell_result_dict(flowcell_id)
328 # c_name == 'CN-M' (i.e. C1-33)
331 if d[c_name]['summary'] is not None:
332 output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' \
333 % (flowcell_id, c_name, c_name))
335 erd = d[c_name]['eland_results']
338 output.append('<a href="/results/%s/%s/eland_result/%s">eland_result(%s)</a>' % (flowcell_id, c_name, lane, c_name))
339 output.append('<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>' % (flowcell_id, c_name, lane, c_name))
344 return '(' + '|'.join(output) + ')'