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