2 Extract information about the IPAR run
4 IPAR - class holding the properties we found
5 IPAR - IPAR factory function initalized from a directory name
6 fromxml - IPAR factory function initalized from an xml dump from
17 from htsworkflow.pipelines.runfolder import \
23 def __init__(self, tree):
24 self.tree = tree.find("TileSelection")
28 for c in self.tree.getchildren():
29 k = c.attrib.get('Index', None)
36 for lane in self.tree.getchildren():
38 for child in lane.getchildren():
39 if child.tag == "Sample":
40 attributes['Sample'] = child.text
41 elif child.tag == 'TileRange':
42 attributes['TileRange'] = (int(child.attrib['Min']),int(child.attrib['Max']))
43 value_list.append(attributes)
47 return zip(self.keys(), self.values())
49 def __getitem__(self, key):
50 # FIXME: this is inefficient. building the dictionary be rescanning the xml.
51 v = dict(self.items())
59 TIMESTAMP = 'timestamp'
63 def __init__(self, xml=None):
65 self.date = datetime.datetime.today()
68 self.set_elements(xml)
71 return time.mktime(self.date.timetuple())
72 def _set_time(self, value):
73 mtime_tuple = time.localtime(value)
74 self.date = datetime.datetime(*(mtime_tuple[0:7]))
75 time = property(_get_time, _set_time,
76 doc='run time as seconds since epoch')
78 def _get_cycles(self):
81 cycles = self.tree.find("Cycles")
90 cycles = self._get_cycles()
91 if cycles is not None:
92 return int(cycles['First'])
95 start = property(_get_start, doc="get cycle start")
101 cycles = self._get_cycles()
102 if cycles is not None:
103 return int(cycles['Last'])
106 stop = property(_get_stop, doc="get cycle stop")
108 def _get_tiles(self):
109 if self._tiles is None:
110 self._tiles = Tiles(self.tree)
112 tiles = property(_get_tiles)
114 def _get_version(self):
115 software = self.tree.find('Software')
116 if software is not None:
117 return software.attrib['Version']
118 version = property(_get_version, "IPAR software version")
123 Generate list of all files that should be generated by the IPAR unit
125 suffix_node = self.tree.find('RunParameters/CompressionSuffix')
126 if suffix_node is None:
127 print "find compression suffix failed"
129 suffix = suffix_node.text
131 format = "%s_%s_%04d_%s.txt%s"
132 for lane, attrib in self.tiles.items():
133 for file_type in ["int","nse"]:
134 start, stop = attrib['TileRange']
135 for tile in range(start, stop+1):
136 files.append(format % (attrib['Sample'], lane, tile, file_type, suffix))
140 print "Matrix:", self.matrix
141 print "Tree:", self.tree
143 def get_elements(self):
144 attribs = {'version': str(IPAR.XML_VERSION) }
145 root = ElementTree.Element(IPAR.IPAR, attrib=attribs)
146 timestamp = ElementTree.SubElement(root, IPAR.TIMESTAMP)
147 timestamp.text = str(int(self.time))
148 root.append(self.tree)
149 matrix = ElementTree.SubElement(root, IPAR.MATRIX)
150 matrix.text = self.matrix
153 def set_elements(self, tree):
154 if tree.tag != IPAR.IPAR:
155 raise ValueError('Expected "IPAR" SubElements')
156 xml_version = int(tree.attrib.get('version', 0))
157 if xml_version > IPAR.XML_VERSION:
158 logging.warn('IPAR XML tree is a higher version than this class')
159 for element in list(tree):
160 if element.tag == IPAR.RUN:
162 elif element.tag == IPAR.TIMESTAMP:
163 self.time = int(element.text)
164 elif element.tag == IPAR.MATRIX:
165 self.matrix = element.text
167 raise ValueError("Unrecognized tag: %s" % (element.tag,))
169 def load_ipar_param_tree(paramfile):
171 look for a .param file and load it if it is an IPAR tree
174 tree = ElementTree.parse(paramfile).getroot()
175 run = tree.find('Run')
176 if run.attrib.has_key('Name') and run.attrib['Name'].startswith("IPAR"):
183 Examine the directory at pathname and initalize a IPAR object
185 logging.info("Searching IPAR directory")
188 # parse firecrest directory name
189 path, name = os.path.split(pathname)
190 groups = name.split('_')
191 if groups[0] != 'IPAR':
192 raise ValueError('ipar can only process IPAR directories')
194 # contents of the matrix file?
195 matrix_pathname = os.path.join(pathname, 'Matrix', 's_matrix.txt')
196 if not os.path.exists(matrix_pathname):
198 i.matrix = open(matrix_pathname, 'r').read()
200 # look for parameter xml file
201 paramfile = os.path.join(path, '.params')
202 if os.path.exists(paramfile):
203 i.tree = load_ipar_param_tree(paramfile)
204 mtime_local = os.stat(paramfile)[stat.ST_MTIME]
210 Initialize a IPAR object from an element tree node
216 if __name__ == "__main__":
217 i = ipar(os.path.expanduser('~/gec/081021_HWI-EAS229_0063_30HKUAAXX/Data/IPAR_1.01'))
226 print j.tiles.items()