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_stats(fc, lane)
83 data_dict_list.extend(dicts)
88 html = t.render(Context({'data_dict_list': data_dict_list}))
89 output.append('<br />')
90 output.append('<br />')
92 output.append('<br />')
93 output.append('<br />')
96 output.append("None Found")
98 return HttpResponse('<br />\n'.join(output))
101 def summaryhtm_fc_cnm(request, fc_id, cnm):
103 returns a Summary.htm file if it exists.
105 fc_id = flowcellIdStrip(fc_id)
106 d = get_flowcell_result_dict(fc_id)
109 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
112 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
114 summary_filepath = d[cnm]['summary']
116 if summary_filepath is None:
117 return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
119 f = open(summary_filepath, 'r')
121 return HttpResponse(f)
124 def result_fc_cnm_eland_lane(request, fc_id, cnm, lane):
126 returns an eland_file upon calling.
128 fc_id = flowcellIdStrip(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 erd = d[cnm]['eland_results']
141 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
145 f = opener.autoopen(filepath, 'r')
147 return HttpResponse(f, mimetype="application/x-elandresult")
150 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
152 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
154 return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
157 def bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=False):
159 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
161 fc_id = flowcellIdStrip(fc_id)
162 d = get_flowcell_result_dict(fc_id)
165 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
168 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
170 erd = d[cnm]['eland_results']
174 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
179 fi = opener.autoopen(filepath, 'r')
183 name, description = makebed.make_description(settings.DATABASE_NAME,
187 bedgen = makebed.make_bed_from_eland_stream_generator(fi, name, description)
190 return HttpResponse(bedgen)
192 return HttpResponse(bedgen, mimetype="application/x-bedfile")
195 def _summary_stats(flowcell_id, lane):
197 return a dictionary of summary stats for a given flowcell_id & lane.
199 fc_id = flowcellIdStrip(flowcell_id)
200 fc_result_dict = get_flowcell_result_dict(fc_id)
205 if fc_result_dict is None:
206 err_list.append('Results for Flowcell %s not found.' % (fc_id))
207 return (dict_list, err_list)
209 for cnm in fc_result_dict:
211 xmlpath = fc_result_dict[cnm]['runx_xml']
214 err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cnm))
217 tree = ElementTree.parse(xmlpath).getroot()
218 results = runfolder.PipelineRun(pathname='', xml=tree)
220 lane_results = results.gerald.summary[str(lane)]
225 d['average_alignment_score'] = lrs.average_alignment_score
226 d['average_first_cycle_intensity'] = lrs.average_first_cycle_intensity
227 d['cluster'] = lrs.cluster
229 d['flowcell'] = flowcell_id
230 d['percent_error_rate'] = lrs.percent_error_rate
231 d['percent_intensity_after_20_cycles'] = lrs.percent_intensity_after_20_cycles
232 d['percent_pass_filter_align'] = lrs.percent_pass_filter_align
233 d['percent_pass_filter_clusters'] = lrs.percent_pass_filter_clusters
235 #FIXME: function finished, but need to take advantage of
236 # may need to take in a list of lanes so we only have to
237 # load the xml file once per flowcell rather than once
241 return (dict_list, err_list)
246 def _files(flowcell_id, lane):
248 Sets up available files for download
250 flowcell_id = flowcellIdStrip(flowcell_id)
251 d = get_flowcell_result_dict(flowcell_id)
258 # c_name == 'CN-M' (i.e. C1-33)
261 if d[c_name]['summary'] is not None:
262 output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' \
263 % (flowcell_id, c_name, c_name))
265 erd = d[c_name]['eland_results']
268 output.append('<a href="/results/%s/%s/eland_result/%s">eland_result(%s)</a>' % (flowcell_id, c_name, lane, c_name))
269 output.append('<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>' % (flowcell_id, c_name, lane, c_name))
274 return '(' + '|'.join(output) + ')'