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