Make the public library browsing page support several features from
[htsworkflow.git] / htsworkflow / frontend / samples / views.py
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
10
11 from django.http import HttpResponse
12 from django.template import RequestContext
13 from django.template.loader import get_template
14
15 import StringIO
16 import logging
17 import os
18
19 LANE_LIST = [1,2,3,4,5,6,7,8]
20
21 def create_library_context(cl):
22     """
23     Create a list of libraries that includes how many lanes were run
24     """
25     records = []
26     #for lib in library_items.object_list:
27     for lib in cl.result_list:
28        summary = {}
29        summary['library_id'] = lib.library_id
30        summary['library_name'] = lib.library_name
31        summary['species_name' ] = lib.library_species.scientific_name
32        lanes_run = 0
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 }
40
41 def library(request):
42    # build changelist
43     fcl = ChangeList(request, Library,
44         list_filter=['library_species','affiliations'],
45         search_fields=['library_id', 'library_name'],
46         list_per_page=25,
47         queryset=Library.objects.filter(hidden__exact=0)
48     )
49
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) )
55
56 def library_to_flowcells(request, lib_id):
57     """
58     Display information about all the flowcells a library has been run on.
59     """
60     t = get_template("samples/library_detail.html")
61     
62     try:
63       lib = Library.objects.get(library_id=lib_id)
64     except:
65       return HttpResponse("Library %s does not exist" % (lib_id))
66     
67     output = []
68     
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))
72     output.append('')
73     
74     output.append('<b>FLOWCELL - LANE:</b>')
75     
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() ])
84     
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()
93     
94     flowcell_list = []
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() ])
103     flowcell_list.sort()
104     
105     lane_summary_list = []
106     for fc, lane in flowcell_list:
107         lane_summary, err_list = _summary_stats(fc, lane)
108         
109         lane_summary_list.extend(lane_summary)
110     
111         for err in err_list:    
112             output.append(err)
113    
114     output.append('<br />')
115     output.append(t.render(RequestContext(request, {'lane_summary_list': lane_summary_list})))
116     output.append('<br />')
117     
118     if record_count == 0:
119         output.append("None Found")
120     
121     return HttpResponse('<br />\n'.join(output))
122
123
124 def summaryhtm_fc_cnm(request, fc_id, cnm):
125     """
126     returns a Summary.htm file if it exists.
127     """
128     fc_id, status = parse_flowcell_id(fc_id)
129     d = get_flowcell_result_dict(fc_id)
130     
131     if d is None:
132         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
133     
134     if cnm not in d:
135         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
136     
137     summary_filepath = d[cnm]['summary']
138     
139     if summary_filepath is None:
140         return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
141     
142     f = open(summary_filepath, 'r')
143     
144     return HttpResponse(f)
145
146
147 def result_fc_cnm_eland_lane(request, fc_id, cnm, lane):
148     """
149     returns an eland_file upon calling.
150     """
151     fc_id, status = parse_flowcell_id(fc_id)
152     d = get_flowcell_result_dict(fc_id)
153     
154     if d is None:
155         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
156     
157     if cnm not in d:
158         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
159     
160     erd = d[cnm]['eland_results']
161     lane = int(lane)
162     
163     if lane not in erd:
164         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
165     
166     filepath = erd[lane]
167     
168     f = opener.autoopen(filepath, 'r')
169     
170     return HttpResponse(f, mimetype="application/x-elandresult")
171
172
173 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
174     """
175     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
176     """
177     return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
178
179
180 def bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=False):
181     """
182     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
183     """
184     fc_id, status = parse_flowcell_id(fc_id)
185     d = get_flowcell_result_dict(fc_id)
186     
187     if d is None:
188         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
189     
190     if cnm not in d:
191         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
192     
193     erd = d[cnm]['eland_results']
194     lane = int(lane)
195     
196     if lane not in erd:
197         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
198     
199     filepath = erd[lane]
200     
201     # Eland result file
202     fi = opener.autoopen(filepath, 'r')
203     # output memory file
204     
205     name, description = makebed.make_description( fc_id, lane )
206     
207     bedgen = makebed.make_bed_from_eland_generator(fi, name, description)
208     
209     if ucsc_compatible:
210         return HttpResponse(bedgen)
211     else:
212         return HttpResponse(bedgen, mimetype="application/x-bedfile")
213
214
215 def _summary_stats(flowcell_id, lane_id):
216     """
217     Return the summary statistics for a given flowcell, lane, and end.
218     """
219     fc_id, status = parse_flowcell_id(flowcell_id)
220     fc_result_dict = get_flowcell_result_dict(fc_id)
221
222     summary_list = []
223     err_list = []
224     
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)
228
229     for cycle_width in fc_result_dict:
230         xmlpath = fc_result_dict[cycle_width]['run_xml']
231         
232         if xmlpath is None:
233             err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cycle_width))
234             continue
235         
236         try:
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)
247
248         except Exception, e:
249             summary_list.append("Summary report needs to be updated.")
250             logging.error("Exception: " + str(e))
251     
252     return (summary_list, err_list)
253
254 def _summary_stats_old(flowcell_id, lane):
255     """
256     return a dictionary of summary stats for a given flowcell_id & lane.
257     """
258     fc_id, status = parse_flowcell_id(flowcell_id)
259     fc_result_dict = get_flowcell_result_dict(fc_id)
260     
261     dict_list = []
262     err_list = []
263     summary_list = []
264     
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)
268     
269     for cnm in fc_result_dict:
270     
271         xmlpath = fc_result_dict[cnm]['run_xml']
272         
273         if xmlpath is None:
274             err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cnm))
275             continue
276         
277         tree = ElementTree.parse(xmlpath).getroot()
278         results = runfolder.PipelineRun(pathname='', xml=tree)
279         try:
280             lane_report = runfolder.summarize_lane(results.gerald, lane)
281             summary_list.append(os.linesep.join(lane_report))
282         except Exception, e:
283             summary_list.append("Summary report needs to be updated.")
284             logging.error("Exception: " + str(e))
285        
286         print "----------------------------------"
287         print "-- DOES NOT SUPPORT PAIRED END ---"
288         print "----------------------------------"
289         lane_results = results.gerald.summary[0][lane]
290         lrs = lane_results
291         
292         d = {}
293         
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
297         d['lane'] = lrs.lane
298         d['flowcell'] = flowcell_id
299         d['cnm'] = cnm
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
304         
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
308         #   per lane.
309         dict_list.append(d)
310     
311     return (dict_list, err_list, summary_list)
312     
313     
314
315     
316 def _files(flowcell_id, lane):
317     """
318     Sets up available files for download
319     """
320     flowcell_id, id = parse_flowcell_id(flowcell_id)
321     d = get_flowcell_result_dict(flowcell_id)
322     
323     if d is None:
324         return ''
325     
326     output = []
327     
328     # c_name == 'CN-M' (i.e. C1-33)
329     for c_name in d:
330         
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))
334         
335         erd = d[c_name]['eland_results']
336         
337         if int(lane) in erd:
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))
340     
341     if len(output) == 0:
342         return ''
343     
344     return '(' + '|'.join(output) + ')'
345