Show the "Amplified from sample" as the parent library id and allow
[htsworkflow.git] / htsworkflow / frontend / samples / views.py
index f36a0c3060932af86a43d8889758286f532fe2c8..d6647d1dba38311733d9f3d07530df9f77442a6b 100644 (file)
@@ -1,4 +1,6 @@
 # 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
@@ -8,8 +10,8 @@ from htsworkflow.util import makebed
 from htsworkflow.util import opener
 
 from django.http import HttpResponse
+from django.template import RequestContext
 from django.template.loader import get_template
-from django.template import Context
 
 import StringIO
 import logging
@@ -17,28 +19,43 @@ import os
 
 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.order_by('-library_id'):
+    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):
@@ -100,7 +117,7 @@ def library_to_flowcells(request, lib_id):
             output.append(err)
    
     output.append('<br />')
-    output.append(t.render(Context({'lane_summary_list': lane_summary_list})))
+    output.append(t.render(RequestContext(request, {'lane_summary_list': lane_summary_list})))
     output.append('<br />')
     
     if record_count == 0:
@@ -230,7 +247,13 @@ def _summary_stats(flowcell_id, lane_id):
                 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:
@@ -299,12 +322,26 @@ def _summary_stats_old(flowcell_id, lane):
     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 _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)
     
@@ -321,10 +358,13 @@ def _files(flowcell_id, lane):
                           % (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 ''