1 # Create your views here.
10 import simplejson as json
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
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
36 LANE_LIST = [1,2,3,4,5,6,7,8]
37 SAMPLES_CONTEXT_DEFAULTS = {
38 'app_name': 'Flowcell/Library Tracker',
39 'bcmagic': BarcodeMagicForm()
42 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
67 def create_library_context(cl):
69 Create a list of libraries that includes how many lanes were run
72 #for lib in library_items.object_list:
73 for lib in cl.result_list:
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
82 summary['amplified_from'] = ''
83 lanes_run = count_lanes(lib.lane_set)
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 }
95 def library(request, todo_only=False):
96 queryset = Library.objects.filter(hidden__exact=0)
98 queryset = queryset.filter(lane=None)
100 fcl = ChangeList(request, Library,
101 list_filter=['affiliations', 'library_species'],
102 search_fields=['id', 'library_name', 'amplified_from_sample__id'],
107 context = { 'cl': fcl, 'title': 'Library Index', 'todo_only': todo_only}
108 context.update(create_library_context(fcl))
109 t = get_template('samples/library_index.html')
110 c = RequestContext(request, context)
111 return HttpResponse( t.render(c) )
113 def library_not_run(request):
114 return library(request, todo_only=True)
116 def library_to_flowcells(request, lib_id):
118 Display information about all the flowcells a library has been run on.
121 lib = Library.objects.get(id=lib_id)
123 raise Http404('Library %s does not exist' % (lib_id,))
126 flowcell_run_results = {} # aka flowcells we're looking at
127 for lane in lib.lane_set.all():
129 flowcell_id, id = parse_flowcell_id(fc.flowcell_id)
130 if flowcell_id not in flowcell_run_results:
131 flowcell_run_results[flowcell_id] = get_flowcell_result_dict(flowcell_id)
132 flowcell_list.append((fc.flowcell_id, lane.lane_number))
135 lane_summary_list = []
137 for fc, lane_number in flowcell_list:
138 lane_summary, err_list = _summary_stats(fc, lane_number, lib_id)
139 lane_summary_list.extend(lane_summary)
141 eland_results.extend(_make_eland_results(fc, lane_number, flowcell_run_results))
144 'page_name': 'Library Details',
146 'eland_results': eland_results,
147 'lane_summary_list': lane_summary_list,
149 context.update(SAMPLES_CONTEXT_DEFAULTS)
151 return render_to_response(
152 'samples/library_detail.html',
154 context_instance = RequestContext(request))
156 def lanes_for(request, username=None):
158 Generate a report of recent activity for a user
161 if username is not None:
162 user = HTSUser.objects.get(username=username)
163 query.update({'library__affiliations__users__id':user.id})
164 fcl = ChangeList(request, Lane,
166 search_fields=['flowcell__flowcell_id', 'library__id', 'library__library_name'],
168 queryset=Lane.objects.filter(**query)
171 context = { 'lanes': fcl, 'title': 'Lane Index'}
173 return render_to_response(
174 'samples/lanes_for.html',
176 context_instance = RequestContext(request)
180 def summaryhtm_fc_cnm(request, flowcell_id, cnm):
182 returns a Summary.htm file if it exists.
184 fc_id, status = parse_flowcell_id(flowcell_id)
185 d = get_flowcell_result_dict(fc_id)
188 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
191 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
193 summary_filepath = d[cnm]['summary']
195 if summary_filepath is None:
196 return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
198 f = open(summary_filepath, 'r')
200 return HttpResponse(f)
203 def result_fc_cnm_eland_lane(request, flowcell_id, cnm, lane):
205 returns an eland_file upon calling.
207 fc_id, status = parse_flowcell_id(flowcell_id)
208 d = get_flowcell_result_dict(fc_id)
211 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
214 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
216 erd = d[cnm]['eland_results']
220 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
224 #f = opener.autoopen(filepath, 'r')
225 # return HttpResponse(f, mimetype="application/x-elandresult")
227 f = open(filepath, 'r')
228 return HttpResponse(f, mimetype='application/x-bzip2')
232 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
234 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
236 return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
239 def bedfile_fc_cnm_eland_lane(request, flowcell_id, cnm, lane, ucsc_compatible=False):
241 returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
243 fc_id, status = parse_flowcell_id(flowcell_id)
244 d = get_flowcell_result_dict(fc_id)
247 return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
250 return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
252 erd = d[cnm]['eland_results']
256 return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
261 fi = opener.autoopen(filepath, 'r')
264 name, description = makebed.make_description( fc_id, lane )
266 bedgen = makebed.make_bed_from_eland_generator(fi, name, description)
269 return HttpResponse(bedgen)
271 return HttpResponse(bedgen, mimetype="application/x-bedfile")
274 def _summary_stats(flowcell_id, lane_id, library_id):
276 Return the summary statistics for a given flowcell, lane, and end.
278 fc_id, status = parse_flowcell_id(flowcell_id)
279 fc_result_dict = get_flowcell_result_dict(fc_id)
284 if fc_result_dict is None:
285 err_list.append('Results for Flowcell %s not found.' % (fc_id))
286 return (summary_list, err_list)
288 for cycle_width in fc_result_dict:
289 xmlpath = fc_result_dict[cycle_width]['run_xml']
292 err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cycle_width))
295 run = load_pipeline_run_xml(xmlpath)
296 gerald_summary = run.gerald.summary.lane_results
297 key = SampleKey(lane=lane_id, sample='s')
298 eland_results = list(run.gerald.eland_results.find_keys(key))
299 key = SampleKey(lane=lane_id, sample=library_id)
300 eland_results.extend(run.gerald.eland_results.find_keys(key))
301 for key in eland_results:
302 eland_summary = run.gerald.eland_results.results[key]
303 # add information to lane_summary
304 eland_summary.flowcell_id = flowcell_id
306 read = key.read-1 if key.read is not None else 0
308 eland_summary.clusters = gerald_summary[read][key.lane].cluster
309 except IndexError as e:
310 eland_summary.clustes = None
311 eland_summary.cycle_width = cycle_width
312 if hasattr(eland_summary, 'genome_map'):
313 eland_summary.summarized_reads = runfolder.summarize_mapped_reads(
314 eland_summary.genome_map,
315 eland_summary.mapped_reads)
317 # grab some more information out of the flowcell db
318 flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
319 #pm_field = 'lane_%d_pM' % (lane_id)
320 lanes = flowcell.lane_set.filter(lane_number=lane_id)
321 eland_summary.flowcell = flowcell
322 eland_summary.lanes = lanes
324 summary_list.append(eland_summary)
326 #except Exception, e:
327 # summary_list.append("Summary report needs to be updated.")
328 # LOGGER.error("Exception: " + str(e))
330 return (summary_list, err_list)
333 def get_eland_result_type(pathname):
335 Guess the eland result file type from the filename
337 path, filename = os.path.split(pathname)
338 if 'extended' in filename:
340 elif 'multi' in filename:
342 elif 'result' in filename:
347 def _make_eland_results(flowcell_id, lane_number, interesting_flowcells):
348 fc_id, status = parse_flowcell_id(flowcell_id)
349 cur_fc = interesting_flowcells.get(fc_id, None)
353 flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
354 lanes = flowcell.lane_set.filter(lane_number=lane_number)
355 # Loop throw storage devices if a result has been archived
357 if cur_fc is not None:
358 for lts in flowcell.longtermstorage_set.all():
359 for sd in lts.storage_devices.all():
360 # Use barcode_id if it exists
361 if sd.barcode_id is not None and sd.barcode_id != '':
362 storage_id_list.append(sd.barcode_id)
365 storage_id_list.append(sd.uuid)
367 # Formatting for template use
368 if len(storage_id_list) == 0:
371 storage_ids = ', '.join([ '<a href="/inventory/%s/">%s</a>' % (s,s) for s in storage_id_list ])
374 for cycle in cur_fc.keys():
375 result_path = cur_fc[cycle]['eland_results'].get(lanes[0], None)
376 result_link = make_result_link(fc_id, cycle, lanes[0], result_path)
377 results.append({'flowcell_id': fc_id,
378 'flowcell': flowcell,
379 'run_date': flowcell.run_date,
382 'summary_url': make_summary_url(flowcell_id, cycle),
383 'result_url': result_link[0],
384 'result_label': result_link[1],
385 'bed_url': result_link[2],
386 'storage_ids': storage_ids
390 def make_summary_url(flowcell_id, cycle_name):
391 url = '/results/%s/%s/summary/' % (flowcell_id, cycle_name)
394 def make_result_link(flowcell_id, cycle_name, lane, eland_result_path):
395 if eland_result_path is None:
398 result_type = get_eland_result_type(eland_result_path)
399 result_url = '/results/%s/%s/eland_result/%s' % (flowcell_id, cycle_name, lane)
400 result_label = 'eland %s' % (result_type,)
402 if result_type == 'result':
403 bed_url_pattern = '/results/%s/%s/bedfile/%s'
404 bed_url = bed_url_pattern % (flowcell_id, cycle_name, lane)
406 return (result_url, result_label, bed_url)
408 def _files(flowcell_id, lane):
410 Sets up available files for download
414 flowcell_id, id = parse_flowcell_id(flowcell_id)
415 d = get_flowcell_result_dict(flowcell_id)
422 # c_name == 'CN-M' (i.e. C1-33)
425 if d[c_name]['summary'] is not None:
426 output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' \
427 % (flowcell_id, c_name, c_name))
429 erd = d[c_name]['eland_results']
431 result_type = get_eland_result_type(erd[lane])
432 result_url_pattern = '<a href="/results/%s/%s/eland_result/%s">eland %s(%s)</a>'
433 output.append(result_url_pattern % (flowcell_id, c_name, lane, result_type, c_name))
434 if result_type == 'result':
435 bed_url_pattern = '<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>'
436 output.append(bed_url_pattern % (flowcell_id, c_name, lane, c_name))
441 return '(' + '|'.join(output) + ')'
443 def library_id_to_admin_url(request, lib_id):
444 lib = Library.objects.get(id=lib_id)
445 return HttpResponseRedirect('/admin/samples/library/%s' % (lib.id,))
447 def library_dict(library_id):
449 Given a library id construct a dictionary containing important information
450 return None if nothing was found
453 lib = Library.objects.get(id = library_id)
454 except Library.DoesNotExist, e:
457 #lane_info = lane_information(lib.lane_set)
459 for lane in lib.lane_set.all():
460 lane_info.append( {'flowcell':lane.flowcell.flowcell_id,
461 'lane_number': lane.lane_number,
462 'paired_end': lane.flowcell.paired_end,
463 'read_length': lane.flowcell.read_length,
464 'status_code': lane.status,
465 'status': LANE_STATUS_MAP[lane.status]} )
469 # 'aligned_reads': lib.aligned_reads,
470 #'amplified_into_sample': lib.amplified_into_sample, # into is a colleciton...
471 #'amplified_from_sample_id': lib.amplified_from_sample,
472 #'antibody_name': lib.antibody_name(), # we have no antibodies.
473 'antibody_id': lib.antibody_id,
474 'cell_line_id': lib.cell_line_id,
475 'cell_line': unicode_or_none(lib.cell_line),
476 'experiment_type': lib.experiment_type.name,
477 'experiment_type_id': lib.experiment_type_id,
478 'gel_cut_size': lib.gel_cut_size,
479 'hidden': lib.hidden,
481 'insert_size': lib.insert_size,
482 'lane_set': lane_info,
483 'library_id': lib.id,
484 'library_name': lib.library_name,
485 'library_species': lib.library_species.scientific_name,
486 'library_species_id': lib.library_species_id,
487 #'library_type': lib.library_type.name,
488 'library_type_id': lib.library_type_id,
489 'made_for': lib.made_for,
490 'made_by': lib.made_by,
492 'replicate': lib.replicate,
493 'stopping_point': lib.stopping_point,
494 'successful_pM': unicode_or_none(lib.successful_pM),
495 'undiluted_concentration': unicode_or_none(lib.undiluted_concentration)
497 if lib.library_type_id is None:
498 info['library_type'] = None
500 info['library_type'] = lib.library_type.name
504 def library_json(request, library_id):
506 Return a json formatted library dictionary
508 require_api_key(request)
509 # what validation should we do on library_id?
511 lib = library_dict(library_id)
515 lib_json = json.dumps(lib)
516 return HttpResponse(lib_json, mimetype='application/json')
519 def species_json(request, species_id):
521 Return information about a species.
525 def species(request, species_id):
526 species = get_object_or_404(Species, id=species_id)
528 context = RequestContext(request,
529 { 'species': species })
531 return render_to_response("samples/species_detail.html", context)
533 def antibodies(request):
534 context = RequestContext(request,
535 {'antibodies': Antibody.objects.order_by('antigene')})
536 return render_to_response("samples/antibody_index.html", context)
539 def user_profile(request):
541 Information about the user
544 'page_name': 'User Profile',
546 #'bcmagic': BarcodeMagicForm(),
547 #'select': 'settings',
549 context.update(SAMPLES_CONTEXT_DEFAULTS)
550 return render_to_response('registration/profile.html', context,
551 context_instance=RequestContext(request))