annotate Population.py @ 0:c3ba43704319 draft default tip

Uploaded
author rico
date Wed, 12 Sep 2012 14:35:13 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c3ba43704319 Uploaded
rico
parents:
diff changeset
1 #!/usr/bin/env python
c3ba43704319 Uploaded
rico
parents:
diff changeset
2
c3ba43704319 Uploaded
rico
parents:
diff changeset
3 from OrderedDict import OrderedDict
c3ba43704319 Uploaded
rico
parents:
diff changeset
4
c3ba43704319 Uploaded
rico
parents:
diff changeset
5 class Individual(object):
c3ba43704319 Uploaded
rico
parents:
diff changeset
6 __slots__ = ['_column', '_name', '_alias']
c3ba43704319 Uploaded
rico
parents:
diff changeset
7
c3ba43704319 Uploaded
rico
parents:
diff changeset
8 def __init__(self, column, name, alias=None):
c3ba43704319 Uploaded
rico
parents:
diff changeset
9 self._column = column
c3ba43704319 Uploaded
rico
parents:
diff changeset
10 self._name = name
c3ba43704319 Uploaded
rico
parents:
diff changeset
11 self._alias = alias
c3ba43704319 Uploaded
rico
parents:
diff changeset
12
c3ba43704319 Uploaded
rico
parents:
diff changeset
13 @property
c3ba43704319 Uploaded
rico
parents:
diff changeset
14 def column(self):
c3ba43704319 Uploaded
rico
parents:
diff changeset
15 return self._column
c3ba43704319 Uploaded
rico
parents:
diff changeset
16
c3ba43704319 Uploaded
rico
parents:
diff changeset
17 @property
c3ba43704319 Uploaded
rico
parents:
diff changeset
18 def name(self):
c3ba43704319 Uploaded
rico
parents:
diff changeset
19 return self._name if self._alias is None else self._alias
c3ba43704319 Uploaded
rico
parents:
diff changeset
20
c3ba43704319 Uploaded
rico
parents:
diff changeset
21 @property
c3ba43704319 Uploaded
rico
parents:
diff changeset
22 def alias(self):
c3ba43704319 Uploaded
rico
parents:
diff changeset
23 return self._alias
c3ba43704319 Uploaded
rico
parents:
diff changeset
24
c3ba43704319 Uploaded
rico
parents:
diff changeset
25 @alias.setter
c3ba43704319 Uploaded
rico
parents:
diff changeset
26 def alias(self, alias):
c3ba43704319 Uploaded
rico
parents:
diff changeset
27 self._alias = alias
c3ba43704319 Uploaded
rico
parents:
diff changeset
28
c3ba43704319 Uploaded
rico
parents:
diff changeset
29 @property
c3ba43704319 Uploaded
rico
parents:
diff changeset
30 def real_name(self):
c3ba43704319 Uploaded
rico
parents:
diff changeset
31 return self._name
c3ba43704319 Uploaded
rico
parents:
diff changeset
32
c3ba43704319 Uploaded
rico
parents:
diff changeset
33 def __eq__(self, other):
c3ba43704319 Uploaded
rico
parents:
diff changeset
34 return self._column == other._column and self._name == other._name
c3ba43704319 Uploaded
rico
parents:
diff changeset
35
c3ba43704319 Uploaded
rico
parents:
diff changeset
36 def __ne__(self, other):
c3ba43704319 Uploaded
rico
parents:
diff changeset
37 return not self.__eq__(other)
c3ba43704319 Uploaded
rico
parents:
diff changeset
38
c3ba43704319 Uploaded
rico
parents:
diff changeset
39 def __repr__(self):
c3ba43704319 Uploaded
rico
parents:
diff changeset
40 return 'Individual: column={0} name={1} alias={2}'.format(self._column, self._name, self._alias)
c3ba43704319 Uploaded
rico
parents:
diff changeset
41
c3ba43704319 Uploaded
rico
parents:
diff changeset
42
c3ba43704319 Uploaded
rico
parents:
diff changeset
43 class Population(object):
c3ba43704319 Uploaded
rico
parents:
diff changeset
44 def __init__(self, name=None):
c3ba43704319 Uploaded
rico
parents:
diff changeset
45 self._columns = OrderedDict()
c3ba43704319 Uploaded
rico
parents:
diff changeset
46 self._name = name
c3ba43704319 Uploaded
rico
parents:
diff changeset
47
c3ba43704319 Uploaded
rico
parents:
diff changeset
48 @property
c3ba43704319 Uploaded
rico
parents:
diff changeset
49 def name(self):
c3ba43704319 Uploaded
rico
parents:
diff changeset
50 return self._name
c3ba43704319 Uploaded
rico
parents:
diff changeset
51
c3ba43704319 Uploaded
rico
parents:
diff changeset
52 @name.setter
c3ba43704319 Uploaded
rico
parents:
diff changeset
53 def name(self, name):
c3ba43704319 Uploaded
rico
parents:
diff changeset
54 self._name = name
c3ba43704319 Uploaded
rico
parents:
diff changeset
55
c3ba43704319 Uploaded
rico
parents:
diff changeset
56 def add_individual(self, individual, alias=None):
c3ba43704319 Uploaded
rico
parents:
diff changeset
57 if individual.column not in self._columns:
c3ba43704319 Uploaded
rico
parents:
diff changeset
58 self._columns[individual.column] = individual
c3ba43704319 Uploaded
rico
parents:
diff changeset
59 elif self._columns[individual.column] == individual:
c3ba43704319 Uploaded
rico
parents:
diff changeset
60 # should should this be an error?
c3ba43704319 Uploaded
rico
parents:
diff changeset
61 # should we replace the alias using this entry?
c3ba43704319 Uploaded
rico
parents:
diff changeset
62 pass
c3ba43704319 Uploaded
rico
parents:
diff changeset
63 else:
c3ba43704319 Uploaded
rico
parents:
diff changeset
64 raise 'Duplicate column: {0}'.format(individual)
c3ba43704319 Uploaded
rico
parents:
diff changeset
65
c3ba43704319 Uploaded
rico
parents:
diff changeset
66 def is_superset(self, other):
c3ba43704319 Uploaded
rico
parents:
diff changeset
67 for column, other_individual in other._columns.items():
c3ba43704319 Uploaded
rico
parents:
diff changeset
68 our_individual = self._columns.get(column)
c3ba43704319 Uploaded
rico
parents:
diff changeset
69 if our_individual is None or our_individual != other_individual:
c3ba43704319 Uploaded
rico
parents:
diff changeset
70 return False
c3ba43704319 Uploaded
rico
parents:
diff changeset
71 return True
c3ba43704319 Uploaded
rico
parents:
diff changeset
72
c3ba43704319 Uploaded
rico
parents:
diff changeset
73 def is_disjoint(self, other):
c3ba43704319 Uploaded
rico
parents:
diff changeset
74 for column, our_individual in self._columns.items():
c3ba43704319 Uploaded
rico
parents:
diff changeset
75 other_individual = other._columns.get(column)
c3ba43704319 Uploaded
rico
parents:
diff changeset
76 if other_individual is not None and other_individual == our_individual:
c3ba43704319 Uploaded
rico
parents:
diff changeset
77 return False
c3ba43704319 Uploaded
rico
parents:
diff changeset
78 return True
c3ba43704319 Uploaded
rico
parents:
diff changeset
79
c3ba43704319 Uploaded
rico
parents:
diff changeset
80 def column_list(self):
c3ba43704319 Uploaded
rico
parents:
diff changeset
81 return self._columns.keys()
c3ba43704319 Uploaded
rico
parents:
diff changeset
82
c3ba43704319 Uploaded
rico
parents:
diff changeset
83 def individual_with_column(self, column):
c3ba43704319 Uploaded
rico
parents:
diff changeset
84 if column in self._columns:
c3ba43704319 Uploaded
rico
parents:
diff changeset
85 return self._columns[column]
c3ba43704319 Uploaded
rico
parents:
diff changeset
86 return None
c3ba43704319 Uploaded
rico
parents:
diff changeset
87
c3ba43704319 Uploaded
rico
parents:
diff changeset
88 def tag_list(self, delimiter=':'):
c3ba43704319 Uploaded
rico
parents:
diff changeset
89 entries = []
c3ba43704319 Uploaded
rico
parents:
diff changeset
90 for column, individual in self._columns.items():
c3ba43704319 Uploaded
rico
parents:
diff changeset
91 entry = '{0}{1}{2}'.format(column, delimiter, individual.name)
c3ba43704319 Uploaded
rico
parents:
diff changeset
92 entries.append(entry)
c3ba43704319 Uploaded
rico
parents:
diff changeset
93 return entries
c3ba43704319 Uploaded
rico
parents:
diff changeset
94
c3ba43704319 Uploaded
rico
parents:
diff changeset
95 def to_string(self, delimiter=':', separator=' ', replace_names_with=None):
c3ba43704319 Uploaded
rico
parents:
diff changeset
96 entries = []
c3ba43704319 Uploaded
rico
parents:
diff changeset
97 for column, individual in self._columns.items():
c3ba43704319 Uploaded
rico
parents:
diff changeset
98 value = individual.name
c3ba43704319 Uploaded
rico
parents:
diff changeset
99 if replace_names_with is not None:
c3ba43704319 Uploaded
rico
parents:
diff changeset
100 value = replace_names_with
c3ba43704319 Uploaded
rico
parents:
diff changeset
101 entry = '{0}{1}{2}'.format(column, delimiter, value)
c3ba43704319 Uploaded
rico
parents:
diff changeset
102 entries.append(entry)
c3ba43704319 Uploaded
rico
parents:
diff changeset
103 return separator.join(entries)
c3ba43704319 Uploaded
rico
parents:
diff changeset
104
c3ba43704319 Uploaded
rico
parents:
diff changeset
105 def __str__(self):
c3ba43704319 Uploaded
rico
parents:
diff changeset
106 return self.to_string()
c3ba43704319 Uploaded
rico
parents:
diff changeset
107
c3ba43704319 Uploaded
rico
parents:
diff changeset
108 def from_population_file(self, filename):
c3ba43704319 Uploaded
rico
parents:
diff changeset
109 with open(filename) as fh:
c3ba43704319 Uploaded
rico
parents:
diff changeset
110 for line in fh:
c3ba43704319 Uploaded
rico
parents:
diff changeset
111 line = line.rstrip('\r\n')
c3ba43704319 Uploaded
rico
parents:
diff changeset
112 column, name, alias = line.split('\t')
c3ba43704319 Uploaded
rico
parents:
diff changeset
113 alias = alias.strip()
c3ba43704319 Uploaded
rico
parents:
diff changeset
114 individual = Individual(column, name)
c3ba43704319 Uploaded
rico
parents:
diff changeset
115 if alias:
c3ba43704319 Uploaded
rico
parents:
diff changeset
116 individual.alias = alias
c3ba43704319 Uploaded
rico
parents:
diff changeset
117 self.add_individual(individual)
c3ba43704319 Uploaded
rico
parents:
diff changeset
118
c3ba43704319 Uploaded
rico
parents:
diff changeset
119 def from_tag_list(self, tag_list):
c3ba43704319 Uploaded
rico
parents:
diff changeset
120 for tag in tag_list:
c3ba43704319 Uploaded
rico
parents:
diff changeset
121 column, name = tag.split(':')
c3ba43704319 Uploaded
rico
parents:
diff changeset
122 individual = Individual(column, name)
c3ba43704319 Uploaded
rico
parents:
diff changeset
123 self.add_individual(individual)
c3ba43704319 Uploaded
rico
parents:
diff changeset
124
c3ba43704319 Uploaded
rico
parents:
diff changeset
125 def individual_names(self):
c3ba43704319 Uploaded
rico
parents:
diff changeset
126 for column, individual in self._columns.items():
c3ba43704319 Uploaded
rico
parents:
diff changeset
127 yield individual.name
c3ba43704319 Uploaded
rico
parents:
diff changeset
128