Libraries Detail View integrated.
[htsworkflow.git] / htsworkflow / frontend / samples / views.py
1 # Create your views here.
2 from htsworkflow.frontend.experiments.models import FlowCell
3 from htsworkflow.frontend.samples.changelist import ChangeList
4 from htsworkflow.frontend.samples.models import Library
5 from htsworkflow.frontend.samples.results import get_flowcell_result_dict, parse_flowcell_id
6 from htsworkflow.pipelines.runfolder import load_pipeline_run_xml
7 from htsworkflow.pipelines import runfolder
8 from htsworkflow.frontend import settings
9 from htsworkflow.util import makebed
10 from htsworkflow.util import opener
11
12 from django.core.exceptions import ObjectDoesNotExist
13 from django.http import HttpResponse, HttpResponseRedirect
14 from django.shortcuts import render_to_response
15 from django.template import RequestContext
16 from django.template.loader import get_template
17 from django.contrib.auth.decorators import login_required
18
19 import StringIO
20 import logging
21 import os
22
23 LANE_LIST = [1,2,3,4,5,6,7,8]
24 SAMPLES_CONTEXT_DEFAULTS = {
25     'app_name': 'Flowcell/Library Tracker'
26 }
27
28 def create_library_context(cl):
29     """
30     Create a list of libraries that includes how many lanes were run
31     """
32     records = []
33     #for lib in library_items.object_list:
34     for lib in cl.result_list:
35        summary = {}
36        summary['library_id'] = lib.library_id
37        summary['library_name'] = lib.library_name
38        summary['species_name' ] = lib.library_species.scientific_name
39        if lib.amplified_from_sample is not None:
40            summary['amplified_from'] = lib.amplified_from_sample.library_id
41        else:
42            summary['amplified_from'] = ''
43        lanes_run = 0
44        for lane_id in LANE_LIST:
45            lane = getattr(lib, 'lane_%d_library' % (lane_id,))
46            lanes_run += len( lane.all() )
47        summary['lanes_run'] = lanes_run
48        summary['is_archived'] = lib.is_archived()
49        records.append(summary)
50     cl.result_count = unicode(cl.paginator._count) + u" libraries"
51     return {'library_list': records }
52
53 def library(request):
54    # build changelist
55     fcl = ChangeList(request, Library,
56         list_filter=['affiliations', 'library_species'],
57         search_fields=['library_id', 'library_name', 'amplified_from_sample__library_id'],
58         list_per_page=200,
59         queryset=Library.objects.filter(hidden__exact=0)
60     )
61
62     context = { 'cl': fcl, 'title': 'Library Index'}
63     context.update(create_library_context(fcl))
64     t = get_template('samples/library_index.html')
65     c = RequestContext(request, context)
66     
67     app_context = {
68         'page_name': 'Library Index',
69         'east_region_config_div': 'changelist-filter',
70         'body': t.render(c)
71     }
72     app_context.update(SAMPLES_CONTEXT_DEFAULTS)
73     
74     app_t = get_template('flowcell_libraries_app.html')
75     app_c = RequestContext(request, app_context)
76     return HttpResponse( app_t.render(app_c) )
77
78 def library_to_flowcells(request, lib_id):
79     """
80     Display information about all the flowcells a library has been run on.
81     """
82     
83     try:
84       lib = Library.objects.get(library_id=lib_id)
85     except:
86       return HttpResponse("Library %s does not exist" % (lib_id))
87    
88     flowcell_list = []
89     interesting_flowcells = {} # aka flowcells we're looking at
90     for lane in LANE_LIST:
91         lane_library = getattr(lib, 'lane_%d_library' % (lane,))
92         for fc in lane_library.all():
93             flowcell_id, id = parse_flowcell_id(fc.flowcell_id)
94             if flowcell_id not in interesting_flowcells:
95                 interesting_flowcells[flowcell_id] = get_flowcell_result_dict(flowcell_id)
96             flowcell_list.append((fc.flowcell_id, lane))
97
98     flowcell_list.sort()
99     
100     lane_summary_list = []
101     eland_results = []
102     for fc, lane in flowcell_list:
103         lane_summary, err_list = _summary_stats(fc, lane)
104
105         eland_results.extend(_make_eland_results(fc, lane, interesting_flowcells))
106         lane_summary_list.extend(lane_summary)
107
108     context = {
109         'page_name': 'Library Details',
110         'lib': lib,
111         'eland_results': eland_results,
112         'lane_summary_list': lane_summary_list,
113     }
114     context.update(SAMPLES_CONTEXT_DEFAULTS)
115
116     return render_to_response(
117         'samples/library_detail.html',
118         context,
119         context_instance = RequestContext(request))
120
121 def summaryhtm_fc_cnm(request, flowcell_id, cnm):
122     """
123     returns a Summary.htm file if it exists.
124     """
125     fc_id, status = parse_flowcell_id(flowcell_id)
126     d = get_flowcell_result_dict(fc_id)
127     
128     if d is None:
129         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
130     
131     if cnm not in d:
132         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
133     
134     summary_filepath = d[cnm]['summary']
135     
136     if summary_filepath is None:
137         return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
138     
139     f = open(summary_filepath, 'r')
140     
141     return HttpResponse(f)
142
143
144 def result_fc_cnm_eland_lane(request, flowcell_id, cnm, lane):
145     """
146     returns an eland_file upon calling.
147     """
148     fc_id, status = parse_flowcell_id(flowcell_id)
149     d = get_flowcell_result_dict(fc_id)
150     
151     if d is None:
152         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
153     
154     if cnm not in d:
155         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
156     
157     erd = d[cnm]['eland_results']
158     lane = int(lane)
159     
160     if lane not in erd:
161         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
162     
163     filepath = erd[lane]
164     
165     #f = opener.autoopen(filepath, 'r')
166     # return HttpResponse(f, mimetype="application/x-elandresult")
167
168     f = open(filepath, 'r')
169     return HttpResponse(f, mimetype='application/x-bzip2')
170     
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, flowcell_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(flowcell_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         run = load_pipeline_run_xml(xmlpath)
237         gerald_summary = run.gerald.summary.lane_results
238         for end in range(len(gerald_summary)):
239             eland_summary = run.gerald.eland_results.results[end][lane_id]
240             # add information to lane_summary
241             eland_summary.flowcell_id = flowcell_id
242             eland_summary.clusters = gerald_summary[end][lane_id].cluster
243             eland_summary.cycle_width = cycle_width
244             if hasattr(eland_summary, 'genome_map'):
245                 eland_summary.summarized_reads = runfolder.summarize_mapped_reads( 
246                                                    eland_summary.genome_map, 
247                                                    eland_summary.mapped_reads)
248
249             # grab some more information out of the flowcell db
250             flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
251             pm_field = 'lane_%d_pM' % (lane_id)
252             eland_summary.successful_pm = getattr(flowcell, pm_field)
253
254             summary_list.append(eland_summary)
255
256         #except Exception, e:
257         #    summary_list.append("Summary report needs to be updated.")
258         #    logging.error("Exception: " + str(e))
259     
260     return (summary_list, err_list)
261
262 def _summary_stats_old(flowcell_id, lane):
263     """
264     return a dictionary of summary stats for a given flowcell_id & lane.
265     """
266     fc_id, status = parse_flowcell_id(flowcell_id)
267     fc_result_dict = get_flowcell_result_dict(fc_id)
268     
269     dict_list = []
270     err_list = []
271     summary_list = []
272     
273     if fc_result_dict is None:
274         err_list.append('Results for Flowcell %s not found.' % (fc_id))
275         return (dict_list, err_list, summary_list)
276     
277     for cnm in fc_result_dict:
278     
279         xmlpath = fc_result_dict[cnm]['run_xml']
280         
281         if xmlpath is None:
282             err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cnm))
283             continue
284         
285         tree = ElementTree.parse(xmlpath).getroot()
286         results = runfolder.PipelineRun(pathname='', xml=tree)
287         try:
288             lane_report = runfolder.summarize_lane(results.gerald, lane)
289             summary_list.append(os.linesep.join(lane_report))
290         except Exception, e:
291             summary_list.append("Summary report needs to be updated.")
292             logging.error("Exception: " + str(e))
293        
294         print "----------------------------------"
295         print "-- DOES NOT SUPPORT PAIRED END ---"
296         print "----------------------------------"
297         lane_results = results.gerald.summary[0][lane]
298         lrs = lane_results
299         
300         d = {}
301         
302         d['average_alignment_score'] = lrs.average_alignment_score
303         d['average_first_cycle_intensity'] = lrs.average_first_cycle_intensity
304         d['cluster'] = lrs.cluster
305         d['lane'] = lrs.lane
306         d['flowcell'] = flowcell_id
307         d['cnm'] = cnm
308         d['percent_error_rate'] = lrs.percent_error_rate
309         d['percent_intensity_after_20_cycles'] = lrs.percent_intensity_after_20_cycles
310         d['percent_pass_filter_align'] = lrs.percent_pass_filter_align
311         d['percent_pass_filter_clusters'] = lrs.percent_pass_filter_clusters
312         
313         #FIXME: function finished, but need to take advantage of
314         #   may need to take in a list of lanes so we only have to
315         #   load the xml file once per flowcell rather than once
316         #   per lane.
317         dict_list.append(d)
318     
319     return (dict_list, err_list, summary_list)
320     
321     
322 def get_eland_result_type(pathname):
323     """
324     Guess the eland result file type from the filename
325     """
326     path, filename = os.path.split(pathname)
327     if 'extended' in filename:
328         return 'extended'
329     elif 'multi' in filename:
330         return 'multi'
331     elif 'result' in filename:
332         return 'result'
333     else:
334         return 'unknown'
335
336 def _make_eland_results(flowcell_id, lane, interesting_flowcells):
337     fc_id, status = parse_flowcell_id(flowcell_id)
338     cur_fc = interesting_flowcells.get(fc_id, None)
339     if cur_fc is None:
340       return []
341
342     flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
343     # Loop throw storage devices if a result has been archived
344     storage_id_list = []
345     if cur_fc is not None:
346         for lts in flowcell.longtermstorage_set.all():
347             for sd in lts.storage_devices.all():
348                 # Use barcode_id if it exists
349                 if sd.barcode_id is not None and sd.barcode_id != '':
350                     storage_id_list.append(sd.barcode_id)
351                 # Otherwise use UUID
352                 else:
353                     storage_id_list.append(sd.uuid)
354         
355     # Formatting for template use
356     if len(storage_id_list) == 0:
357         storage_ids = None
358     else:
359         storage_ids = ', '.join(storage_id_list)
360
361     results = []
362     for cycle in cur_fc.keys():
363         result_path = cur_fc[cycle]['eland_results'].get(lane, None)
364         result_link = make_result_link(fc_id, cycle, lane, result_path)
365         results.append({'flowcell_id': fc_id,
366                         'cycle': cycle, 
367                         'lane': lane, 
368                         'summary_url': make_summary_url(flowcell_id, cycle),
369                         'result_url': result_link[0],
370                         'result_label': result_link[1],
371                         'bed_url': result_link[2],
372                         'storage_ids': storage_ids
373         })
374     return results
375
376 def make_summary_url(flowcell_id, cycle_name):
377     url = '/results/%s/%s/summary/' % (flowcell_id, cycle_name)
378     return url
379
380 def make_result_link(flowcell_id, cycle_name, lane, eland_result_path):
381     if eland_result_path is None:
382         return ("", "", "")
383
384     result_type = get_eland_result_type(eland_result_path)
385     result_url = '/results/%s/%s/eland_result/%s' % (flowcell_id, cycle_name, lane)
386     result_label = 'eland %s' % (result_type,)
387     bed_url = None
388     if result_type == 'result':
389        bed_url_pattern = '/results/%s/%s/bedfile/%s'
390        bed_url = bed_url_pattern % (flowcell_id, cycle_name, lane)
391     
392     return (result_url, result_label, bed_url)
393
394 def _files(flowcell_id, lane):
395     """
396     Sets up available files for download
397     """
398     lane = int(lane)
399
400     flowcell_id, id = parse_flowcell_id(flowcell_id)
401     d = get_flowcell_result_dict(flowcell_id)
402     
403     if d is None:
404         return ''
405     
406     output = []
407     
408     # c_name == 'CN-M' (i.e. C1-33)
409     for c_name in d:
410         
411         if d[c_name]['summary'] is not None:
412             output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' \
413                           % (flowcell_id, c_name, c_name))
414         
415         erd = d[c_name]['eland_results']
416         if lane in erd:
417             result_type = get_eland_result_type(erd[lane])
418             result_url_pattern = '<a href="/results/%s/%s/eland_result/%s">eland %s(%s)</a>'
419             output.append(result_url_pattern % (flowcell_id, c_name, lane, result_type, c_name))
420             if result_type == 'result':
421                 bed_url_pattern = '<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>'
422                 output.append(bed_url_pattern % (flowcell_id, c_name, lane, c_name))
423     
424     if len(output) == 0:
425         return ''
426     
427     return '(' + '|'.join(output) + ')'
428
429 def library_id_to_admin_url(request, lib_id):
430     lib = Library.objects.get(library_id=lib_id)
431     return HttpResponseRedirect('/admin/samples/library/%s' % (lib.id,))
432
433 @login_required
434 def user_profile(request):
435     """
436     Information about the user
437     """
438     context = {
439                 'page_name': 'User Profile',
440                 'media': '',
441                 #'bcmagic': BarcodeMagicForm(),
442                 #'select': 'settings',
443             }
444     context.update(SAMPLES_CONTEXT_DEFAULTS)
445     return render_to_response('registration/profile.html', context,
446                               context_instance=RequestContext(request))