2 Utilities to work with the various eras of sequence archive files
8 eland_re = re.compile('s_(?P<lane>\d)(_(?P<read>\d))?_eland_')
9 raw_seq_re = re.compile('woldlab_[0-9]{6}_[^_]+_[\d]+_[\dA-Za-z]+')
10 qseq_re = re.compile('woldlab_[0-9]{6}_[^_]+_[\d]+_[\dA-Za-z]+_l[\d]_r[\d].tar.bz2')
12 SEQUENCE_TABLE_NAME = "sequences"
13 def create_sequence_table(cursor):
15 Create a SQL table to hold SequenceFile entries
18 CREATE TABLE %(table)s (
27 """ %( {'table': SEQUENCE_TABLE_NAME} )
28 return cursor.execute(sql)
30 class SequenceFile(object):
32 Simple container class that holds the path to a sequence archive
33 and basic descriptive information.
35 def __init__(self, filetype, path, flowcell, lane, read=None, pf=None, cycle=None):
36 self.filetype = filetype
38 self.flowcell = flowcell
45 return hash(self.key())
48 return (self.flowcell, self.lane)
51 return unicode(self.path)
53 def __eq__(self, other):
55 Equality is defined if everything but the path matches
57 attributes = ['filetype','flowcell', 'lane', 'read', 'pf', 'cycle']
59 if getattr(self, a) != getattr(other, a):
65 return u"<%s %s %s %s>" % (self.filetype, self.flowcell, self.lane, self.path)
67 def make_target_name(self, root):
69 Create target name for where we need to link this sequence too
71 path, basename = os.path.split(self.path)
72 # Because the names aren't unque we include the flowcel name
73 # because there were different eland files for different length
74 # analyses, we include the cycle length in the name.
75 if self.filetype == 'eland':
76 template = "%(flowcell)s_%(cycle)s_%(eland)s"
77 basename = template % { 'flowcell': self.flowcell,
81 # all the other file types have names that include flowcell/lane
82 # information and thus are unique so we don't have to do anything
83 return os.path.join(root, basename)
85 def save(self, cursor):
87 Add this entry to a DB2.0 database.
89 #FIXME: NEEDS SQL ESCAPING
90 header_macro = {'table': SEQUENCE_TABLE_NAME }
91 sql_header = "insert into %(table)s (" % header_macro
92 sql_columns = ['filetype','path','flowcell','lane']
93 sql_middle = ") values ("
94 sql_values = [self.filetype, self.path, self.flowcell, self.lane]
96 for name in ['read', 'pf', 'cycle']:
97 value = getattr(self, name)
99 sql_columns.append(name)
100 sql_values.append(value)
102 sql = " ".join([sql_header,
103 ", ".join(sql_columns),
105 # note the following makes a string like ?,?,?
106 ",".join(["?"] * len(sql_values)),
109 return cursor.execute(sql, sql_values)
111 def get_flowcell_cycle(path):
113 Extract flowcell, cycle from pathname
115 rest, cycle = os.path.split(path)
116 rest, flowcell = os.path.split(rest)
117 cycle_match = re.match("C(?P<start>[0-9]+)-(?P<stop>[0-9]+)", cycle)
118 if cycle_match is None:
120 "Expected .../flowcell/cycle/ directory structure in %s" % \
122 start = cycle_match.group('start')
123 if start is not None:
125 stop = cycle_match.group('stop')
129 return flowcell, start, stop
131 def parse_srf(path, filename):
132 flowcell_dir, start, stop = get_flowcell_cycle(path)
133 basename, ext = os.path.splitext(filename)
134 records = basename.split('_')
135 flowcell = records[4]
136 lane = int(records[5][0])
137 fullpath = os.path.join(path, filename)
139 if flowcell_dir != flowcell:
140 logging.warn("flowcell %s found in wrong directory %s" % \
143 return SequenceFile('srf', fullpath, flowcell, lane, cycle=stop)
145 def parse_qseq(path, filename):
146 flowcell_dir, start, stop = get_flowcell_cycle(path)
147 basename, ext = os.path.splitext(filename)
148 records = basename.split('_')
149 fullpath = os.path.join(path, filename)
150 flowcell = records[4]
151 lane = int(records[5][1])
152 read = int(records[6][1])
154 if flowcell_dir != flowcell:
155 logging.warn("flowcell %s found in wrong directory %s" % \
158 return SequenceFile('qseq', fullpath, flowcell, lane, read, cycle=stop)
160 def parse_fastq(path, filename):
163 flowcell_dir, start, stop = get_flowcell_cycle(path)
164 basename, ext = os.path.splitext(filename)
165 records = basename.split('_')
166 fullpath = os.path.join(path, filename)
167 flowcell = records[4]
168 lane = int(records[5][1])
169 read = int(records[6][1])
170 pf = parse_fastq_pf_flag(records)
172 if flowcell_dir != flowcell:
173 logging.warn("flowcell %s found in wrong directory %s" % \
176 return SequenceFile('fastq', fullpath, flowcell, lane, read, pf=pf, cycle=stop)
178 def parse_fastq_pf_flag(records):
179 """Take a fastq filename split on _ and look for the pass-filter flag
184 fastq_type = records[-1].lower()
185 if fastq_type.startswith('pass'):
187 elif fastq_type.startswith('nopass'):
189 elif fastq_type.startswith('all'):
192 raise ValueError("Unrecognized fastq name %s at %s" % \
193 (records[-1], os.path.join(path,filename)))
197 def parse_eland(path, filename, eland_match=None):
198 if eland_match is None:
199 eland_match = eland_re.match(filename)
200 fullpath = os.path.join(path, filename)
201 flowcell, start, stop = get_flowcell_cycle(path)
202 if eland_match.group('lane'):
203 lane = int(eland_match.group('lane'))
206 if eland_match.group('read'):
207 read = int(eland_match.group('read'))
210 return SequenceFile('eland', fullpath, flowcell, lane, read, cycle=stop)
212 def scan_for_sequences(dirs):
214 Scan through a list of directories for sequence like files
218 logging.info("Scanning %s for sequences" % (d,))
219 if not os.path.exists(d):
220 logging.warn("Flowcell directory %s does not exist" % (d,))
223 for path, dirname, filenames in os.walk(d):
226 # find sequence files
227 if raw_seq_re.match(f):
228 if f.endswith('.md5'):
230 elif f.endswith('.srf') or f.endswith('.srf.bz2'):
231 seq = parse_srf(path, f)
232 elif qseq_re.match(f):
233 seq = parse_qseq(path, f)
234 elif f.endswith('fastq') or f.endswith('.fastq.bz2'):
235 seq = parse_fastq(path, f)
236 eland_match = eland_re.match(f)
238 if f.endswith('.md5'):
240 seq = parse_eland(path, f, eland_match)
242 sequences.append(seq)
243 logging.debug("Found sequence at %s" % (f,))