-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmutexprob.py
458 lines (374 loc) · 15.5 KB
/
mutexprob.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
__author__ = 'jlu96'
import scipy.stats as stats
import numpy as np
import time
import matplotlib.pyplot as plt
import bisect
class Memoize:
def __init__(self, f):
self.f = f
self.memo = {}
def __call__(self, *args):
if not args in self.memo:
self.memo[args] = self.f(*args)
return self.memo[args]
class TableLookup:
def __init__(self, f):
self.f = f
#A dictionary of mut_average to overlap,
#WHere mut_average is the average of the mutation frequencies,
#And overlap is the amount of overlap needed to reach significance
self.n = 0
self.p = 0.0
self.trials = 0
self.p_buffer = 0.0
self.set_size = 0
self.interval = 0
self.max_percentage = 0
self.min_frequencies = []
self.necessary_overlaps = []
def __call__(self, *args, **kwargs):
# # First check the table of computed overlaps necessary for significance.
# # If the number of overlaps you have is less than the number required,
# # Then simply return False.
#
#Unzip the commands to issigoverlap_multi
n, mut_nums, p, overlap, trials = args
#
# p_buffer = kwargs['p_buffer'] if ('p_buffer' in kwargs) else 0.01
# precision = kwargs['precision'] if ('precision' in kwargs) else 0.5
# interval = kwargs['interval'] if ('interval' in kwargs) else 10
# max_frequency = kwargs['max_frequency'] if ('max_frequency' in kwargs) else 0.5 * n
#
# #In case the function has been called with other parameters, reset the table.
# if (self.n != n or self.p != p or self.set_size != len(mut_nums) or self.trials != trials):
# self.n = n
# self.p = p
# self.trials = trials
# self.p_buffer = p_buffer
# self.set_size = len(mut_nums)
# self.interval = interval
# self.max_frequency = max_frequency
#
# #set the minimum frequencies and table
#
# self.min_frequencies = np.round(np.arange(1, max_frequency, interval))
# self.necessary_overlaps = np.zeros(len(self.min_frequencies))
#
# p_lax = p + p_buffer
#
# print "Precomputing thresholds for significant overlap for ", len(self.min_frequencies), "frequencies"
# t = time.time()
# for i in range(len(self.min_frequencies)):
# self.necessary_overlaps[i] = precomputesigoverlap(n, trials, p_lax, precision,
# *[self.min_frequencies[i] for j in range(self.set_size)])
#
# print "Total time needed to precompute thresholds", time.time() - t
#
# # Check for the lowest mutation number in the set, and find the overlap needed for significance
# min_mut_num = min(mut_nums)
# index = bisect.bisect_left(self.min_frequencies, min_mut_num)
# if overlap < self.necessary_overlaps[index]:
# return False
#
# else:
# return cooccurprob_approximate(n, overlap, trials, *mut_nums) <= p
if overlap < min(mut_nums) * 0.1:
return False
else:
return cooccurprob_approximate(n, overlap, trials, *mut_nums) <= p
@TableLookup
def issigcooccur_multi(n, mut_nums, p, overlap, trials):
return
def issigmutex(n, mut_nums, p, overlap = 0):
"""Determines the statistical significance of the mutually exclusive mutations,
which occur at mut_nums frequencies in the n samples.
**n** (*int*) - number of samples
**mut_nums** (*list*) - list of each mutation's frequency in the samples
**p** (*float*) - p-value
"""
surprise = mutexprob(n, mut_nums, overlap)
return surprise < p
def issigcooccur(n, mut_nums, p, overlap = 0):
"""Determines the statistical significance of the mutually exclusive mutations,
which occur at mut_nums frequencies in the n samples.
**n** (*int*) - number of samples
**mut_nums** (*list*) - list of each mutation's frequency in the samples
**p** (*float*) - p-value
"""
surprise = cooccurprob(n, mut_nums, overlap)
return surprise < p
def mutexprob(n, mut_nums, overlap = 0):
"""Calculates the tail probability of mutually exclusive
mutations occurring in at mut_nums frequencies in the
n samples."""
if (len(mut_nums) == 2): return mutexprob2(n, mut_nums[0], mut_nums[1], overlap)
def cooccurprob(n, mut_nums, overlap = 0):
"""Calculates the tail probability of cooccurring
mutations occurring in at mut_nums frequencies in the
n samples."""
if (len(mut_nums) == 2): return cooccurprob2(n, mut_nums[0], mut_nums[1], overlap)
@Memoize
def mutexprob2(n, a, b, overlap):
oddsratio, pvalue = stats.fisher_exact([[overlap, a - overlap], [b - overlap, n - a - b + overlap]],
alternative= 'less')
return pvalue
@Memoize
def cooccurprob2(n, a, b, overlap):
oddsratio, pvalue = stats.fisher_exact([[overlap, a - overlap], [b - overlap, n - a - b + overlap]],
alternative= 'greater')
return pvalue
# p = 1.0
# prev_m = 0
# mut_nums = [a, b]
# for m in mut_nums:
# p *= ratio(n, prev_m, m)
# prev_m += m
# return p
@Memoize
def prob3(n, a, b, c):
p = 1.0
prev_m = 0
mut_nums = [a, b, c]
for m in mut_nums:
p *= ratio(n, prev_m, m)
prev_m += m
return p
@Memoize
def ratio(n, a, b):
"""Calculates (n-a_C_b)/(n_C_b): the number of ways of mutating samples not already mutated,
divided by the number of ways of mutating samples, period."""
i = 1.0
for c in range(b):
i *= (n-a-c)
i /= (n-c)
return i
@Memoize
def p_exclusive(n, a, b, pA, pB):
return ratio(n, a, b) * stats.binom.pmf(a, n, pA) * stats.binom.pmf(b, n, pB)
@Memoize
def p_exclusive_total(n, pA, pB):
a_range = range(n + 1)
b_range = range(n + 1)
p_value = 0.0
for a in a_range:
for b in b_range:
p_value += p_exclusive(n, a, b, pA, pB)
return p_value
# class Memoize_CooccurProb:
# def __init__(self, f):
# self.f = f
# self.memo = {}
# def __call__(self, *args, **kwargs):
# keyword_tuple = tuple(sorted(kwargs.items()))
# if not (args, keyword_tuple) in self.memo:
# n = args[0]
# overlap = args[1]
# trials = kwargs['trials']
# self.memo[(args, keyword_tuple)] = self.f(n, overlap, trials, *args[2:])
# return self.memo[(args, keyword_tuple)]
#@Memoize_CooccurProb
@Memoize
def cooccurprob_approximate(n, overlap, trials, *mut_nums):
t = time.time()
overlap_limit = len(mut_nums) - 1
cooccur_num = 0
for i in range(trials):
# Create a random mutation matrix and sum across columns.
row = np.zeros(n)
numOverlaps = 0
for m in mut_nums:
random_row = np.concatenate([np.ones(m), np.zeros(n - m)])
np.random.shuffle(random_row)
row += random_row
if (len([entry for entry in row if entry > overlap_limit]) >= overlap): cooccur_num += 1
print "Time used", time.time() - t, 'for mut_nums', mut_nums, ' and overlap ', overlap, ' p equals ', cooccur_num * 1.0/trials
return cooccur_num * 1.0/trials
#This needs to be adjusted like below for cooccur
#mut_nums is a key word arg: add the *
#chang
def mutexprob_approximate(n, num_mutex, trials, *mut_nums):
t = time.time()
overlap_limit = len(mut_nums) - 1
mutex_num = 0
for i in range(trials):
# Create a random mutation matrix and sum across columns.
row = np.zeros(n)
numOverlaps = 0
for m in mut_nums:
random_row = np.concatenate([np.ones(m), np.zeros(n - m)])
np.random.shuffle(random_row)
row += random_row
if len([entry for entry in row if entry == 1.0]) >= num_mutex: mutex_num += 1
print "Time used", time.time() - t, 'for mut_nums', mut_nums, ' and number of mutually exclusive alterations ', \
num_mutex, ' p equals ', mutex_num * 1.0/trials
return mutex_num * 1.0/trials
# t = time.time()
# print "Number of trials", trials
# mutex_num = 0
# for i in range(trials):
# # Create a random mutation matrix and sum across columns.
# row = np.zeros(n)
# numOverlaps = overlap
# for m in mut_nums:
# row += np.random.permutation(np.concatenate([np.ones(m), np.zeros(n - m)]))
#
# #Check if it's mutually exclusive
#
# for entry in row:
# if (entry > 1.1): #in case of floating point error
# numOverlaps -= 1
# if (numOverlaps < 0):
# break
# if (numOverlaps >= 0): mutex_num += 1
# print "Time used", time.time() - t
#
# return mutex_num * 1.0/trials
class PrecomputeSigOverlap:
def __init__(self, f):
self.f = f
self.n = 0
self.p = 0.0
self.trials = 0
self.set_size = 0
self.min_frequencies = []
self.necessary_overlaps = []
def __call__(self, *args):
n, trials, p, precision, mut_nums = args
#In case the function has been called with other parameters, reset the table.
if (self.n != n or self.p != p or self.set_size != len(mut_nums) or self.trials != trials):
self.n = n
self.p = p
self.trials = trials
self.set_size = len(mut_nums)
self.min_frequencies = []
self.necessary_overlaps = []
# This better be equal among all of the indices...
min_mut_num = min(mut_nums)
index = bisect.bisect_left(self.min_frequencies, min_mut_num)
min_overlap = self.necessary_overlaps[index]
self.min_frequencies.append(min_mut_num)
new_necessary_overlap = self.f(*args, min_overlap=min_overlap)
self.necessary_overlaps.append(new_necessary_overlap)
return new_necessary_overlap
def precomputesigoverlap(n, trials, p, precision, *mut_nums, **kwargs):
# For a given number of samples n and mutation numbers mut_nums,
# returns the minimum amount of overlap for it to be significant.
#Return -1 if can't get significance..
min_overlap = kwargs['min_overlap'] if ('min_overlap' in kwargs) else 1
#Check all overlaps up to the minimum.
overlap_range_length = int(round((min(mut_nums) - min_overlap) * precision))
overlap_range = np.round(np.linspace(min_overlap, min(mut_nums), overlap_range_length))
for overlap in overlap_range:
pvalue = cooccurprob_approximate(n, overlap, trials, *mut_nums)
if pvalue <= p:
return overlap
return -1
def getsigoverlap(n, trials, p, precision, *mut_nums, **kwargs):
# For a given number of samples n and mutation numbers mut_nums,
# returns the minimum amount of overlap for it to be significant.
#Return -1 if can't get significance..
min_overlap = kwargs['min_overlap'] if ('min_overlap' in kwargs) else 1
#Check all overlaps up to the minimum.
overlap_range_length = int(round((min(mut_nums) - min_overlap) * precision))
overlap_range = np.round(np.linspace(min_overlap, min(mut_nums), overlap_range_length))
for overlap in overlap_range:
pvalue = cooccurprob_approximate(n, overlap, trials, *mut_nums)
if pvalue <= p:
return overlap
return -1
def plotmutnumsvsoverlap(n, constants=[1, 1, 1], p=0.05, mut_precision=0.1, overlap_precision=0.5, minFraction = 0.1,
maxFraction = 0.5, trials=10000):
t = time.time()
global mut_range
global overlaps
length = int(round(mut_precision * n))
constants = np.array(constants)
constants /= (sum(constants) / len(constants)) #normalize the constants
mut_range = np.round(np.linspace(n * minFraction/min(constants), n * maxFraction/max(constants), length))
overlaps = np.zeros(length)
numGenes = 3
for i in range(length):
m = mut_range[i]
mut_nums = [round(m * constants[g]) for g in range(numGenes)]
# overlaps[i] = getsigoverlap(n, mut_nums, p, overlap_precision)
overlaps[i] = precomputesigoverlap(n, trials, p, overlap_precision, *mut_nums)
plt.figure()
plt.title('Alteration Number versus Overlap Needed for Significance. n = ' + str(n) + ', p = ' + str(p))
plt.xlabel("Average Alteration Number. mut_nums = " + str(constants))
plt.ylabel("Overlap for significance")
plt.plot(mut_range, overlaps)
plt.show()
# plt.figure()
# plt.title('Alteration Number versus Cooccurrence Ratio Needed for Significance. n = ' + str(n) + ', p = ' + str(p))
# plt.xlabel("Average Alteration Number. mut_nums = " + str(constants))
# plt.ylabel("Ratio for significance")
# plt.plot(mut_range, sigratios)
# plt.show()
print "Time used: ", time.time() - t, " for n = ", n, ", length = ", length, \
"mutation precision = ", mut_precision, ", overlap precision = ", overlap_precision
return mut_range, overlaps
def multiplotmutnumsvsoverlap(n, constants_list=[[1, 1, 1], [1, 2, 2]], p=0.05, mut_precision=0.1, overlap_precision=0.5,
minFraction = 0.1, maxFraction = 0.5):
mut_range_list = []
overlap_list = []
for constants in constants_list:
mut_range, overlap = plotmutnumsvsoverlap(n, constants, p, mut_precision, overlap_precision, minFraction, maxFraction)
mut_range_list.append(mut_range)
overlap_list.append(overlap)
return mut_range_list, overlap_list
def ratiopvalue(n, mut_nums):
length = 20
overlap_range = np.linspace(0, min(mut_nums) * 2 / 3, length)
ratios = np.zeros(length)
pvalues = np.zeros(length)
for i in range(length):
overlap = overlap_range[i]
ratios[i] = cooccurrence_ratio(n, mut_nums, overlap)
pvalues[i] = cooccurprob_approximate(n, mut_nums, overlap)
return ratios, pvalues
def plotratiovspvalue(n, mut_nums):
ratios, pvalues = ratiopvalue(n, mut_nums)
plt.figure()
plt.plot(ratios, pvalues, 'bo')
plt.xlabel('Cooccurrence Ratio')
plt.ylabel('pvalue')
plt.title('Cooccurrence Ratio vs pvalue for n = ' + str(n) + ' and mutation frequencies' + str(mut_nums))
plt.show()
#
# def random_matrix_row(n, mut_nums):
# """Returns an array of sums across mutations of a random mutation matrix;
# i.e. the row of the total number of mutations each sample has"""
# row = np.zeros(n)
# for m in mut_nums:
# row += np.random.permutation(np.concatenate([np.ones(m), np.zeros(n - m)]))
# return row
#
# def matrix_row_mutex(row, overlap = 0):
# for entry in row:
# if (entry > 1.1): #in case of floating point error
# overlap -= 1
# if (overlap < 0):
# return False
# return True
#
# def random_mut_matrix(n, mut_nums):
# matrix = []
# for m in mut_nums:
# random_row = np.random.permutation(np.concatenate([np.ones(m), np.zeros(n - m)]))
# matrix.append(random_row)
# return matrix
# def matrix_mutex(matrix, overlap = 0):
# column_sum = sum(matrix)
# for entry in column_sum:
# if (entry > 1.1): #in case of floating point error
# overlap -= 1
# if (overlap < 0):
# return False
# return True
# def mutexprob_approximate(n, mut_nums, overlap = 0, trials = 10000):
# mutex_num = 0
# for i in range(trials):
# row = random_matrix_row(n, mut_nums)
# mutex_num += matrix_row_mutex(row, overlap)
# return mutex_num * 1.0/trials