1 from __future__ import absolute_import, print_function, unicode_literals
3 # Create your views here.
10 from django.views.decorators.csrf import csrf_exempt
11 from django.core.exceptions import ObjectDoesNotExist
12 from django.http import HttpResponse, HttpResponseRedirect, Http404
13 from django.shortcuts import render_to_response, get_object_or_404
14 from django.template import RequestContext
15 from django.template.loader import get_template
16 from django.contrib.auth.decorators import login_required
17 from django.conf import settings
19 from htsworkflow.auth import require_api_key
20 from experiments.models import FlowCell, Lane, LANE_STATUS_MAP
21 from experiments.admin import LaneOptions
22 from .changelist import HTSChangeList
23 from .models import Antibody, Library, Species, HTSUser
24 from .admin import LibraryOptions
25 from .results import get_flowcell_result_dict
26 from bcmagic.forms import BarcodeMagicForm
27 from htsworkflow.pipelines import runfolder
28 from htsworkflow.pipelines.eland import ResultLane
29 from htsworkflow.pipelines.samplekey import SampleKey
30 from htsworkflow.util.conversion import str_or_none, parse_flowcell_id
31 from htsworkflow.util import makebed
32 from htsworkflow.util import opener
35 LANE_LIST = [1, 2, 3, 4, 5, 6, 7, 8]
36 SAMPLES_CONTEXT_DEFAULTS = {
37 'app_name': 'Flowcell/Library Tracker',
38 'bcmagic': BarcodeMagicForm()
41 LOGGER = logging.getLogger(__name__)
44 def count_lanes(lane_set):
50 counts = [[0, 0, 0], [0, 0, 0]]
52 for lane in lane_set.all():
53 if lane.flowcell.paired_end:
57 if lane.flowcell.read_length < 40:
58 read_type = short_read
59 elif lane.flowcell.read_length < 100:
60 read_type = medium_read
63 counts[lane_type][read_type] += 1
68 def create_library_context(cl):
70 Create a list of libraries that includes how many lanes were run
73 #for lib in library_items.object_list:
74 for lib in cl.result_list:
76 summary['library'] = lib
77 summary['library_id'] = lib.id
78 summary['library_name'] = lib.library_name
79 summary['species_name'] = lib.library_species.scientific_name
80 if lib.amplified_from_sample is not None:
81 summary['amplified_from'] = lib.amplified_from_sample.id
83 summary['amplified_from'] = ''
84 lanes_run = count_lanes(lib.lane_set)
86 for row_index, row in enumerate(lanes_run):
87 for col_index, cell in enumerate(row):
88 if lanes_run[row_index][col_index] == 0:
89 lanes_run[row_index][col_index] = ''
90 summary['lanes_run'] = lanes_run
91 summary['is_archived'] = lib.is_archived()
92 records.append(summary)
93 cl.result_count = str(cl.paginator._count)
94 return {'library_list': records}
97 def library(request, todo_only=False):
98 queryset = Library.objects.filter(hidden__exact=0)
99 filters = {'hidden__exact': 0}
101 filters['lane'] = None
103 fcl = HTSChangeList(request, Library,
104 list_filter=['affiliations', 'library_species'],
105 search_fields=['id', 'library_name', 'amplified_from_sample__id'],
107 model_admin=LibraryOptions(Library, None),
108 extra_filters=filters
111 context = {'cl': fcl, 'title': 'Library Index', 'todo_only': todo_only}
112 context.update(create_library_context(fcl))
113 t = get_template('samples/library_index.html')
114 c = RequestContext(request, context)
115 return HttpResponse(t.render(c))
118 def library_not_run(request):
119 return library(request, todo_only=True)
122 def library_to_flowcells(request, lib_id):
124 Display information about all the flowcells a library has been run on.
127 lib = Library.objects.get(id=lib_id)
129 raise Http404('Library %s does not exist' % (lib_id,))
132 flowcell_run_results = {} # aka flowcells we're looking at
133 for lane in lib.lane_set.all():
135 flowcell_id, id = parse_flowcell_id(fc.flowcell_id)
136 if flowcell_id not in flowcell_run_results:
137 flowcell_run_results[flowcell_id] = get_flowcell_result_dict(flowcell_id)
138 flowcell_list.append((fc.flowcell_id, lane.lane_number))
141 lane_summary_list = []
143 for fc, lane_number in flowcell_list:
144 lane_summary, err_list = _summary_stats(fc, lane_number, lib_id)
145 lane_summary_list.extend(lane_summary)
147 eland_results.extend(_make_eland_results(fc, lane_number, flowcell_run_results))
150 'page_name': 'Library Details',
152 'eland_results': eland_results,
153 'lane_summary_list': lane_summary_list,
155 context.update(SAMPLES_CONTEXT_DEFAULTS)
157 return render_to_response(
158 'samples/library_detail.html',
160 context_instance=RequestContext(request))
163 def summaryhtm_fc_cnm(request, flowcell_id, cnm):
165 returns a Summary.htm file if it exists.
167 fc_id, status = parse_flowcell_id(flowcell_id)
168 d = get_flowcell_result_dict(fc_id)
171 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
174 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
176 summary_filepath = d[cnm]['summary']
178 if summary_filepath is None:
179 return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
181 f = open(summary_filepath, 'r')
183 return HttpResponse(f)
186 def result_fc_cnm_eland_lane(request, flowcell_id, cnm, lane):
188 returns an eland_file upon calling.
190 fc_id, status = parse_flowcell_id(flowcell_id)
191 d = get_flowcell_result_dict(fc_id)
194 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
197 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
199 erd = d[cnm]['eland_results']
203 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
207 #f = opener.autoopen(filepath, 'r')
208 # return HttpResponse(f, content_type="application/x-elandresult")
210 f = open(filepath, 'r')
211 return HttpResponse(f, content_type='application/x-bzip2')
214 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
216 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
218 return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
221 def bedfile_fc_cnm_eland_lane(request, flowcell_id, cnm, lane, ucsc_compatible=False):
223 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
225 fc_id, status = parse_flowcell_id(flowcell_id)
226 d = get_flowcell_result_dict(fc_id)
229 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
232 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
234 erd = d[cnm]['eland_results']
238 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
243 fi = opener.autoopen(filepath, 'r')
246 name, description = makebed.make_description(fc_id, lane)
248 bedgen = makebed.make_bed_from_eland_generator(fi, name, description)
251 return HttpResponse(bedgen)
253 return HttpResponse(bedgen, content_type="application/x-bedfile")
256 def _summary_stats(flowcell_id, lane_id, library_id):
258 Return the summary statistics for a given flowcell, lane, and end.
260 fc_id, status = parse_flowcell_id(flowcell_id)
261 fc_result_dict = get_flowcell_result_dict(fc_id)
266 if fc_result_dict is None:
267 err_list.append('Results for Flowcell %s not found.' % (fc_id))
268 return (summary_list, err_list)
270 for cycle_width in fc_result_dict:
271 xmlpath = fc_result_dict[cycle_width]['run_xml']
274 err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cycle_width))
277 run = runfolder.load_pipeline_run_xml(xmlpath)
278 # skip if we don't have available metadata.
279 if run.gerald is None or run.gerald.summary is None:
282 gerald_summary = run.gerald.summary.lane_results
283 key = SampleKey(lane=lane_id, sample='s')
284 eland_results = list(run.gerald.eland_results.find_keys(key))
285 key = SampleKey(lane=lane_id, sample=library_id)
286 eland_results.extend(run.gerald.eland_results.find_keys(key))
287 for key in eland_results:
288 eland_summary = run.gerald.eland_results.results[key]
289 # add information to lane_summary
290 eland_summary.flowcell_id = flowcell_id
292 read = key.read-1 if key.read is not None else 0
294 eland_summary.clusters = gerald_summary[read][key.lane].cluster
295 except (IndexError, KeyError) as e:
296 eland_summary.clustes = None
297 eland_summary.cycle_width = cycle_width
298 if hasattr(eland_summary, 'genome_map'):
299 eland_summary.summarized_reads = runfolder.summarize_mapped_reads(
300 eland_summary.genome_map,
301 eland_summary.mapped_reads)
303 # grab some more information out of the flowcell db
304 flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
305 #pm_field = 'lane_%d_pM' % (lane_id)
306 lanes = flowcell.lane_set.filter(lane_number=lane_id)
307 eland_summary.flowcell = flowcell
308 eland_summary.lanes = lanes
310 summary_list.append(eland_summary)
312 #except Exception as e:
313 # summary_list.append("Summary report needs to be updated.")
314 # LOGGER.error("Exception: " + str(e))
316 return (summary_list, err_list)
319 def get_eland_result_type(pathname):
321 Guess the eland result file type from the filename
323 path, filename = os.path.split(pathname)
324 if 'extended' in filename:
326 elif 'multi' in filename:
328 elif 'result' in filename:
334 def _make_eland_results(flowcell_id, lane_number, interesting_flowcells):
335 fc_id, status = parse_flowcell_id(flowcell_id)
336 cur_fc = interesting_flowcells.get(fc_id, None)
340 flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
341 lanes = flowcell.lane_set.filter(lane_number=lane_number)
342 # Loop throw storage devices if a result has been archived
344 if cur_fc is not None:
345 for lts in flowcell.longtermstorage_set.all():
346 for sd in lts.storage_devices.all():
347 # Use barcode_id if it exists
348 if sd.barcode_id is not None and sd.barcode_id != '':
349 storage_id_list.append(sd.barcode_id)
352 storage_id_list.append(sd.uuid)
354 # Formatting for template use
355 if len(storage_id_list) == 0:
358 storage_ids = ', '.join(['<a href="/inventory/%s/">%s</a>' % (s, s) for s in storage_id_list])
361 for cycle in cur_fc.keys():
362 result_path = cur_fc[cycle]['eland_results'].get(lanes[0], None)
363 result_link = make_result_link(fc_id, cycle, lanes[0], result_path)
364 results.append({'flowcell_id': fc_id,
365 'flowcell': flowcell,
366 'run_date': flowcell.run_date,
369 'summary_url': make_summary_url(flowcell_id, cycle),
370 'result_url': result_link[0],
371 'result_label': result_link[1],
372 'bed_url': result_link[2],
373 'storage_ids': storage_ids})
377 def make_summary_url(flowcell_id, cycle_name):
378 url = '/results/%s/%s/summary/' % (flowcell_id, cycle_name)
382 def make_result_link(flowcell_id, cycle_name, lane, eland_result_path):
383 if eland_result_path is None:
386 result_type = get_eland_result_type(eland_result_path)
387 result_url = '/results/%s/%s/eland_result/%s' % (flowcell_id, cycle_name, lane)
388 result_label = 'eland %s' % (result_type,)
390 if result_type == 'result':
391 bed_url_pattern = '/results/%s/%s/bedfile/%s'
392 bed_url = bed_url_pattern % (flowcell_id, cycle_name, lane)
394 return (result_url, result_label, bed_url)
397 def _files(flowcell_id, lane):
399 Sets up available files for download
403 flowcell_id, id = parse_flowcell_id(flowcell_id)
404 d = get_flowcell_result_dict(flowcell_id)
411 # c_name == 'CN-M' (i.e. C1-33)
414 if d[c_name]['summary'] is not None:
415 output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' %
416 (flowcell_id, c_name, c_name))
418 erd = d[c_name]['eland_results']
420 result_type = get_eland_result_type(erd[lane])
421 result_url_pattern = '<a href="/results/%s/%s/eland_result/%s">eland %s(%s)</a>'
422 output.append(result_url_pattern % (flowcell_id, c_name, lane, result_type, c_name))
423 if result_type == 'result':
424 bed_url_pattern = '<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>'
425 output.append(bed_url_pattern % (flowcell_id, c_name, lane, c_name))
430 return '(' + '|'.join(output) + ')'
433 def library_id_to_admin_url(request, lib_id):
434 lib = Library.objects.get(id=lib_id)
435 return HttpResponseRedirect('/admin/samples/library/%s' % (lib.id,))
438 def library_dict(library_id):
440 Given a library id construct a dictionary containing important information
441 return None if nothing was found
444 lib = Library.objects.get(id=library_id)
445 except Library.DoesNotExist as e:
448 #lane_info = lane_information(lib.lane_set)
450 for lane in lib.lane_set.all():
451 lane_info.append({'flowcell': lane.flowcell.flowcell_id,
452 'lane_number': lane.lane_number,
454 'paired_end': lane.flowcell.paired_end,
455 'read_length': lane.flowcell.read_length,
456 'status_code': lane.status,
457 'status': LANE_STATUS_MAP[lane.status]})
461 # 'aligned_reads': lib.aligned_reads,
462 #'amplified_into_sample': lib.amplified_into_sample, # into is a colleciton...
463 #'amplified_from_sample_id': lib.amplified_from_sample,
464 #'antibody_name': lib.antibody_name(), # we have no antibodies.
465 'antibody_id': lib.antibody_id,
466 'cell_line_id': lib.cell_line_id,
467 'cell_line': str_or_none(lib.cell_line),
468 'experiment_type': lib.experiment_type.name,
469 'experiment_type_id': lib.experiment_type_id,
470 'gel_cut_size': lib.gel_cut_size,
471 'hidden': lib.hidden,
473 'insert_size': lib.insert_size,
474 'lane_set': lane_info,
475 'library_id': lib.id,
476 'library_name': lib.library_name,
477 'library_species': lib.library_species.scientific_name,
478 'library_species_id': lib.library_species_id,
479 #'library_type': lib.library_type.name,
480 'library_type_id': lib.library_type_id,
481 'made_for': lib.made_for,
482 'made_by': lib.made_by,
484 'replicate': lib.replicate,
485 'stopping_point': lib.stopping_point,
486 'successful_pM': str_or_none(lib.successful_pM),
487 'undiluted_concentration': str_or_none(lib.undiluted_concentration)
489 if lib.library_type_id is None:
490 info['library_type'] = None
492 info['library_type'] = lib.library_type.name
497 def library_json(request, library_id):
499 Return a json formatted library dictionary
501 require_api_key(request)
502 # what validation should we do on library_id?
504 lib = library_dict(library_id)
508 lib_json = json.dumps({'result': lib})
509 return HttpResponse(lib_json, content_type='application/json')
513 def species_json(request, species_id):
515 Return information about a species.
520 def species(request, species_id):
521 species = get_object_or_404(Species, id=species_id)
523 context = RequestContext(request,
524 {'species': species})
526 return render_to_response("samples/species_detail.html", context)
529 def antibodies(request):
530 context = RequestContext(request,
531 {'antibodies': Antibody.objects.order_by('antigene')})
532 return render_to_response("samples/antibody_index.html", context)
536 def user_profile(request):
538 Information about the user
541 'page_name': 'User Profile',
543 #'bcmagic': BarcodeMagicForm(),
544 #'select': 'settings',
546 context.update(SAMPLES_CONTEXT_DEFAULTS)
547 return render_to_response('registration/profile.html', context,
548 context_instance=RequestContext(request))