The summary parsing code now seems to handle paired end runs
[htsworkflow.git] / htsworkflow / pipelines / gerald.py
1 """
2 Provide access to information stored in the GERALD directory.
3 """
4 from datetime import datetime, date
5 import logging
6 import os
7 import time
8
9 from htsworkflow.pipelines.summary import Summary
10 from htsworkflow.pipelines.eland import eland, ELAND
11
12 from htsworkflow.pipelines.runfolder import \
13    ElementTree, \
14    EUROPEAN_STRPTIME, \
15    LANES_PER_FLOWCELL, \
16    VERSION_RE
17 from htsworkflow.util.ethelp import indent, flatten
18
19 class Gerald(object):
20     """
21     Capture meaning out of the GERALD directory
22     """
23     XML_VERSION = 1
24     GERALD='Gerald'
25     RUN_PARAMETERS='RunParameters'
26     SUMMARY='Summary'
27
28     class LaneParameters(object):
29         """
30         Make it easy to access elements of LaneSpecificRunParameters from python
31         """
32         def __init__(self, gerald, lane_id):
33             self._gerald = gerald
34             self._lane_id = lane_id
35
36         def __get_attribute(self, xml_tag):
37             subtree = self._gerald.tree.find('LaneSpecificRunParameters')
38             container = subtree.find(xml_tag)
39             if container is None:
40                 return None
41             if len(container.getchildren()) > LANES_PER_FLOWCELL:
42                 raise RuntimeError('GERALD config.xml file changed')
43             lanes = [x.tag.split('_')[1] for x in container.getchildren()]
44             index = lanes.index(self._lane_id)
45             element = container[index]
46             return element.text
47         def _get_analysis(self):
48             return self.__get_attribute('ANALYSIS')
49         analysis = property(_get_analysis)
50
51         def _get_eland_genome(self):
52             genome = self.__get_attribute('ELAND_GENOME')
53             # default to the chipwide parameters if there isn't an
54             # entry in the lane specific paramaters
55             if genome is None:
56                 subtree = self._gerald.tree.find('ChipWideRunParameters')
57                 container = subtree.find('ELAND_GENOME')
58                 genome = container.text
59             return genome
60         eland_genome = property(_get_eland_genome)
61
62         def _get_read_length(self):
63             return self.__get_attribute('READ_LENGTH')
64         read_length = property(_get_read_length)
65
66         def _get_use_bases(self):
67             return self.__get_attribute('USE_BASES')
68         use_bases = property(_get_use_bases)
69
70     class LaneSpecificRunParameters(object):
71         """
72         Provide access to LaneSpecificRunParameters
73         """
74         def __init__(self, gerald):
75             self._gerald = gerald
76             self._lane = None
77
78         def _initalize_lanes(self):
79             """
80             build dictionary of LaneParameters
81             """
82             self._lanes = {}
83             tree = self._gerald.tree
84             analysis = tree.find('LaneSpecificRunParameters/ANALYSIS')
85             # according to the pipeline specs I think their fields
86             # are sampleName_laneID, with sampleName defaulting to s
87             # since laneIDs are constant lets just try using
88             # those consistently.
89             for element in analysis:
90                 sample, lane_id = element.tag.split('_')
91                 self._lanes[int(lane_id)] = Gerald.LaneParameters(
92                                               self._gerald, lane_id)
93
94         def __getitem__(self, key):
95             if self._lane is None:
96                 self._initalize_lanes()
97             return self._lanes[key]
98         def keys(self):
99             if self._lane is None:
100                 self._initalize_lanes()
101             return self._lanes.keys()
102         def values(self):
103             if self._lane is None:
104                 self._initalize_lanes()
105             return self._lanes.values()
106         def items(self):
107             if self._lane is None:
108                 self._initalize_lanes()
109             return self._lanes.items()
110         def __len__(self):
111             if self._lane is None:
112                 self._initalize_lanes()
113             return len(self._lanes)
114
115     def __init__(self, xml=None):
116         self.pathname = None
117         self.tree = None
118
119         # parse lane parameters out of the config.xml file
120         self.lanes = Gerald.LaneSpecificRunParameters(self)
121
122         self.summary = None
123         self.eland_results = None
124
125         if xml is not None:
126             self.set_elements(xml)
127
128     def _get_date(self):
129         if self.tree is None:
130             return datetime.today()
131         timestamp = self.tree.findtext('ChipWideRunParameters/TIME_STAMP')
132         epochstamp = time.mktime(time.strptime(timestamp, '%c'))
133         return datetime.fromtimestamp(epochstamp)
134     date = property(_get_date)
135
136     def _get_time(self):
137         return time.mktime(self.date.timetuple())
138     time = property(_get_time, doc='return run time as seconds since epoch')
139
140     def _get_version(self):
141         if self.tree is None:
142             return None
143         return self.tree.findtext('ChipWideRunParameters/SOFTWARE_VERSION')
144     version = property(_get_version)
145
146     def dump(self):
147         """
148         Debugging function, report current object
149         """
150         print 'Gerald version:', self.version
151         print 'Gerald run date:', self.date
152         print 'Gerald config.xml:', self.tree
153         self.summary.dump()
154
155     def get_elements(self):
156         if self.tree is None or self.summary is None:
157             return None
158
159         gerald = ElementTree.Element(Gerald.GERALD,
160                                      {'version': unicode(Gerald.XML_VERSION)})
161         gerald.append(self.tree)
162         gerald.append(self.summary.get_elements())
163         if self.eland_results:
164             gerald.append(self.eland_results.get_elements())
165         return gerald
166
167     def set_elements(self, tree):
168         if tree.tag !=  Gerald.GERALD:
169             raise ValueError('exptected GERALD')
170         xml_version = int(tree.attrib.get('version', 0))
171         if xml_version > Gerald.XML_VERSION:
172             logging.warn('XML tree is a higher version than this class')
173         for element in list(tree):
174             tag = element.tag.lower()
175             if tag == Gerald.RUN_PARAMETERS.lower():
176                 self.tree = element
177             elif tag == Gerald.SUMMARY.lower():
178                 self.summary = Summary(xml=element)
179             elif tag == ELAND.ELAND.lower():
180                 self.eland_results = ELAND(xml=element)
181             else:
182                 logging.warn("Unrecognized tag %s" % (element.tag,))
183
184
185 def gerald(pathname):
186     g = Gerald()
187     g.pathname = pathname
188     path, name = os.path.split(pathname)
189     logging.info("Parsing gerald config.xml")
190     config_pathname = os.path.join(pathname, 'config.xml')
191     g.tree = ElementTree.parse(config_pathname).getroot()
192
193     # parse Summary.htm file
194     logging.info("Parsing Summary.htm")
195     summary_pathname = os.path.join(pathname, 'Summary.htm')
196     g.summary = Summary(summary_pathname)
197     # parse eland files
198     g.eland_results = eland(g.pathname, g)
199     return g
200
201 if __name__ == "__main__":
202   # quick test code
203   import sys
204   g = gerald(sys.argv[1])
205   #ElementTree.dump(g.get_elements())