2 Extract information about the IPAR run
5 class holding the properties we found
7 IPAR factory function initalized from a directory name
9 IPAR factory function initalized from an xml dump from
12 __docformat__ = "restructuredtext en"
21 from htsworkflow.pipelines.runfolder import \
27 def __init__(self, tree):
28 self.tree = tree.find("TileSelection")
32 for c in self.tree.getchildren():
33 k = c.attrib.get('Index', None)
40 for lane in self.tree.getchildren():
42 for child in lane.getchildren():
43 if child.tag == "Sample":
44 attributes['Sample'] = child.text
45 elif child.tag == 'TileRange':
46 attributes['TileRange'] = (int(child.attrib['Min']),int(child.attrib['Max']))
47 value_list.append(attributes)
51 return zip(self.keys(), self.values())
53 def __getitem__(self, key):
54 # FIXME: this is inefficient. building the dictionary be rescanning the xml.
55 v = dict(self.items())
63 TIMESTAMP = 'timestamp'
67 def __init__(self, xml=None):
69 self.date = datetime.datetime.today()
72 self.set_elements(xml)
75 return time.mktime(self.date.timetuple())
76 def _set_time(self, value):
77 mtime_tuple = time.localtime(value)
78 self.date = datetime.datetime(*(mtime_tuple[0:7]))
79 time = property(_get_time, _set_time,
80 doc='run time as seconds since epoch')
82 def _get_cycles(self):
85 cycles = self.tree.find("Cycles")
94 cycles = self._get_cycles()
95 if cycles is not None:
96 return int(cycles['First'])
99 start = property(_get_start, doc="get cycle start")
105 cycles = self._get_cycles()
106 if cycles is not None:
107 return int(cycles['Last'])
110 stop = property(_get_stop, doc="get cycle stop")
112 def _get_tiles(self):
113 if self._tiles is None:
114 self._tiles = Tiles(self.tree)
116 tiles = property(_get_tiles)
118 def _get_version(self):
119 software = self.tree.find('Software')
120 if software is not None:
121 return software.attrib['Version']
122 version = property(_get_version, "IPAR software version")
127 Generate list of all files that should be generated by the IPAR unit
129 suffix_node = self.tree.find('RunParameters/CompressionSuffix')
130 if suffix_node is None:
131 print "find compression suffix failed"
133 suffix = suffix_node.text
135 format = "%s_%s_%04d_%s.txt%s"
136 for lane, attrib in self.tiles.items():
137 for file_type in ["int","nse"]:
138 start, stop = attrib['TileRange']
139 for tile in range(start, stop+1):
140 files.append(format % (attrib['Sample'], lane, tile, file_type, suffix))
144 print "Matrix:", self.matrix
145 print "Tree:", self.tree
147 def get_elements(self):
148 attribs = {'version': str(IPAR.XML_VERSION) }
149 root = ElementTree.Element(IPAR.IPAR, attrib=attribs)
150 timestamp = ElementTree.SubElement(root, IPAR.TIMESTAMP)
151 timestamp.text = str(int(self.time))
152 root.append(self.tree)
153 matrix = ElementTree.SubElement(root, IPAR.MATRIX)
154 matrix.text = self.matrix
157 def set_elements(self, tree):
158 if tree.tag != IPAR.IPAR:
159 raise ValueError('Expected "IPAR" SubElements')
160 xml_version = int(tree.attrib.get('version', 0))
161 if xml_version > IPAR.XML_VERSION:
162 logging.warn('IPAR XML tree is a higher version than this class')
163 for element in list(tree):
164 if element.tag == IPAR.RUN:
166 elif element.tag == IPAR.TIMESTAMP:
167 self.time = int(element.text)
168 elif element.tag == IPAR.MATRIX:
169 self.matrix = element.text
171 raise ValueError("Unrecognized tag: %s" % (element.tag,))
173 def load_ipar_param_tree(paramfile):
175 look for a .param file and load it if it is an IPAR tree
178 tree = ElementTree.parse(paramfile).getroot()
179 run = tree.find('Run')
180 if run.attrib.has_key('Name') and run.attrib['Name'].startswith("IPAR"):
187 Examine the directory at pathname and initalize a IPAR object
189 logging.info("Searching IPAR directory")
192 # parse firecrest directory name
193 path, name = os.path.split(pathname)
194 groups = name.split('_')
195 if groups[0] != 'IPAR':
196 raise ValueError('ipar can only process IPAR directories')
198 # contents of the matrix file?
199 matrix_pathname = os.path.join(pathname, 'Matrix', 's_matrix.txt')
200 if not os.path.exists(matrix_pathname):
202 i.matrix = open(matrix_pathname, 'r').read()
204 # look for parameter xml file
205 paramfile = os.path.join(path, '.params')
206 if os.path.exists(paramfile):
207 i.tree = load_ipar_param_tree(paramfile)
208 mtime_local = os.stat(paramfile)[stat.ST_MTIME]
214 Initialize a IPAR object from an element tree node
220 if __name__ == "__main__":
221 i = ipar(os.path.expanduser('~/gec/081021_HWI-EAS229_0063_30HKUAAXX/Data/IPAR_1.01'))
230 print j.tiles.items()