Added a display column (not DB) to show the total count of alligned reads for each...
[htsworkflow.git] / htswfrontend / htswfrontend / fctracker / models.py
1 from django.db import models
2 from htswfrontend import settings
3 from htswfrontend.htsw_reports.libinfopar import *
4
5 class Primer(models.Model):
6   primer_name = models.CharField(max_length=100, db_index=True)
7   primer_seq = models.CharField(max_length=50, blank=True, null=True)
8   notes = models.TextField(blank=True)
9   def __str__(self):
10     return '%s' % (self.primer_name)
11   class Meta:
12     ordering = ["primer_name"]
13   class Admin:
14       list_display = ('primer_name','primer_seq','notes')
15       fields = (
16         (None, {
17             'fields': (('primer_name'),('primer_seq'),('notes'))
18         }),
19        )
20
21 class Antibody(models.Model):
22   antigene = models.CharField(max_length=500, db_index=True)
23   # New field Aug/20/08                                                                                                                                                            
24   # SQL to add column: alter table fctracker_antibody add column "nickname" varchar(20) NULL;
25   nickname = models.CharField(max_length=20,blank=True,null=True, db_index=True,verbose_name = 'Short Name')
26   catalog = models.CharField(max_length=50, unique=True, db_index=True)
27   antibodies = models.CharField(max_length=500, db_index=True)
28   source = models.CharField(max_length=500, blank=True, db_index=True)
29   biology = models.TextField(blank=True)
30   notes = models.TextField(blank=True)
31   def __str__(self):
32     return '%s - %s (%s)' % (self.antigene, self.antibodies, self.catalog)
33   class Meta:
34     verbose_name_plural = "antibodies"
35     ordering = ["antigene"]
36   class Admin:
37       list_display = ('antigene','nickname','antibodies','catalog','source','biology','notes')
38       list_filter = ('antibodies','source')
39       fields = (
40         (None, {
41             'fields': (('antigene','nickname','antibodies'),('catalog','source'),('biology'),('notes'))
42         }),
43        )
44
45 class Cellline(models.Model):
46   cellline_name = models.CharField(max_length=100, unique=True, db_index=True)
47   notes = models.TextField(blank=True)
48   def __str__(self):
49     return '%s' % (self.cellline_name)
50
51   class Meta:
52     ordering = ["cellline_name"]
53
54   class Admin:
55       list_display = ('cellline_name','notes')
56       fields = (
57         (None, {
58             'fields': (('cellline_name'),('notes'),)
59         }),
60        )
61
62 class Condition(models.Model):
63   condition_name = models.CharField(max_length=2000, unique=True, db_index=True)
64   notes = models.TextField(blank=True)
65   def __str__(self):
66     return '%s' % (self.condition_name)
67
68   class Meta:
69     ordering = ["condition_name"]
70
71   class Admin:
72       list_display = ('condition_name','notes')
73       fields = (
74         (None, {
75             'fields': (('condition_name'),('notes'),)
76         }),
77        )
78
79 class Species(models.Model):
80   scientific_name = models.CharField(max_length=256, unique=False, db_index=True, core=True)
81   common_name = models.CharField(max_length=256, blank=True)
82   use_genome_build = models.CharField(max_length=100, blank=False, null=False)
83
84   def __str__(self):
85     return '%s (%s)|%s' % (self.scientific_name, self.common_name, self.use_genome_build)
86   
87   class Meta:
88     verbose_name_plural = "species"
89     ordering = ["scientific_name"]
90   
91   class Admin:
92       fields = (
93         (None, {
94             'fields': (('scientific_name', 'common_name'), ('use_genome_build'))
95         }),
96       )
97
98 class Affiliation(models.Model):
99   name = models.CharField(max_length=256, db_index=True, core=True,verbose_name='Group Name')
100   contact = models.CharField(max_length=256, null=True, blank=True,verbose_name='Contact Name')  
101   email = models.EmailField(null=True,blank=True)
102   
103   def __str__(self):
104     str = self.name
105     if self.contact != '':
106       str += ' ('+self.contact+')' 
107     return str
108
109   class Meta:
110     ordering = ["name","contact"]
111     unique_together = (("name", "contact"),)
112
113   class Admin:
114       list_display = ('name','contact','email')
115       fields = (
116         (None, {
117             'fields': (('name','contact','email'))
118         }),
119       )
120
121 class Library(models.Model):
122   library_id = models.CharField(max_length=30, unique=True, db_index=True, core=True)
123   library_name = models.CharField(max_length=100, unique=True, core=True)
124   library_species = models.ForeignKey(Species, core=True)
125   cell_line = models.ForeignKey(Cellline,core=True)
126   condition = models.ForeignKey(Condition,core=True)
127   antibody = models.ForeignKey(Antibody,blank=True,null=True,core=True)
128   # New field Aug/25/08. SQL: alter table fctracker_library add column "lib_affiliation" varchar(256)  NULL;
129   affiliations = models.ManyToManyField(Affiliation,related_name='library_affiliations',null=True,filter_interface=models.HORIZONTAL)
130   # New field Aug/19/08
131   # SQL to add column: alter table fctracker_library add column "replicate" smallint unsigned NULL;
132   REPLICATE_NUM = ((1,1),(2,2),(3,3),(4,4))
133   replicate =  models.PositiveSmallIntegerField(choices=REPLICATE_NUM,default=1) 
134
135   EXPERIMENT_TYPES = (
136       ('INPUT_RXLCh','INPUT_RXLCh'),
137       ('ChIP-seq', 'ChIP-seq'),
138       ('Sheared', 'Sheared'),
139       ('RNA-seq', 'RNA-seq'),
140       ('Methyl-seq', 'Methyl-seq'),
141       ('DIP-seq', 'DIP-seq'),
142     ) 
143   experiment_type = models.CharField(max_length=50, choices=EXPERIMENT_TYPES, default='ChIP-seq')
144
145   creation_date = models.DateField(blank=True, null=True)
146   made_for = models.CharField(max_length=50, blank=True,verbose_name = 'ChIP/DNA/RNA Made By')
147   made_by = models.CharField(max_length=50, blank=True,verbose_name = 'Library Made By')
148   
149   PROTOCOL_END_POINTS = (
150       ('Completed','Completed'),
151       ('?', 'Unknown'),
152       ('Sample', 'Raw sample'),
153       ('Progress', 'In progress'),
154       ('1A', 'Ligation, then gel'),
155       ('PCR', 'Ligation, then PCR'),
156       ('1Ab', 'Ligation, PCR, then gel'),
157       ('1Aa', 'Ligation, gel, then PCR'),
158       ('2A', 'Ligation, PCR, gel, PCR'),
159     )
160   stopping_point = models.CharField(max_length=50, choices=PROTOCOL_END_POINTS, default='Completed')
161   amplified_from_sample = models.ForeignKey('self', blank=True, null=True)
162   
163   undiluted_concentration = models.DecimalField("Template concentr. (ng/ul)",max_digits=5, decimal_places=2, blank=True, null=True)
164   ten_nM_dilution = models.BooleanField()
165   successful_pM = models.DecimalField(max_digits=5, decimal_places=2,blank=True, null=True)
166   avg_lib_size = models.IntegerField(default=225, blank=True, null=True)
167   notes = models.TextField(blank=True)
168   
169   def __str__(self):
170     return '%s: %s' % (self.library_id, self.library_name)
171   
172   class Meta:
173     verbose_name_plural = "libraries"
174     ordering = ["-creation_date"] #["-library_id"]
175   
176   def antibody_name(self):
177     return self.antibody.nickname
178
179   def org(self):
180     return self.library_species.common_name
181
182   def affiliation(self):
183     affs = self.affiliations.all().order_by('name')
184     tstr = ''
185     ar = []
186     for t in affs:
187         ar.append(t.__str__())
188     return '%s' % (", ".join(ar))
189
190
191   def aligned_reads(self):
192     res = getLibReads(self.library_id)
193     rc = "%1.2f" % (res[1]/1000000.0)
194     # Color Scheme: green is more than 10M, blue is more than 5M, orange is more than 3M and red is less. For RNAseq, all those thresholds should be doubled
195     if res[0] > 0:
196       bgcolor = '#ff3300'  # Red
197       rc_thr = [10000000,5000000,3000000]
198       if self.experiment_type == 'RNA-seq':
199         rc_thr = [20000000,10000000,6000000]
200
201       if res[1] > rc_thr[0]:
202         bgcolor = '#66ff66'  # Green
203       else:
204         if res[1] > rc_thr[1]:
205           bgcolor ='#00ccff'  # Blue
206         else:
207            if res[1] > rc_thr[2]: 
208              bgcolor ='#ffcc33'  # Orange
209       tstr = '<div style="background-color:'+bgcolor+';color:black">'
210       tstr += res[0].__str__()+' Lanes, '+rc+' M Reads'
211       tstr += '</div>'
212     else: tstr = 'not processed yet' 
213     return tstr
214   aligned_reads.allow_tags = True
215
216   class Admin:
217     date_hierarchy = "creation_date"
218     save_as = True
219     save_on_top = True
220     ##search_fields = ['library_id','library_name','affiliations__name','affiliations__contact','made_by','made_for','antibody__antigene','antibody__catalog','antibody__antibodies','antibody__source','cell_line__cellline_name','library_species__scientific_name','library_species__common_name','library_species__use_genome_build']
221     search_fields = ['library_id','library_name','cell_line__cellline_name','library_species__scientific_name','library_species__common_name','library_species__use_genome_build']
222     list_display = ('affiliation','library_id','aligned_reads','library_name','experiment_type','org','replicate','antibody_name','cell_line','made_by','creation_date')
223     list_display_links = ('library_id', 'library_name')
224     list_filter = ('experiment_type','affiliations','library_species','made_for', 'made_by','replicate')
225     fields = (
226         (None, {
227             'fields': (('replicate','library_id','library_name'),('library_species'),('experiment_type'),('cell_line','condition','antibody'),)
228         }),
229         ('Creation Information:', {
230             'fields' : (('made_for', 'made_by', 'creation_date'), ('stopping_point', 'amplified_from_sample'), ('avg_lib_size','undiluted_concentration', 'ten_nM_dilution', 'successful_pM'), 'notes',)
231         }),
232         ('Library/Project Affiliation:', {
233             'fields' : (('affiliations'),)
234         }),
235         )