-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOMP565_A2_finemap.py
387 lines (291 loc) · 11.8 KB
/
COMP565_A2_finemap.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
import pandas as pd
import numpy as np
from itertools import combinations
from scipy.stats import multivariate_normal
import matplotlib.pyplot as plt
from tqdm import tqdm
import scipy
import math
from abc import ABC, abstractmethod
# GitHub: https://github.com/mikemikezhu/finemap
"""
Constants
"""
DATA_PATH_LD = "data/LD.csv.gz"
DATA_PATH_Z_SCORE = "data/zscore.csv.gz"
DATA_PATH_SNP_PIP_REAL = "data/SNP_pip.csv.gz"
COMPRESSION_GZIP = "gzip"
CAUSAL_SNPS = ["rs10104559", "rs1365732", "rs12676370"]
CALCULATOR_TYPE_BAYES_FACTOR = "calculator_bayes_factor"
CALCULATOR_TYPE_PRIOR = "calculator_prior"
CALCULATOR_TYPE_POSTERIOR = "calculator_posterior"
CALCULATOR_TYPE_PIP = "calculator_pip"
"""
Calculator Factory
"""
class AbstractCalculator(ABC):
@abstractmethod
def calculate(self, **kwargs):
raise NotImplementedError("Abstract method shall not be implemented")
@abstractmethod
def is_eligible(self, type: str) -> bool:
raise NotImplementedError("Abstract method shall not be implemented")
class CalculatorFactory:
def __init__(self) -> None:
self._calculators = []
children = AbstractCalculator.__subclasses__()
if len(children) > 0:
for child in children:
self._calculators.append(child())
def get_calculator(self, type: str) -> AbstractCalculator:
if len(self._calculators) == 0:
return None
for calculator in self._calculators:
if calculator.is_eligible(type):
return calculator
return None
"""
Q1: Bayes Factor
"""
class BayesFactorCalculator(AbstractCalculator):
def calculate(self, **kwargs):
result = None
configs = kwargs.get("configs")
ld = kwargs.get("ld")
z_score = kwargs.get("z_score")
silent = kwargs.get("silent") if kwargs.get(
"silent") is not None else False
assert configs is not None
assert ld is not None
assert z_score is not None
# Z Score
causal_z_scores = []
for snp in configs:
causal_z_score = (
(z_score[z_score['Unnamed: 0'] == snp])["V1"]).to_numpy()
causal_z_scores.append(causal_z_score)
causal_z_scores = np.asarray(causal_z_scores)
causal_z_scores = causal_z_scores[:, 0]
if not silent:
print("Causal Z Scores: \n", causal_z_scores)
# RCC
column_idx = [ld.columns.get_loc(c) for c in configs]
row_idx = [id - 1 for id in column_idx]
rcc = ld.iloc[row_idx, column_idx].to_numpy()
np.fill_diagonal(rcc, 1.0)
if not silent:
print("RCC: \n", rcc)
# RCC Star
N = 498
s_2 = 0.005
sigma_cc = N * s_2 * np.identity(len(configs)) # Sigma_cc
if not silent:
print("Sigma CC: \n", sigma_cc)
rcc_star = rcc + rcc @ sigma_cc @ rcc # RCC Star = RCC + RCC @ Sigma_cc @ RCC
if not silent:
print("RCC Star: \n", rcc_star)
# Bayes Factor
mean = np.zeros(shape=(len(configs),))
numerator = multivariate_normal.pdf(causal_z_scores,
mean=mean,
cov=rcc_star)
denominator = multivariate_normal.pdf(causal_z_scores,
mean=mean,
cov=rcc)
result = numerator / denominator
return result
def is_eligible(self, type: str) -> bool:
return type == CALCULATOR_TYPE_BAYES_FACTOR
"""
Q2: Prior
"""
class PriorCalculator(AbstractCalculator):
def calculate(self, **kwargs):
m = kwargs.get("num_total_snps")
k = kwargs.get("num_causal_snps")
assert m is not None
assert k is not None
# Prior: (1 / m)^k * ((m - 1) / m)^(m - k)
return (1 / m)**k * ((m - 1) / m)**(m - k)
def is_eligible(self, type: str) -> bool:
return type == CALCULATOR_TYPE_PRIOR
"""
Q3: Posterior
"""
class PosteriorCalculator(AbstractCalculator):
def calculate(self, **kwargs):
ld = kwargs.get("ld")
z_score = kwargs.get("z_score")
total_snps = kwargs.get("total_snps")
max_causal_snps = kwargs.get("max_causal_snps")
bayes_factor_calculator = kwargs.get("bayes_factor_calculator")
prior_calculator = kwargs.get("prior_calculator")
assert ld is not None
assert z_score is not None
assert total_snps is not None
assert max_causal_snps is not None
assert bayes_factor_calculator is not None
assert prior_calculator is not None
configs = []
for num_causal in range(1, max_causal_snps + 1):
configs += list(combinations(total_snps, num_causal))
print("Total {} configs".format(len(configs)))
columns = list(total_snps) + ['marginal', 'posterior', 'valid']
configs_df = pd.DataFrame(np.zeros((len(configs), len(columns))),
columns=columns)
for i, c in enumerate(tqdm(configs)):
c = list(c)
# Bayes factor (Likelihood)
pos_bf = bayes_factor_calculator.calculate(configs=c,
ld=ld,
z_score=z_score,
silent=True)
# Prior
pos_prior = prior_calculator.calculate(num_total_snps=len(total_snps),
num_causal_snps=len(c))
# Mark the SNPs in the configuration
configs_df.loc[i, c] = [1.0] * len(c)
# Likelihood * prior
configs_df.loc[i, 'marginal'] = pos_bf * pos_prior
# Configuration is valid
configs_df.loc[i, 'valid'] = 1.0
# Remove invalid configurations
configs_df = configs_df[configs_df["valid"] == 1.0]
assert np.any(configs_df["valid"].to_numpy() == 0.0) == False
print(configs_df.shape)
# Calculate posteriors
marginals = configs_df['marginal'].to_numpy()
total_marginals = np.sum(marginals)
posteriors = marginals / total_marginals
configs_df.loc[:, 'posterior'] = posteriors
print("Configs: {}\n", configs_df)
print("Total marginals: ", total_marginals)
print("Posteriors: ", posteriors)
return posteriors, configs_df
def is_eligible(self, type: str) -> bool:
return type == CALCULATOR_TYPE_POSTERIOR
"""
Q4: PIP
"""
class PipCalculator(AbstractCalculator):
def calculate(self, **kwargs):
posteriors = kwargs.get("posteriors")
configs_df = kwargs.get("configs_df")
total_snps = kwargs.get("total_snps")
assert posteriors is not None
assert configs_df is not None
assert total_snps is not None
result = []
for snp in total_snps:
snp_df = configs_df[configs_df[snp] == 1.0]
snp_posteriors = snp_df["posterior"].to_numpy()
snp_pip = np.sum(snp_posteriors) / np.sum(posteriors)
result.append(snp_pip)
return result
def is_eligible(self, type: str) -> bool:
return type == CALCULATOR_TYPE_PIP
"""
Main
"""
def main():
# Load data
ld = pd.read_csv(DATA_PATH_LD, compression=COMPRESSION_GZIP)
z_score = pd.read_csv(DATA_PATH_Z_SCORE, compression=COMPRESSION_GZIP)
pip_real = pd.read_csv(DATA_PATH_SNP_PIP_REAL,
compression=COMPRESSION_GZIP)
print("LD shape: {} \n{}".format(ld.shape, ld.head()))
print("Z Score: {} \n{}".format(z_score.shape, z_score.head()))
print("PIP real: {} \n{}".format(pip_real.shape, pip_real.head()))
# Init calculator factory
calculator_factory = CalculatorFactory()
bayes_factor_calculator = calculator_factory.get_calculator(
CALCULATOR_TYPE_BAYES_FACTOR)
prior_calculator = calculator_factory.get_calculator(CALCULATOR_TYPE_PRIOR)
# Calculate posterior
posterior_calculator = calculator_factory.get_calculator(
CALCULATOR_TYPE_POSTERIOR)
total_snps = z_score.iloc[:, 0].to_numpy()
num_total_snps = len(total_snps)
posteriors, configs_df = posterior_calculator.calculate(ld=ld,
z_score=z_score,
total_snps=total_snps,
num_total_snps=num_total_snps,
max_causal_snps=3, # Assume at maximum 3 causal SNPs
bayes_factor_calculator=bayes_factor_calculator, # Dependency injection
prior_calculator=prior_calculator) # Dependency injection
# Plot
sorted_posteriors = np.sort(posteriors)
plt.clf()
plt.scatter(np.arange(len(sorted_posteriors)), sorted_posteriors)
plt.xlabel("Sorted configurations")
plt.ylabel("Configuration posterior")
plt.title("Posteriors of all of the valid configurations in increasing order")
plt.grid(0.5)
plt.savefig("posteriors.png")
plt.close()
# Caclulate PIP
pip_calculator = calculator_factory.get_calculator(CALCULATOR_TYPE_PIP)
pips = pip_calculator.calculate(posteriors=posteriors,
configs_df=configs_df,
total_snps=total_snps)
pips = np.asarray(pips)
print("PIP: ", pips)
# Output inferred PIPs
pips_df = pd.DataFrame(columns=["SNPs", "Inferred PIPs"])
pips_df[pips_df.columns[0]] = total_snps
pips_df[pips_df.columns[1]] = pips
pips_df.to_csv("COMP565 A2 SNP pip.csv.gz", compression=COMPRESSION_GZIP)
print(pips_df)
# Plot
causal_index = np.empty((len(CAUSAL_SNPS)), dtype=int)
for i, snp in enumerate(CAUSAL_SNPS):
index = np.argwhere((total_snps == snp))[0, 0]
causal_index[i] = index
total_snps_index = np.arange(len(pips))
non_causal_index = np.delete(total_snps_index, obj=causal_index)
causal_pips = pips[causal_index]
non_causal_pips = pips[non_causal_index]
# Two-tailed test p-value for z-score
p_values = np.empty((num_total_snps), dtype=float)
z_scores = z_score.iloc[:, 1]
for i, z in enumerate(z_scores):
p_value = scipy.stats.norm.sf(abs(z)) * 2
p_values[i] = -1 * math.log10(p_value)
causal_p_values = p_values[causal_index]
non_causal_p_values = p_values[non_causal_index]
plt.clf()
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
fig.suptitle("Inference results")
ax1.scatter(causal_index, causal_p_values,
c="red", label="True causal SNP")
ax1.scatter(non_causal_index, non_causal_p_values,
c="#2b70ad", label="Non-causal SNP", alpha=0.3)
ax1.set_ylabel("-log10p")
# ax1.set_xlabel("SNPs")
ax1.grid(0.5)
ax2.scatter(causal_index, causal_pips, c="red")
ax2.scatter(non_causal_index, non_causal_pips, c="#2b70ad", alpha=0.3)
ax2.set_ylabel("PIP")
ax2.set_xlabel("SNPs")
ax2.grid(0.5)
fig.legend()
fig.savefig("pip.png")
plt.close()
# PIP provided by professor
real_pips = pip_real.iloc[:, 1].to_numpy()
causal_pips = real_pips[causal_index]
non_causal_pips = real_pips[non_causal_index]
plt.clf()
plt.scatter(causal_index, causal_pips,
c="red", label="True causal SNP")
plt.scatter(non_causal_index, non_causal_pips,
c="#2b70ad", label="Non-causal SNP", alpha=0.3)
plt.ylabel("PIP")
plt.xlabel("SNPs")
plt.title("PIP provided by professor")
plt.grid(0.5)
plt.legend()
plt.savefig("ref_pip.png")
plt.close()
if __name__ == "__main__":
main()