1 # Create your views here.
2 from gaworkflow.frontend.fctracker.models import Library
3 from gaworkflow.frontend.fctracker.results import get_flowcell_result_dict, flowcellIdStrip
4 from gaworkflow.pipeline.runfolder import ElementTree
5 from gaworkflow.pipeline import runfolder
6 from gaworkflow.frontend import settings
7 from gaworkflow.util import makebed
8 from gaworkflow.util import opener
10 from django.http import HttpResponse
11 from django.template.loader import get_template
12 from django.template import Context
16 #from django.db.models import base
19 library_list = Library.objects.all() #.order_by('-pub_date')
20 rep_string = '<a href="/library/%s/">%s - %s (%s)</a>'
21 output = '<br />\n'.join([rep_string \
25 l.library_species.scientific_name) for l in library_list])
26 return HttpResponse(output)
28 def library_to_flowcells(request, lib_id):
30 Display information about all the flowcells a library has been run on.
32 t = get_template("summary_stats.html")
35 lib = Library.objects.get(library_id=lib_id)
37 return HttpResponse("Library %s does not exist" % (lib_id))
41 output.append('<b>Library ID:</b> %s' % (lib.library_id))
42 output.append('<b>Name:</b> %s' % (lib.library_name))
43 output.append('<b>Species:</b> %s' % (lib.library_species.scientific_name))
46 output.append('<b>FLOWCELL - LANE:</b>')
48 output.extend([ '%s - Lane 1 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 1)) for fc in lib.lane_1_library.all() ])
49 output.extend([ '%s - Lane 2 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 2)) for fc in lib.lane_2_library.all() ])
50 output.extend([ '%s - Lane 3 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 3)) for fc in lib.lane_3_library.all() ])
51 output.extend([ '%s - Lane 4 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 4)) for fc in lib.lane_4_library.all() ])
52 output.extend([ '%s - Lane 5 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 5)) for fc in lib.lane_5_library.all() ])
53 output.extend([ '%s - Lane 6 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 6)) for fc in lib.lane_6_library.all() ])
54 output.extend([ '%s - Lane 7 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 7)) for fc in lib.lane_7_library.all() ])
55 output.extend([ '%s - Lane 8 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 8)) for fc in lib.lane_8_library.all() ])
57 record_count = lib.lane_1_library.count() + \
58 lib.lane_2_library.count() + \
59 lib.lane_3_library.count() + \
60 lib.lane_4_library.count() + \
61 lib.lane_5_library.count() + \
62 lib.lane_6_library.count() + \
63 lib.lane_7_library.count() + \
64 lib.lane_8_library.count()
67 flowcell_list.extend([ (fc.flowcell_id, 1) for fc in lib.lane_1_library.all() ])
68 flowcell_list.extend([ (fc.flowcell_id, 2) for fc in lib.lane_2_library.all() ])
69 flowcell_list.extend([ (fc.flowcell_id, 3) for fc in lib.lane_3_library.all() ])
70 flowcell_list.extend([ (fc.flowcell_id, 4) for fc in lib.lane_4_library.all() ])
71 flowcell_list.extend([ (fc.flowcell_id, 5) for fc in lib.lane_5_library.all() ])
72 flowcell_list.extend([ (fc.flowcell_id, 6) for fc in lib.lane_6_library.all() ])
73 flowcell_list.extend([ (fc.flowcell_id, 7) for fc in lib.lane_7_library.all() ])
74 flowcell_list.extend([ (fc.flowcell_id, 8) for fc in lib.lane_8_library.all() ])
77 output.append('<br />')
80 for fc, lane in flowcell_list:
81 dicts, err_list, summary_list = _summary_stats(fc, lane)
83 data_dict_list.extend(dicts)
88 for summary in summary_list:
89 output.append(summary.replace('\n', '<br />\n'))
90 html = t.render(Context({'data_dict_list': data_dict_list}))
91 output.append('<br />')
92 output.append('<br />')
94 output.append('<br />')
95 output.append('<br />')
98 output.append("None Found")
100 return HttpResponse('<br />\n'.join(output))
103 def summaryhtm_fc_cnm(request, fc_id, cnm):
105 returns a Summary.htm file if it exists.
107 fc_id = flowcellIdStrip(fc_id)
108 d = get_flowcell_result_dict(fc_id)
111 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
114 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
116 summary_filepath = d[cnm]['summary']
118 if summary_filepath is None:
119 return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
121 f = open(summary_filepath, 'r')
123 return HttpResponse(f)
126 def result_fc_cnm_eland_lane(request, fc_id, cnm, lane):
128 returns an eland_file upon calling.
130 fc_id = flowcellIdStrip(fc_id)
131 d = get_flowcell_result_dict(fc_id)
134 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
137 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
139 erd = d[cnm]['eland_results']
143 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
147 f = opener.autoopen(filepath, 'r')
149 return HttpResponse(f, mimetype="application/x-elandresult")
152 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
154 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
156 return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
159 def bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=False):
161 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
163 fc_id = flowcellIdStrip(fc_id)
164 d = get_flowcell_result_dict(fc_id)
167 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
170 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
172 erd = d[cnm]['eland_results']
176 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
181 fi = opener.autoopen(filepath, 'r')
185 name, description = makebed.make_description(settings.DATABASE_NAME,
189 bedgen = makebed.make_bed_from_eland_stream_generator(fi, name, description)
192 return HttpResponse(bedgen)
194 return HttpResponse(bedgen, mimetype="application/x-bedfile")
197 def _summary_stats(flowcell_id, lane):
199 return a dictionary of summary stats for a given flowcell_id & lane.
201 fc_id = flowcellIdStrip(flowcell_id)
202 fc_result_dict = get_flowcell_result_dict(fc_id)
208 if fc_result_dict is None:
209 err_list.append('Results for Flowcell %s not found.' % (fc_id))
210 return (dict_list, err_list, summary_list)
212 for cnm in fc_result_dict:
214 xmlpath = fc_result_dict[cnm]['run_xml']
217 err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cnm))
220 tree = ElementTree.parse(xmlpath).getroot()
221 results = runfolder.PipelineRun(pathname='', xml=tree)
223 summary_list.append(runfolder.summary_report([results]))
225 summary_list.append("Summary report needs to be updated.")
227 lane_results = results.gerald.summary[0][lane]
232 d['average_alignment_score'] = lrs.average_alignment_score
233 d['average_first_cycle_intensity'] = lrs.average_first_cycle_intensity
234 d['cluster'] = lrs.cluster
236 d['flowcell'] = flowcell_id
238 d['percent_error_rate'] = lrs.percent_error_rate
239 d['percent_intensity_after_20_cycles'] = lrs.percent_intensity_after_20_cycles
240 d['percent_pass_filter_align'] = lrs.percent_pass_filter_align
241 d['percent_pass_filter_clusters'] = lrs.percent_pass_filter_clusters
243 #FIXME: function finished, but need to take advantage of
244 # may need to take in a list of lanes so we only have to
245 # load the xml file once per flowcell rather than once
249 return (dict_list, err_list, summary_list)
254 def _files(flowcell_id, lane):
256 Sets up available files for download
258 flowcell_id = flowcellIdStrip(flowcell_id)
259 d = get_flowcell_result_dict(flowcell_id)
266 # c_name == 'CN-M' (i.e. C1-33)
269 if d[c_name]['summary'] is not None:
270 output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' \
271 % (flowcell_id, c_name, c_name))
273 erd = d[c_name]['eland_results']
276 output.append('<a href="/results/%s/%s/eland_result/%s">eland_result(%s)</a>' % (flowcell_id, c_name, lane, c_name))
277 output.append('<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>' % (flowcell_id, c_name, lane, c_name))
282 return '(' + '|'.join(output) + ')'