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