1 from __future__ import absolute_import, print_function
3 # Create your views here.
11 except ImportError as e:
12 import simplejson as json
14 from django.views.decorators.csrf import csrf_exempt
15 from django.core.exceptions import ObjectDoesNotExist
16 from django.http import HttpResponse, HttpResponseRedirect, Http404
17 from django.shortcuts import render_to_response, get_object_or_404
18 from django.template import RequestContext
19 from django.template.loader import get_template
20 from django.contrib.auth.decorators import login_required
21 from django.conf import settings
23 from htsworkflow.auth import require_api_key
24 from experiments.models import FlowCell, Lane, LANE_STATUS_MAP
25 from experiments.admin import LaneOptions
26 from .changelist import HTSChangeList
27 from .models import Antibody, Library, Species, HTSUser
28 from .admin import LibraryOptions
29 from .results import get_flowcell_result_dict
30 from bcmagic.forms import BarcodeMagicForm
31 from htsworkflow.pipelines import runfolder
32 from htsworkflow.pipelines.eland import ResultLane
33 from htsworkflow.pipelines.samplekey import SampleKey
34 from htsworkflow.util.conversion import unicode_or_none, parse_flowcell_id
35 from htsworkflow.util import makebed
36 from htsworkflow.util import opener
39 LANE_LIST = [1, 2, 3, 4, 5, 6, 7, 8]
40 SAMPLES_CONTEXT_DEFAULTS = {
41 'app_name': 'Flowcell/Library Tracker',
42 'bcmagic': BarcodeMagicForm()
45 LOGGER = logging.getLogger(__name__)
48 def count_lanes(lane_set):
54 counts = [[0, 0, 0], [0, 0, 0]]
56 for lane in lane_set.all():
57 if lane.flowcell.paired_end:
61 if lane.flowcell.read_length < 40:
62 read_type = short_read
63 elif lane.flowcell.read_length < 100:
64 read_type = medium_read
67 counts[lane_type][read_type] += 1
72 def create_library_context(cl):
74 Create a list of libraries that includes how many lanes were run
77 #for lib in library_items.object_list:
78 for lib in cl.result_list:
80 summary['library'] = lib
81 summary['library_id'] = lib.id
82 summary['library_name'] = lib.library_name
83 summary['species_name'] = lib.library_species.scientific_name
84 if lib.amplified_from_sample is not None:
85 summary['amplified_from'] = lib.amplified_from_sample.id
87 summary['amplified_from'] = ''
88 lanes_run = count_lanes(lib.lane_set)
90 for row in xrange(len(lanes_run)):
91 for col in xrange(len(lanes_run[row])):
92 if lanes_run[row][col] == 0:
93 lanes_run[row][col] = ''
94 summary['lanes_run'] = lanes_run
95 summary['is_archived'] = lib.is_archived()
96 records.append(summary)
97 cl.result_count = unicode(cl.paginator._count)
98 return {'library_list': records}
101 def library(request, todo_only=False):
102 queryset = Library.objects.filter(hidden__exact=0)
103 filters = {'hidden__exact': 0}
105 filters['lane'] = None
107 fcl = HTSChangeList(request, Library,
108 list_filter=['affiliations', 'library_species'],
109 search_fields=['id', 'library_name', 'amplified_from_sample__id'],
111 model_admin=LibraryOptions(Library, None),
112 extra_filters=filters
115 context = {'cl': fcl, 'title': 'Library Index', 'todo_only': todo_only}
116 context.update(create_library_context(fcl))
117 t = get_template('samples/library_index.html')
118 c = RequestContext(request, context)
119 return HttpResponse(t.render(c))
122 def library_not_run(request):
123 return library(request, todo_only=True)
126 def library_to_flowcells(request, lib_id):
128 Display information about all the flowcells a library has been run on.
131 lib = Library.objects.get(id=lib_id)
133 raise Http404('Library %s does not exist' % (lib_id,))
136 flowcell_run_results = {} # aka flowcells we're looking at
137 for lane in lib.lane_set.all():
139 flowcell_id, id = parse_flowcell_id(fc.flowcell_id)
140 if flowcell_id not in flowcell_run_results:
141 flowcell_run_results[flowcell_id] = get_flowcell_result_dict(flowcell_id)
142 flowcell_list.append((fc.flowcell_id, lane.lane_number))
145 lane_summary_list = []
147 for fc, lane_number in flowcell_list:
148 lane_summary, err_list = _summary_stats(fc, lane_number, lib_id)
149 lane_summary_list.extend(lane_summary)
151 eland_results.extend(_make_eland_results(fc, lane_number, flowcell_run_results))
154 'page_name': 'Library Details',
156 'eland_results': eland_results,
157 'lane_summary_list': lane_summary_list,
159 context.update(SAMPLES_CONTEXT_DEFAULTS)
161 return render_to_response(
162 'samples/library_detail.html',
164 context_instance=RequestContext(request))
167 def lanes_for(request, username=None):
169 Generate a report of recent activity for a user
172 if username is not None:
173 user = HTSUser.objects.get(username=username)
174 query.update({'library__affiliations__users__id': user.id})
175 fcl = HTSChangeList(request, Lane,
177 search_fields=['flowcell__flowcell_id', 'library__id', 'library__library_name'],
179 model_admin=LaneOptions,
180 queryset=Lane.objects.filter(**query)
183 context = {'lanes': fcl, 'title': 'Lane Index'}
185 return render_to_response(
186 'samples/lanes_for.html',
188 context_instance=RequestContext(request)
192 def summaryhtm_fc_cnm(request, flowcell_id, cnm):
194 returns a Summary.htm file if it exists.
196 fc_id, status = parse_flowcell_id(flowcell_id)
197 d = get_flowcell_result_dict(fc_id)
200 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
203 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
205 summary_filepath = d[cnm]['summary']
207 if summary_filepath is None:
208 return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
210 f = open(summary_filepath, 'r')
212 return HttpResponse(f)
215 def result_fc_cnm_eland_lane(request, flowcell_id, cnm, lane):
217 returns an eland_file upon calling.
219 fc_id, status = parse_flowcell_id(flowcell_id)
220 d = get_flowcell_result_dict(fc_id)
223 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
226 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
228 erd = d[cnm]['eland_results']
232 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
236 #f = opener.autoopen(filepath, 'r')
237 # return HttpResponse(f, content_type="application/x-elandresult")
239 f = open(filepath, 'r')
240 return HttpResponse(f, content_type='application/x-bzip2')
243 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
245 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
247 return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
250 def bedfile_fc_cnm_eland_lane(request, flowcell_id, cnm, lane, ucsc_compatible=False):
252 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
254 fc_id, status = parse_flowcell_id(flowcell_id)
255 d = get_flowcell_result_dict(fc_id)
258 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
261 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
263 erd = d[cnm]['eland_results']
267 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
272 fi = opener.autoopen(filepath, 'r')
275 name, description = makebed.make_description(fc_id, lane)
277 bedgen = makebed.make_bed_from_eland_generator(fi, name, description)
280 return HttpResponse(bedgen)
282 return HttpResponse(bedgen, content_type="application/x-bedfile")
285 def _summary_stats(flowcell_id, lane_id, library_id):
287 Return the summary statistics for a given flowcell, lane, and end.
289 fc_id, status = parse_flowcell_id(flowcell_id)
290 fc_result_dict = get_flowcell_result_dict(fc_id)
295 if fc_result_dict is None:
296 err_list.append('Results for Flowcell %s not found.' % (fc_id))
297 return (summary_list, err_list)
299 for cycle_width in fc_result_dict:
300 xmlpath = fc_result_dict[cycle_width]['run_xml']
303 err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cycle_width))
306 run = runfolder.load_pipeline_run_xml(xmlpath)
307 # skip if we don't have available metadata.
308 if run.gerald is None or run.gerald.summary is None:
311 gerald_summary = run.gerald.summary.lane_results
312 key = SampleKey(lane=lane_id, sample='s')
313 eland_results = list(run.gerald.eland_results.find_keys(key))
314 key = SampleKey(lane=lane_id, sample=library_id)
315 eland_results.extend(run.gerald.eland_results.find_keys(key))
316 for key in eland_results:
317 eland_summary = run.gerald.eland_results.results[key]
318 # add information to lane_summary
319 eland_summary.flowcell_id = flowcell_id
321 read = key.read-1 if key.read is not None else 0
323 eland_summary.clusters = gerald_summary[read][key.lane].cluster
324 except (IndexError, KeyError) as e:
325 eland_summary.clustes = None
326 eland_summary.cycle_width = cycle_width
327 if hasattr(eland_summary, 'genome_map'):
328 eland_summary.summarized_reads = runfolder.summarize_mapped_reads(
329 eland_summary.genome_map,
330 eland_summary.mapped_reads)
332 # grab some more information out of the flowcell db
333 flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
334 #pm_field = 'lane_%d_pM' % (lane_id)
335 lanes = flowcell.lane_set.filter(lane_number=lane_id)
336 eland_summary.flowcell = flowcell
337 eland_summary.lanes = lanes
339 summary_list.append(eland_summary)
341 #except Exception as e:
342 # summary_list.append("Summary report needs to be updated.")
343 # LOGGER.error("Exception: " + str(e))
345 return (summary_list, err_list)
348 def get_eland_result_type(pathname):
350 Guess the eland result file type from the filename
352 path, filename = os.path.split(pathname)
353 if 'extended' in filename:
355 elif 'multi' in filename:
357 elif 'result' in filename:
363 def _make_eland_results(flowcell_id, lane_number, interesting_flowcells):
364 fc_id, status = parse_flowcell_id(flowcell_id)
365 cur_fc = interesting_flowcells.get(fc_id, None)
369 flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
370 lanes = flowcell.lane_set.filter(lane_number=lane_number)
371 # Loop throw storage devices if a result has been archived
373 if cur_fc is not None:
374 for lts in flowcell.longtermstorage_set.all():
375 for sd in lts.storage_devices.all():
376 # Use barcode_id if it exists
377 if sd.barcode_id is not None and sd.barcode_id != '':
378 storage_id_list.append(sd.barcode_id)
381 storage_id_list.append(sd.uuid)
383 # Formatting for template use
384 if len(storage_id_list) == 0:
387 storage_ids = ', '.join(['<a href="/inventory/%s/">%s</a>' % (s, s) for s in storage_id_list])
390 for cycle in cur_fc.keys():
391 result_path = cur_fc[cycle]['eland_results'].get(lanes[0], None)
392 result_link = make_result_link(fc_id, cycle, lanes[0], result_path)
393 results.append({'flowcell_id': fc_id,
394 'flowcell': flowcell,
395 'run_date': flowcell.run_date,
398 'summary_url': make_summary_url(flowcell_id, cycle),
399 'result_url': result_link[0],
400 'result_label': result_link[1],
401 'bed_url': result_link[2],
402 'storage_ids': storage_ids})
406 def make_summary_url(flowcell_id, cycle_name):
407 url = '/results/%s/%s/summary/' % (flowcell_id, cycle_name)
411 def make_result_link(flowcell_id, cycle_name, lane, eland_result_path):
412 if eland_result_path is None:
415 result_type = get_eland_result_type(eland_result_path)
416 result_url = '/results/%s/%s/eland_result/%s' % (flowcell_id, cycle_name, lane)
417 result_label = 'eland %s' % (result_type,)
419 if result_type == 'result':
420 bed_url_pattern = '/results/%s/%s/bedfile/%s'
421 bed_url = bed_url_pattern % (flowcell_id, cycle_name, lane)
423 return (result_url, result_label, bed_url)
426 def _files(flowcell_id, lane):
428 Sets up available files for download
432 flowcell_id, id = parse_flowcell_id(flowcell_id)
433 d = get_flowcell_result_dict(flowcell_id)
440 # c_name == 'CN-M' (i.e. C1-33)
443 if d[c_name]['summary'] is not None:
444 output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' %
445 (flowcell_id, c_name, c_name))
447 erd = d[c_name]['eland_results']
449 result_type = get_eland_result_type(erd[lane])
450 result_url_pattern = '<a href="/results/%s/%s/eland_result/%s">eland %s(%s)</a>'
451 output.append(result_url_pattern % (flowcell_id, c_name, lane, result_type, c_name))
452 if result_type == 'result':
453 bed_url_pattern = '<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>'
454 output.append(bed_url_pattern % (flowcell_id, c_name, lane, c_name))
459 return '(' + '|'.join(output) + ')'
462 def library_id_to_admin_url(request, lib_id):
463 lib = Library.objects.get(id=lib_id)
464 return HttpResponseRedirect('/admin/samples/library/%s' % (lib.id,))
467 def library_dict(library_id):
469 Given a library id construct a dictionary containing important information
470 return None if nothing was found
473 lib = Library.objects.get(id=library_id)
474 except Library.DoesNotExist as e:
477 #lane_info = lane_information(lib.lane_set)
479 for lane in lib.lane_set.all():
480 lane_info.append({'flowcell': lane.flowcell.flowcell_id,
481 'lane_number': lane.lane_number,
483 'paired_end': lane.flowcell.paired_end,
484 'read_length': lane.flowcell.read_length,
485 'status_code': lane.status,
486 'status': LANE_STATUS_MAP[lane.status]})
490 # 'aligned_reads': lib.aligned_reads,
491 #'amplified_into_sample': lib.amplified_into_sample, # into is a colleciton...
492 #'amplified_from_sample_id': lib.amplified_from_sample,
493 #'antibody_name': lib.antibody_name(), # we have no antibodies.
494 'antibody_id': lib.antibody_id,
495 'cell_line_id': lib.cell_line_id,
496 'cell_line': unicode_or_none(lib.cell_line),
497 'experiment_type': lib.experiment_type.name,
498 'experiment_type_id': lib.experiment_type_id,
499 'gel_cut_size': lib.gel_cut_size,
500 'hidden': lib.hidden,
502 'insert_size': lib.insert_size,
503 'lane_set': lane_info,
504 'library_id': lib.id,
505 'library_name': lib.library_name,
506 'library_species': lib.library_species.scientific_name,
507 'library_species_id': lib.library_species_id,
508 #'library_type': lib.library_type.name,
509 'library_type_id': lib.library_type_id,
510 'made_for': lib.made_for,
511 'made_by': lib.made_by,
513 'replicate': lib.replicate,
514 'stopping_point': lib.stopping_point,
515 'successful_pM': unicode_or_none(lib.successful_pM),
516 'undiluted_concentration': unicode_or_none(lib.undiluted_concentration)
518 if lib.library_type_id is None:
519 info['library_type'] = None
521 info['library_type'] = lib.library_type.name
526 def library_json(request, library_id):
528 Return a json formatted library dictionary
530 require_api_key(request)
531 # what validation should we do on library_id?
533 lib = library_dict(library_id)
537 lib_json = json.dumps({'result': lib})
538 return HttpResponse(lib_json, content_type='application/json')
542 def species_json(request, species_id):
544 Return information about a species.
549 def species(request, species_id):
550 species = get_object_or_404(Species, id=species_id)
552 context = RequestContext(request,
553 {'species': species})
555 return render_to_response("samples/species_detail.html", context)
558 def antibodies(request):
559 context = RequestContext(request,
560 {'antibodies': Antibody.objects.order_by('antigene')})
561 return render_to_response("samples/antibody_index.html", context)
565 def user_profile(request):
567 Information about the user
570 'page_name': 'User Profile',
572 #'bcmagic': BarcodeMagicForm(),
573 #'select': 'settings',
575 context.update(SAMPLES_CONTEXT_DEFAULTS)
576 return render_to_response('registration/profile.html', context,
577 context_instance=RequestContext(request))