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