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

change genomicSegment input data
author jingchunzhu@gmail.com
date Tue, 27 Oct 2015 16:07:09 -0700
parents ab20c0d04f4a
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 import csv
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
3 import CGData
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
4 import math
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
5 from copy import copy
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
6 try:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
7 import numpy
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
8 except ImportError:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
9 numpy = None
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
10
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
11 class BaseMatrix(CGData.CGDataMatrixObject):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
12 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
13 Core matrix class. Implements data matrix using numpy or native python objects
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
14 depending up avaliblity and user request
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
15 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
16 corner_name = "#"
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
17 element_type = str
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
18 null_type = None
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
19 def __init__(self,type=str):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
20 CGData.CGDataMatrixObject.__init__(self)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
21 self.free()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
22 if 'cgformat' in self and 'valueType' in self['cgformat']:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
23 if self['cgformat']["valueType"] == 'float':
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
24 self.element_type = float
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
25 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
26 self.element_type = type
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
27
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
28 def free(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
29 self.col_map = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
30 self.row_map = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
31 self.matrix = None
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
32
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
33 def init_blank(self, cols, rows, skip_numpy=False):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
34 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
35 Initlize matrix with NA (or nan) values using row/column names
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
36 provided by user. User can also force usage of native python objects
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
37 (which is useful for string based matrices, and numpy matrices fix cel string length)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
38 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
39 if numpy is not None and not skip_numpy:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
40 self.matrix = numpy.matrix( numpy.zeros( (len(rows), len(cols)), dtype=self.element_type) )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
41 self.matrix.fill( numpy.nan )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
42 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
43 self.matrix = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
44 for i in range(len(rows)):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
45 self.matrix.append([self.null_type]*len(cols))
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
46 for i, c in enumerate(cols):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
47 self.col_map[c] = i
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
48 for i, r in enumerate(rows):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
49 self.row_map[r] = i
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
50 self.loaded = True
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
51
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
52 def read(self, handle, skip_vals=False):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
53 self.col_map = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
54 self.row_map = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
55 pos_hash = None
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
56
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
57 if numpy is not None:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
58 #txtMatrix = numpy.loadtxt(handle, delimiter="\t", comments="%%%%%%%%%%%%%%", dtype=str)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
59 t = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
60 for line in handle:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
61 t.append(line.replace("\n", "").split("\t"))
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
62 txtMatrix = numpy.array(t)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
63 del t
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
64 if self.element_type == float:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
65 txtMatrix[ txtMatrix=="NA" ] = 'nan'
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
66 txtMatrix[ txtMatrix=="null" ] = 'nan'
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
67 self.matrix = numpy.matrix( numpy.zeros( (txtMatrix.shape[0]-1, txtMatrix.shape[1]-1) ) )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
68 self.matrix.fill(numpy.nan)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
69 for i in range(self.matrix.shape[0]):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
70 for j in range(self.matrix.shape[1]):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
71 try:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
72 self.matrix[i,j] = self.element_type(txtMatrix[i+1,j+1])
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
73 except ValueError:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
74 pass
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
75 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
76 self.matrix = numpy.matrix(txtMatrix[1:,1:], dtype=self.element_type)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
77
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
78 for i, col in enumerate( txtMatrix[0,1:] ):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
79 self.col_map[col] = i
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
80 for i, row in enumerate( txtMatrix[1:,0] ):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
81 self.row_map[row] = i
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
82 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
83 self.matrix = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
84 for row in csv.reader(handle, delimiter="\t"):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
85 if pos_hash is None:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
86 pos_hash = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
87 pos = 0
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
88 for name in row[1:]:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
89 i = 1
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
90 orig_name = name
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
91 while name in pos_hash:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
92 name = orig_name + "#" + str(i)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
93 i += 1
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
94 pos_hash[name] = pos
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
95 pos += 1
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
96 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
97 newRow = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
98 if not skip_vals:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
99 newRow = [self.null_type] * (len(pos_hash))
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
100 for col in pos_hash:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
101 i = pos_hash[col] + 1
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
102 if row[i] != 'NA' and row[i] != 'null' and row[i] != 'NONE' and row[i] != "N/A" and len(row[i]):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
103 newRow[i - 1] = self.element_type(row[i])
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
104 self.row_map[row[0]] = len(self.matrix)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
105 self.matrix.append(newRow)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
106
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
107 self.col_map = {}
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
108 for col in pos_hash:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
109 self.col_map[col] = pos_hash[col]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
110 self.loaded = True
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
111
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
112 def write(self, handle, missing='NA'):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
113 write = csv.writer(handle, delimiter="\t", lineterminator='\n')
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
114 col_list = self.get_col_list()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
115
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
116 write.writerow([self.corner_name] + col_list)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
117 for rowName in self.row_map:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
118 out = [rowName]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
119 row = self.get_row(rowName)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
120 for col in col_list:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
121 val = row[self.col_map[col]]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
122 if val == self.null_type or val is None or (type(val)==float and math.isnan(val)):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
123 val = missing
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
124 out.append(val)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
125 write.writerow(out)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
126
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
127 def read_keyset(self, handle, key_predicate):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
128 if key_predicate == "rowKeySrc":
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
129 reader = csv.reader( handle, delimiter="\t")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
130 head = None
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
131 for row in reader:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
132 if head is None:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
133 head = row
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
134 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
135 yield row[0]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
136
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
137 if key_predicate=="columnKeySrc":
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
138 reader = csv.reader( handle, delimiter="\t")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
139 head = None
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
140 for row in reader:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
141 for col in row[1:]:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
142 yield col
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
143 break
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
144
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
145 def get_col_namespace(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
146 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
147 Return the name of the column namespace
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
148 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
149 return self.get("colNamespace", None)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
150
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
151 def get_row_namespace(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
152 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
153 Return the name of the row namespace
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
154 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
155 return self.get("rowNamespace", None)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
156
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
157 def get_col_list(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
158 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
159 Returns names of columns
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 out = self.col_map.keys()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
164 out.sort( lambda x,y: self.col_map[x]-self.col_map[y])
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
165 return out
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
166
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
167 def get_row_list(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
168 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
169 Returns names of rows
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
170 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
171 out = self.row_map.keys()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
172 out.sort( lambda x,y: self.row_map[x]-self.row_map[y])
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
173 return out
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
174
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
175 def get_row_pos(self, row):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
176 return self.row_map[row]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
177
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
178 def get_col_pos(self, col):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
179 return self.col_map[col]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
180
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
181 def get_row_count(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
182 return len(self.row_map)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
183
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
184 def get_col_count(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
185 return len(self.col_map)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
186
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
187 def get_row_map(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
188 return copy(self.row_map)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
189
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
190 def get_col_map(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
191 return copy(self.col_map)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
192
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
193 def get_shape(self):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
194 return len(self.row_map), len(self.col_map)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
195
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
196 def get_row(self, row_name):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
197 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
198 self.load( )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
199 if isinstance(self.matrix, list):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
200 return self.matrix[ self.row_map[row_name] ]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
201 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
202 return self.matrix[ self.row_map[row_name] ].tolist()[0]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
203
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
204 def get_col(self, col_name):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
205 if not self.loaded:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
206 self.load( )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
207 if isinstance(self.matrix, list):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
208 out = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
209 for row_name in self.get_row_list():
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
210 out.append( self.get_val(col_name, row_name) )
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
211 return out
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
212 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
213 return self.matrix[:,self.col_map[col_name]].reshape(-1).tolist()[0]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
214
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
215 def get_val(self, col_name, row_name):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
216 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
217 Get cell value based on row and column names
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
218 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
219 if isinstance(self.matrix, list):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
220 return self.matrix[self.row_map[row_name]][self.col_map[col_name]]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
221 return self.matrix[self.row_map[row_name],self.col_map[col_name]]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
222
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
223 def set_val(self, col_name, row_name, value):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
224 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
225 Set cell value based on row and column names
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
226 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
227 if isinstance(self.matrix, list):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
228 self.matrix[self.row_map[row_name]][self.col_map[col_name]] = value
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
229 else:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
230 self.matrix[self.row_map[row_name],self.col_map[col_name]] = value
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
231
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
232 def write_gct(self, handle, missing=''):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
233 write = csv.writer(handle, delimiter="\t", lineterminator='\n')
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
234 cols = self.get_col_list()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
235 write.writerow(["#1.2"])
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
236 write.writerow([len(self.get_row_list()), len(self.get_col_list())])
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
237 write.writerow(["NAME", "Description"] + cols)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
238 for row in self.get_row_list():
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
239 out = [row, row]
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
240 for col in cols:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
241 val = self.get_val(row_name=row, col_name=col)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
242 if val is None:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
243 val = missing
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
244 out.append(val)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
245 write.writerow(out)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
246
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
247