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