added parse LibInfo by FlowcellId and count reads. This allows us to report data...
[htsworkflow.git] / htswfrontend / htswfrontend / htsw_reports / libinfopar.py
1 from htswfrontend import settings
2 from django.http import HttpResponse
3 from datetime import datetime
4 from string import *
5 import re
6 from xml.sax import make_parser
7 from xml.sax.handler import ContentHandler
8 import urllib
9 import urllib2
10 import os
11
12 '''
13 Example library node from LibraryInfo.xml:
14 <Library Name="SL14">
15 <Track Flowcell="FC10135" Lane="4" Filename="071005_FC10135_s4_FoxP2_polyclonal_pfsk1_SL14.align_25.hg18.txt" Count=" 2438679" Complexity="4.51989e-06"/>
16 <Track Flowcell="FC11977" Lane="6" Filename="070928_FC11977_s6_FoxP2_polyclonal_pfsk1_SL14.align_25.hg18.txt" Count=" 2007880" Complexity="0"/>
17 <Track Flowcell="FC13593" Lane="5" Filename="071002_FC13593_s5_FoxP2_polyclonal_pfsk1_SL14.align_25.hg18.txt" Count=" 2533720" Complexity="1.97771e-06"/>
18 </Library>
19 '''
20 class LibInfoHandler(ContentHandler):
21   def __init__ (self, searchTerm):
22     self.searchTerm= searchTerm
23     self.currlibid = ''
24     self.LanesCount, self.ReadsCount = 0, 0
25     self.Msg = 'OK'
26        
27   def startElement(self, name, attrs):
28     try:
29       if name == 'Library':     
30         self.currlibid = attrs.get('Name',"")      
31       elif name == 'Track' and self.searchTerm == self.currlibid:
32         self.LanesCount += len(attrs.get('Lane',""))
33         self.ReadsCount += int(attrs.get('Count',""))
34     except: 
35       self.Msg = 'failed parsing xml file'
36     return
37
38   #def characters (self, ch):
39     # return ..
40
41   #def endElement(self, name):
42     # return ..
43
44 class LibInfoHandlerByFlowCell(ContentHandler):
45   def __init__ (self, searchTerm):
46     self.searchTerm = searchTerm
47     self.LanesCount, self.ReadsCount = 0, 0
48     self.Msg = 'OK'
49
50   def startElement(self, name, attrs):
51     try:
52       if name == 'Track' and attrs.get('Flowcell',"") == self.searchTerm:
53         self.LanesCount += len(attrs.get('Lane',""))
54         self.ReadsCount += int(attrs.get('Count',""))
55     except:
56       self.Msg = 'failed parsing xml file'
57     return
58
59
60 ## 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
61
62 def getLibReads(search_term,search_by):
63   searchTerm= search_term
64   parser = make_parser() 
65
66   if search_by == 'ByLib':  
67     curHandler = LibInfoHandler(searchTerm)
68   elif search_by == 'ByFC':
69     curHandler = LibInfoHandlerByFlowCell(searchTerm)
70
71   parser.setContentHandler(curHandler)
72
73   folder_loc = '/htsworkflow/htswfrontend/htswfrontend'  # DEV
74   #folder_loc = '/Library/WebServer/gaworkflow/gaworkflow/frontend'  # PROD
75   parser.parse(open(folder_loc+'/htsw_reports/LibInfo/LibraryInfo.xml'))
76   arRes = []
77   arRes.append(curHandler.LanesCount) 
78   arRes.append(curHandler.ReadsCount)
79   arRes.append(curHandler.Msg)
80
81   return arRes
82
83 def getWebPage(url,params):
84   pdata = urllib.urlencode(params)
85   req = urllib2.Request(url,pdata)
86   wpage = urllib2.urlopen(req)
87   restext = wpage.read()
88   wpage.close()
89   return restext
90
91 def refreshLibInfoFile(request): 
92  varStatus = 'getting conf file from exp trac server'
93  url = settings.TASKS_PROJS_SERVER+'/LibraryInfo.xml'
94  params = {}
95  readw = getWebPage(url,params)
96  # make sure file content starts as xml
97  match_str = re.compile('^<\?xml.+')
98  if match_str.search(readw): ##tempstr):
99    # Rename current file with timestamp
100    year = datetime.today().year.__str__()
101    year = replace(year,'20','')
102    month = datetime.today().month
103    if month < 10: month = "0"+month.__str__()
104    else: month = month.__str__()
105    day = datetime.today().day
106    if day < 10: day = "0"+day.__str__()
107    else: day = day.__str__()
108    mydate = year+month+day
109    folder_loc = '/htsworkflow/htswfrontend/htswfrontend'  # DEV                                                                                                                          
110    #folder_loc = '/Library/WebServer/gaworkflow/gaworkflow/frontend'  # PROD
111    folder = folder_loc+'/htsw_reports/LibInfo/'
112    os.rename(folder+'LibraryInfo.xml',folder+mydate+'_LibraryInfo.xml')
113    # create file in curret folder
114    file_path = os.path.join(folder,'LibraryInfo.xml')
115    f = open(file_path, 'w')
116    f.write(readw)
117    f.close()
118    varStatus = 'OK. LibraryInfo.xml refreshed at Web server.'
119  else:
120    varStatus = 'Failed reading valid LibraryInfo.xml server reply:\n'+readw
121  return HttpResponse(varStatus)