# Create your views here.
+from htsworkflow.frontend.experiments.models import FlowCell
+from htsworkflow.frontend.samples.changelist import ChangeList
from htsworkflow.frontend.samples.models import Library
from htsworkflow.frontend.samples.results import get_flowcell_result_dict, parse_flowcell_id
from htsworkflow.pipelines.runfolder import load_pipeline_run_xml
from htsworkflow.util import opener
from django.http import HttpResponse
+from django.shortcuts import render_to_response
+from django.template import RequestContext
from django.template.loader import get_template
-from django.template import Context
import StringIO
import logging
LANE_LIST = [1,2,3,4,5,6,7,8]
-def create_library_list():
+def create_library_context(cl):
"""
Create a list of libraries that includes how many lanes were run
"""
- library_list = []
- for lib in Library.objects.all():
+ records = []
+ #for lib in library_items.object_list:
+ for lib in cl.result_list:
summary = {}
summary['library_id'] = lib.library_id
summary['library_name'] = lib.library_name
summary['species_name' ] = lib.library_species.scientific_name
+ if lib.amplified_from_sample is not None:
+ summary['amplified_from'] = lib.amplified_from_sample.library_id
+ else:
+ summary['amplified_from'] = ''
lanes_run = 0
for lane_id in LANE_LIST:
lane = getattr(lib, 'lane_%d_library' % (lane_id,))
lanes_run += len( lane.all() )
summary['lanes_run'] = lanes_run
- library_list.append(summary)
- return library_list
+ records.append(summary)
+ cl.result_count = unicode(cl.paginator._count) + u" libraries"
+ return {'library_list': records }
def library(request):
- library_list = create_library_list()
+ # build changelist
+ fcl = ChangeList(request, Library,
+ list_filter=['affiliations', 'library_species'],
+ search_fields=['library_id', 'library_name', 'amplified_from_sample__library_id'],
+ list_per_page=200,
+ queryset=Library.objects.filter(hidden__exact=0)
+ )
+
+ context = { 'cl': fcl, 'title': 'Library Index'}
+ context.update(create_library_context(fcl))
t = get_template('samples/library_index.html')
- c = Context({'library_list': library_list })
+ c = RequestContext(request, context)
return HttpResponse( t.render(c) )
def library_to_flowcells(request, lib_id):
"""
Display information about all the flowcells a library has been run on.
"""
- t = get_template("samples/library_detail.html")
try:
lib = Library.objects.get(library_id=lib_id)
except:
return HttpResponse("Library %s does not exist" % (lib_id))
-
- output = []
-
- output.append('<b>Library ID:</b> %s' % (lib.library_id))
- output.append('<b>Name:</b> %s' % (lib.library_name))
- output.append('<b>Species:</b> %s' % (lib.library_species.scientific_name))
- output.append('')
-
- output.append('<b>FLOWCELL - LANE:</b>')
-
- output.extend([ '%s - Lane 1 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 1)) for fc in lib.lane_1_library.all() ])
- output.extend([ '%s - Lane 2 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 2)) for fc in lib.lane_2_library.all() ])
- output.extend([ '%s - Lane 3 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 3)) for fc in lib.lane_3_library.all() ])
- output.extend([ '%s - Lane 4 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 4)) for fc in lib.lane_4_library.all() ])
- output.extend([ '%s - Lane 5 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 5)) for fc in lib.lane_5_library.all() ])
- output.extend([ '%s - Lane 6 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 6)) for fc in lib.lane_6_library.all() ])
- output.extend([ '%s - Lane 7 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 7)) for fc in lib.lane_7_library.all() ])
- output.extend([ '%s - Lane 8 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 8)) for fc in lib.lane_8_library.all() ])
-
- record_count = lib.lane_1_library.count() + \
- lib.lane_2_library.count() + \
- lib.lane_3_library.count() + \
- lib.lane_4_library.count() + \
- lib.lane_5_library.count() + \
- lib.lane_6_library.count() + \
- lib.lane_7_library.count() + \
- lib.lane_8_library.count()
-
+
flowcell_list = []
- flowcell_list.extend([ (fc.flowcell_id, 1) for fc in lib.lane_1_library.all() ])
- flowcell_list.extend([ (fc.flowcell_id, 2) for fc in lib.lane_2_library.all() ])
- flowcell_list.extend([ (fc.flowcell_id, 3) for fc in lib.lane_3_library.all() ])
- flowcell_list.extend([ (fc.flowcell_id, 4) for fc in lib.lane_4_library.all() ])
- flowcell_list.extend([ (fc.flowcell_id, 5) for fc in lib.lane_5_library.all() ])
- flowcell_list.extend([ (fc.flowcell_id, 6) for fc in lib.lane_6_library.all() ])
- flowcell_list.extend([ (fc.flowcell_id, 7) for fc in lib.lane_7_library.all() ])
- flowcell_list.extend([ (fc.flowcell_id, 8) for fc in lib.lane_8_library.all() ])
+ interesting_flowcells = {} # aka flowcells we're looking at
+ for lane in LANE_LIST:
+ lane_library = getattr(lib, 'lane_%d_library' % (lane,))
+ for fc in lane_library.all():
+ flowcell_id, id = parse_flowcell_id(fc.flowcell_id)
+ if flowcell_id not in interesting_flowcells:
+ interesting_flowcells[flowcell_id] = get_flowcell_result_dict(flowcell_id)
+ flowcell_list.append((fc.flowcell_id, lane))
+
flowcell_list.sort()
lane_summary_list = []
+ eland_results = []
for fc, lane in flowcell_list:
lane_summary, err_list = _summary_stats(fc, lane)
+ eland_results.extend(_make_eland_results(fc, lane, interesting_flowcells))
lane_summary_list.extend(lane_summary)
-
- for err in err_list:
- output.append(err)
-
- output.append('<br />')
- output.append(t.render(Context({'lane_summary_list': lane_summary_list})))
- output.append('<br />')
-
- if record_count == 0:
- output.append("None Found")
-
- return HttpResponse('<br />\n'.join(output))
+ return render_to_response(
+ 'samples/library_detail.html',
+ {'lib': lib,
+ 'eland_results': eland_results,
+ 'lane_summary_list': lane_summary_list,
+ },
+ context_instance = RequestContext(request))
+
def summaryhtm_fc_cnm(request, fc_id, cnm):
"""
returns a Summary.htm file if it exists.
eland_summary.flowcell_id = flowcell_id
eland_summary.clusters = gerald_summary[end][lane_id].cluster
eland_summary.cycle_width = cycle_width
- eland_summary.summarized_reads = runfolder.summarize_mapped_reads(eland_summary.mapped_reads)
+ eland_summary.summarized_reads = runfolder.summarize_mapped_reads(eland_summary.genome_map, eland_summary.mapped_reads)
+
+ # grab some more information out of the flowcell db
+ flowcell = FlowCell.objects.get(flowcell_id=fc_id)
+ pm_field = 'lane_%d_pM' % (lane_id)
+ eland_summary.successful_pm = getattr(flowcell, pm_field)
+
summary_list.append(eland_summary)
except Exception, e:
return (dict_list, err_list, summary_list)
+def get_eland_result_type(pathname):
+ """
+ Guess the eland result file type from the filename
+ """
+ path, filename = os.path.split(pathname)
+ if 'extended' in filename:
+ return 'extended'
+ elif 'multi' in filename:
+ return 'multi'
+ elif 'result' in filename:
+ return 'result'
+ else:
+ return 'unknown'
+
+def _make_eland_results(flowcell_id, lane, interesting_flowcells):
+
+ cur_fc = interesting_flowcells.get(flowcell_id, None)
+ if cur_fc is None:
+ return []
+
+ results = []
+ for cycle in cur_fc.keys():
+ result_path = cur_fc[cycle]['eland_results'][lane]
+ result_link = make_result_link(flowcell_id, cycle, lane, result_path)
+ results.append({'flowcell_id': flowcell_id,
+ 'cycle': cycle,
+ 'lane': lane,
+ 'summary_url': make_summary_url(flowcell_id, cycle),
+ 'result_url': result_link[0],
+ 'result_label': result_link[1],
+ 'bed_url': result_link[2],
+ })
+ return results
+
+def make_summary_url(flowcell_id, cycle_name):
+ url = '/results/%s/%s/summary/' % (flowcell_id, cycle_name)
+ return url
+
+def make_result_link(flowcell_id, cycle_name, lane, eland_result_path):
+ result_type = get_eland_result_type(eland_result_path)
+ result_url = '/results/%s/%s/eland_result/%s' % (flowcell_id, cycle_name, lane)
+ result_label = 'eland %s' % (result_type,)
+ bed_url = None
+ if result_type == 'result':
+ bed_url_pattern = '/results/%s/%s/bedfile/%s'
+ bed_url = bed_url_pattern % (flowcell_id, cycle_name, lane)
+
+ return (result_url, result_label, bed_url)
-
def _files(flowcell_id, lane):
"""
Sets up available files for download
"""
+ lane = int(lane)
+
flowcell_id, id = parse_flowcell_id(flowcell_id)
d = get_flowcell_result_dict(flowcell_id)
% (flowcell_id, c_name, c_name))
erd = d[c_name]['eland_results']
-
- if int(lane) in erd:
- output.append('<a href="/results/%s/%s/eland_result/%s">eland_result(%s)</a>' % (flowcell_id, c_name, lane, c_name))
- output.append('<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>' % (flowcell_id, c_name, lane, c_name))
+ if lane in erd:
+ result_type = get_eland_result_type(erd[lane])
+ result_url_pattern = '<a href="/results/%s/%s/eland_result/%s">eland %s(%s)</a>'
+ output.append(result_url_pattern % (flowcell_id, c_name, lane, result_type, c_name))
+ if result_type == 'result':
+ bed_url_pattern = '<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>'
+ output.append(bed_url_pattern % (flowcell_id, c_name, lane, c_name))
if len(output) == 0:
return ''