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