-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtxburstPL.py
236 lines (209 loc) · 8.37 KB
/
txburstPL.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
#!/usr/bin/python3
from scipy import special
from scipy.stats import poisson,norm, chi2
from scipy.special import j_roots
from scipy.special import beta as beta_fun
from scipy.optimize import minimize
import argparse
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
from joblib import delayed,Parallel
import os
from scipy.interpolate import interp1d
from scipy.optimize import minimize, fsolve
from scipy import special
from scipy.stats import poisson,norm, chi2
from scipy.special import j_roots
from scipy.special import beta as beta_fun
import numpy as np
def dBP(at, alpha, bet, lam):
at.shape = (len(at), 1)
np.repeat(at, 50, axis = 1)
def fun(at, m):
if(max(m) < 1e6):
return(poisson.pmf(at,m))
else:
return(norm.pdf(at,loc=m,scale=np.sqrt(m)))
x,w = j_roots(50,alpha = bet - 1, beta = alpha - 1)
gs = np.sum(w*fun(at, m = lam*(1+x)/2), axis=1)
prob = 1/beta_fun(alpha, bet)*2**(-alpha-bet+1)*gs
return(prob)
def ll_kon(x, l, obs):
mu = x[0]
v = x[1]
return(-np.sum(np.log(dBP(obs,l,mu,v) + 1e-10)))
def ll_burst_size(x, burst_size, obs):
l = x[0]
v = x[1]
mu = v/burst_size
return(-np.sum(np.log(dBP(obs,l,mu,v) + 1e-10)))
def minll_kon(x, kon, obs):
bnds = ((1e-3, 1e3), (1,1e10))
res = minimize(ll_kon, x, args = (kon,obs), method='L-BFGS-B', bounds = bnds)
ll = res.fun
x0 = res.x
return ll, x0
def minll_burst_size(x, burst_size, obs):
bnds = ((1e-3, 1e3), (1,1e6))
res = minimize(ll_burst_size, x, args = (burst_size,obs), method='L-BFGS-B', bounds = bnds)
ll = res.fun
x0 = res.x
return ll, x0
def get_h(param):
x = np.log10(param)
h = 10**(x-2)
return(h)
def kon_ll(vals,start_estim):
cutoff = chi2.ppf(1-alph, 1)/2
x0 = [start_estim[1], start_estim[2]]
bnds = ((1e-3, 1e3), (1,1e10))
N = 100
start = start_estim[0]
res = minimize(ll_kon, x0, args = (start,vals), method='L-BFGS-B', bounds = bnds)
h = get_h(start)
h = 5*h
ll_p = res.fun
ll_l = np.zeros(N)
kon_l = np.array([])
try:
for i in range(N):
kon = start - h*i
if kon <= 0:
break
kon_l = np.append(kon_l, kon)
try:
res = minimize(ll_kon, res.x, args = (kon,vals), method='L-BFGS-B', bounds = bnds)
except ValueError:
break
ll_l[i] = res.fun
if (2*(ll_l[i] - min(ll_l[ll_l > 0])) > cutoff + 0.5) and (ll_l[i] > ll_l[i-1]):
break
except ValueError:
return np.array([np.nan,np.nan,np.nan]), np.nan,np.nan
ll_l = ll_l[:i+1]
ll_l = ll_l[::-1]
kon_l = kon_l[::-1]
res = minimize(ll_kon, x0, args = (start,vals), method='L-BFGS-B', bounds = bnds)
ll_u = np.zeros(N)
kon_u = np.array([])
try:
for j in range(N):
kon = start + h*j
kon_u = np.append(kon_u, kon)
try:
res = minimize(ll_kon, res.x, args = (kon,vals), method='L-BFGS-B', bounds = bnds)
except ValueError:
break
ll_u[j] = res.fun
if (2*(ll_u[j] - min(ll_u[ll_u > 0])) > cutoff + 0.5) and (ll_u[j] > ll_u[j-1]):
break
except ValueError:
return np.array([np.nan,np.nan,np.nan]), np.nan, np.nan
ll_u = ll_u[:j+1]
ll = np.concatenate((ll_l[:-1],np.array([ll_p]),ll_u[1:]))
kon_indexed = np.concatenate((kon_l[:-1],np.array([start]),kon_u[1:])).squeeze()
ll_ratio = 2*(ll - min(ll)).squeeze()
minimum_idx = np.argmin(ll_ratio)
ll_right_side = ll_ratio[minimum_idx:]
ll_left_side = ll_ratio[:minimum_idx]
minimum = kon_indexed[minimum_idx]
kon_right_side = kon_indexed[minimum_idx:]
kon_left_side = kon_indexed[:minimum_idx]
try:
f_1 = interp1d(ll_left_side,kon_left_side, kind='cubic')
f_2 = interp1d(ll_right_side,kon_right_side , kind='cubic')
res = np.array([minimum, f_1(cutoff), f_2(cutoff)])
return res, kon_indexed, ll_ratio
except (ValueError,np.linalg.linalg.LinAlgError, TypeError):
return np.array([minimum,np.nan,np.nan]), kon_indexed, ll_ratio
def burst_size_ll(vals, start_estim):
cutoff = chi2.ppf(1-alph, 1)/2
x0 = [start_estim[0], start_estim[2]]
bnds = ((1e-3, 1e3), (1,1e10))
N = 100
start = start_estim[2]/start_estim[1]
res = minimize(ll_burst_size, x0, args = (start,vals), method='L-BFGS-B', bounds = bnds)
h = get_h(start)
h = 3*h
ll_p = res.fun
ll_l = np.zeros(N)
burst_size_l = np.array([])
try:
for i in range(N):
burst_size = start - h*i
if burst_size <= 0:
break
burst_size_l = np.append(burst_size_l, burst_size)
try:
res = minimize(ll_burst_size, res.x, args = (burst_size,vals), method='L-BFGS-B', bounds = bnds)
except ValueError:
break
ll_l[i] = res.fun
if (2*(ll_l[i] - min(ll_l[ll_l > 0])) > cutoff + 0.5) and (ll_l[i] > ll_l[i-1]):
break
except ValueError:
return np.array([np.nan,np.nan,np.nan]), np.nan,np.nan
ll_l = ll_l[:i+1]
ll_l = ll_l[::-1]
burst_size_l = burst_size_l[::-1]
res = minimize(ll_burst_size, x0, args = (start,vals), method='L-BFGS-B', bounds = bnds)
ll_u = np.zeros(N)
burst_size_u = np.array([])
try:
for j in range(N):
burst_size = start + h*j
burst_size_u = np.append(burst_size_u, burst_size)
try:
res = minimize(ll_burst_size, res.x, args = (burst_size,vals), method='L-BFGS-B', bounds = bnds)
except ValueError:
break
ll_u[j] = res.fun
if (2*(ll_u[j] - min(ll_u[ll_u > 0])) > cutoff + 0.5) and (ll_u[j] > ll_u[j-1]):
break
except ValueError:
return np.array([np.nan,np.nan,np.nan]), np.nan,np.nan
ll_u = ll_u[:j+1]
ll = np.concatenate((ll_l[:-1],np.array([ll_p]),ll_u[1:]))
burst_size_indexed = np.concatenate((burst_size_l[:-1],np.array([start]),burst_size_u[1:])).squeeze()
ll_ratio = 2*(ll - min(ll)).squeeze()
minimum_idx = np.argmin(ll_ratio)
ll_right_side = ll_ratio[minimum_idx:]
ll_left_side = ll_ratio[:minimum_idx]
minimum = burst_size_indexed[minimum_idx]
burst_size_right_side = burst_size_indexed[minimum_idx:]
burst_size_left_side = burst_size_indexed[:minimum_idx]
try:
f_1 = interp1d(ll_left_side,burst_size_left_side, kind='linear')
f_2 = interp1d(ll_right_side,burst_size_right_side , kind='linear')
res = np.array([minimum, f_1(cutoff), f_2(cutoff)])
return res, burst_size_indexed, ll_ratio
except (ValueError,np.linalg.linalg.LinAlgError, TypeError):
return np.array([minimum,np.nan,np.nan]), burst_size_indexed, ll_ratio
def PL2(vals, start_estim):
vals_ = np.copy(vals)
res_kon, kon_idx, kon_ll_ratio = kon_ll(vals_, start_estim)
res_burst_size, bs_idx, bs_ll_ratio = burst_size_ll(vals_,start_estim)
return start_estim, res_kon, res_burst_size
parser = argparse.ArgumentParser(description='Confidence intervals on parameters of bursting kinetics from scRNA-seq data')
parser.add_argument('--file', metavar='file', type=str, nargs=1,help='.csv file file with allelic-resolution transcript counts' )
parser.add_argument('--njobs', default=[50], nargs=1, type=int, help='Number of jobs for the parallelization, default 50')
parser.add_argument('--alpha', default=[0.05], nargs=1, type=float, help='Alpha significance level (default 0.05)')
parser.add_argument("--MLFile", default=None, nargs=1, type=str, help='Maximum Likelihood file (required)')
args = parser.parse_args()
filename = args.file[0]
njobs = args.njobs[0]
ML = args.MLFile[0]
alph = args.alpha[0]
print('Reading file ' + filename)
counts = pd.read_csv(filename, index_col=0)
print('Reading file ' + ML)
MLPickle = pd.read_pickle(ML)
MLPickle = MLPickle[MLPickle[1]][0]
MLPickle = MLPickle[~MLPickle.index.duplicated(keep='first')]
genes = np.intersect1d(MLPickle.index.values, counts.index.values)
PL_CIs = Parallel(n_jobs=njobs, verbose = 3)(delayed(PL2)(np.around(counts.loc[gene][pd.notnull(counts.loc[gene])]), MLPickle[gene]) for gene in genes)
base1 = os.path.splitext(os.path.basename(filename))[0]
base = base1 + '_PL.pkl'
print('Saving result to: {}'.format(base))
pd.to_pickle(pd.DataFrame(PL_CIs, index=counts.loc[genes].index), base)