1 from htswfrontend import settings
2 from django.http import HttpResponse
3 from datetime import datetime
6 from xml.sax import make_parser
7 from xml.sax.handler import ContentHandler
13 Example library node from LibraryInfo.xml:
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"/>
20 class LibInfoHandler(ContentHandler):
21 def __init__ (self, searchTerm):
22 self.searchTerm= searchTerm
24 self.LanesCount, self.ReadsCount = 0, 0
27 def startElement(self, name, attrs):
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',""))
35 self.Msg = 'failed parsing xml file'
38 #def characters (self, ch):
41 #def endElement(self, name):
44 class LibInfoHandlerByFlowCell(ContentHandler):
45 def __init__ (self, searchTerm):
46 self.searchTerm = searchTerm
47 self.LanesCount, self.ReadsCount = 0, 0
50 def startElement(self, name, attrs):
52 if name == 'Track' and attrs.get('Flowcell',"") == self.searchTerm:
53 self.LanesCount += len(attrs.get('Lane',""))
54 self.ReadsCount += int(attrs.get('Count',""))
56 self.Msg = 'failed parsing xml file'
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
62 def getLibReads(search_term,search_by):
63 searchTerm= search_term
64 parser = make_parser()
66 if search_by == 'ByLib':
67 curHandler = LibInfoHandler(searchTerm)
68 elif search_by == 'ByFC':
69 curHandler = LibInfoHandlerByFlowCell(searchTerm)
71 parser.setContentHandler(curHandler)
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'))
77 arRes.append(curHandler.LanesCount)
78 arRes.append(curHandler.ReadsCount)
79 arRes.append(curHandler.Msg)
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()
91 def refreshLibInfoFile(request):
92 varStatus = 'getting conf file from exp trac server'
93 url = settings.TASKS_PROJS_SERVER+'/LibraryInfo.xml'
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')
118 varStatus = 'OK. LibraryInfo.xml refreshed at Web server.'
120 varStatus = 'Failed reading valid LibraryInfo.xml server reply:\n'+readw
121 return HttpResponse(varStatus)