-
Notifications
You must be signed in to change notification settings - Fork 6
/
parameter_estimation.py
180 lines (148 loc) · 5.79 KB
/
parameter_estimation.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
import scipy
import scipy.optimize
import random
import math
import gzip
import statsmodels.api as sm
lowess = sm.nonparametric.lowess
import re
import operator as op
from functools import reduce
import numpy as np
w_rho_vals = []
def compute_mafs(vcf_input,max_window_size):
global w_rho_vals
# w_rho_vals.append(1)
f = gzip.open(vcf_input,'rt')
entries_started = False
expected_vals = []
site_counter = 0
mafs = []
for line in f:
if ('#CHROM' in line):
entries_started = True
elif (entries_started):
_values = line.split()
_pos = _values[1]
alt = _values[4]
if (len(alt.split(',')) > 1):
continue
i = 2
while(i < len(_values) and _values[i] != 'GT'):
i += 1
i += 1
tags = _values[7]
i = 9
num_ones = 0
num_zeros = 0
while (i < len(_values)):
vals = re.split('\||/',_values[i])
if (vals[0] == '1'):
num_ones = num_ones + 1
elif (vals[0] == '0'):
num_zeros = num_zeros + 1
if (vals[1] == '1'):
num_ones = num_ones + 1
elif (vals[1] == '0'):
num_zeros = num_zeros + 1
i = i + 1
v = min(num_zeros,num_ones)/float(num_zeros+num_ones)
mafs.append(v)
f.close()
x_vals = [0]
y_vals = [0]
for o in range(1,max_window_size):
window_size = o
expected_vals = []
for i in range(0,len(mafs),window_size):
expected_maf = 0
_sum = 0.0
for j in range(0,window_size):
if (i + j >= len(mafs)):
continue
_sum += mafs[i+j]
for j in range(0,window_size):
if (i + j >= len(mafs)):
continue
if (_sum != 0):
expected_maf += (mafs[i+j] * mafs[i+j]/_sum)
expected_vals.append(expected_maf)
import numpy
_a = numpy.array(expected_vals)
pa = numpy.percentile(_a,1)
rho_v = pa*pa + (1-pa)*(1-pa)
x_vals.append(o)
y_vals.append(rho_v)
w_rho_vals = lowess(y_vals, x_vals,frac=0.1,return_sorted=False)
#for k in range(0,len(x_vals)):
# print (x_vals[k],w_rho_vals[k])
# for j in w_rho_vals:
# print l,rho_v
def ncr_old(n, r):
r = min(r, n-r)
numer = reduce(op.mul, range(n, n-r, -1), 1)
denom = reduce(op.mul, range(1, r+1), 1)
return numer / denom
def w_rho_func(_w):
if (_w >= len(w_rho_vals)):
return w_rho_vals[len(w_rho_vals)-1]
else:
# w = int(round(_w,0))
return w_rho_vals[int(_w)]
def ncr(n,r):
f = math.factorial
return f(n) / f(r) / f(n-r)
def fp(e,N,L,rho,r,c,w):
sum = 0.0
#rho = w_rho_func(w)
for i in range(0,c):
sum += (ncr(r,i)* ((rho**(L/w))**i) * ( (1-rho**(L/w))**(r-i) ))
return 1 - sum
def tp(er,N,L,rho,r,c,w):
sum = 0.0
for i in range(0,c):
sum += (ncr(r,i) * (math.exp(-(er*L)/w)**i) * ((1-math.exp(-(L*er)/w))**(r-i)) )
return 1 - sum
def compute_w(error_rate,N,L,rho,r,c,max_w=300):
fun = lambda w: 0.5* N*(N-1)*fp(error_rate,N,L,w_rho_func(w),r,c,w) / tp(error_rate,N,L,w_rho_func(int(w)),r,c,w)
#bnds = ((0, None))
#x0 = 40
lambda_val = 0.5* N*(N-1)
w_min = -1
_started = False
for w in range(1,max_w):
_tp = tp(error_rate,N,L,w_rho_func(int(w)),r,c,w)
_fp = fp(error_rate,N,L,w_rho_func(w),r,c,w)
if (round(_tp,2) - 0.5* N*(N-1)* round(_fp,2)) == 1:
if (_started):
continue
else:
w_min = w
_started = True
elif _started:
print (w_min,w)
return 0
print (w_min,max_w)
#cons = ({'type': 'ineq', 'fun': lambda w: 0.5*N*(N-1)*fp(error_rate,N,L,w_rho_func(w),r,c,w) - tp(error_rate,N,L,w_rho_func(int(w)),r,c,w)})
#res = scipy.optimize.minimize(fun,[x0], method='COBYLA', tol=1e-1,bounds=bnds)#,constraints=cons)
#print (res)
import sys
if __name__ == '__main__':
if (len(sys.argv) < 2):
print ("Usage: python parameter_estimation <vcf_input_file> <error_rate> <num_haplotypes> <min_snps>, or\\ python parameter_estimation <vcf_input_file> <error_rate> <num_haplotypes> <min_snps> <num_run> <num_success>" )
sys.exit(0)
vcf_input = sys.argv[1] # VCF input file
error_rate = float(sys.argv[2])
num_haps = int(sys.argv[3])
min_length_SNPs = int(sys.argv[4])
num_runs = 10
num_success = 2
if (len(sys.argv) > 4):
num_runs = int(sys.argv[5])
num_success = int(sys.argv[6])
max_window_size = 300
#error_rate = 0.0025
#min_length_SNPs = 12000#14000
rho_initial = 0.9
compute_mafs(vcf_input,max_window_size)
compute_w(error_rate,num_haps,min_length_SNPs,rho_initial,num_runs,num_success,max_window_size)