Turn the library_id back into the primary key for samples_library (SCHEMA CHANGE!)
[htsworkflow.git] / htsworkflow / frontend / samples / views.py
index 14e3b02b1fc3fc3440db40878e2beda1b074661e..20c3d98bd5171821dedc8e535eccab73b101b2dd 100644 (file)
@@ -1,4 +1,13 @@
 # Create your views here.
+import StringIO
+import logging
+import os
+try:
+    import json
+except ImportError, e:
+    import simplejson as json
+
+from htsworkflow.frontend.auth import require_api_key
 from htsworkflow.frontend.experiments.models import FlowCell
 from htsworkflow.frontend.samples.changelist import ChangeList
 from htsworkflow.frontend.samples.models import Library
@@ -12,16 +21,12 @@ from htsworkflow.util import makebed
 from htsworkflow.util import opener
 
 from django.core.exceptions import ObjectDoesNotExist
-from django.http import HttpResponse, HttpResponseRedirect
+from django.http import HttpResponse, HttpResponseRedirect, Http404
 from django.shortcuts import render_to_response
 from django.template import RequestContext
 from django.template.loader import get_template
 from django.contrib.auth.decorators import login_required
 
-import StringIO
-import logging
-import os
-
 LANE_LIST = [1,2,3,4,5,6,7,8]
 SAMPLES_CONTEXT_DEFAULTS = {
     'app_name': 'Flowcell/Library Tracker',
@@ -36,11 +41,11 @@ def create_library_context(cl):
     #for lib in library_items.object_list:
     for lib in cl.result_list:
        summary = {}
-       summary['library_id'] = lib.library_id
+       summary['library_id'] = lib.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
+           summary['amplified_from'] = lib.amplified_from_sample.id
        else:
            summary['amplified_from'] = ''
        lanes_run = 0
@@ -58,7 +63,7 @@ def library(request):
    # build changelist
     fcl = ChangeList(request, Library,
         list_filter=['affiliations', 'library_species'],
-        search_fields=['library_id', 'library_name', 'amplified_from_sample__library_id'],
+        search_fields=['id', 'library_name', 'amplified_from_sample__id'],
         list_per_page=200,
         queryset=Library.objects.filter(hidden__exact=0)
     )
@@ -85,7 +90,7 @@ def library_to_flowcells(request, lib_id):
     """
     
     try:
-      lib = Library.objects.get(library_id=lib_id)
+      lib = Library.objects.get(id=lib_id)
     except:
       return HttpResponse("Library %s does not exist" % (lib_id))
    
@@ -249,7 +254,10 @@ def _summary_stats(flowcell_id, lane_id):
                 eland_summary = ResultLane(lane_id=lane_id, end=end)
             # add information to lane_summary
             eland_summary.flowcell_id = flowcell_id
-            eland_summary.clusters = gerald_summary[end][lane_id].cluster
+            if len(gerald_summary) > end and gerald_summary[end].has_key(lane_id):
+                eland_summary.clusters = gerald_summary[end][lane_id].cluster
+            else:
+                eland_summary.clusters = None
             eland_summary.cycle_width = cycle_width
             if hasattr(eland_summary, 'genome_map'):
                 eland_summary.summarized_reads = runfolder.summarize_mapped_reads( 
@@ -258,8 +266,9 @@ def _summary_stats(flowcell_id, lane_id):
 
             # grab some more information out of the flowcell db
             flowcell = FlowCell.objects.get(flowcell_id=flowcell_id)
-            pm_field = 'lane_%d_pM' % (lane_id)
-            eland_summary.successful_pm = getattr(flowcell, pm_field)
+            #pm_field = 'lane_%d_pM' % (lane_id)
+            lane_obj = flowcell.lane_set.get(lane_number=lane_id)
+            eland_summary.successful_pm = lane_obj.pM
 
             summary_list.append(eland_summary)
 
@@ -437,9 +446,72 @@ def _files(flowcell_id, lane):
     return '(' + '|'.join(output) + ')'
 
 def library_id_to_admin_url(request, lib_id):
-    lib = Library.objects.get(library_id=lib_id)
+    lib = Library.objects.get(id=lib_id)
     return HttpResponseRedirect('/admin/samples/library/%s' % (lib.id,))
 
+def library_dict(library_id):
+    """
+    Given a library id construct a dictionary containing important information
+    return None if nothing was found
+    """
+    try:
+        lib = Library.objects.get(id = library_id)
+    except Library.DoesNotExist, e:
+        return None
+
+    info = {
+        # 'affiliations'?
+        # 'aligned_reads': lib.aligned_reads,
+        #'amplified_into_sample': lib.amplified_into_sample, # into is a colleciton...
+        #'amplified_from_sample_id': lib.amplified_from_sample, 
+        #'antibody_name': lib.antibody_name(), # we have no antibodies.
+        'antibody_id': lib.antibody_id,
+        'avg_lib_size': lib.avg_lib_size,
+        'cell_line': lib.cell_line.cellline_name,
+        'cell_line_id': lib.cell_line_id,
+        'experiment_type': lib.experiment_type.name,
+        'experiment_type_id': lib.experiment_type_id,
+        'id': lib.id,
+        'library_id': lib.id,
+        'library_name': lib.library_name,
+        'library_species': lib.library_species.scientific_name,
+        'library_species_id': lib.library_species_id,
+        #'library_type': lib.library_type.name,
+        'library_type_id': lib.library_type_id,
+        'made_for': lib.made_for,
+        'made_by': lib.made_by,
+        'notes': lib.notes,
+        'replicate': lib.replicate,
+        'stopping_point': lib.stopping_point,
+        'successful_pM': lib.successful_pM,
+        'undiluted_concentration': unicode(lib.undiluted_concentration)
+        }
+    if lib.library_type_id is None:
+        info['library_type'] = None
+    else:
+        info['library_type'] = lib.library_type.name
+    return info
+
+def library_json(request, library_id):
+    """
+    Return a json formatted library dictionary
+    """
+    require_api_key(request)
+    # what validation should we do on library_id?
+    
+    lib = library_dict(library_id)
+    if lib is None:
+        raise Http404
+
+    lib_json = json.dumps(lib)
+    return HttpResponse(lib_json, mimetype='application/json')
+
+def species_json(request, species_id):
+    """
+    Return information about a species.
+    """
+    raise Http404
+    
 @login_required
 def user_profile(request):
     """
@@ -454,3 +526,4 @@ def user_profile(request):
     context.update(SAMPLES_CONTEXT_DEFAULTS)
     return render_to_response('registration/profile.html', context,
                               context_instance=RequestContext(request))
+