Turn the library_id back into the primary key for samples_library (SCHEMA CHANGE!)
[htsworkflow.git] / htsworkflow / frontend / experiments / experiments.py
index cfea75f895e31ca2fcb2c4aba2559f768678e337..7e6c7343faec43204ef0640f99cb2d85d4ea2ef4 100755 (executable)
@@ -1,15 +1,80 @@
 # some core functions of the exp tracker module
 from datetime import datetime, timedelta
+try:
+    import json
+except ImportError, e:
+    import simplejson as json
+    
 import os
 import re
 
-from django.http import HttpResponse
+from django.contrib.auth.decorators import login_required
+from django.core.exceptions import ObjectDoesNotExist
+from django.core.mail import send_mail, mail_admins
+from django.http import HttpResponse, Http404
+
 from htsworkflow.frontend import settings
 from htsworkflow.frontend.experiments.models import FlowCell, DataRun
 from htsworkflow.frontend.samples.models import Library
-from django.core.exceptions import ObjectDoesNotExist
-from django.core.mail import send_mail, mail_admins
+from htsworkflow.frontend.auth import require_api_key
+
+def flowcell_information(flowcell_id):
+    """
+    Return a dictionary describing a flowcell
+    """
+    try:
+        fc = FlowCell.objects.get(flowcell_id=flowcell_id)
+    except FlowCell.DoesNotExist, e:
+        return None
 
+    lane_set = {}
+    for lane in fc.lane_set.all():
+        lane_set[lane.lane_number] = {
+            'cluster_estimate': lane.cluster_estimate,
+            'comment': lane.comment,
+            'experiment_type': lane.library.experiment_type.name,
+            'experiment_type_id': lane.library.experiment_type_id,
+            'flowcell': lane.flowcell.flowcell_id,
+            'lane_number': int(lane.lane_number),
+            'library_name': lane.library.library_name,
+            'library_id': lane.library.id,
+            'library_species': lane.library.library_species.scientific_name,
+            'pM': float(lane.pM),
+            'read_length': fc.read_length
+        }
+    info = {
+        'advanced_run': fc.advanced_run,
+        'cluster_station_id': fc.cluster_station_id,
+        'cluster_station': fc.cluster_station.name,
+        'control_lane': int(fc.control_lane),
+        # 'datarun_set': how should this be represented?,
+        'flowcell_id': fc.flowcell_id,
+        'id': fc.id,
+        'lane_set': lane_set,
+        'notes': fc.notes,
+        'paired_end': fc.paired_end,
+        'read_length': fc.read_length,
+        'run_date': fc.run_date.isoformat(),
+        'sequencer_id': fc.sequencer_id,
+        'sequencer': fc.sequencer.name,
+    }
+    
+    return info
+
+def flowcell_json(request, fc_id):
+    """
+    Return a JSON blob containing enough information to generate a config file.
+    """
+    require_api_key(request)
+    
+    fc_dict = flowcell_information(fc_id)
+
+    if fc_dict is None:
+        raise Http404
+    
+    fc_json = json.dumps(fc_dict)
+    return HttpResponse(fc_json, mimetype = 'application/json')
+    
 def updStatus(request):
     output=''
     user = 'none'
@@ -90,7 +155,6 @@ def generateConfile(request,fcid):
     try:                                                                                                                                              
       fc = FlowCell.objects.get(flowcell_id=fcid)
       for lane in fc.lane_set.all():
