Make antibody catalog number optional for creating entries
[htsworkflow.git] / htsworkflow / frontend / samples / models.py
index 32d63a35d545fafc9f2f78ca295364db266d11a3..531edc6c84d0e417683e609218ac75ba2f18a1fa 100644 (file)
@@ -1,12 +1,13 @@
+import logging
 import urlparse
 from django.db import models
 from django.contrib.auth.models import User, UserManager
+from django.core import urlresolvers
 from django.db.models.signals import pre_save, post_save
 from django.db import connection
-from htsworkflow.frontend import settings
 from htsworkflow.frontend.reports.libinfopar import *
 
-# Create your models here.
+logger = logging.getLogger(__name__)
 
 class Antibody(models.Model):
     antigene = models.CharField(max_length=500, db_index=True)
@@ -17,16 +18,15 @@ class Antibody(models.Model):
         max_length=20,
         blank=True,
         null=True, 
-        db_index=True,
-        verbose_name = 'Short Name'
+        db_index=True
     )
-    catalog = models.CharField(max_length=50, unique=True, db_index=True)
+    catalog = models.CharField(max_length=50, blank=True, null=True)
     antibodies = models.CharField(max_length=500, db_index=True)
-    source = models.CharField(max_length=500, blank=True, db_index=True)
-    biology = models.TextField(blank=True)
-    notes = models.TextField(blank=True)
+    source = models.CharField(max_length=500, blank=True, null=True, db_index=True)
+    biology = models.TextField(blank=True, null=True)
+    notes = models.TextField(blank=True, null=True)
     def __unicode__(self):
-        return u'%s - %s (%s)' % (self.antigene, self.antibodies, self.catalog)
+        return u'%s - %s' % (self.antigene, self.antibodies)
     class Meta:
         verbose_name_plural = "antibodies"
         ordering = ["antigene"]
@@ -36,8 +36,8 @@ class Cellline(models.Model):
     nickname = models.CharField(max_length=20,
         blank=True,
         null=True, 
-        db_index=True,
-        verbose_name = 'Short Name')
+        db_index=True)
+    
     notes = models.TextField(blank=True)
     def __unicode__(self):
         return unicode(self.cellline_name)
@@ -61,6 +61,7 @@ class Condition(models.Model):
     class Meta:
         ordering = ["condition_name"]
 
+    
 class ExperimentType(models.Model):
   name = models.CharField(max_length=50, unique=True)
 
@@ -99,6 +100,10 @@ class Species(models.Model):
   class Meta:
     verbose_name_plural = "species"
     ordering = ["scientific_name"]
+
+  @models.permalink
+  def get_absolute_url(self):
+    return ('htsworkflow.frontend.samples.views.species', [str(self.id)])
   
 class Affiliation(models.Model):
   name = models.CharField(max_length=256, db_index=True, verbose_name='Name')
@@ -127,30 +132,29 @@ class LibraryType(models.Model):
   def __unicode__(self):
     return unicode(self.name)
 
+
 class Library(models.Model):
   id = models.CharField(max_length=10, primary_key=True)
   library_name = models.CharField(max_length=100, unique=True)
   library_species = models.ForeignKey(Species)
-  # new field 2008 Mar 5, alter table samples_library add column "hidden" NOT NULL default 0;
   hidden = models.BooleanField()
-  # new field 2009 Oct 6, alter table samples_library add column "account_number" varchar(100) NULL
   account_number = models.CharField(max_length=100, null=True, blank=True)
-  cell_line = models.ForeignKey(Cellline, blank=True, null=True, verbose_name="Background")
+  cell_line = models.ForeignKey(Cellline, blank=True, null=True,
+                                verbose_name="Background")
   condition = models.ForeignKey(Condition, blank=True, null=True)
   antibody = models.ForeignKey(Antibody,blank=True,null=True)
-  # New field Aug/25/08. SQL: alter table fctracker_library add column "lib_affiliation" varchar(256)  NULL;
-  affiliations = models.ManyToManyField(Affiliation,related_name='library_affiliations',null=True)
-  # new field Nov/14/08
-  tags = models.ManyToManyField(Tag,related_name='library_tags',blank=True,null=True)
-  # New field Aug/19/08
-  # SQL to add column: alter table fctracker_library add column "replicate" smallint unsigned NULL;
+  affiliations = models.ManyToManyField(
+      Affiliation,related_name='library_affiliations',null=True)
+  tags = models.ManyToManyField(Tag,related_name='library_tags',
+                                blank=True,null=True)
   REPLICATE_NUM = ((1,1),(2,2),(3,3),(4,4))
