Adding the new Reports component.
[htsworkflow.git] / gaworkflow / frontend / htsw_reports / libinfopar.py
diff --git a/gaworkflow/frontend/htsw_reports/libinfopar.py b/gaworkflow/frontend/htsw_reports/libinfopar.py
new file mode 100644 (file)
index 0000000..b54d742
--- /dev/null
@@ -0,0 +1,50 @@
+from xml.sax import make_parser
+from xml.sax.handler import ContentHandler
+
+
+'''
+Example library node from LibraryInfo.xml:
+<Library Name="SL14">
+<Track Flowcell="FC10135" Lane="4" Filename="071005_FC10135_s4_FoxP2_polyclonal_pfsk1_SL14.align_25.hg18.txt" Count=" 2438679" Complexity="4.51989e-06"/>
+<Track Flowcell="FC11977" Lane="6" Filename="070928_FC11977_s6_FoxP2_polyclonal_pfsk1_SL14.align_25.hg18.txt" Count=" 2007880" Complexity="0"/>
+<Track Flowcell="FC13593" Lane="5" Filename="071002_FC13593_s5_FoxP2_polyclonal_pfsk1_SL14.align_25.hg18.txt" Count=" 2533720" Complexity="1.97771e-06"/>
+</Library>
+'''
+class LibInfoHandler(ContentHandler):
+
+  def __init__ (self, searchTerm):
+    self.searchTerm= searchTerm
+    self.currlibid = ''
+    self.LanesCount, self.ReadsCount = 0, 0
+    self.Msg = ''
+       
+  def startElement(self, name, attrs):
+    if name == 'Library':     
+      self.currlibid = attrs.get('Name',"")      
+    elif name == 'Track' and self.searchTerm == self.currlibid:
+      self.LanesCount += len(attrs.get('Lane',""))
+      self.ReadsCount += int(attrs.get('Count',""))
+    else:
+      self.Msg += ' | name = '+name+', currlibid = '+ self.currlibid
+    return
+
+  #def characters (self, ch):
+    # return ..
+
+  #def endElement(self, name):
+    # return ..
+
+
+## TO DO: Change this to read the LibraryInfo.xml only ONCE per ReoprtRequest (do it in the models.py). + Read it directly from the analysis_server
+
+def getLibReads(libid):
+  searchTerm= libid
+  parser = make_parser()   
+  curHandler = LibInfoHandler(searchTerm)
+  parser.setContentHandler(curHandler)
+  parser.parse(open('/gaworkflow/gaworkflow/frontend/htsw_reports/LibraryInfo.xml'))
+  arRes = []
+  arRes.append(curHandler.LanesCount) 
+  arRes.append(curHandler.ReadsCount)
+  return arRes
+