-          print dir(lane.library.library_species)
           config += [ str(lane.lane_number) +":" + \
                       genome_dir + lane.library.library_species.scientific_name ]
           config += [ str(lane.lane_number) +":" + \
@@ -112,9 +176,6 @@ def getConfile(req):
     cnfgfile = 'Nothing found'
     runfolder = 'unknown'
     request = req.REQUEST
-    print request, dir(request)
-    print request['fcid'], request.has_key('fcid')
-    print request['runf']
     if request.has_key('fcid'):
       fcid = request['fcid']
       if request.has_key('runf'):
@@ -164,14 +225,14 @@ def getLaneLibs(req):
         mydate = year+month+day
         outputfile = '<?xml version="1.0" ?>'
         outputfile += '\n<SolexaResult Date="'+mydate+'" Flowcell="'+fcid+'" Client="'+settings.ALLOWED_IPS[ClIP]+'">'
-        outputfile += '\n<Lane Index="1" Name="'+rec.lane_1_library.library_name+'" Library="'+rec.lane_1_library.library_id+'" Genome="'+rec.lane_1_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
-        outputfile += '\n<Lane Index="2" Name="'+rec.lane_2_library.library_name+'" Library="'+rec.lane_2_library.library_id+'" Genome="'+rec.lane_2_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
-        outputfile += '\n<Lane Index="3" Name="'+rec.lane_3_library.library_name+'" Library="'+rec.lane_3_library.library_id+'" Genome="'+rec.lane_3_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
-        outputfile += '\n<Lane Index="4" Name="'+rec.lane_4_library.library_name+'" Library="'+rec.lane_4_library.library_id+'" Genome="'+rec.lane_4_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
-        outputfile += '\n<Lane Index="5" Name="'+rec.lane_5_library.library_name+'" Library="'+rec.lane_5_library.library_id+'" Genome="'+rec.lane_5_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
-        outputfile += '\n<Lane Index="6" Name="'+rec.lane_6_library.library_name+'" Library="'+rec.lane_6_library.library_id+'" Genome="'+rec.lane_6_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
-        outputfile += '\n<Lane Index="7" Name="'+rec.lane_7_library.library_name+'" Library="'+rec.lane_7_library.library_id+'" Genome="'+rec.lane_7_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
-        outputfile += '\n<Lane Index="8" Name="'+rec.lane_8_library.library_name+'" Library="'+rec.lane_8_library.library_id+'" Genome="'+rec.lane_8_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
+        outputfile += '\n<Lane Index="1" Name="'+rec.lane_1_library.library_name+'" Library="'+rec.lane_1_library.id+'" Genome="'+rec.lane_1_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
+        outputfile += '\n<Lane Index="2" Name="'+rec.lane_2_library.library_name+'" Library="'+rec.lane_2_library.id+'" Genome="'+rec.lane_2_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
+        outputfile += '\n<Lane Index="3" Name="'+rec.lane_3_library.library_name+'" Library="'+rec.lane_3_library.id+'" Genome="'+rec.lane_3_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
+        outputfile += '\n<Lane Index="4" Name="'+rec.lane_4_library.library_name+'" Library="'+rec.lane_4_library.id+'" Genome="'+rec.lane_4_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
+        outputfile += '\n<Lane Index="5" Name="'+rec.lane_5_library.library_name+'" Library="'+rec.lane_5_library.id+'" Genome="'+rec.lane_5_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
+        outputfile += '\n<Lane Index="6" Name="'+rec.lane_6_library.library_name+'" Library="'+rec.lane_6_library.id+'" Genome="'+rec.lane_6_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
+        outputfile += '\n<Lane Index="7" Name="'+rec.lane_7_library.library_name+'" Library="'+rec.lane_7_library.id+'" Genome="'+rec.lane_7_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
+        outputfile += '\n<Lane Index="8" Name="'+rec.lane_8_library.library_name+'" Library="'+rec.lane_8_library.id+'" Genome="'+rec.lane_8_library.library_species.use_genome_build+'" PrimerName="" PrimerSeq=""/>'
         outputfile += '\n</SolexaResult>'
       except ObjectDoesNotExist:
         outputfile = 'Flowcell entry not found for: '+fcid
@@ -217,7 +278,17 @@ def makeUserLaneMap(flowcell):
 
     return users
 
-def makeUserLibrarMap(libraries):
+def getUsersForFlowcell(flowcell):
+    users = set()
+    
+    for lane in flowcell.lane_set.all():
+        for affiliation in lane.library.affiliations.all():
+            for user in affiliation.users.all():
+                users.add(user)
+                
+    return users
+    
+def makeUserLibraryMap(libraries):
     """
     Given an interable set of libraries return a mapping or
     users interested in those libraries.
@@ -230,3 +301,29 @@ def makeUserLibrarMap(libraries):
                 users.setdefault(user,[]).append(library)
                 
     return users
+
+def makeAffiliationLaneMap(flowcell):
+    affs = {}
+
+    for lane in flowcell.lane_set.all():
+        for affiliation in lane.library.affiliations.all():
+            affs.setdefault(affiliation,[]).append(lane)
+
+    return affs
+
+def makeEmailLaneMap(flowcell):
+    """
+    Create a list of email addresses and the lanes associated with those users.
+
+    The email addresses can come from both the "users" table and the "affiliations" table.
+    """
+    emails = {}
+    for lane in flowcell.lane_set.all():
+        for affiliation in lane.library.affiliations.all():
+            if affiliation.email is not None and len(affiliation.email) > 0:
+                emails.setdefault(affiliation.email,set()).add(lane)
+            for user in affiliation.users.all():
+                if user.email is not None and len(user.email) > 0:
+                    emails.setdefault(user.email,set()).add(lane)
+
+    return emails