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