Merge in the library list, detail, and results downloading feature from
[htsworkflow.git] / htsworkflow / frontend / samples / views.py
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
9
10 from django.http import HttpResponse
11 from django.template.loader import get_template
12 from django.template import Context
13
14 import StringIO
15 import logging
16 import os
17
18 LANE_LIST = [1,2,3,4,5,6,7,8]
19
20 def create_library_list():
21     """
22     Create a list of libraries that includes how many lanes were run
23     """
24     library_list = []
25     for lib in Library.objects.all():
26        summary = {}
27        summary['library_id'] = lib.library_id
28        summary['library_name'] = lib.library_name
29        summary['species_name' ] = lib.library_species.scientific_name
30        lanes_run = 0
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)
36     return library_list
37
38 def library(request):
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) )
43
44 def library_to_flowcells(request, lib_id):
45     """
46     Display information about all the flowcells a library has been run on.
47     """
48     t = get_template("samples/library_detail.html")
49     
50     try:
51       lib = Library.objects.get(library_id=lib_id)
52     except:
53       return HttpResponse("Library %s does not exist" % (lib_id))
54     
55     output = []
56     
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))
60     output.append('')
61     
62     output.append('<b>FLOWCELL - LANE:</b>')
63     
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() ])
72     
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()
81     
82     flowcell_list = []
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() ])
91     flowcell_list.sort()
92     
93     lane_summary_list = []
94     for fc, lane in flowcell_list:
95         lane_summary, err_list = _summary_stats(fc, lane)
96         
97         lane_summary_list.extend(lane_summary)
98     
99         for err in err_list:    
100             output.append(err)
101    
102     output.append('<br />')
103     output.append(t.render(Context({'lane_summary_list': lane_summary_list})))
104     output.append('<br />')
105     
106     if record_count == 0:
107         output.append("None Found")
108     
109     return HttpResponse('<br />\n'.join(output))
110
111
112 def summaryhtm_fc_cnm(request, fc_id, cnm):
113     """
114     returns a Summary.htm file if it exists.
115     """
116     fc_id, status = parse_flowcell_id(fc_id)
117     d = get_flowcell_result_dict(fc_id)
118     
119     if d is None:
120         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
121     
122     if cnm not in d:
123         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
124     
125     summary_filepath = d[cnm]['summary']
126     
127     if summary_filepath is None:
128         return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
129     
130     f = open(summary_filepath, 'r')
131     
132     return HttpResponse(f)
133
134
135 def result_fc_cnm_eland_lane(request, fc_id, cnm, lane):
136     """
137     returns an eland_file upon calling.
138     """
139     fc_id, status = parse_flowcell_id(fc_id)
140     d = get_flowcell_result_dict(fc_id)
141     
142     if d is None:
143         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
144     
145     if cnm not in d:
146         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
147     
148     erd = d[cnm]['eland_results']
149     lane = int(lane)
150     
151     if lane not in erd:
152         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
153     
154     filepath = erd[lane]
155     
156     f = opener.autoopen(filepath, 'r')
157     
158     return HttpResponse(f, mimetype="application/x-elandresult")
159
160
161 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
162     """
163     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
164     """
165     return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
166
167
168 def bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=False):
169     """
170     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
171     """
172     fc_id, status = parse_flowcell_id(fc_id)
173     d = get_flowcell_result_dict(fc_id)
174     
175     if d is None:
176         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
177     
178     if cnm not in d:
179         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
180     
181     erd = d[cnm]['eland_results']
182     lane = int(lane)
183     
184     if lane not in erd:
185         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
186     
187     filepath = erd[lane]
188     
189     # Eland result file
190     fi = opener.autoopen(filepath, 'r')
191     # output memory file
192     
193     name, description = makebed.make_description( fc_id, lane )
194     
195     bedgen = makebed.make_bed_from_eland_generator(fi, name, description)
196     
197     if ucsc_compatible:
198         return HttpResponse(bedgen)
199     else:
200         return HttpResponse(bedgen, mimetype="application/x-bedfile")
201
202
203 def _summary_stats(flowcell_id, lane_id):
204     """
205     Return the summary statistics for a given flowcell, lane, and end.
206     """
207     fc_id, status = parse_flowcell_id(flowcell_id)
208     fc_result_dict = get_flowcell_result_dict(fc_id)
209
210     summary_list = []
211     err_list = []
212     
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)
216
217     for cycle_width in fc_result_dict:
218         xmlpath = fc_result_dict[cycle_width]['run_xml']
219         
220         if xmlpath is None:
221             err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cycle_width))
222             continue
223         
224         try:
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)
235
236         except Exception, e:
237             summary_list.append("Summary report needs to be updated.")
238             logging.error("Exception: " + str(e))
239     
240     return (summary_list, err_list)
241
242 def _summary_stats_old(flowcell_id, lane):
243     """
244     return a dictionary of summary stats for a given flowcell_id & lane.
245     """
246     fc_id, status = parse_flowcell_id(flowcell_id)
247     fc_result_dict = get_flowcell_result_dict(fc_id)
248     
249     dict_list = []
250     err_list = []
251     summary_list = []
252     
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)
256     
257     for cnm in fc_result_dict:
258     
259         xmlpath = fc_result_dict[cnm]['run_xml']
260         
261         if xmlpath is None:
262             err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cnm))
263             continue
264         
265         tree = ElementTree.parse(xmlpath).getroot()
266         results = runfolder.PipelineRun(pathname='', xml=tree)
267         try:
268             lane_report = runfolder.summarize_lane(results.gerald, lane)
269             summary_list.append(os.linesep.join(lane_report))
270         except Exception, e:
271             summary_list.append("Summary report needs to be updated.")
272             logging.error("Exception: " + str(e))
273        
274         print "----------------------------------"
275         print "-- DOES NOT SUPPORT PAIRED END ---"
276         print "----------------------------------"
277         lane_results = results.gerald.summary[0][lane]
278         lrs = lane_results
279         
280         d = {}
281         
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
285         d['lane'] = lrs.lane
286         d['flowcell'] = flowcell_id
287         d['cnm'] = cnm
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
292         
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
296         #   per lane.
297         dict_list.append(d)
298     
299     return (dict_list, err_list, summary_list)
300     
301     
302
303     
304 def _files(flowcell_id, lane):
305     """
306     Sets up available files for download
307     """
308     flowcell_id, id = parse_flowcell_id(flowcell_id)
309     d = get_flowcell_result_dict(flowcell_id)
310     
311     if d is None:
312         return ''
313     
314     output = []
315     
316     # c_name == 'CN-M' (i.e. C1-33)
317     for c_name in d:
318         
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))
322         
323         erd = d[c_name]['eland_results']
324         
325         if int(lane) in erd:
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))
328     
329     if len(output) == 0:
330         return ''
331     
332     return '(' + '|'.join(output) + ')'
333