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 import \
27 LOGGER = logging.getLogger(__name__)
28 SOFTWARE_NAMES = ('IPAR_1.01', 'IPAR_1.3', 'Intensities')
31 def __init__(self, tree):
32 self.tree = tree.find("TileSelection")
36 for c in self.tree.getchildren():
37 k = c.attrib.get('Index', None)
44 for lane in self.tree.getchildren():
46 for child in lane.getchildren():
47 if child.tag == "Sample":
48 attributes['Sample'] = child.text
49 elif child.tag == 'TileRange':
50 attributes['TileRange'] = (int(child.attrib['Min']),int(child.attrib['Max']))
51 value_list.append(attributes)
55 return zip(self.keys(), self.values())
57 def __getitem__(self, key):
58 # FIXME: this is inefficient. building the dictionary be rescanning the xml.
59 v = dict(self.items())
67 TIMESTAMP = 'timestamp'
71 def __init__(self, xml=None):
73 self.date = datetime.datetime.today()
76 self.set_elements(xml)
78 def _get_software(self):
79 """Return software name"""
81 raise ValueError("Can't determine software name, please load a run")
82 software = self.tree.xpath('Software')
83 if len(software) == 0:
85 elif len(software) > 1:
86 raise RuntimeError("Too many software tags, please update ipar.py")
88 return software[0].attrib['Name']
89 software = property(_get_software)
92 return time.mktime(self.date.timetuple())
93 def _set_time(self, value):
94 mtime_tuple = time.localtime(value)
95 self.date = datetime.datetime(*(mtime_tuple[0:7]))
96 time = property(_get_time, _set_time,
97 doc='run time as seconds since epoch')
99 def _get_cycles(self):
100 if self.tree is None:
101 raise RuntimeError("get cycles called before xml tree initalized")
102 cycles = self.tree.find("Cycles")
103 assert cycles is not None
108 def _get_start(self):
112 cycles = self._get_cycles()
113 if cycles is not None:
114 return int(cycles['First'])
117 start = property(_get_start, doc="get cycle start")
123 cycles = self._get_cycles()
124 if cycles is not None:
125 return int(cycles['Last'])
128 stop = property(_get_stop, doc="get cycle stop")
130 def _get_tiles(self):
131 if self._tiles is None:
132 self._tiles = Tiles(self.tree)
134 tiles = property(_get_tiles)
136 def _get_version(self):
137 software = self.tree.find('Software')
138 if software is not None:
139 return software.attrib['Version']
140 version = property(_get_version, "IPAR software version")
145 Generate list of all files that should be generated by the IPAR unit
147 suffix_node = self.tree.find('RunParameters/CompressionSuffix')
148 if suffix_node is None:
149 print "find compression suffix failed"
151 suffix = suffix_node.text
153 format = "%s_%s_%04d_%s.txt%s"
154 for lane, attrib in self.tiles.items():
155 for file_type in ["int","nse"]:
156 start, stop = attrib['TileRange']
157 for tile in range(start, stop+1):
158 files.append(format % (attrib['Sample'], lane, tile, file_type, suffix))
162 print "Matrix:", self.matrix
163 print "Tree:", self.tree
165 def get_elements(self):
166 attribs = {'version': str(IPAR.XML_VERSION) }
167 root = ElementTree.Element(IPAR.IPAR, attrib=attribs)
168 timestamp = ElementTree.SubElement(root, IPAR.TIMESTAMP)
169 timestamp.text = str(int(self.time))
170 root.append(self.tree)
171 matrix = ElementTree.SubElement(root, IPAR.MATRIX)
172 matrix.text = self.matrix
175 def set_elements(self, tree):
176 if tree.tag != IPAR.IPAR:
177 raise ValueError('Expected "IPAR" SubElements')
178 xml_version = int(tree.attrib.get('version', 0))
179 if xml_version > IPAR.XML_VERSION:
180 LOGGER.warn('IPAR XML tree is a higher version than this class')
181 for element in list(tree):
182 if element.tag == IPAR.RUN:
184 elif element.tag == IPAR.TIMESTAMP:
185 self.time = int(element.text)
186 elif element.tag == IPAR.MATRIX:
187 self.matrix = element.text
189 raise ValueError("Unrecognized tag: %s" % (element.tag,))
191 def load_ipar_param_tree(paramfile):
193 look for a .param file and load it if it is an IPAR tree
196 tree = ElementTree.parse(paramfile).getroot()
197 run = tree.find('Run')
198 if run.attrib.has_key('Name') and run.attrib['Name'] in SOFTWARE_NAMES:
201 LOGGER.info("No run found")
206 Examine the directory at pathname and initalize a IPAR object
208 LOGGER.info("Searching IPAR directory %s" % (pathname,))
210 i.pathname = pathname
212 # parse firecrest directory name
213 path, name = os.path.split(pathname)
214 groups = name.split('_')
215 if not (groups[0] == 'IPAR' or groups[0] == 'Intensities'):
216 raise ValueError('ipar can only process IPAR directories')
218 bustard_pattern = os.path.join(pathname, 'Bustard*')
219 # contents of the matrix file?
220 matrix_pathname = os.path.join(pathname, 'Matrix', 's_matrix.txt')
221 if os.path.exists(matrix_pathname):
223 i.matrix = open(matrix_pathname, 'r').read()
224 elif glob(bustard_pattern) > 0:
228 # look for parameter xml file
229 paramfiles = [os.path.join(pathname, 'RTAConfig.xml'),
230 os.path.join(pathname, 'config.xml'),
231 os.path.join(path, '.params')]
232 for paramfile in paramfiles:
233 if os.path.exists(paramfile):
234 LOGGER.info("Found IPAR Config file at: %s" % ( paramfile, ))
235 i.tree = load_ipar_param_tree(paramfile)
236 mtime_local = os.stat(paramfile)[stat.ST_MTIME]
244 Initialize a IPAR object from an element tree node
250 #if __name__ == "__main__":
251 #i = ipar(os.path.expanduser('~/gec/081021_HWI-EAS229_0063_30HKUAAXX/Data/IPAR_1.01'))
252 #x = i.get_elements()
258 #print i.tiles.keys()
259 #print j.tiles.keys()
260 #print j.tiles.items()