2 Extract information about the Firecrest run
5 class holding the properties we found
7 Firecrest factory function initalized from a directory name
9 Firecrest factory function initalized from an xml dump from
13 from datetime import date
19 from htsworkflow.pipelines.runfolder import \
24 __docformat__ = "restructuredtext en"
26 class Firecrest(object):
30 FIRECREST = 'Firecrest'
31 SOFTWARE_VERSION = 'version'
38 def __init__(self, xml=None):
42 self.date = date.today()
47 self.set_elements(xml)
50 return time.mktime(self.date.timetuple())
51 time = property(_get_time, doc='return run time as seconds since epoch')
54 print "Starting cycle:", self.start
55 print "Ending cycle:", self.stop
56 print "Firecrest version:", self.version
57 print "Run date:", self.date
58 print "user:", self.user
60 def get_elements(self):
61 attribs = {'version': str(Firecrest.XML_VERSION) }
62 root = ElementTree.Element(Firecrest.FIRECREST, attrib=attribs)
63 version = ElementTree.SubElement(root, Firecrest.SOFTWARE_VERSION)
64 version.text = self.version
65 start_cycle = ElementTree.SubElement(root, Firecrest.START)
66 start_cycle.text = str(self.start)
67 stop_cycle = ElementTree.SubElement(root, Firecrest.STOP)
68 stop_cycle.text = str(self.stop)
69 run_date = ElementTree.SubElement(root, Firecrest.DATE)
70 run_date.text = str(self.time)
71 user = ElementTree.SubElement(root, Firecrest.USER)
73 matrix = ElementTree.SubElement(root, Firecrest.MATRIX)
74 matrix.text = self.matrix
77 def set_elements(self, tree):
78 if tree.tag != Firecrest.FIRECREST:
79 raise ValueError('Expected "Firecrest" SubElements')
80 xml_version = int(tree.attrib.get('version', 0))
81 if xml_version > Firecrest.XML_VERSION:
82 logging.warn('Firecrest XML tree is a higher version than this class')
83 for element in list(tree):
84 if element.tag == Firecrest.SOFTWARE_VERSION:
85 self.version = element.text
86 elif element.tag == Firecrest.START:
87 self.start = int(element.text)
88 elif element.tag == Firecrest.STOP:
89 self.stop = int(element.text)
90 elif element.tag == Firecrest.DATE:
91 self.date = date.fromtimestamp(float(element.text))
92 elif element.tag == Firecrest.USER:
93 self.user = element.text
94 elif element.tag == Firecrest.MATRIX:
95 self.matrix = element.text
97 raise ValueError("Unrecognized tag: %s" % (element.tag,))
99 def firecrest(pathname):
101 Examine the directory at pathname and initalize a Firecrest object
105 # parse firecrest directory name
106 path, name = os.path.split(pathname)
107 groups = name.split('_')
108 # grab the start/stop cycle information
109 cycle = re.match("C([0-9]+)-([0-9]+)", groups[0])
110 f.start = int(cycle.group(1))
111 f.stop = int(cycle.group(2))
113 version = re.search(VERSION_RE, groups[1])
114 f.version = (version.group(1))
116 t = time.strptime(groups[2], EUROPEAN_STRPTIME)
117 f.date = date(*t[0:3])
121 bustard_pattern = os.path.join(pathname, 'Bustard*')
122 # should I parse this deeper than just stashing the
123 # contents of the matrix file?
124 matrix_pathname = os.path.join(pathname, 'Matrix', 's_matrix.txt')
125 if os.path.exists(matrix_pathname):
126 # this is for firecrest < 1.3.2
127 f.matrix = open(matrix_pathname, 'r').read()
128 elif glob(bustard_pattern) > 0:
130 # there are runs here. Bustard should save the matrix.
138 Initialize a Firecrest object from an element tree node