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