Move some common runfolder path management code into its own module
[htsworkflow.git] / htsworkflow / automation / solexa.py
1 """Utilities to help process solexa/illumina runfolders
2 """
3 import os
4 import re
5
6 def is_runfolder(name):
7     """
8     Is it a runfolder?
9
10     >>> print is_runfolder('090630_HWUSI-EAS999_0006_30LNFAAXX')
11     True
12     >>> print is_runfolder('hello')
13     False
14     """
15     if re.match("^[0-9]{6}_[-A-Za-z0-9_]*$", name):
16         return True
17     else:
18         return False
19
20 def get_top_dir(root, path):
21     """
22     Return the directory in path that is a subdirectory of root.
23     e.g.
24
25     >>> print get_top_dir('/a/b/c', '/a/b/c/d/e/f')
26     d
27     >>> print get_top_dir('/a/b/c/', '/a/b/c/d/e/f')
28     d
29     >>> print get_top_dir('/a/b/c', '/g/e/f')
30     None
31     >>> print get_top_dir('/a/b/c', '/a/b/c')
32     <BLANKLINE>
33     """
34     if path.startswith(root):
35         subpath = path[len(root):]
36         if subpath.startswith('/'):
37             subpath = subpath[1:]
38         return subpath.split(os.path.sep)[0]
39     else:
40         return None
41