-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutual_rank_rs.py
executable file
·213 lines (197 loc) · 5.74 KB
/
mutual_rank_rs.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
#!/usr/bin/env python
import math
import sys
from my_argparse import *
from make_csv2 import *
import random
import numpy as np
pseudo_cnt = 0.000000001
def mi(vec1, vec2):
len1 = len(vec1)
len2 = len(vec2)
#p1 = {0: 0.5, 1: 0.5}
p1 = {}
p2 = {}
for elem in vec1:
if not elem in p1:
p1[elem] = 0.0
p1[elem] += 1.0
for key in p1.keys():
p1[key] = (p1[key] / len1)
for elem in vec2:
if not elem in p2:
p2[elem] = 0.0
p2[elem] += 1.0
for key in p2.keys():
p2[key] = (p2[key] / len2)
p12 = {}
for k1 in p1.keys():
for k2 in p2.keys():
k12 = str(k1)+','+str(k2)
if not k12 in p12:
p12[k12] = pseudo_cnt
for i in range(len1):
key = str(vec1[i])+','+str(vec2[i])
p12[key] += 1.0
#print len1
#print p12
for key in p12.keys():
p12[key] = p12[key] / len1
#print p12
mi = 0.0
for k1 in p1.keys():
for k2 in p2.keys():
k12 = str(k1)+','+str(k2)
mi += p12[k12]*math.log((p12[k12]/(p1[k1]*p2[k2])),2)
#print mi
return mi
def discretize_values(values, bin_num):
sorted_vals = sorted(values)
length = len(sorted_vals)
bin_edges = []
for i in range(bin_num):
bin_edges.append(sorted_vals[length * i / bin_num])
bin_edges.append(float('inf'))
#print bin_edges
ranks = []
for val in values:
for i in range(bin_num):
if bin_edges[i] <= val and val < bin_edges[i+1]:
ranks.append(i)
break
#print ranks
return ranks
def rand_permut(clsList, bin_num):
numSample = len(clsList)
cls2idx = {}
idx=0
for cls in clsList:
if not cls in cls2idx:
cls2idx[cls]=idx
idx+=1
ranks = []
#for cls in clsList:
# ranks.append(cls2idx[cls])
for i in range(len(clsList)):
r = (i / bin_num) + 1
ranks.append(r)
res = []
for i in range(100000):
res.append(mi(clsList, ranks))
random.shuffle(ranks)
#print ranks
#print res[-1]
hist, bin_edges = np.histogram(res, bins=50)
return hist, bin_edges
def get_pvalue(hist, bin_edges, score):
sum_hist = sum(hist)
pre_pval = 0
for i in range(0, len(hist)):
if bin_edges[i+1] >= score:
pre_pval += hist[i]
pvalue = float(pre_pval) / sum_hist
return pvalue
# run_mutual : main function. calculate the mutual information of every gene.
# return form : each line has (gene_id, score, p_value)
def run_mutual(input_matrix, geneList, clsList, bin_num):
ret_lines = []
hist, bin_edges = rand_permut(clsList, bin_num)
for li in range(len(input_matrix)):
values = input_matrix[li]
id = geneList[li]
disc_values = discretize_values(values, bin_num)
score = mi(clsList, disc_values)
pvalue = get_pvalue(hist, bin_edges, score)
ret_lines += [(id, score, pvalue)]
#ret_lines += [(id, score, 0)]
return ret_lines
# check_num_samples : if the numbers of samples are same, return true
# else, return flase
# return form : true or false, minimum number of samples
def check_num_samples(clsList):
clsHash = {} # class to the number of samples
for cls in clsList:
if not cls in clsHash:
clsHash[cls] = 0
clsHash[cls] += 1
minNum = 100000000
maxNum = 0
for cls in clsHash.keys():
if clsHash[cls] <= minNum:
minNum = clsHash[cls]
if clsHash[cls] >= maxNum:
maxNum = clsHash[cls]
if minNum == maxNum:
return True, minNum
else:
return False, minNum
# random_sampling : make new fmat to make the numbers of samples
# return form : fmat class
def random_sampling(oldFmat, numSample):
clsHash = {} # class to index list
totalClsHash = {} # index to class
newClsHash = {} # class to index list
for i in range(len(oldFmat.clsList)):
cls = oldFmat.clsList[i]
if not cls in clsHash:
clsHash[cls] = []
clsHash[cls].append(i)
for cls in clsHash.keys():
newClsHash[cls] = random.sample(clsHash[cls], numSample)
for i in newClsHash[cls]:
totalClsHash[i] = cls
indexList = totalClsHash.keys()
indexList.sort()
# set newFmat
newFmat = fin_matrix()
newFmat.geneList = oldFmat.geneList
newFmat.clsList = []
for i in indexList:
newFmat.clsList.append(totalClsHash[i])
newFmat.input_matrix = [[] for j in range(len(oldFmat.input_matrix))]
for j in range(len(oldFmat.input_matrix)):
for i in indexList:
newFmat.input_matrix[j].append(oldFmat.input_matrix[j][i])
newFmat.nSample = len(indexList)
newFmat.nClass = len(clsHash.keys())
return newFmat
if __name__=='__main__':
info = arg_parsing()
fmat = fin_parsing(info.fin_name, info.cond)
#binNum = int(math.log(len(fmat.clsList),2)+1)
binNum = fmat.nClass
# if the numbers of samples in other groups are not same, do random sampling to make the numbers same.
isSame, minNumSample = check_num_samples(fmat.clsList)
if(isSame==False):
totalGeneScores = {} # store gene scores for each iteration
# initialize totalGeneScores.
for gene in fmat.geneList:
totalGeneScores[gene] = []
for i in range(2): # repeat 10 times.
newFmat = random_sampling(fmat, minNumSample)
output_tuples = run_mutual(newFmat.input_matrix, newFmat.geneList, newFmat.clsList, binNum)
for tup in output_tuples:
(gene, score) = (tup[0], tup[1])
totalGeneScores[gene].append(score)
# calculate a new score by averaging the old scores.
result_tuples = []
for gene in totalGeneScores.keys():
totalGeneScores[gene] = np.mean(totalGeneScores[gene])
result_tuples.append((gene,totalGeneScores[gene]))
result_tuples.sort(key=lambda tuple: tuple[1], reverse=True)
# print the results
i=1
for tup in result_tuples[0:info.ntop]:
print str(i)+'\t'+tup[0]+'\t'+str(tup[1])
i+=1
else:
output_tuples = run_mutual(fmat.input_matrix, fmat.geneList, fmat.clsList, binNum)
output_tuples.sort(key=lambda tuple: tuple[1], reverse=True)
#print 'Order\tName\tScore\tP-value'
i=1
for tup in output_tuples[0:info.ntop]:
print str(i)+'\t'+tup[0]+'\t'+str(tup[1])+'\t'+str(tup[2])
#print tup[0]
i+=1
#if(info.outdir != None):
# make_csv(info.outdir, output_tuples, info.fin_name, fmat.clsList, 50)