0ff6c31fbbb918f4f516150130abde0986c41fdd
[htsworkflow.git] / htsworkflow / pipelines / samplekey.py
1 class SampleKey(object):
2     """Identifier for a sample in a particular 'location' on a flowcell.
3     """
4     def __init__(self, lane=None, read=None, sample=None):
5         self.lane = int(lane) if lane is not None else None
6         self.read = int(read) if read is not None else None
7         self.sample = sample
8
9     def _iswild(self):
10         return self.lane is None or \
11                self.read is None or \
12                self.sample is None
13     iswild = property(_iswild)
14
15     def matches(self, other):
16         """Test non-None attributes
17         """
18         if not (self.lane is None or other.lane is None):
19             if self.lane != other.lane: return False
20         if not (self.read is None or other.read is None):
21             if self.read != other.read:  return False
22         if not (self.sample is None or other.sample is None):
23             if self.sample != other.sample: return False
24         return True
25
26     def __eq__(self, other):
27         return (self.lane == other.lane) and \
28                (self.read == other.read) and \
29                (self.sample == other.sample)
30
31     def __ne__(self, other):
32         return (self.lane != other.lane) or \
33                (self.read != other.read) or \
34                (self.sample != other.sample)
35
36     def __lt__(self, other):
37         if self.lane < other.lane:
38             return True
39         elif self.lane > other.lane:
40             return False
41         elif self.sample < other.sample:
42             return True
43         elif self.sample > other.sample:
44             return False
45         elif self.read < other.read:
46             return True
47         elif self.read > other.read:
48             return False
49         else:
50             # equal
51             return False
52
53     def __le__(self, other):
54         if self == other: return True
55         else: return self < other
56
57     def __gt__(self, other):
58         return not self <= other
59
60     def __ge__(self, other):
61         return not self < other
62
63     def __hash__(self):
64         return hash((self.sample, self.lane, self.read))
65
66     def __repr__(self):
67         name = []
68
69         name.append('L%s' % (self.lane,))
70         name.append('R%s' % (self.read,))
71         name.append('S%s' % (self.sample,))
72
73         return '<SampleKey(' + ",".join(name) + ')>'
74