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