annotate seg2matrix/CGData/BaseTable.py @ 60:bf57076e27b9 default tip

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