1 from django.db import models
2 from django.db.models import Q
3 from django.core.exceptions import ObjectDoesNotExist
4 from datetime import datetime
5 from gaworkflow.frontend.fctracker.models import *
6 from gaworkflow.frontend.analys_track.models import *
7 from gaworkflow.frontend.exp_track.models import *
10 ##from p1 import LibInfo
11 from libinfopar import *
13 ## This is a table based REPORT generator. The goal is to display a Progress Report for all the ENCODE projects, based on Study Name (e.g. NRSF, FOXP2, Methy-Seq on .. etc).
15 class ProgressReport(models.Model):
16 st_sbj = models.ForeignKey(Project,limit_choices_to = Q(project_name__startswith='ENCODE '),related_name='project',db_index=True,verbose_name="Studied Subject")
17 interactome_complete = models.BooleanField(default=False)
20 str = self.st_sbj.__str__()
22 str += '<a title="open Project record" href="/admin/analys_track/project/'+self.st_sbj.id.__str__()+'/" target=_self style="font-size:85%">Edit Project</a>'
24 Study.allow_tags = True
26 def submit_to_DCC(self):
28 if self.note_about_DCC:
29 varText += '<br/><u>Note:</u><br/>'+self.note_about_DCC
30 return '%s<br/>%s' % (self.submitted_to_DCC,varText)
31 submit_to_DCC.allow_tags = True
33 def submit_to_NCBI(self):
35 if self.note_about_NCBI:
36 varText += '<br/><u>Note:</u><br/>'+self.note_about_NCBI
37 return '%s<br/>%s' % (self.submitted_to_NCBI,varText)
38 submit_to_NCBI.allow_tags = True
40 #REPS = ((1,1),(2,2),(3,3))
41 #replicate = models.PositiveSmallIntegerField(choices=REPS,verbose_name='Replicate Number')
43 ## -- Utility functions
45 """Return a list of the elements in s, but without duplicates.
46 For example, unique([1,2,3,1,2,3]) is some permutation of [1,2,3],
47 unique("abcabc") some permutation of ["a", "b", "c"], and
48 unique(([1, 2], [2, 3], [1, 2])) some permutation of
50 For best speed, all sequence elements should be hashable. Then
51 unique() will usually work in linear time.
52 If not possible, the sequence elements should enjoy a total
53 ordering, and if list(s).sort() doesn't raise TypeError it's
54 assumed that they do enjoy a total ordering. Then unique() will
55 usually work in O(N*log2(N)) time.
56 If that's not possible either, the sequence elements must support
57 equality-testing. Then unique() will usually work in quadratic
65 # Try using a dict first, as that's the fastest and will usually
66 # work. If it doesn't work, it will usually fail quickly, so it
67 # usually doesn't cost much to *try* it. It requires that all the
68 # sequence elements be hashable, and support equality comparison.
74 del u # move on to the next method
78 # We can't hash all the elements. Second fastest is to sort,
79 # which brings the equal elements together; then duplicates are
80 # easy to weed out in a single pass.
81 # NOTE: Python's list.sort() was designed to be efficient in the
82 # presence of many duplicate elements. This isn't true of all
83 # sort functions in all languages or libraries, so this approach
84 # is more effective in Python than it may be elsewhere.
89 del t # move on to the next method
96 t[lasti] = last = t[i]
101 # Brute force is all that's left.
109 ## --- LIBARAY PREPARATION SECTION
111 ptasks = self.st_sbj.tasks.distinct()
114 if t.subject1 is not None:
115 arLibs.append(t.subject1.library_id)
116 if t.subject2 is not None:
117 arLibs.append(t.subject2.library_id)
118 arLibs = self.unique(arLibs)
119 return arLibs #.sort()
121 def getFCInfo(self,libid): ## This is the haviest function
123 ##Test return arFCLanes
124 # can't get this to work: FC_L1 = FlowCell.objects.filter(lane_5_library__exact=libid)
125 allFCs = FlowCell.objects.all()
130 # for i in range(1,9):
131 # if eval('f.lane_'+i.__str__()+'_library.library_id==libid'):
132 # lanes.append(i.__str__())
135 # maybe a bit faster this way:
136 if f.lane_1_library.library_id==libid:
139 if f.lane_2_library.library_id==libid:
142 if f.lane_3_library.library_id==libid:
145 if f.lane_4_library.library_id==libid:
148 if f.lane_5_library.library_id==libid:
151 if f.lane_6_library.library_id==libid:
154 if f.lane_7_library.library_id==libid:
157 if f.lane_8_library.library_id==libid:
164 rundate = re.sub(pattern="\s.*$",repl="",string=f.run_date.__str__())
165 entry = '<b>'+f.flowcell_id + '</b> Lanes No.: '+','.join(lanes)+' ('+rundate+')'
166 arFCLanes.append(entry)
167 if len(arFCLanes)==0:
168 arFCLanes.append('<font color=red>Flowcell not found.</font>')
172 ## To have the Company's lot number, apearing on the (source) tube, we need to add new Field in Library.
173 arlibs = self.getLibIds()
174 tstr = '<ul>' ##<u><b>Ab</b> from '+len(arlibs).__str__()+' libs</u>: '
178 rec = Library.objects.get(library_id=l,antibody__isnull=False)
179 arRows.append('<li>'+rec.antibody.antibodies+' for <b>'+rec.antibody.antigene+'</b> (src:'+rec.antibody.source+', cat:'+rec.antibody.catalog+')</li>')
180 except ObjectDoesNotExist:
182 tstr += "".join(self.unique(arRows))+'</ul>'
184 ab_batch.allow_tags = True
187 arlibs = self.getLibIds()
192 rec = Library.objects.get(library_id=l)
193 arRows.append('<li><b>'+rec.cell_line.cellline_name+'</b> ('+rec.condition.condition_name+')</li>')
194 except ObjectDoesNotExist:
196 tstr += "".join(self.unique(arRows))+'</ul>'
198 cell_line.allow_tags = True
200 def cell_harvest_batch(self): # <- data now displayed in "cell_line"
202 arlibs = self.getLibIds()
207 rec = Library.objects.get(library_id=l)
208 arRows.append('<li><b>'+rec.condition.condition_name+'</b></li>')
209 except ObjectDoesNotExist:
211 tstr += "".join(self.unique(arRows))+'</ul>'
213 cell_harvest_batch.allow_tags = True
220 ## Lib Id + Date + Person
222 tstr += 'function togit(eid){'
223 tstr += 'f=document.getElementById(eid);'
224 tstr += 'if(f.style.display==\'none\'){'
225 tstr += 'f.style.display=\'block\';'
227 tstr += 'f.style.display=\'none\';'
231 arlibs = self.getLibIds() ##.sort()
233 tstr +='<a href=# onClick="togit(\'libInfo'+self.st_sbj.project_name+'\')">view /hide</a>'
234 tstr += '<div id="libInfo'+self.st_sbj.project_name+'" style="display:block;border:solid #cccccc 1px;width:200px;height:300px;overflow:auto"><ul>'
238 rec = Library.objects.get(library_id=l)
239 arRows.append('<li><b>'+rec.library_id+'</b>: '+rec.library_name+'.<br/>Made By: '+rec.made_by+', On: '+ rec.creation_date.__str__()+'</li>')
240 except ObjectDoesNotExist:
242 tstr += "".join(self.unique(arRows))+'</ul></div>'
244 library.allow_tags = True
247 ## -- SEQUENCING SECTION
248 def sequencing(self):
249 ## FCId + Lane + Date
250 arlibs = self.getLibIds()
251 tstr ='<a href=# onClick="togit(\'seqInfo'+self.st_sbj.project_name+'\')">view /hide</a>'
252 tstr += '<div id="seqInfo'+self.st_sbj.project_name+'" style="display:block;border:solid #cccccc 1px;width:200px;height:300px;overflow:auto"><ul>'
254 tstr += '<li><b>'+l+'</b>:<br/>'+(' / '.join(self.getFCInfo(l)))+'</li>'
255 tstr += '</ul></div>'
257 sequencing.allow_tags = True
259 def aligned_reads(self):
261 arlibs = self.getLibIds()
262 tstr = '<a href=# onClick="togit(\'readsInfo'+self.st_sbj.project_name+'\')">view /hide</a>'
263 tstr += '<div id="readsInfo'+self.st_sbj.project_name+'" style="display:block;border:solid #cccccc 1px;width:200px;height:300px;overflow:auto">'
264 tstr += '<table><tr><td>Library Id</td><td>Total Lanes</td><td>M Reads</td></tr>'
265 LanesCnt, ReadsCnt = 0, 0
270 rc = "%1.2f" % (res[1]/1000000.0)
271 tstr += '<tr><td><b>'+l+'</b></td><td>'+res[0].__str__()+'</td></td><td>'+rc+'</td></tr>'
273 #tstr += '<a target=_blank href="http://m304-apple-server.stanford.edu/projects/'+self.st_sbj.id.__str__()+'">Project results page</a>'
275 myNum = (ReadsCnt/1000000.0)
276 myNum = "%1.2f" % (myNum)
277 tstr += '<div>Total: <b>'+LanesCnt.__str__()+'</b> lanes and <b>'+myNum+'</b> M Reads</div>'
278 tstr += '<a target=_blank href="http://m304-apple-server.stanford.edu/projects/'+self.st_sbj.id.__str__()+'">Project results page</a>'
280 aligned_reads.allow_tags = True
282 def peak_calling(self):
284 return 'coming up ..'
286 QPCR = models.CharField(max_length=500,blank=True,null=True)
287 submitted_to_DCC = models.DateTimeField(core=True,blank=True,null=True)
288 submitted_to_NCBI = models.DateTimeField(core=True,blank=True,null=True)
289 note_about_DCC = models.TextField(blank=True)
290 note_about_NCBI = models.TextField(blank=True)
293 return '"%s" - %s' % (self.st_sbj,self.interactome_complete)
296 #verbose_name_plural = "Reports"
300 list_display = ('Study','ab_batch','cell_line','library','sequencing','aligned_reads','QPCR','submit_to_DCC','submit_to_NCBI','interactome_complete')
301 ## list_filter = ('interactome_complete')