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