1 from __future__ import absolute_import, print_function, unicode_literals
3 # Create your views here.
10 except ImportError as e:
11 import simplejson as json
13 from django.views.decorators.csrf import csrf_exempt
14 from django.core.exceptions import ObjectDoesNotExist
15 from django.http import HttpResponse, HttpResponseRedirect, Http404
16 from django.shortcuts import render_to_response, get_object_or_404
17 from django.template import RequestContext
18 from django.template.loader import get_template
19 from django.contrib.auth.decorators import login_required
20 from django.conf import settings
22 from htsworkflow.auth import require_api_key
23 from experiments.models import FlowCell, Lane, LANE_STATUS_MAP
24 from experiments.admin import LaneOptions
25 from .changelist import HTSChangeList
26 from .models import Antibody, Library, Species, HTSUser
27 from .admin import LibraryOptions
28 from .results import get_flowcell_result_dict
29 from bcmagic.forms import BarcodeMagicForm
30 from htsworkflow.pipelines import runfolder
31 from htsworkflow.pipelines.eland import ResultLane
32 from htsworkflow.pipelines.samplekey import SampleKey
33 from htsworkflow.util.conversion import str_or_none, parse_flowcell_id
34 from htsworkflow.util import makebed
35 from htsworkflow.util import opener
38 LANE_LIST = [1, 2, 3, 4, 5, 6, 7, 8]
39 SAMPLES_CONTEXT_DEFAULTS = {
40 'app_name': 'Flowcell/Library Tracker',
41 'bcmagic': BarcodeMagicForm()
44 LOGGER = logging.getLogger(__name__)
47 def count_lanes(lane_set):
53 counts = [[0, 0, 0], [0, 0, 0]]
55 for lane in lane_set.all():
56 if lane.flowcell.paired_end:
60 if lane.flowcell.read_length < 40:
61 read_type = short_read
62 elif lane.flowcell.read_length < 100:
63 read_type = medium_read
66 counts[lane_type][read_type] += 1
71 def create_library_context(cl):
73 Create a list of libraries that includes how many lanes were run
76 #for lib in library_items.object_list:
77 for lib in cl.result_list:
79 summary['library'] = lib
80 summary['library_id'] = lib.id
81 summary['library_name'] = lib.library_name
82 summary['species_name'] = lib.library_species.scientific_name
83 if lib.amplified_from_sample is not None:
84 summary['amplified_from'] = lib.amplified_from_sample.id
86 summary['amplified_from'] = ''
87 lanes_run = count_lanes(lib.lane_set)
89 for row in xrange(len(lanes_run)):
90 for col in xrange(len(lanes_run[row])):
91 if lanes_run[row][col] == 0:
92 lanes_run[row][col] = ''
93 summary['lanes_run'] = lanes_run
94 summary['is_archived'] = lib.is_archived()
95 records.append(summary)
96 cl.result_count = str(cl.paginator._count)
97 return {'library_list': records}
100 def library(request, todo_only=False):
101 queryset = Library.objects.filter(hidden__exact=0)
102 filters = {'hidden__exact': 0}
104 filters['lane'] = None
106 fcl = HTSChangeList(request, Library,
107 list_filter=['affiliations', 'library_species'],
108 search_fields=['id', 'library_name', 'amplified_from_sample__id'],
110 model_admin=LibraryOptions(Library, None),
111 extra_filters=filters
114 context = {'cl': fcl, 'title': 'Library Index', 'todo_only': todo_only}
115 context.update(create_library_context(fcl))
116 t = get_template('samples/library_index.html')
117 c = RequestContext(request, context)
118 return HttpResponse(t.render(c))
121 def library_not_run(request):
122 return library(request, todo_only=True)
125 def library_to_flowcells(request, lib_id):
127 Display information about all the flowcells a library has been run on.
130 lib = Library.objects.get(id=lib_id)
132 raise Http404('Library %s does not exist' % (lib_id,))
135 flowcell_run_results = {} # aka flowcells we're looking at
136 for lane in lib.lane_set.all():
138 flowcell_id, id = parse_flowcell_id(fc.flowcell_id)
139 if flowcell_id not in flowcell_run_results:
140 flowcell_run_results[flowcell_id] = get_flowcell_result_dict(flowcell_id)
141 flowcell_list.append((fc.flowcell_id, lane.lane_number))
144 lane_summary_list = []
146 for fc, lane_number in flowcell_list:
147 lane_summary, err_list = _summary_stats(fc, lane_number, lib_id)
148 lane_summary_list.extend(lane_summary)
150 eland_results.extend(_make_eland_results(fc, lane_number, flowcell_run_results))
153 'page_name': 'Library Details',
155 'eland_results': eland_results,
156 'lane_summary_list': lane_summary_list,
158 context.update(SAMPLES_CONTEXT_DEFAULTS)
160 return render_to_response(
161 'samples/library_detail.html',
163 context_instance=RequestContext(request))
166 def lanes_for(request, username=None):
168 Generate a report of recent activity for a user
171 if username is not None:
172 user = HTSUser.objects.get(username=username)
173 query.update({'library__affiliations__users__id': user.id})
174 fcl = HTSChangeList(request, Lane,
176 search_fields=['flowcell__flowcell_id', 'library__id', 'library__library_name'],
178 model_admin=LaneOptions,
179 queryset=Lane.objects.filter(**query)
182 context = {'lanes': fcl, 'title': 'Lane Index'}
184 return render_to_response(
185 'samples/lanes_for.html',
187 context_instance=RequestContext(request)
191 def summaryhtm_fc_cnm(request, flowcell_id, cnm):
193 returns a Summary.htm file if it exists.
195 fc_id, status = parse_flowcell_id(flowcell_id)
196 d = get_flowcell_result_dict(fc_id)
199 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
202 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
204 summary_filepath = d[cnm]['summary']
206 if summary_filepath is None:
207 return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
209 f = open(summary_filepath, 'r')
211 return HttpResponse(f)
214 def result_fc_cnm_eland_lane(request, flowcell_id, cnm, lane):
216 returns an eland_file upon calling.
218 fc_id, status = parse_flowcell_id(flowcell_id)
219 d = get_flowcell_result_dict(fc_id)
222 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
225 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
227 erd = d[cnm]['eland_results']
231 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
235 #f = opener.autoopen(filepath, 'r')
236 # return HttpResponse(f, content_type="application/x-elandresult")
238 f = open(filepath, 'r')
239 return HttpResponse(f, content_type='application/x-bzip2')
242 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
244 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
246 return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
249 def bedfile_fc_cnm_eland_lane(request, flowcell_id, cnm, lane, ucsc_compatible=False):
251 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
253 fc_id, status = parse_flowcell_id(flowcell_id)
254 d = get_flowcell_result_dict(fc_id)
257 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
260 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
262 erd = d[cnm]['eland_results']
266 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
271 fi = opener.autoopen(filepath, 'r')
274 name, description = makebed.make_description(fc_id, lane)
276 bedgen = makebed.make_bed_from_eland_generator(fi, name, description)
279 return HttpResponse(bedgen)
281 return HttpResponse(bedgen, content_type="application/x-bedfile")
284 def _summary_stats(flowcell_id, lane_id, library_id):
286 Return the summary statistics for a given flowcell, lane, and end.
288 fc_id, status = parse_flowcell_id(flowcell_id)
289 fc_result_dict = get_flowcell_result_dict(fc_id)
294 if fc_result_dict is None:
295 err_list.append('Results for Flowcell %s not found.' % (fc_id))
296 return (summary_list, err_list)
298 for cycle_width in fc_result_dict:
299 xmlpath = fc_result_dict[cycle_width]['run_xml']
302 err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cycle_width))
305 run = runfolder.load_pipeline_run_xml(xmlpath)
306 # skip if we don't have available metadata.
307 if run.gerald is None or run.gerald.summary is None:
310 gerald_summary = run.gerald.summary.lane_results
311 key = SampleKey(lane=lane_id, sample='s')
312 eland_results = list(run.gerald.eland_results.find_keys(key))
313 key = SampleKey(lane=lane_id, sample=library_id)
314 eland_results.extend(run.gerald.eland_results.find_keys(key))
315 for key in eland_results:
316 eland_summary = run.gerald.eland_results.results[key]
317 # add information to lane_summary
318 eland_summary.flowcell_id = flowcell_id
320 read = key.read-1 if key.read is not None else 0
322 eland_summary.clusters = gerald_summary[read][key.lane].cluster
323 except (IndexError, KeyError) as e:
324 eland_summary.clustes = None
325 eland_summary.cycle_width = cycle_width
326 if hasattr(eland_summary, 'genome_map'):
327 eland_summary.summarized_reads = runfolder.summarize_mapped_reads(
328 eland_summary.genome_map,
329 eland_summary.mapped_reads)
331 # grab some more information out of the flowcell db
332 flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
333 #pm_field = 'lane_%d_pM' % (lane_id)
334 lanes = flowcell.lane_set.filter(lane_number=lane_id)
335 eland_summary.flowcell = flowcell
336 eland_summary.lanes = lanes
338 summary_list.append(eland_summary)
340 #except Exception as e:
341 # summary_list.append("Summary report needs to be updated.")
342 # LOGGER.error("Exception: " + str(e))
344 return (summary_list, err_list)
347 def get_eland_result_type(pathname):
349 Guess the eland result file type from the filename
351 path, filename = os.path.split(pathname)
352 if 'extended' in filename:
354 elif 'multi' in filename:
356 elif 'result' in filename:
362 def _make_eland_results(flowcell_id, lane_number, interesting_flowcells):
363 fc_id, status = parse_flowcell_id(flowcell_id)
364 cur_fc = interesting_flowcells.get(fc_id, None)
368 flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
369 lanes = flowcell.lane_set.filter(lane_number=lane_number)
370 # Loop throw storage devices if a result has been archived
372 if cur_fc is not None:
373 for lts in flowcell.longtermstorage_set.all():
374 for sd in lts.storage_devices.all():
375 # Use barcode_id if it exists
376 if sd.barcode_id is not None and sd.barcode_id != '':
377 storage_id_list.append(sd.barcode_id)
380 storage_id_list.append(sd.uuid)
382 # Formatting for template use
383 if len(storage_id_list) == 0:
386 storage_ids = ', '.join(['<a href="/inventory/%s/">%s</a>' % (s, s) for s in storage_id_list])
389 for cycle in cur_fc.keys():
390 result_path = cur_fc[cycle]['eland_results'].get(lanes[0], None)
391 result_link = make_result_link(fc_id, cycle, lanes[0], result_path)
392 results.append({'flowcell_id': fc_id,
393 'flowcell': flowcell,
394 'run_date': flowcell.run_date,
397 'summary_url': make_summary_url(flowcell_id, cycle),
398 'result_url': result_link[0],
399 'result_label': result_link[1],
400 'bed_url': result_link[2],
401 'storage_ids': storage_ids})
405 def make_summary_url(flowcell_id, cycle_name):
406 url = '/results/%s/%s/summary/' % (flowcell_id, cycle_name)
410 def make_result_link(flowcell_id, cycle_name, lane, eland_result_path):
411 if eland_result_path is None:
414 result_type = get_eland_result_type(eland_result_path)
415 result_url = '/results/%s/%s/eland_result/%s' % (flowcell_id, cycle_name, lane)
416 result_label = 'eland %s' % (result_type,)
418 if result_type == 'result':
419 bed_url_pattern = '/results/%s/%s/bedfile/%s'
420 bed_url = bed_url_pattern % (flowcell_id, cycle_name, lane)
422 return (result_url, result_label, bed_url)
425 def _files(flowcell_id, lane):
427 Sets up available files for download
431 flowcell_id, id = parse_flowcell_id(flowcell_id)
432 d = get_flowcell_result_dict(flowcell_id)
439 # c_name == 'CN-M' (i.e. C1-33)
442 if d[c_name]['summary'] is not None:
443 output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' %
444 (flowcell_id, c_name, c_name))
446 erd = d[c_name]['eland_results']
448 result_type = get_eland_result_type(erd[lane])
449 result_url_pattern = '<a href="/results/%s/%s/eland_result/%s">eland %s(%s)</a>'
450 output.append(result_url_pattern % (flowcell_id, c_name, lane, result_type, c_name))
451 if result_type == 'result':
452 bed_url_pattern = '<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>'
453 output.append(bed_url_pattern % (flowcell_id, c_name, lane, c_name))
458 return '(' + '|'.join(output) + ')'
461 def library_id_to_admin_url(request, lib_id):
462 lib = Library.objects.get(id=lib_id)
463 return HttpResponseRedirect('/admin/samples/library/%s' % (lib.id,))
466 def library_dict(library_id):
468 Given a library id construct a dictionary containing important information
469 return None if nothing was found
472 lib = Library.objects.get(id=library_id)
473 except Library.DoesNotExist as e:
476 #lane_info = lane_information(lib.lane_set)
478 for lane in lib.lane_set.all():
479 lane_info.append({'flowcell': lane.flowcell.flowcell_id,
480 'lane_number': lane.lane_number,
482 'paired_end': lane.flowcell.paired_end,
483 'read_length': lane.flowcell.read_length,
484 'status_code': lane.status,
485 'status': LANE_STATUS_MAP[lane.status]})
489 # 'aligned_reads': lib.aligned_reads,
490 #'amplified_into_sample': lib.amplified_into_sample, # into is a colleciton...
491 #'amplified_from_sample_id': lib.amplified_from_sample,
492 #'antibody_name': lib.antibody_name(), # we have no antibodies.
493 'antibody_id': lib.antibody_id,
494 'cell_line_id': lib.cell_line_id,
495 'cell_line': str_or_none(lib.cell_line),
496 'experiment_type': lib.experiment_type.name,
497 'experiment_type_id': lib.experiment_type_id,
498 'gel_cut_size': lib.gel_cut_size,
499 'hidden': lib.hidden,
501 'insert_size': lib.insert_size,
502 'lane_set': lane_info,
503 'library_id': lib.id,
504 'library_name': lib.library_name,
505 'library_species': lib.library_species.scientific_name,
506 'library_species_id': lib.library_species_id,
507 #'library_type': lib.library_type.name,
508 'library_type_id': lib.library_type_id,
509 'made_for': lib.made_for,
510 'made_by': lib.made_by,
512 'replicate': lib.replicate,
513 'stopping_point': lib.stopping_point,
514 'successful_pM': str_or_none(lib.successful_pM),
515 'undiluted_concentration': str_or_none(lib.undiluted_concentration)
517 if lib.library_type_id is None:
518 info['library_type'] = None
520 info['library_type'] = lib.library_type.name
525 def library_json(request, library_id):
527 Return a json formatted library dictionary
529 require_api_key(request)
530 # what validation should we do on library_id?
532 lib = library_dict(library_id)
536 lib_json = json.dumps({'result': lib})
537 return HttpResponse(lib_json, content_type='application/json')
541 def species_json(request, species_id):
543 Return information about a species.
548 def species(request, species_id):
549 species = get_object_or_404(Species, id=species_id)
551 context = RequestContext(request,
552 {'species': species})
554 return render_to_response("samples/species_detail.html", context)
557 def antibodies(request):
558 context = RequestContext(request,
559 {'antibodies': Antibody.objects.order_by('antigene')})
560 return render_to_response("samples/antibody_index.html", context)
564 def user_profile(request):
566 Information about the user
569 'page_name': 'User Profile',
571 #'bcmagic': BarcodeMagicForm(),
572 #'select': 'settings',
574 context.update(SAMPLES_CONTEXT_DEFAULTS)
575 return render_to_response('registration/profile.html', context,
576 context_instance=RequestContext(request))