annotate seg2matrix/CGData/BaseTable.py @ 34:d8e98030eca9

remove binary
author jingchunzhu <jingchunzhu@gmail.com>
date Fri, 24 Jul 2015 14:56:37 -0700
parents ab20c0d04f4a
children b6f5d2d1b047
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
1
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
2 from CGData import CGObjectBase
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
3
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
4 import csv
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
5 import types
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
6
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
7 class TableRow(object):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
8 def __init__(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
9 pass
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
10
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
11 def __str__(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
12 return "<" + ",".join( "%s=%s" % (col, getattr(self,col)) for col in self.__format__['columnOrder']) + ">"
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
13
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
14 class InvalidFormat(Exception):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
15 def __init__(self, txt):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
16 Exception.__init__(self, txt)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
17
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
18 class BaseTable(CGObjectBase):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
19 def __init__(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
20 super(BaseTable,self).__init__()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
21 self.__row_class__ = type( "TableRow_" + self['cgformat']['name'], (TableRow,), dict(__format__=self.__format__) )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
22 self.free()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
23
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
24 def free(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
25 self.firstKey = None
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
26 self.secondKey = None
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
27 self.groupKey = None
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
28 self.loaded = False
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
29 if 'primaryKey' in self['cgformat']:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
30 self.firstKey = self['cgformat']['primaryKey']
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
31 setattr(self, self['cgformat']['primaryKey'] + "_map", {} )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
32 self.groupKey = False
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
33
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
34 #setup the map for groupKeys
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
35 if 'groupKey' in self['cgformat']:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
36 self.firstKey = self['cgformat']['groupKey']
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
37 setattr(self, self['cgformat']['groupKey'] + "_map", {} )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
38 self.groupKey = True
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
39
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
40 if 'secondaryKey' in self['cgformat']:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
41 self.secondKey = self['cgformat']['secondaryKey']
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
42
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
43 def read(self, handle):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
44 cols = self['cgformat']['columnOrder']
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
45 colType = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
46 for col in cols:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
47 if 'columnDef' in self['cgformat'] and col in self['cgformat']['columnDef'] and 'type' in self['cgformat']['columnDef'][col]:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
48 if self['cgformat']['columnDef'][col]['type'] == 'float':
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
49 colType[col] = float
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
50 elif self['cgformat']['columnDef'][col]['type'] == 'int':
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
51 colType[col] = int
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
52 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
53 colType[col] = str
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
54 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
55 colType[col] = str
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
56
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
57 read = csv.reader(handle, delimiter="\t")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
58
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
59 storeMap = getattr(self, self.firstKey + "_map")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
60 comment = None
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
61 if 'comment' in self['cgformat']:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
62 comment = self['cgformat']['comment']
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
63 linenum = 0
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
64 for row in read:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
65 linenum += 1
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
66 r = self.__row_class__()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
67 if comment is None or not row[0].startswith(comment):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
68 for i, col in enumerate(cols):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
69 isOptional = False
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
70 if 'columnDef' in self['cgformat'] and col in self['cgformat']['columnDef'] and 'optional' in self['cgformat']['columnDef'][col]:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
71 isOptional = self['cgformat']['columnDef'][col]['optional']
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
72 if len(row) > i:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
73 try:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
74 setattr(r, col, colType[col](row[i]))
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
75 except ValueError:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
76 raise ValueError( "col invalid type %s on line %d" % (row[i], linenum))
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
77 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
78 if isOptional:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
79 setattr(r, col, None)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
80 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
81 print row
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
82 raise InvalidFormat("missing colum " + col)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
83
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
84 if not self.groupKey:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
85 if self.secondKey is not None:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
86 key1 = getattr(r, self.firstKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
87 key2 = getattr(r, self.secondKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
88 if key1 not in storeMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
89 storeMap[key1] = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
90 storeMap[key1][key2] = r
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
91 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
92 storeMap[ getattr(r, self.firstKey ) ] = r
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
93 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
94 key1 = getattr(r, self.firstKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
95 if self.secondKey is not None:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
96 key2 = getattr(r, self.secondKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
97 if key1 not in storeMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
98 storeMap[key1] = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
99 if key2 not in storeMap[key1]:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
100 storeMap[key1][key2] = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
101 storeMap[key1][key2].append(r)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
102 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
103 if key1 not in storeMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
104 storeMap[key1] = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
105 storeMap[key1].append(r)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
106 self.loaded = True
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
107
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
108 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
109 def __getattr__(self, item):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
110 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
111 self.load()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
112
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
113 if item == "get_" + self.firstKey + "_list":
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
114 return self.__get_firstmap__().keys
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
115 if item == "get_by_" + self.firstKey:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
116 return self.__get_firstmap__().__getitem__
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
117 if item == "get_" + self.firstKey + "_values":
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
118 return self.__get_firstmap__().values
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
119 if item == "get_" + self.firstKey + "_map":
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
120 return self.__get_firstmap__
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
121 if item == "has_" + self.firstKey:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
122 return self.__get_firstmap__().__contains__
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
123 raise AttributeError(item)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
124 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
125
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
126 def get_key_list(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
127 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
128 List keys
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
129 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
130 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
131 self.load()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
132 return self.__get_firstmap__().keys()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
133
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
134 def get_by(self, key):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
135 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
136 get by key
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
137 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
138 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
139 self.load()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
140 return self.__get_firstmap__().__getitem__(key)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
141
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
142 def get_values(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
143 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
144 get values
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
145 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
146 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
147 self.load()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
148 return self.__get_firstmap__().values()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
149
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
150 def get_map(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
151 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
152 get key map
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
153 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
154 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
155 self.load()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
156 return self.__get_firstmap__()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
157
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
158 def has_key(self, key):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
159 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
160 Does the table have a key
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
161 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
162 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
163 self.load()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
164 return self.__get_firstmap__().__contains__(key)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
165 def __get_firstmap__(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
166 return getattr(self, self.firstKey + "_map")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
167
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
168 def init_blank(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
169 self.free()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
170 self['cgdata'] = { 'type' : self['cgformat']['name'] }
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
171 self.loaded = True
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
172
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
173 def insert(self, name, vals):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
174 storeMap = getattr(self, self.firstKey + "_map")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
175 cols = self['cgformat']['columnOrder']
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
176 r = self.__row_class__()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
177 for col in cols:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
178 isOptional = False
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
179 if 'columnDef' in self['cgformat'] and col in self['cgformat']['columnDef'] and 'optional' in self['cgformat']['columnDef'][col]:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
180 isOptional = self['cgformat']['columnDef'][col]['optional']
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
181 if col in vals:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
182 setattr(r, col, vals[col])
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
183 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
184 if isOptional:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
185 setattr(r, col, None)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
186 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
187 raise InvalidFormat("missing colum " + col)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
188
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
189 if not self.groupKey:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
190 if self.secondKey is not None:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
191 key1 = getattr(r, self.firstKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
192 key2 = getattr(r, self.secondKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
193 if key1 not in storeMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
194 storeMap[key1] = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
195 storeMap[key1][key2] = r
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
196 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
197 storeMap[ getattr(r, self.firstKey ) ] = r
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
198 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
199 key1 = getattr(r, self.firstKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
200 if self.secondKey is not None:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
201 key2 = getattr(r, self.secondKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
202 if key1 not in storeMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
203 storeMap[key1] = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
204 if key2 not in storeMap[key1]:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
205 storeMap[key1][key2] = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
206 storeMap[key1][key2].append(r)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
207 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
208 if key1 not in storeMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
209 storeMap[key1] = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
210 storeMap[key1].append(r)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
211
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
212 def write(self, handle):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
213 writer = csv.writer(handle, delimiter="\t", lineterminator="\n")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
214 for row in self.row_iter():
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
215 orow = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
216 for col in self['cgformat']['columnOrder']:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
217 orow.append( getattr(row, col) )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
218 writer.writerow(orow)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
219
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
220
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
221 def row_iter(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
222 if not self.groupKey:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
223 keyMap = getattr(self, self.firstKey + "_map")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
224 for rowKey in keyMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
225 yield keyMap[rowKey]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
226 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
227 keyMap = getattr(self, self.firstKey + "_map")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
228 for rowKey in keyMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
229 for elem in keyMap[rowKey]:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
230 yield elem
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
231
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
232 def __get_firstmap__(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
233 return getattr(self, self.firstKey + "_map")