annotate seg2matrix/CGData/BaseTable.py @ 52:3a036a34c362

better handle of input file
author jingchunzhu
date Thu, 17 Sep 2015 15:00:45 -0700
parents b6f5d2d1b047
children b88b95642f59
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
52
3a036a34c362 better handle of input file
jingchunzhu
parents: 50
diff changeset
66 if linenum ==1: #ignore header line
3a036a34c362 better handle of input file
jingchunzhu
parents: 50
diff changeset
67 continue
31
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
68 r = self.__row_class__()
50
jingchunzhu
parents: 31
diff changeset
69 if (comment is None or not row[0].startswith(comment)) and not row[0].startswith("#"):
31
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
70 for i, col in enumerate(cols):
52
3a036a34c362 better handle of input file
jingchunzhu
parents: 50
diff changeset
71 skip =0
31
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
72 isOptional = False
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
73 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
74 isOptional = self['cgformat']['columnDef'][col]['optional']
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
75 if len(row) > i:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
76 try:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
77 setattr(r, col, colType[col](row[i]))
52
3a036a34c362 better handle of input file
jingchunzhu
parents: 50
diff changeset
78 except ValueError:
31
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
79 raise ValueError( "col invalid type %s on line %d" % (row[i], linenum))
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
80 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
81 if isOptional:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
82 setattr(r, col, None)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
83 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
84 print row
52
3a036a34c362 better handle of input file
jingchunzhu
parents: 50
diff changeset
85 skip =1 # ignore bad lines
3a036a34c362 better handle of input file
jingchunzhu
parents: 50
diff changeset
86 break
31
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
87 raise InvalidFormat("missing colum " + col)
52
3a036a34c362 better handle of input file
jingchunzhu
parents: 50
diff changeset
88 if skip:
3a036a34c362 better handle of input file
jingchunzhu
parents: 50
diff changeset
89 continue
3a036a34c362 better handle of input file
jingchunzhu
parents: 50
diff changeset
90
31
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
91 if not self.groupKey:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
92 if self.secondKey is not None:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
93 key1 = getattr(r, self.firstKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
94 key2 = getattr(r, self.secondKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
95 if key1 not in storeMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
96 storeMap[key1] = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
97 storeMap[key1][key2] = r
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
98 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
99 storeMap[ getattr(r, self.firstKey ) ] = r
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
100 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
101 key1 = getattr(r, self.firstKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
102 if self.secondKey is not None:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
103 key2 = getattr(r, self.secondKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
104 if key1 not in storeMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
105 storeMap[key1] = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
106 if key2 not in storeMap[key1]:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
107 storeMap[key1][key2] = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
108 storeMap[key1][key2].append(r)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
109 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
110 if key1 not in storeMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
111 storeMap[key1] = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
112 storeMap[key1].append(r)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
113 self.loaded = True
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
114
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
115 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
116 def __getattr__(self, item):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
117 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
118 self.load()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
119
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
120 if item == "get_" + self.firstKey + "_list":
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
121 return self.__get_firstmap__().keys
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
122 if item == "get_by_" + self.firstKey:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
123 return self.__get_firstmap__().__getitem__
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
124 if item == "get_" + self.firstKey + "_values":
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
125 return self.__get_firstmap__().values
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
126 if item == "get_" + self.firstKey + "_map":
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
127 return self.__get_firstmap__
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
128 if item == "has_" + self.firstKey:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
129 return self.__get_firstmap__().__contains__
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
130 raise AttributeError(item)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
131 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
132
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
133 def get_key_list(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
134 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
135 List keys
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
136 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
137 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
138 self.load()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
139 return self.__get_firstmap__().keys()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
140
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
141 def get_by(self, key):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
142 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
143 get by key
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
144 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
145 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
146 self.load()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
147 return self.__get_firstmap__().__getitem__(key)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
148
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
149 def get_values(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
150 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
151 get values
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
152 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
153 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
154 self.load()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
155 return self.__get_firstmap__().values()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
156
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
157 def get_map(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
158 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
159 get key map
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
160 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
161 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
162 self.load()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
163 return self.__get_firstmap__()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
164
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
165 def has_key(self, key):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
166 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
167 Does the table have a key
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
168 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
169 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
170 self.load()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
171 return self.__get_firstmap__().__contains__(key)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
172 def __get_firstmap__(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
173 return getattr(self, self.firstKey + "_map")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
174
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
175 def init_blank(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
176 self.free()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
177 self['cgdata'] = { 'type' : self['cgformat']['name'] }
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
178 self.loaded = True
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
179
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
180 def insert(self, name, vals):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
181 storeMap = getattr(self, self.firstKey + "_map")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
182 cols = self['cgformat']['columnOrder']
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
183 r = self.__row_class__()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
184 for col in cols:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
185 isOptional = False
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
186 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
187 isOptional = self['cgformat']['columnDef'][col]['optional']
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
188 if col in vals:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
189 setattr(r, col, vals[col])
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
190 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
191 if isOptional:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
192 setattr(r, col, None)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
193 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
194 raise InvalidFormat("missing colum " + col)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
195
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
196 if not self.groupKey:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
197 if self.secondKey is not None:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
198 key1 = getattr(r, self.firstKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
199 key2 = getattr(r, self.secondKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
200 if key1 not in storeMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
201 storeMap[key1] = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
202 storeMap[key1][key2] = r
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
203 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
204 storeMap[ getattr(r, self.firstKey ) ] = r
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
205 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
206 key1 = getattr(r, self.firstKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
207 if self.secondKey is not None:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
208 key2 = getattr(r, self.secondKey )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
209 if key1 not in storeMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
210 storeMap[key1] = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
211 if key2 not in storeMap[key1]:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
212 storeMap[key1][key2] = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
213 storeMap[key1][key2].append(r)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
214 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
215 if key1 not in storeMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
216 storeMap[key1] = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
217 storeMap[key1].append(r)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
218
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
219 def write(self, handle):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
220 writer = csv.writer(handle, delimiter="\t", lineterminator="\n")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
221 for row in self.row_iter():
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
222 orow = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
223 for col in self['cgformat']['columnOrder']:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
224 orow.append( getattr(row, col) )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
225 writer.writerow(orow)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
226
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
227
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
228 def row_iter(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
229 if not self.groupKey:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
230 keyMap = getattr(self, self.firstKey + "_map")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
231 for rowKey in keyMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
232 yield keyMap[rowKey]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
233 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
234 keyMap = getattr(self, self.firstKey + "_map")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
235 for rowKey in keyMap:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
236 for elem in keyMap[rowKey]:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
237 yield elem
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
238
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
239 def __get_firstmap__(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
240 return getattr(self, self.firstKey + "_map")