Initial port to python3
[htsworkflow.git] / htsworkflow / pipelines / firecrest.py
index ee6fded6371c45b02c10c40d3c16df76923a52a1..d8fa8721a6aad2237e1578f019ae41fafcba7a08 100644 (file)
@@ -1,23 +1,32 @@
 """
 Extract information about the Firecrest run
 
-Firecrest - class holding the properties we found
-firecrest - Firecrest factory function initalized from a directory name
-fromxml - Firecrest factory function initalized from an xml dump from
-          the Firecrest object.
+Firecrest
+  class holding the properties we found
+firecrest
+  Firecrest factory function initalized from a directory name
+fromxml
+  Firecrest factory function initalized from an xml dump from
+  the Firecrest object.
 """
 
 from datetime import date
+from glob import glob
+import logging
 import os
 import re
 import time
 
-from htsworkflow.pipelines.runfolder import \
+from htsworkflow.pipelines import \
    ElementTree, \
    VERSION_RE, \
    EUROPEAN_STRPTIME
 
+LOGGER = logging.getLogger(__name__)
+
 class Firecrest(object):
+    """Gather information about older firecrest runs
+    """
     XML_VERSION=1
 
     # xml tag names
@@ -30,6 +39,12 @@ class Firecrest(object):
     MATRIX = 'matrix'
 
     def __init__(self, xml=None):
+        """Initialize a Firecrest object
+        
+        consider using factory :function:firecrest
+        
+        :param xml: xml serialzation element to initialze from [optional]
+        """
         self.start = None
         self.stop = None
         self.version = None
@@ -39,19 +54,27 @@ class Firecrest(object):
 
         if xml is not None:
             self.set_elements(xml)
-        
+
+    def _get_software(self):
+        return "Firecrest"
+    software = property(_get_software)
+
     def _get_time(self):
         return time.mktime(self.date.timetuple())
     time = property(_get_time, doc='return run time as seconds since epoch')
 
     def dump(self):
-        print "Starting cycle:", self.start
-        print "Ending cycle:", self.stop
-        print "Firecrest version:", self.version
-        print "Run date:", self.date
-        print "user:", self.user
+        """Report debugginf information
+        """
+        print("Starting cycle:", self.start)
+        print("Ending cycle:", self.stop)
+        print("Firecrest version:", self.version)
+        print("Run date:", self.date)
+        print("user:", self.user)
 
     def get_elements(self):
+        """Return XML serialization structure.
+        """
         attribs = {'version': str(Firecrest.XML_VERSION) }
         root = ElementTree.Element(Firecrest.FIRECREST, attrib=attribs)
         version = ElementTree.SubElement(root, Firecrest.SOFTWARE_VERSION)
@@ -64,8 +87,9 @@ class Firecrest(object):
         run_date.text = str(self.time)
         user = ElementTree.SubElement(root, Firecrest.USER)
         user.text = self.user
-        matrix = ElementTree.SubElement(root, Firecrest.MATRIX)
-        matrix.text = self.matrix
+        if self.matrix is not None:
+            matrix = ElementTree.SubElement(root, Firecrest.MATRIX)
+            matrix.text = self.matrix
         return root
 
     def set_elements(self, tree):
@@ -73,7 +97,7 @@ class Firecrest(object):
             raise ValueError('Expected "Firecrest" SubElements')
         xml_version = int(tree.attrib.get('version', 0))
         if xml_version > Firecrest.XML_VERSION:
-            logging.warn('Firecrest XML tree is a higher version than this class')
+            LOGGER.warn('Firecrest XML tree is a higher version than this class')
         for element in list(tree):
             if element.tag == Firecrest.SOFTWARE_VERSION:
                 self.version = element.text
@@ -95,6 +119,7 @@ def firecrest(pathname):
     Examine the directory at pathname and initalize a Firecrest object
     """
     f = Firecrest()
+    f.pathname = pathname
 
     # parse firecrest directory name
     path, name = os.path.split(pathname)
@@ -112,10 +137,19 @@ def firecrest(pathname):
     # username
     f.user = groups[3]
 
-    # should I parse this deeper than just stashing the 
+    bustard_pattern = os.path.join(pathname, 'Bustard*')
+    # should I parse this deeper than just stashing the
     # contents of the matrix file?
     matrix_pathname = os.path.join(pathname, 'Matrix', 's_matrix.txt')
-    f.matrix = open(matrix_pathname, 'r').read()
+    if os.path.exists(matrix_pathname):
+        # this is for firecrest < 1.3.2
+        f.matrix = open(matrix_pathname, 'r').read()
+    elif glob(bustard_pattern) > 0:
+        f.matrix = None
+        # there are runs here. Bustard should save the matrix.
+    else:
+        return None
+
     return f
 
 def fromxml(tree):