-  replicate =  models.PositiveSmallIntegerField(choices=REPLICATE_NUM,default=1) 
+  replicate =  models.PositiveSmallIntegerField(choices=REPLICATE_NUM,
+                                                blank=True,null=True) 
   experiment_type = models.ForeignKey(ExperimentType)
   library_type = models.ForeignKey(LibraryType, blank=True, null=True)
   creation_date = models.DateField(blank=True, null=True)
   made_for = models.CharField(max_length=50, blank=True, 
-      verbose_name='ChIP/DNA/RNA Made By')
+                              verbose_name='ChIP/DNA/RNA Made By')
   made_by = models.CharField(max_length=50, blank=True, default="Lorian")
   
   PROTOCOL_END_POINTS = (
@@ -160,32 +164,47 @@ class Library(models.Model):
       ('1A', 'Ligation, then gel'),
       ('PCR', 'Ligation, then PCR'),
       ('1Ab', 'Ligation, PCR, then gel'),
-      ('1Aa', 'Ligation, gel, then PCR'),
+      ('1Ac', 'Ligation, gel, then 12x PCR'),
+      ('1Aa', 'Ligation, gel, then 18x PCR'),
       ('2A', 'Ligation, PCR, gel, PCR'),
       ('Done', 'Completed'),
     )
-  stopping_point = models.CharField(max_length=25, choices=PROTOCOL_END_POINTS, default='Done')
-  amplified_from_sample = models.ForeignKey('self', blank=True, null=True, related_name='amplified_into_sample')  
+  PROTOCOL_END_POINTS_DICT = dict(PROTOCOL_END_POINTS)
+  stopping_point = models.CharField(max_length=25,
+                                    choices=PROTOCOL_END_POINTS,
+                                    default='Done')
+  
+  amplified_from_sample = models.ForeignKey('self',
+                            related_name='amplified_into_sample',
+                            blank=True, null=True)
   
   undiluted_concentration = models.DecimalField("Concentration", 
       max_digits=5, decimal_places=2, blank=True, null=True,
       help_text=u"Undiluted concentration (ng/\u00b5l)") 
       # note \u00b5 is the micro symbol in unicode
-  successful_pM = models.DecimalField(max_digits=9, decimal_places=1, blank=True, null=True)
+  successful_pM = models.DecimalField(max_digits=9,
+                                      decimal_places=1, blank=True, null=True)
   ten_nM_dilution = models.BooleanField()
-  avg_lib_size = models.IntegerField(default=225, blank=True, null=True)
+  gel_cut_size = models.IntegerField(default=225, blank=True, null=True)
+  insert_size = models.IntegerField(blank=True, null=True)
   notes = models.TextField(blank=True)
+
+  bioanalyzer_summary = models.TextField(blank=True,default="")
+  bioanalyzer_concentration = models.DecimalField(max_digits=5, 
+                                decimal_places=2, blank=True, null=True,
+                                help_text=u"(ng/\u00b5l)")
+  bioanalyzer_image_url = models.URLField(blank=True,default="")
   
   def __unicode__(self):
     return u'#%s: %s' % (self.id, self.library_name)
   
   class Meta:
-    verbose_name_plural = "libraries"
-    #ordering = ["-creation_date"] 
-    ordering = ["-id"]
+      verbose_name_plural = "libraries"
+      #ordering = ["-creation_date"] 
+      ordering = ["-id"]
   
   def antibody_name(self):
-    str ='<a target=_self href="/admin/samples/antibody/'+self.antibody.id.__str__()+'/" title="'+self.antibody.__str__()+'">'+self.antibody.nickname+'</a>' 
+    str ='<a target=_self href="/admin/samples/antibody/'+self.antibody.id.__str__()+'/" title="'+self.antibody.__str__()+'">'+self.antibody.label+'</a>' 
     return str
   antibody_name.allow_tags = True
 
@@ -209,6 +228,15 @@ class Library(models.Model):
     else:
         return False
 
+  def stopping_point_name(self):
+      end_points = Library.PROTOCOL_END_POINTS_DICT
+      name = end_points.get(self.stopping_point, None)
+      if name is None:
+          name = "Lookup Error"
+          logger.error("protocol stopping point in database didn't match names in library model")
+      return name
+      
+
   def libtags(self):
     affs = self.tags.all().order_by('tag_name')
     ar = []
@@ -253,17 +281,21 @@ class Library(models.Model):
     else: tstr = 'not processed yet' 
     return tstr
   aligned_reads.allow_tags = True
-
+  
   def public(self):
     SITE_ROOT = '/'
     summary_url = self.get_absolute_url()
     return '<a href="%s">S</a>' % (summary_url,)
   public.allow_tags = True
-
+    
   @models.permalink
   def get_absolute_url(self):
     return ('htsworkflow.frontend.samples.views.library_to_flowcells', [str(self.id)])
 
+  def get_admin_url(self):
+      return urlresolvers.reverse('admin:samples_library_change',
+                                  args=(self.id,))
+
 class HTSUser(User):
     """
     Provide some site-specific customization for the django user class