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"
22 from htsworkflow.pipelines.runfolder import \
27 SOFTWARE_NAMES = ('IPAR_1.01', 'IPAR_1.3', 'Intensities')
30 def __init__(self, tree):
31 self.tree = tree.find("TileSelection")
35 for c in self.tree.getchildren():
36 k = c.attrib.get('Index', None)
43 for lane in self.tree.getchildren():
45 for child in lane.getchildren():
46 if child.tag == "Sample":
47 attributes['Sample'] = child.text
48 elif child.tag == 'TileRange':
49 attributes['TileRange'] = (int(child.attrib['Min']),int(child.attrib['Max']))
50 value_list.append(attributes)
54 return zip(self.keys(), self.values())
56 def __getitem__(self, key):
57 # FIXME: this is inefficient. building the dictionary be rescanning the xml.
58 v = dict(self.items())
66 TIMESTAMP = 'timestamp'
70 def __init__(self, xml=None):
72 self.date = datetime.datetime.today()
75 self.set_elements(xml)
78 return time.mktime(self.date.timetuple())
79 def _set_time(self, value):
80 mtime_tuple = time.localtime(value)
81 self.date = datetime.datetime(*(mtime_tuple[0:7]))
82 time = property(_get_time, _set_time,
83 doc='run time as seconds since epoch')
85 def _get_cycles(self):
87 raise RuntimeError("get cycles called before xml tree initalized")
88 cycles = self.tree.find("Cycles")
89 assert cycles is not None
98 cycles = self._get_cycles()
99 if cycles is not None:
100 return int(cycles['First'])
103 start = property(_get_start, doc="get cycle start")
109 cycles = self._get_cycles()
110 if cycles is not None:
111 return int(cycles['Last'])
114 stop = property(_get_stop, doc="get cycle stop")
116 def _get_tiles(self):
117 if self._tiles is None:
118 self._tiles = Tiles(self.tree)
120 tiles = property(_get_tiles)
122 def _get_version(self):
123 software = self.tree.find('Software')
124 if software is not None:
125 return software.attrib['Version']
126 version = property(_get_version, "IPAR software version")
131 Generate list of all files that should be generated by the IPAR unit
133 suffix_node = self.tree.find('RunParameters/CompressionSuffix')
134 if suffix_node is None:
135 print "find compression suffix failed"
137 suffix = suffix_node.text
139 format = "%s_%s_%04d_%s.txt%s"
140 for lane, attrib in self.tiles.items():
141 for file_type in ["int","nse"]:
142 start, stop = attrib['TileRange']
143 for tile in range(start, stop+1):
144 files.append(format % (attrib['Sample'], lane, tile, file_type, suffix))
148 print "Matrix:", self.matrix
149 print "Tree:", self.tree
151 def get_elements(self):
152 attribs = {'version': str(IPAR.XML_VERSION) }
153 root = ElementTree.Element(IPAR.IPAR, attrib=attribs)
154 timestamp = ElementTree.SubElement(root, IPAR.TIMESTAMP)
155 timestamp.text = str(int(self.time))
156 root.append(self.tree)
157 matrix = ElementTree.SubElement(root, IPAR.MATRIX)
158 matrix.text = self.matrix
161 def set_elements(self, tree):
162 if tree.tag != IPAR.IPAR:
163 raise ValueError('Expected "IPAR" SubElements')
164 xml_version = int(tree.attrib.get('version', 0))
165 if xml_version > IPAR.XML_VERSION:
166 logging.warn('IPAR XML tree is a higher version than this class')
167 for element in list(tree):
168 if element.tag == IPAR.RUN:
170 elif element.tag == IPAR.TIMESTAMP:
171 self.time = int(element.text)
172 elif element.tag == IPAR.MATRIX:
173 self.matrix = element.text
175 raise ValueError("Unrecognized tag: %s" % (element.tag,))
177 def load_ipar_param_tree(paramfile):
179 look for a .param file and load it if it is an IPAR tree
182 tree = ElementTree.parse(paramfile).getroot()
183 run = tree.find('Run')
184 if run.attrib.has_key('Name') and run.attrib['Name'] in SOFTWARE_NAMES:
187 logging.info("No run found")
192 Examine the directory at pathname and initalize a IPAR object
194 logging.info("Searching IPAR directory %s" % (pathname,))
196 i.pathname = pathname
198 # parse firecrest directory name
199 path, name = os.path.split(pathname)
200 groups = name.split('_')
201 if not (groups[0] == 'IPAR' or groups[0] == 'Intensities'):
202 raise ValueError('ipar can only process IPAR directories')
204 bustard_pattern = os.path.join(pathname, 'Bustard*')
205 # contents of the matrix file?
206 matrix_pathname = os.path.join(pathname, 'Matrix', 's_matrix.txt')
207 if os.path.exists(matrix_pathname):
209 i.matrix = open(matrix_pathname, 'r').read()
210 elif glob(bustard_pattern) > 0:
214 # look for parameter xml file
215 paramfiles = [os.path.join(pathname, 'config.xml'),
216 os.path.join(path, '.params')]
217 for paramfile in paramfiles:
218 if os.path.exists(paramfile):
219 logging.info("Found IPAR Config file at: %s" % ( paramfile, ))
220 i.tree = load_ipar_param_tree(paramfile)
221 mtime_local = os.stat(paramfile)[stat.ST_MTIME]
229 Initialize a IPAR object from an element tree node
235 #if __name__ == "__main__":
236 #i = ipar(os.path.expanduser('~/gec/081021_HWI-EAS229_0063_30HKUAAXX/Data/IPAR_1.01'))
237 #x = i.get_elements()
243 #print i.tiles.keys()
244 #print j.tiles.keys()
245 #print j.tiles.items()