1 """Standardize reading and writing fastq submission names.
5 PAIRED_TEMPLATE = '{lib_id}_{flowcell}_c{cycle}_l{lane}_r{read}.fastq'
6 SINGLE_TEMPLATE = '{lib_id}_{flowcell}_c{cycle}_l{lane}.fastq'
9 '(?P<lib_id>[^_]+)_(?P<flowcell>[^_]+)_'\
10 'c(?P<cycle>[\d]+)_l(?P<lane>[\d]+)(_r(?P<read>[\d]))?\.fastq')
12 class FastqName(collections.Mapping):
13 """Utility class to convert to the standardized submission fastq name.
15 def __init__(self, is_paired=None, **kwargs):
16 """Create a fastq name handler.
18 Takes filename or common attributes like flowcell, lib_id, lane, read, cycle
20 self._attributes = ('flowcell', 'lib_id', 'lane', 'read', 'cycle')
21 self._is_paired = is_paired
25 if 'filename' in kwargs:
26 self._init_by_filename(**kwargs)
28 self._init_by_attributes(**kwargs)
30 def _init_by_attributes(self, **kwargs):
31 for k in self._attributes:
37 def _init_by_filename(self, filename):
38 match = FASTQ_RE.match(filename)
40 raise ValueError('Is "{0}" a submission fastq?'.format(filename))
42 for k in self._attributes:
43 self[k] = match.group(k)
45 def _get_is_paired(self):
46 if self._is_paired is None:
47 return getattr(self, 'read', None) is not None
49 return self._is_paired
50 def _set_is_paired(self, value):
51 self._is_paired = value
52 is_paired = property(_get_is_paired, _set_is_paired)
55 if self.is_paired and self['read'] is None:
64 is_valid = property(_is_valid)
66 def _get_filename(self):
69 "Please set all needed variables before generating a filename")
71 T = PAIRED_TEMPLATE if self.is_paired else SINGLE_TEMPLATE
72 return T.format(**self)
73 filename = property(_get_filename)
76 return iter(self._attributes)
78 def __getitem__(self, key):
79 return getattr(self, key, None)
81 def __setitem__(self, key, value):
82 if key in self._attributes:
83 setattr(self, key, value)
85 raise ValueError("Unrecognized key {0}".format(key))
88 return len([k for k in self if self[k] is not None])