-
Notifications
You must be signed in to change notification settings - Fork 1
/
biclus.py
executable file
·465 lines (409 loc) · 13.7 KB
/
biclus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import time
from urllib.request import urlopen
import math
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from numpy.random import RandomState
from pandas.plotting import parallel_coordinates
class Bicluster:
def __init__(self, rows, cols, inverted_rows, msr):
if isinstance(rows, np.ndarray) and isinstance(cols, np.ndarray) and isinstance(inverted_rows, np.ndarray):
self.rows = rows
self.cols = cols
self.inverted_rows = inverted_rows
else:
raise Exception("rows, cols and inverted_rows must be np.ndarray. TIPS: np.array(rows_list) is the way.")
self.msr = msr
def __str__(self):
return "Shape:{0}, {1}\tMSR:{2}".format(self.rows.size+self.inverted_rows.size, self.cols.size, self.msr)
def read_matrix(filename, url=False):
"""
Read a .matrix file from a path or a url
Parameters
----------
filename : string
The path or the url of the .matrix file
url : boolean
Indicate whether the name parameter is an url or a path
Returns
-------
Numpy array
The file as a Numpy array
"""
try:
if url:
lines = urlopen(filename).read().decode('utf-8').strip().split('\n')
else:
matrix_file = open(filename, "r")
lines = matrix_file.read().strip().split("\n")
matrix_file.close()
except Exception as e:
raise
lines = list(' -'.join(line.split('-')).split(' ') for line in lines)
lines = list(list(int(i) for i in line if i) for line in lines)
return np.array(lines)
def readFile(filename):
generator = RandomState(0)
matrix_file = open(filename, "r")
lines = matrix_file.read().split("\n")
matrix = []
for line in lines:
matrix.append(line.split('\t'))
for i in range(len(matrix) -1):
for j in range(len(matrix[0])):
try:
matrix[i][j] = float(matrix[i][j])
except ValueError:
matrix[i][j] = generator.randint(0, 801)
matrix.pop()
return np.array(matrix)
def clean(matrix, missing_value=-1):
"""
Replace the missing value with random one's.
Parameters
----------
matrix : Numpy array
Values matrix
missing_values : float
Value to be considered as missing (default -1)
Returns
-------
Numpy array
missing
"""
temp_matrix = np.copy(matrix)
generator = RandomState(0)
idx = np.where(temp_matrix == missing_value)
temp_matrix[idx] = generator.randint(0, 801, len(idx[0]))
return temp_matrix
def mean_squared_residue_np(matrix, rows, cols, inverted_rows=np.array([])):
"""
Compute the MSR(Mean Squared Residue) of the submatrix defined by rows,cols and inverted_rows over the matrix.
Parameters
----------
matrix : Numpy array
Values matrix
rows : Numpy array
Array of rows indexes of submatrix
cols : Numpy array
Array of columns indexes of submatrix
inverted_rows : Numpy array (default np.array([]))
Array of inverted rows indexesof submatrix
Returns
-------
float
The MSR of the submatrix
"""
matrix2 = matrix[rows][:, cols]
if inverted_rows.size > 0:
matrix_inverted = np.flip(matrix[inverted_rows][:, cols], 1)
matrix2 = np.append(matrix2, matrix_inverted, 0)
def msr(a): return (np.power(a - a.mean(axis=1, keepdims=True) - a.mean(axis=0) + a.mean(), 2).mean())
return msr(matrix2)
def multiple_deletion_node_np(matrix, msr_threshold=300, alpha=1.2):
"""
Multiple deletion node on matrix.
Parameters
----------
matrix : Numpy array
Values matrix
msr_threshold : float (default 300)
Minimum MSR of submatrix to be considered acceptable
alpha : float (default 1.2)
Value of alpha
Returns
-------
A tuple of Numpy array
The rows and columns indexes of the submatrix obtained.
"""
rows = np.arange(0, matrix.shape[0])
cols = np.arange(0, matrix.shape[1])
msr = mean_squared_residue_np(matrix, rows, cols)
print("MSR before multiple_deletion_node\t" + str(msr))
print(len(rows))
print(len(cols))
rows_mean = matrix[rows].mean(axis=1, keepdims=True)
cols_mean = matrix[rows][:, cols].mean(axis=0)
deletion = True
while deletion and msr > msr_threshold:
arr = matrix[rows][:, cols] - rows_mean - cols_mean
arr += np.mean(matrix[rows][:, cols])
msr_rows = np.power(arr, 2).mean(axis=1)
rows_to_remove = msr_rows <= (alpha * msr)
rows = rows[rows_to_remove]
msr = mean_squared_residue_np(matrix, rows, cols)
rows_mean = matrix[rows].mean(axis=1, keepdims=True)
cols_mean = matrix[rows][:, cols].mean(axis=0)
cols_to_remove = np.array([])
if matrix.shape[1] > 100:
arr = matrix[rows][:, cols] - rows_mean - cols_mean
arr += np.mean(matrix[rows][:, cols])
msr_cols = np.power(arr, 2).mean(axis=0)
cols_to_remove = msr_cols <= (alpha * msr)
cols = cols[cols_to_remove]
msr = mean_squared_residue_np(matrix, rows, cols)
rows_mean = matrix[rows].mean(axis=1, keepdims=True)
cols_mean = matrix[rows][:, cols].mean(axis=0)
elements_removed = np.count_nonzero(rows_to_remove == False) + np.count_nonzero(cols_to_remove == False)
if(elements_removed == 0):
deletion = False
print("MSR after multiple_deletion_node\t" + str(msr))
return rows, cols
def single_deletion_node_np(matrix, rows, cols, msr_threshold=300):
"""
Single deletion node on submatrix defined by rows and cols.
Parameters
----------
matrix : Numpy array
Values matrix
rows : Numpy array
Array of rows indexes of submatrix
cols : Numpy array
Array of columns indexes of submatrix
msr_threshold : float (default 300)
Minimum MSR of submatrix to be considered acceptable
Returns
-------
A tuple of Numpy array
The rows and columns indexes of the submatrix obtained.
"""
msr = mean_squared_residue_np(matrix, rows, cols)
print("MSR before single_deletion_node\t\t" + str(msr))
rows_mean = matrix[rows].mean(axis=1, keepdims=True)
cols_mean = matrix[rows][:, cols].mean(axis=0)
while msr > msr_threshold:
arr = matrix[rows][:, cols] - rows_mean - cols_mean
arr += np.mean(matrix[rows][:, cols])
msr_rows = np.power(arr, 2).mean(axis=1)
msr_cols = np.power(arr, 2).mean(axis=0)
rows_max = np.amax(msr_rows)
cols_max = np.amax(msr_cols)
if rows_max > cols_max:
rows = np.delete(rows, np.argmax(msr_rows))
else:
cols = np.delete(cols, np.argmax(msr_cols))
msr = mean_squared_residue_np(matrix, rows, cols)
rows_mean = matrix[rows].mean(axis=1, keepdims=True)
cols_mean = matrix[rows][:, cols].mean(axis=0)
print("MSR after single_deletion_node\t\t" + str(msr))
return rows, cols
def node_addition_np(matrix, rows, cols):
"""
Node addition on submatrix defined by rows and cols.
Parameters
----------
matrix : Numpy array
Values matrix
rows : Numpy array
Array of rows indexes of submatrix
cols : Numpy array
Array of columns indexes of submatrix
Returns
-------
A tuple of Numpy array
The rows and columns indexes of the submatrix obtained.
"""
inverted_rows = np.array([])
matrix_rows = np.arange(0, matrix.shape[0])
matrix_cols = np.arange(0, matrix.shape[1])
msr = mean_squared_residue_np(matrix, rows, cols)
print("MSR before node_addition\t\t" + str(msr))
rows_mean = matrix[rows].mean(axis=1, keepdims=True)
cols_mean = matrix[rows][:, cols].mean(axis=0)
rows_not = np.setdiff1d(matrix_rows, rows)
cols_not = np.setdiff1d(matrix_cols, cols)
rows_mean_not = matrix[rows_not].mean(axis=1, keepdims=True)
cols_mean_not = matrix[rows][:, cols_not].mean(axis=0)
addition = True
while addition:
arr = matrix[rows_not][:, cols] - rows_mean_not - cols_mean
arr += np.mean(matrix[rows][:, cols]) # dubbio
msr_rows = np.power(arr, 2).mean(axis=1)
rows_to_append = msr_rows < msr
rows = np.append(rows, rows_not[rows_to_append])
rows_not = np.setdiff1d(rows_not, rows_not[rows_to_append])
msr = mean_squared_residue_np(matrix, rows, cols, inverted_rows)
rows_mean = matrix[rows].mean(axis=1, keepdims=True)
cols_mean = matrix[rows][:, cols].mean(axis=0)
rows_mean_not = matrix[rows_not].mean(axis=1, keepdims=True)
cols_mean_not = matrix[rows][:, cols_not].mean(axis=0)
arr = matrix[rows][:, cols_not] - rows_mean - cols_mean_not
arr += np.mean(matrix[rows][:, cols]) # dubbio
msr_cols = np.power(arr, 2).mean(axis=0)
cols_to_append = msr_cols < msr
cols = np.append(cols, cols_not[cols_to_append])
cols_not = np.setdiff1d(cols_not, cols_not[cols_to_append])
msr = mean_squared_residue_np(matrix, rows, cols, inverted_rows)
rows_mean = matrix[rows].mean(axis=1, keepdims=True)
cols_mean = matrix[rows][:, cols].mean(axis=0)
arr = -matrix[rows_not][:, cols] + rows_mean_not - cols_mean
arr += np.mean(matrix[rows][:, cols]) # dubbio
msr_rows = np.power(arr, 2).mean(axis=1)
rows_to_append = msr_rows < msr
if(inverted_rows.size == 0):
inverted_rows = rows_not[rows_to_append]
else:
inverted_rows = np.append(inverted_rows, rows_not[rows_to_append])
rows_not = np.setdiff1d(rows_not, rows_not[rows_to_append])
msr = mean_squared_residue_np(matrix, rows, cols, inverted_rows)
rows_mean = matrix[rows].mean(axis=1, keepdims=True)
cols_mean = matrix[rows][:, cols].mean(axis=0)
rows_mean_not = matrix[rows_not].mean(axis=1, keepdims=True)
cols_mean_not = matrix[rows][:, cols_not].mean(axis=0)
elements_removed = np.count_nonzero(rows_to_append == True) + np.count_nonzero(cols_to_append == True)
if(elements_removed == 0):
addition = False
print("MSR after node_addition\t\t\t" + str(msr))
return rows, cols, inverted_rows, msr
def hide_bicluster_np(matrix, rows, cols, inverted_rows=np.array([])):
"""
Mask the submatrix defined by rows, cols and inverted_rows on matrix with random values.
Parameters
----------
matrix : Numpy array
Values matrix
rows : Numpy array
Array of rows indexes of submatrix
cols : Numpy array
Array of columns indexes of submatrix
inverted_rows : Numpy array (default np.array([]))
Array of inverted rows indexesof submatrix
Returns
-------
Numpy array
A copy of matrix in which submatrix has been masked.
"""
matrix2 = np.copy(matrix)
generator = RandomState(0)
for row in rows:
matrix2[row, cols] = generator.randint(0, 801, cols.size)
# print matrix2[row,cols]
if inverted_rows.size > 0:
for row in inverted_rows:
matrix2[row, cols] = generator.randint(0, 801, cols.size)
print("Last bicluster masked")
return matrix2
def get_bicluster(matrix, bicluster):
"""
Get a submatrix given rows,columns and inveted rows indexes.
Parameters
----------
matrix : Numpy array
Values matrix
bicluster : Bicluster object
Returns
-------
Numpy array
Submatrix.
"""
rows = np.append(bicluster.rows, bicluster.inverted_rows)
cols = bicluster.cols
rows.sort()
cols.sort()
return matrix[rows][:, cols]
def plot_bicluster(matrix, bicluster, name="Bicluster"):
"""
Plot a bicluster.
Parameters
----------
matrix: Numpy array
Starting values matrix.
bicluster : Bicluster object
Bicluster to plot
name : string (default "Bicluster")
Name of plotted bicluster.
Returns
-------
None
"""
bicluster_matrix = get_bicluster(matrix, bicluster)
df = pd.DataFrame(bicluster_matrix)
df["index"] = df.index.values
parallel_coordinates(df, "index", linewidth=1.0)
plt.title(name + "\nMean Squared Residue: " + str(bicluster.msr))
plt.xlabel('Condition')
plt.ylabel('Expression level')
plt.gca().legend_ = None
plt.show()
def find_biclusters_np(matrix, n_of_bicluster=100, msr_threshold=20, alpha=1.2):
"""
Find biclusters in a given matrix.
Parameters
----------
matrix : Numpy array
Values matrix
n_of_bicluster : int
Number of desired biclusters to find
msr_threshold : float (default 300)
Minimum MSR of submatrix to be considered acceptable
alpha : float (default 1.2)
Value of alpha(see algorithm definition)
Returns
-------
List of Bicluster object
The list of biclusters.
"""
matrixA = np.copy(matrix)
biclusters = []
for i in range(n_of_bicluster):
rowsB, colsB = multiple_deletion_node_np(matrixA, msr_threshold=msr_threshold, alpha=alpha)
rowsC, colsC = single_deletion_node_np(matrixA, rowsB, colsB, msr_threshold=msr_threshold)
rowsD, colsD, invD, msr = node_addition_np(matrix, rowsC, colsC)
print("Bicluster " + str(i))
biclusters.append(Bicluster(rowsD, colsD, invD, msr))
matrixA = hide_bicluster_np(matrixA, rowsD, colsD, invD)
print(len(rowsD))
print(len(colsD))
return biclusters
def interSets(set1, set2):
res = []
for i in range(len(set1)):
for j in range(len(set2)):
if set1[i]==set2[j]:
res.append(set1[i])
return res
def matchScore(set1, set2):
intersection = interSets(set1, set2)
return len(intersection) / (len(set1) + len(set2) - len(intersection) )
def setTriclustersMatchScore(setTriclusters1, setTriclusters2):
rowsScore = 0
colsScore = 0
# timesScore = 0
for i in range(len(setTriclusters1)):
maxMatchScoreRows = 0
maxMatchScoreCols = 0
maxMatchScoreTimes = 0
for j in range(len(setTriclusters2)):
matchScoreRows = matchScore(setTriclusters1[i].rows, setTriclusters1[j].rows)
matchScoreCols = matchScore(setTriclusters1[i].cols, setTriclusters1[j].cols)
matchScoreTimes = matchScore(setTriclusters1[i].times, setTriclusters1[j].times)
if matchScoreRows > maxMatchScoreRows :
maxMatchScoreRows = matchScoreRows
if matchScoreCols > maxMatchScoreCols :
maxMatchScoreCols = matchScoreCols
# if matchScoreTimes > maxMatchScoreTimes :
# maxMatchScoreTimes = matchScoreTimes
rowsScore += maxMatchScoreRows
colsScore += maxMatchScoreCols
# timesScore += maxMatchScoreTimes
rowsScore = rowsScore/len(setTriclusters1)
colsScore = colsScore/len(setTriclusters1)
# timesScore = timesScore/len(setTriclusters1)
print(rowsScore)
print(colsScore)
# print(timesScore)
return math.sqrt(rowsScore*colsScore)
def main():
data = readFile('tp1')
#data = read_matrix("http://arep.med.harvard.edu/biclustering/lymphoma.matrix",url=True)
data = clean(data)
start = time.time()
biclusters = find_biclusters_np(data, n_of_bicluster=3)
end = (time.time() - start)
print(end, "seconds")
best_bicluster = sorted(biclusters, key=lambda bicluster: bicluster.msr)[0]
print(best_bicluster)
plot_bicluster(data, best_bicluster)
if __name__ == "__main__":
main()