-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path02_prs.visualization.py
295 lines (270 loc) · 12.4 KB
/
02_prs.visualization.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
import os
# os.chdir(r"C:\Study\Europe\Torben\small\2020 08 CVD Target scores")
# os.chdir(r"C:\Study\Europe\Torben\small\2020 08 CVD Target scores\testing CAD")
os.chdir(r"C:\Study\Europe\Torben\small\2020 08 CVD Target scores\olink")
import numpy as np
import seaborn as sns
import statsmodels.formula.api as smf
from statsmodels.stats.multitest import fdrcorrection
import matplotlib
import matplotlib.pyplot as plt
import ppscore as pps
import pandas as pd
sns.color_palette()
# Loading PRS scores
df = pd.DataFrame()
for fname in os.listdir("."):
if not fname.endswith(".profile"):
continue
phenotype = fname.split(".")[0]
_df = pd.read_table(fname, sep="\s+").set_index("IID")
if df.shape[0] == 0:
# initialize indices
df = _df[[]].copy()
# TODO: this will use the *first* file's index
# this works with current pipeline, because all files are generated on the same genetic input ->
# thus index is the same everywhere, but this may potentially cause errors
df[phenotype] = _df["SCORESUM"] / _df["CNT2"]
# Loading phenotypes
cardiotraits = ["z_BMI", "chol_total", "chol_hdl", "chol_ldl", "triglycerides",
"waist", "hip", "bp_sys_avg", "bp_dia_avg", "z_bp_sys", "z_bp_dia",
"glucose", "insulin", "HOMA"]
phenotypes_df = pd.read_excel(
r'C:\Study\Europe\Torben\small\2020 08 CVD Target scores\three_scores_without_rare_variants.xlsx').set_index("IID")
phenotypes_df = phenotypes_df[["age"] + cardiotraits]
# Merging
total_df = pd.merge(df, phenotypes_df, left_index=True, right_index=True)
print(total_df.shape)
# Selecting non-obese
samples = [
f"66-{sid.strip()}" for sid in open(r"C:\Study\Europe\Torben\small\2020 08 CVD Target scores\HealthySamples.txt", 'r')]
# we want to exclude the following samples from the PRS selection process, to avoid overfitting
olinked_samples = [
f"66-{sid.strip()}" for sid in open(r"C:\Study\Europe\Torben\small\2020 08 CVD Target scores\Olink_blood_sample_IDs.txt", 'r')]
translate = {
"olink-PGS000217": "ADM_PRS",
"olink-PGS000218": "AGRP_PRS",
"olink-PGS000221": "CCL3_PRS",
"olink-PGS000230": "CXCL1_PRS",
"olink-PGS000233": "Dkk_1_PRS",
"olink-PGS000239": "FGF_23_PRS",
"olink-PGS000240": "FS_PRS",
"olink-PGS000244": "GH_PRS",
"olink-PGS000245": "HB_EGF_PRS",
"olink-PGS000248": "HSP_27_PRS",
"olink-PGS000249": "IL_18_PRS",
"olink-PGS000250": "IL_1ra_PRS",
"olink-PGS000251": "IL_27_PRS",
"olink-PGS000252": "IL_6_PRS",
"olink-PGS000255": "IL_16_PRS",
"olink-PGS000256": "ITGB1BP2_PRS",
"olink-PGS000257": "KIM_1_PRS",
"olink-PGS000259": "LEP_PRS",
"olink-PGS000260": "LOX_1_PRS",
"olink-PGS000266": "MMP_12_PRS",
"olink-PGS000268": "MMP_7_PRS",
"olink-PGS000272": "PAPPA_PRS",
"olink-PGS000273": "PAR_1_PRS",
"olink-PGS000274": "PDGF_subunit_B_PRS",
"olink-PGS000277": "PSGL_1_PRS",
"olink-PGS000278": "PTX3_PRS",
"olink-PGS000279": "RAGE_PRS",
"olink-PGS000280": "REN_PRS",
"olink-PGS000282": "SCF_PRS",
"olink-PGS000285": "TF_PRS",
"olink-PGS000286": "TM_PRS",
"olink-PGS000288": "TNF_R2_PRS",
"olink-PGS000291": "TRAIL_R2_PRS",
"olink-PGS000295": "VEGF_D_PRS",
}
total_df.rename(columns=translate, inplace=True)
# there are 1863 population samples, for whom genetics is available - len(set(total_df.index).intersection(samples))
# of them, 198 have olink data for proteins
print("from here, use code below if you want only pop analysis, otherwise scroll to the bottom")
assert False
# Selecting non-obese - only healthy, have no olink data (to avoid an overfit)
total_df = total_df.loc[total_df.index.intersection(samples).difference(olinked_samples)] # 1665 samples
total_df_sample = total_df
print(total_df_sample.shape)
# Using PPS
matrix_df = pps.matrix(total_df_sample)[['x', 'y', 'ppscore']].pivot(columns='x', index='y', values='ppscore')
fig = plt.figure(figsize=(16, 16))
ax = sns.heatmap(matrix_df, vmin=0, vmax=1, cmap="Blues", linewidths=0.5, annot=True)
axlim = total_df_sample.shape[1]
ax.set_ylim(0, axlim) # a patch for the heatmap in sns - it was broken in matplotlib 3.1.1
plt.savefig("pps.png")
plt.close()
# No findings
# Correlations?
fig = plt.figure(figsize=(16, 16))
# ax = sns.heatmap(total_df_sample.corr()[["CAD-PGS000013"]].abs(), vmin=0, vmax=1, cmap="Blues", annot=True)
ax = sns.heatmap(total_df_sample.corr().abs(), vmin=0, vmax=1, cmap="Blues", annot=True)
axlim = total_df_sample.shape[1]
ax.set_ylim(0, axlim) # a patch for the heatmap in sns - it was broken in matplotlib 3.1.1
plt.savefig("corr.png")
plt.close()
# For olink PRS
# # Correlations?
# fig = plt.figure(figsize=(32, 32))
# # ax = sns.heatmap(total_df_sample.corr()[["CAD-PGS000013"]].abs(), vmin=0, vmax=1, cmap="Blues", annot=True)
# ax = sns.heatmap(total_df_sample.corr(), vmin=-1, vmax=1, cmap="vlag", annot=True)
# axlim = total_df_sample.shape[1]
# ax.set_ylim(0, axlim) # a patch for the heatmap in sns - it was broken in matplotlib 3.1.1
# plt.savefig("corr.png")
# plt.close()
# Analyzing correlations
studied_prs = ['CAD-PGS000012', 'CAD-PGS000013', 'CAD-PGS000018', 'CAD-PGS000116', 'CAD-PGS000296']
negative_control_prs = ['prostatecancer-PGS000333', 'psychiatristvisit-PGS000141']
prs_corrs = total_df_sample.corr().abs().loc[studied_prs+negative_control_prs, studied_prs+negative_control_prs]
print("Average cross-correlation between the PRS")
print("We recommend to exclude the PRS that are unlike others (i.e. outliers with small cross-correlation), unless they perform extremely well")
for prs in studied_prs:
avg_corr = (prs_corrs.loc[prs, studied_prs].sum() - 1) / (len(studied_prs) - 1) # correcting for 1.0 correlation in the table
print(f"{prs}\t{avg_corr:.3f}")
print("Here are the negative controls cross-correlations for comparison")
for prs in negative_control_prs:
avg_corr = prs_corrs.loc[prs, studied_prs].sum() / len(studied_prs)
print(f"{prs}\t{avg_corr:.3f}")
check_phenotypes = cardiotraits
prs_phen_corrs = total_df_sample.corr().abs().loc[studied_prs+negative_control_prs, check_phenotypes]
avg_control_performance = prs_phen_corrs.loc[negative_control_prs].mean(axis=0)
print("Performance-wise: how much each score outperforms negative control PRS average in each phenotype")
print("We recommend picking the best performing one, if exists; "
"otherwise pick a well-correlated with other PRS (so that it does not matter if you picked wrongly) with good performance")
print(prs_phen_corrs.loc[studied_prs] // avg_control_performance)
# -----
# This code was used to figure out that 500 samples will be enough to select a PRS to use, on average
# for N in (100, 300, 500, 1000):
# corrs = []
# for i in range(20):
# total_df_sample = total_df.sample(n=N)
# corrs.append(total_df_sample.corr().abs().values)
# arr = np.array(corrs)
# means = pd.DataFrame(arr.mean(axis=0), index=total_df.columns, columns=total_df.columns)
# stds = pd.DataFrame(arr.std(axis=0), index=total_df.columns, columns=total_df.columns)
# annotations = np.array([f"{means.values[i][j]:.3f}\n+-{stds.values[i][j]:.3f}" for i in range(total_df.shape[1]) for j in range(total_df.shape[1])]).reshape((total_df.shape[1], total_df.shape[1]))
# fig = plt.figure(figsize=(16, 16))
# sns.heatmap(means, vmin=0, vmax=1, cmap="Blues", annot=annotations, fmt='')
# plt.savefig(f"corr-{N}.png")
# plt.close()
# =======
total_df["population"] = total_df.index.isin(samples)
total_df["obese"] = ~total_df["population"]
olink_df = pd.read_table(r"C:\Study\Europe\Torben\small\2020 08 CVD Target scores\olink\CVDII_INF-panel_NPX_20210205.txt", sep="\t").set_index("blood_sample_ID")
olink_df.index = olink_df.index.map(lambda x: f"66-{x}")
correction_table = {
# WARNING: this table only fixes inconsistencies that we needed, not all of them!
# fix the upstream data delivery to ensure correct name matching in the future!
# .y .x issues
"CXCL1.y": "CXCL1",
"SCF.y": "SCF",
"IL18.y": "IL_18", # subject to this and next issue
"CCL3.y": "CCL3",
"FGF_21.x": "FGF_21",
# separation inconsistency (some interleukins are called "ILXX", others are "IL_XX")
"IL10": "IL_10",
"IL16": "IL_16",
"IL1RL2": "IL_1RL2",
"IL6": "IL_6",
"IL7": "IL_7",
"IL8": "IL_8",
"KIM1": "KIM_1",
"MMP12": "MMP_12",
"MMP7": "MMP_7",
"VEGFA": "VEGF_A",
"VEGFD": "VEGF_D",
}
olink_df.rename(columns=correction_table, inplace=True)
# gPCs for the plots
gpcs_df = pd.read_table(r"C:\Study\Europe\Torben\small\2020 08 CVD Target scores\olink\TARGET_mergedPRS_LDpred_v2020.11.20.txt",
sep="\t", usecols=["IID", "pc1", "pc2", "pc3", "pc4"]).set_index("IID")
# gender info - TODO better
gender_df = pd.read_excel(r"C:\Study\Europe\Torben\small\2020 08 CVD Target scores\Phenotypes.xlsx",
usecols=["blood_sample_ID","gender"]).dropna().set_index("blood_sample_ID")
gender_df.index = gender_df.index.map(lambda x: f"66-{x}")
_full_df1 = pd.merge(total_df, olink_df, how="left", left_index=True, right_index=True)
_full_df2 = pd.merge(_full_df1, gender_df, how="left", left_index=True, right_index=True)
full_df = pd.merge(_full_df2, gpcs_df, how="left", left_index=True, right_index=True)
# QC naming
missing_prs = []
for prs_name in translate.values():
prot_name = prs_name[:-4]
if prot_name not in full_df:
missing_prs.append(prot_name)
assert missing_prs == ['TNF_R2'], "Oops! Some of the olink PRS are not available in data - inconsistent naming?"
proteins = list(sorted(set(x[:-4] for x in translate.values()).difference(missing_prs)))
bonf_adj = 1. / len(proteins)
joint_patch_coords = {
'CXCL1': (0.2, 1.15),
'PTX3': (0.25, 2.25),
'SCF': (0.28, 3.5),
'CCL3': (0.31, 4.5),
'TM': (0.31, 6),
}
pop_patch_coords = {
'MMP_12': (0.2, 6.9),
'CCL3': (0.28, 3.2),
'SCF': (0.15, 2.8),
}
for run, _df in (
("Joint", full_df),
("Population", full_df[full_df.population]),
("Obese", full_df[full_df.obese]),
):
full_df_std = (_df - _df.mean()) / _df.std()
results = pd.DataFrame(index=proteins, columns=["pvalue", "-log10(pvalue)", "Beta", "R2_adj", "Significant"])
for p in proteins:
model = smf.ols(formula=f"{p} ~ 1 + {p}_PRS + "
f"C(gender) + age + "
f"pc1 + pc2 + pc3 + pc4",
data=full_df_std,
missing='drop').fit()
results.at[p, "pvalue"] = model.pvalues[f"{p}_PRS"]
results.at[p, "-log10(pvalue)"] = -np.log10(model.pvalues[f"{p}_PRS"])
results.at[p, "Beta"] = model.params[f"{p}_PRS"]
results.at[p, "R2_adj"] = model.rsquared_adj
results.at[p, "Significant"] = True if model.pvalues[f"{p}_PRS"] < 0.05 * bonf_adj else False
colorbar_limits = (-results["Beta"].abs().max() * 1.1,
results["Beta"].abs().max() * 1.1)
ax = sns.scatterplot(
x="Beta", y="-log10(pvalue)",
hue="Beta", palette="vlag", hue_norm=colorbar_limits,
edgecolor="black",
size="Significant", sizes={True: 250, False: 15},
legend=False,
data=results
)
# Patching: adding labels to significant dots
significant_data = results[results["Significant"]][["Beta", "-log10(pvalue)"]].T.to_dict()
print(significant_data)
for name, d in significant_data.items():
if run == "Joint":
y = d['-log10(pvalue)'] - 0.3
if name in joint_patch_coords:
x, y = joint_patch_coords[name]
else:
x = d['Beta'] - 0.02 * len(name)
elif run == "Population":
y = d['-log10(pvalue)'] - 0.2
if name in pop_patch_coords:
x, y = pop_patch_coords[name]
else:
x = d['Beta'] - 0.02 * len(name)
elif run == "Obese":
y = d['-log10(pvalue)'] - 0.2
if name == "IL_27":
x, y = 0.43, 6.2
elif y > 5:
x = d['Beta'] - 0.025 * len(name)
else:
x = d['Beta'] + 0.05
ax.text(x=x, y=y, s=name).set_backgroundcolor("#ffffff")
# Patching: adding a synthetic colorbar to match existing hue_norm
norm = plt.Normalize(*colorbar_limits)
sm = plt.cm.ScalarMappable(cmap="vlag", norm=norm)
sm.set_array([])
ax.figure.colorbar(sm)
ax.set_title(run)
plt.show()
# TODO: do in R - ask Sara or Evelina
is_fdr_significant, adj_pv = fdrcorrection(results["pvalue"])