Don't throw an error if library.cell_line is None.
[htsworkflow.git] / htsworkflow / frontend / samples / views.py
1 # Create your views here.
2 import StringIO
3 import logging
4 import os
5 import sys
6
7 try:
8     import json
9 except ImportError, e:
10     import simplejson as json
11
12 from htsworkflow.frontend.auth import require_api_key
13 from htsworkflow.frontend.experiments.models import FlowCell
14 from htsworkflow.frontend.samples.changelist import ChangeList
15 from htsworkflow.frontend.samples.models import Library
16 from htsworkflow.frontend.samples.results import get_flowcell_result_dict, parse_flowcell_id
17 from htsworkflow.frontend.bcmagic.forms import BarcodeMagicForm
18 from htsworkflow.pipelines.runfolder import load_pipeline_run_xml
19 from htsworkflow.pipelines import runfolder
20 from htsworkflow.pipelines.eland import ResultLane
21 from htsworkflow.frontend import settings
22 from htsworkflow.util.conversion import unicode_or_none
23 from htsworkflow.util import makebed
24 from htsworkflow.util import opener
25
26
27 from django.core.exceptions import ObjectDoesNotExist
28 from django.http import HttpResponse, HttpResponseRedirect, Http404
29 from django.shortcuts import render_to_response
30 from django.template import RequestContext
31 from django.template.loader import get_template
32 from django.contrib.auth.decorators import login_required
33
34 LANE_LIST = [1,2,3,4,5,6,7,8]
35 SAMPLES_CONTEXT_DEFAULTS = {
36     'app_name': 'Flowcell/Library Tracker',
37     'bcmagic': BarcodeMagicForm()
38 }
39
40 def create_library_context(cl):
41     """
42     Create a list of libraries that includes how many lanes were run
43     """
44     records = []
45     #for lib in library_items.object_list:
46     for lib in cl.result_list:
47        summary = {}
48        summary['library_id'] = lib.id
49        summary['library_name'] = lib.library_name
50        summary['species_name' ] = lib.library_species.scientific_name
51        if lib.amplified_from_sample is not None:
52            summary['amplified_from'] = lib.amplified_from_sample.id
53        else:
54            summary['amplified_from'] = ''
55        lanes_run = 0
56        #for lane_id in LANE_LIST:
57        #    lane = getattr(lib, 'lane_%d_library' % (lane_id,))
58        #    lanes_run += len( lane.all() )
59        lanes_run = lib.lane_set.count()
60        summary['lanes_run'] = lanes_run
61        summary['is_archived'] = lib.is_archived()
62        records.append(summary)
63     cl.result_count = unicode(cl.paginator._count) + u" libraries"
64     return {'library_list': records }
65
66 def library(request):
67    # build changelist
68     fcl = ChangeList(request, Library,
69         list_filter=['affiliations', 'library_species'],
70         search_fields=['id', 'library_name', 'amplified_from_sample__id'],
71         list_per_page=200,
72         queryset=Library.objects.filter(hidden__exact=0)
73     )
74
75     context = { 'cl': fcl, 'title': 'Library Index'}
76     context.update(create_library_context(fcl))
77     t = get_template('samples/library_index.html')
78     c = RequestContext(request, context)
79     
80     app_context = {
81         'page_name': 'Library Index',
82         'east_region_config_div': 'changelist-filter',
83         'body': t.render(c)
84     }
85     app_context.update(SAMPLES_CONTEXT_DEFAULTS)
86     
87     app_t = get_template('flowcell_libraries_app.html')
88     app_c = RequestContext(request, app_context)
89     return HttpResponse( app_t.render(app_c) )
90
91 def library_to_flowcells(request, lib_id):
92     """
93     Display information about all the flowcells a library has been run on.
94     """
95     
96     try:
97       lib = Library.objects.get(id=lib_id)
98     except:
99       return HttpResponse("Library %s does not exist" % (lib_id))
100    
101     flowcell_list = []
102     flowcell_run_results = {} # aka flowcells we're looking at
103     for lane in lib.lane_set.all():
104         fc = lane.flowcell
105         flowcell_id, id = parse_flowcell_id(fc.flowcell_id)
106         if flowcell_id not in flowcell_run_results:
107             flowcell_run_results[flowcell_id] = get_flowcell_result_dict(flowcell_id)
108         flowcell_list.append((fc.flowcell_id, lane.lane_number))
109
110     flowcell_list.sort()
111     lane_summary_list = []
112     eland_results = []
113     for fc, lane_number in flowcell_list:
114         lane_summary, err_list = _summary_stats(fc, lane_number)
115
116         eland_results.extend(_make_eland_results(fc, lane_number, flowcell_run_results))
117         lane_summary_list.extend(lane_summary)
118
119     context = {
120         'page_name': 'Library Details',
121         'lib': lib,
122         'eland_results': eland_results,
123         'lane_summary_list': lane_summary_list,
124     }
125     context.update(SAMPLES_CONTEXT_DEFAULTS)
126
127     return render_to_response(
128         'samples/library_detail.html',
129         context,
130         context_instance = RequestContext(request))
131
132 def summaryhtm_fc_cnm(request, flowcell_id, cnm):
133     """
134     returns a Summary.htm file if it exists.
135     """
136     fc_id, status = parse_flowcell_id(flowcell_id)
137     d = get_flowcell_result_dict(fc_id)
138     
139     if d is None:
140         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
141     
142     if cnm not in d:
143         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
144     
145     summary_filepath = d[cnm]['summary']
146     
147     if summary_filepath is None:
148         return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
149     
150     f = open(summary_filepath, 'r')
151     
152     return HttpResponse(f)
153
154
155 def result_fc_cnm_eland_lane(request, flowcell_id, cnm, lane):
156     """
157     returns an eland_file upon calling.
158     """
159     fc_id, status = parse_flowcell_id(flowcell_id)
160     d = get_flowcell_result_dict(fc_id)
161     
162     if d is None:
163         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
164     
165     if cnm not in d:
166         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
167     
168     erd = d[cnm]['eland_results']
169     lane = int(lane)
170     
171     if lane not in erd:
172         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
173     
174     filepath = erd[lane]
175     
176     #f = opener.autoopen(filepath, 'r')
177     # return HttpResponse(f, mimetype="application/x-elandresult")
178
179     f = open(filepath, 'r')
180     return HttpResponse(f, mimetype='application/x-bzip2')
181     
182
183
184 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
185     """
186     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
187     """
188     return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
189
190
191 def bedfile_fc_cnm_eland_lane(request, flowcell_id, cnm, lane, ucsc_compatible=False):
192     """
193     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
194     """
195     fc_id, status = parse_flowcell_id(flowcell_id)
196     d = get_flowcell_result_dict(fc_id)
197     
198     if d is None:
199         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
200     
201     if cnm not in d:
202         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
203     
204     erd = d[cnm]['eland_results']
205     lane = int(lane)
206     
207     if lane not in erd:
208         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
209     
210     filepath = erd[lane]
211     
212     # Eland result file
213     fi = opener.autoopen(filepath, 'r')
214     # output memory file
215     
216     name, description = makebed.make_description( fc_id, lane )
217     
218     bedgen = makebed.make_bed_from_eland_generator(fi, name, description)
219     
220     if ucsc_compatible:
221         return HttpResponse(bedgen)
222     else:
223         return HttpResponse(bedgen, mimetype="application/x-bedfile")
224
225
226 def _summary_stats(flowcell_id, lane_id):
227     """
228     Return the summary statistics for a given flowcell, lane, and end.
229     """
230     fc_id, status = parse_flowcell_id(flowcell_id)
231     fc_result_dict = get_flowcell_result_dict(fc_id)
232
233     summary_list = []
234     err_list = []
235     
236     if fc_result_dict is None:
237         err_list.append('Results for Flowcell %s not found.' % (fc_id))
238         return (summary_list, err_list)
239
240     for cycle_width in fc_result_dict:
241         xmlpath = fc_result_dict[cycle_width]['run_xml']
242         
243         if xmlpath is None:
244             err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cycle_width))
245             continue
246         
247         run = load_pipeline_run_xml(xmlpath)
248         gerald_summary = run.gerald.summary.lane_results
249         for end in range(len(gerald_summary)):
250             end_summary = run.gerald.eland_results.results[end]
251             if end_summary.has_key(lane_id):
252                 eland_summary = run.gerald.eland_results.results[end][lane_id]
253             else:
254                 eland_summary = ResultLane(lane_id=lane_id, end=end)
255             # add information to lane_summary
256             eland_summary.flowcell_id = flowcell_id
257             if len(gerald_summary) > end and gerald_summary[end].has_key(lane_id):
258                 eland_summary.clusters = gerald_summary[end][lane_id].cluster
259             else:
260                 eland_summary.clusters = None
261             eland_summary.cycle_width = cycle_width
262             if hasattr(eland_summary, 'genome_map'):
263                 eland_summary.summarized_reads = runfolder.summarize_mapped_reads( 
264                                                    eland_summary.genome_map, 
265                                                    eland_summary.mapped_reads)
266
267             # grab some more information out of the flowcell db
268             flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
269             #pm_field = 'lane_%d_pM' % (lane_id)
270             lane_obj = flowcell.lane_set.get(lane_number=lane_id)
271             eland_summary.successful_pm = lane_obj.pM
272
273             summary_list.append(eland_summary)
274
275         #except Exception, e:
276         #    summary_list.append("Summary report needs to be updated.")
277         #    logging.error("Exception: " + str(e))
278     
279     return (summary_list, err_list)
280
281 def _summary_stats_old(flowcell_id, lane):
282     """
283     return a dictionary of summary stats for a given flowcell_id & lane.
284     """
285     fc_id, status = parse_flowcell_id(flowcell_id)
286     fc_result_dict = get_flowcell_result_dict(fc_id)
287     
288     dict_list = []
289     err_list = []
290     summary_list = []
291     
292     if fc_result_dict is None:
293         err_list.append('Results for Flowcell %s not found.' % (fc_id))
294         return (dict_list, err_list, summary_list)
295     
296     for cnm in fc_result_dict:
297     
298         xmlpath = fc_result_dict[cnm]['run_xml']
299         
300         if xmlpath is None:
301             err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cnm))
302             continue
303         
304         tree = ElementTree.parse(xmlpath).getroot()
305         results = runfolder.PipelineRun(pathname='', xml=tree)
306         try:
307             lane_report = runfolder.summarize_lane(results.gerald, lane)
308             summary_list.append(os.linesep.join(lane_report))
309         except Exception, e:
310             summary_list.append("Summary report needs to be updated.")
311             logging.error("Exception: " + str(e))
312        
313         print >>sys.stderr, "----------------------------------"
314         print >>sys.stderr, "-- DOES NOT SUPPORT PAIRED END ---"
315         print >>sys.stderr, "----------------------------------"
316         lane_results = results.gerald.summary[0][lane]
317         lrs = lane_results
318         
319         d = {}
320         
321         d['average_alignment_score'] = lrs.average_alignment_score
322         d['average_first_cycle_intensity'] = lrs.average_first_cycle_intensity
323         d['cluster'] = lrs.cluster
324         d['lane'] = lrs.lane
325         d['flowcell'] = flowcell_id
326         d['cnm'] = cnm
327         d['percent_error_rate'] = lrs.percent_error_rate
328         d['percent_intensity_after_20_cycles'] = lrs.percent_intensity_after_20_cycles
329         d['percent_pass_filter_align'] = lrs.percent_pass_filter_align
330         d['percent_pass_filter_clusters'] = lrs.percent_pass_filter_clusters
331         
332         #FIXME: function finished, but need to take advantage of
333         #   may need to take in a list of lanes so we only have to
334         #   load the xml file once per flowcell rather than once
335         #   per lane.
336         dict_list.append(d)
337     
338     return (dict_list, err_list, summary_list)
339     
340     
341 def get_eland_result_type(pathname):
342     """
343     Guess the eland result file type from the filename
344     """
345     path, filename = os.path.split(pathname)
346     if 'extended' in filename:
347         return 'extended'
348     elif 'multi' in filename:
349         return 'multi'
350     elif 'result' in filename:
351         return 'result'
352     else:
353         return 'unknown'
354
355 def _make_eland_results(flowcell_id, lane, interesting_flowcells):
356     fc_id, status = parse_flowcell_id(flowcell_id)
357     cur_fc = interesting_flowcells.get(fc_id, None)
358     if cur_fc is None:
359       return []
360
361     flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
362     # Loop throw storage devices if a result has been archived
363     storage_id_list = []
364     if cur_fc is not None:
365         for lts in flowcell.longtermstorage_set.all():
366             for sd in lts.storage_devices.all():
367                 # Use barcode_id if it exists
368                 if sd.barcode_id is not None and sd.barcode_id != '':
369                     storage_id_list.append(sd.barcode_id)
370                 # Otherwise use UUID
371                 else:
372                     storage_id_list.append(sd.uuid)
373         
374     # Formatting for template use
375     if len(storage_id_list) == 0:
376         storage_ids = None
377     else:
378         storage_ids = ', '.join([ '<a href="/inventory/%s/">%s</a>' % (s,s) for s in storage_id_list ])
379
380     results = []
381     for cycle in cur_fc.keys():
382         result_path = cur_fc[cycle]['eland_results'].get(lane, None)
383         result_link = make_result_link(fc_id, cycle, lane, result_path)
384         results.append({'flowcell_id': fc_id,
385                         'run_date': flowcell.run_date,
386                         'cycle': cycle, 
387                         'lane': lane, 
388                         'summary_url': make_summary_url(flowcell_id, cycle),
389                         'result_url': result_link[0],
390                         'result_label': result_link[1],
391                         'bed_url': result_link[2],
392                         'storage_ids': storage_ids
393         })
394     return results
395
396 def make_summary_url(flowcell_id, cycle_name):
397     url = '/results/%s/%s/summary/' % (flowcell_id, cycle_name)
398     return url
399
400 def make_result_link(flowcell_id, cycle_name, lane, eland_result_path):
401     if eland_result_path is None:
402         return ("", "", "")
403
404     result_type = get_eland_result_type(eland_result_path)
405     result_url = '/results/%s/%s/eland_result/%s' % (flowcell_id, cycle_name, lane)
406     result_label = 'eland %s' % (result_type,)
407     bed_url = None
408     if result_type == 'result':
409        bed_url_pattern = '/results/%s/%s/bedfile/%s'
410        bed_url = bed_url_pattern % (flowcell_id, cycle_name, lane)
411     
412     return (result_url, result_label, bed_url)
413
414 def _files(flowcell_id, lane):
415     """
416     Sets up available files for download
417     """
418     lane = int(lane)
419
420     flowcell_id, id = parse_flowcell_id(flowcell_id)
421     d = get_flowcell_result_dict(flowcell_id)
422     
423     if d is None:
424         return ''
425     
426     output = []
427     
428     # c_name == 'CN-M' (i.e. C1-33)
429     for c_name in d:
430         
431         if d[c_name]['summary'] is not None:
432             output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' \
433                           % (flowcell_id, c_name, c_name))
434         
435         erd = d[c_name]['eland_results']
436         if lane in erd:
437             result_type = get_eland_result_type(erd[lane])
438             result_url_pattern = '<a href="/results/%s/%s/eland_result/%s">eland %s(%s)</a>'
439             output.append(result_url_pattern % (flowcell_id, c_name, lane, result_type, c_name))
440             if result_type == 'result':
441                 bed_url_pattern = '<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>'
442                 output.append(bed_url_pattern % (flowcell_id, c_name, lane, c_name))
443     
444     if len(output) == 0:
445         return ''
446     
447     return '(' + '|'.join(output) + ')'
448
449 def library_id_to_admin_url(request, lib_id):
450     lib = Library.objects.get(id=lib_id)
451     return HttpResponseRedirect('/admin/samples/library/%s' % (lib.id,))
452
453 def library_dict(library_id):
454     """
455     Given a library id construct a dictionary containing important information
456     return None if nothing was found
457     """
458     try:
459         lib = Library.objects.get(id = library_id)
460     except Library.DoesNotExist, e:
461         return None
462
463     #lane_info = lane_information(lib.lane_set)
464     lane_info = []
465     for lane in lib.lane_set.all():
466         lane_info.append( {'flowcell':lane.flowcell.flowcell_id,
467                            'lane_number': lane.lane_number} )
468         
469     info = {
470         # 'affiliations'?
471         # 'aligned_reads': lib.aligned_reads,
472         #'amplified_into_sample': lib.amplified_into_sample, # into is a colleciton...
473         #'amplified_from_sample_id': lib.amplified_from_sample, 
474         #'antibody_name': lib.antibody_name(), # we have no antibodies.
475         'antibody_id': lib.antibody_id,
476         'avg_lib_size': lib.avg_lib_size,
477         'cell_line_id': lib.cell_line_id,
478         'cell_line': unicode_or_none(lib.cell_line),
479         'experiment_type': lib.experiment_type.name,
480         'experiment_type_id': lib.experiment_type_id,
481         'id': lib.id,
482         'lane_set': lane_info,
483         'library_id': lib.id,
484         'library_name': lib.library_name,
485         'library_species': lib.library_species.scientific_name,
486         'library_species_id': lib.library_species_id,
487         #'library_type': lib.library_type.name,
488         'library_type_id': lib.library_type_id,
489         'made_for': lib.made_for,
490         'made_by': lib.made_by,
491         'notes': lib.notes,
492         'replicate': lib.replicate,
493         'stopping_point': lib.stopping_point,
494         'successful_pM': unicode_or_none(lib.successful_pM),
495         'undiluted_concentration': unicode_or_none(lib.undiluted_concentration)
496         }
497     if lib.library_type_id is None:
498         info['library_type'] = None
499     else:
500         info['library_type'] = lib.library_type.name
501     return info
502
503 def library_json(request, library_id):
504     """
505     Return a json formatted library dictionary
506     """
507     require_api_key(request)
508     # what validation should we do on library_id?
509     
510     lib = library_dict(library_id)
511     if lib is None:
512         raise Http404
513
514     lib_json = json.dumps(lib)
515     return HttpResponse(lib_json, mimetype='application/json')
516
517 def species_json(request, species_id):
518     """
519     Return information about a species.
520     """
521     raise Http404
522     
523 @login_required
524 def user_profile(request):
525     """
526     Information about the user
527     """
528     context = {
529                 'page_name': 'User Profile',
530                 'media': '',
531                 #'bcmagic': BarcodeMagicForm(),
532                 #'select': 'settings',
533             }
534     context.update(SAMPLES_CONTEXT_DEFAULTS)
535     return render_to_response('registration/profile.html', context,
536                               context_instance=RequestContext(request))
537