-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-tvmodels-diag.R
288 lines (228 loc) · 10.3 KB
/
run-tvmodels-diag.R
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
# Main script for generating the publication outputs
library(dplyr)
library(ggplot2)
library(survival)
library(mgcv)
library(pammtools)
library(cowplot)
library(kableExtra)
library(tidyr)
library(broom)
# -------------------------------------------
# SETUP
# mfrfile="/mnt/HARVEST/ga_cleaned.csv"
# mfrfileF="/mnt/HARVEST/ga_cleaned_f.csv"
# gtfile="/mnt/HARVEST/top1-moba30k-dosage.csv.gz"
# gtfileX="/mnt/HARVEST/top1x-moba30k-dosage.csv.gz"
# gtfileF="/mnt/HARVEST/top1f-moba30k-dosage.csv.gz"
mfrfile = snakemake@input$mfr
mfrfileF = snakemake@input$mfrF
gtfile = snakemake@input$gt
gtfileX = snakemake@input$gtX
gtfileF = snakemake@input$gtF
# some constants:
# palette for plotting. colors based on gg_sci::pal_tron
GTpalette = c("#55AEC4", "#F7C530", "#FF410D")
GA_START_TIME = 169
out_prev = read.table(snakemake@input$maintable, h=T)
# -------------------------------------------
# read in filtered phenotypes, MATERNAL
mfr_mid = read.table(mfrfile, h=T, sep=";")
nrow(mfr_mid) # 26875
# Prepare covariates:
mfr_mid$PARITET_5[mfr_mid$PARITET_5==4] = 3 # parity honestly doesn't need 5 levels
mfr_mid$PARITET_5 = factor(mfr_mid$PARITET_5, levels=c(1,0,2,3))
# NOTE: using height will remove extra 1k missing values, so imputing:
mfr_mid$AA87[is.na(mfr_mid$AA87)] = mean(mfr_mid$AA87, na.rm=T)
# relevel to ensure the oldest and one of the largest batches is ref
mfr_mid$BATCH = factor(mfr_mid$BATCH, levels=c("M12A","FEB18","JAN15","JUN15",
"M12B","M24","MAY16",
"ROT1","ROT2"))
mfr_mid$MISD = factor(mfr_mid$MISD) # to prevent gg_slice complaining
mfr_mid$KJONN = factor(mfr_mid$KJONN)
mfr_mid$FAAR = mfr_mid$FAAR-2000 # center, for neater plot scales
# remove irrelevant timeframe for stability
mfr_mid$GAc = mfr_mid$SVLEN_DG-GA_START_TIME
# just some checks
range(mfr_mid$GAc) # 1 139
# -------------------------------------------
# read in filtered phenotypes, FETAL
mfr_fid = read.table(mfrfileF, h=T, sep=";")
nrow(mfr_fid) # 25515
# Prepare covariates:
mfr_fid$PARITET_5[mfr_fid$PARITET_5==4] = 3 # parity honestly doesn't need 5 levels
mfr_fid$PARITET_5 = factor(mfr_fid$PARITET_5, levels=c(1,0,2,3))
# NOTE: using height will remove extra 1k missing values, so imputing:
mfr_fid$AA87[is.na(mfr_fid$AA87)] = mean(mfr_fid$AA87, na.rm=T)
# relevel to ensure the oldest and one of the largest batches is ref
mfr_fid$BATCH = factor(mfr_fid$BATCH, levels=c("M12A","FEB18","JAN15","JUN15",
"M12B","M24","MAY16",
"ROT1","ROT2"))
mfr_fid$MISD = factor(mfr_fid$MISD) # to prevent gg_slice complaining
mfr_fid$KJONN = factor(mfr_fid$KJONN)
mfr_fid$FAAR = mfr_fid$FAAR-2000 # center, for neater plot scales
# remove irrelevant timeframe for stability
mfr_fid$GAc = mfr_fid$SVLEN_DG-GA_START_TIME
# just some checks
range(mfr_fid$GAc) # 13 139
# -------------------------------------------
# read in genotypes
gt = data.table::fread(gtfile, sep=" ", h=T)
gtinfo = gt[,1:6]
gt = t(gt[,7:ncol(gt)])
gt = data.frame(gt)
gt$SENTRIX_ID = rownames(gt)
rownames(gt) = NULL
gt = filter(gt, !is.na(X1)) # tends to add an empty line in the end
dim(gt)
if(ncol(gt)!=24) stop("Currently hardcoded for 23 SNPs, make sure you adapt all the code appropriately if this changes!")
# read in genotypes for X snps
gtX = data.table::fread(gtfileX, sep=" ", h=T)
gtXinfo = gtX[,1:6]
gtX = t(gtX[,7:ncol(gtX)])
gtX = data.frame(gtX)
gtX$SENTRIX_ID = rownames(gtX)
rownames(gtX) = NULL
gtX = filter(gtX, !is.na(X1)) # tends to add an empty line in the end
dim(gtX)
colnames(gtX) = c("X24", "X25", "SENTRIX_ID")
# read in genotypes for fetal snps
gtF = data.table::fread(gtfileF, sep=" ", h=T)
gtFinfo = gtF[,1:6]
gtF = t(gtF[,7:ncol(gtF)])
gtF = data.frame(gtF)
gtF$SENTRIX_ID = rownames(gtF)
rownames(gtF) = NULL
gtF = filter(gtF, !is.na(X1)) # tends to add an empty line in the end
dim(gtF)
colnames(gtF) = c("X26", "X27", "X28", "X29", "SENTRIX_ID")
gt = full_join(gt, gtX, by=c("SENTRIX_ID"))
gt = full_join(gt, gtF, by=c("SENTRIX_ID"))
gtinfo = rbind(gtinfo, gtXinfo, gtFinfo)
# merge w/ pheno data
merged = inner_join(gt, mfr_mid, by=c("SENTRIX_ID"))
nrow(merged) # all 26875 for autosomes
# create ped form for PAMs
ped = as_ped(merged, Surv(GAc, hadevent)~ X1 + X2 + X3 + X4 + X5 + X6 +
X7 + X8 + X9 + X10 + X11 + X12 +
X13 + X14 + X15 + X16 + X17 + X18 +
X19 + X20 + X21 + X22 + X23 +
X24 + X25 +
BATCH + MAGE + FAAR + AA87 + KJONN + MISD + PARITET_5,
id = "id", cut=c(0,seq(20, 137, by=7)))
nrow(ped) # ~380k
# -------------------------------------------
# Analysis loop MATERNAL
out = data.frame(snpnum=1:29, rsid=NA, ref=NA, eff=NA,
pc.int.beta=NA, pc.int.p=NA, cox.sch.p=NA,
cox.beta=NA, cox.se=NA, pc.beta=NA, pc.se=NA)
for(snpnum in 1:25){
print(paste("Working on SNP", snpnum))
# assign the right SNP to GT
merged$GT = merged[,paste0("X",snpnum)]
ped$GT = ped[,paste0("X",snpnum)]
# store marker info
out[snpnum,"rsid"] = gtinfo[snpnum, "ID"]
# NOTE: flipping alleles to effect=minor alignment,
# because the last (factor) analysis is sensitive to that.
if(mean(merged$GT)>1){
merged$GT = 2-merged$GT
ped$GT = 2-ped$GT
# store alleles
out[snpnum,c("ref", "eff")] = gtinfo[snpnum, c("ALT", "REF")]
} else {
out[snpnum,c("ref", "eff")] = gtinfo[snpnum, c("REF", "ALT")]
}
# run the PAM w/ linear interaction on GT dosage
print("PAM model w/ linear interaction...")
mod.tv.pc.int = bam(ped_status ~ ti(tend,bs='cr',k=11) + GT + GT:tend +
BATCH + poly(MAGE,2) + FAAR + AA87 + KJONN + MISD + PARITET_5,
data=ped, offset=offset, family=poisson())
# report the p-value and edf for the smooth
out.tmp = tidy(mod.tv.pc.int, parametric=T)
out[snpnum,c("pc.int.beta", "pc.int.p")] = out.tmp[out.tmp$term=="GT:tend",c("estimate", "p.value")]
# run PAM w/o smooth terms for comparison v Cox
print("no-interaction models...")
mod.pc = bam(ped_status ~ s(tend,bs='cr',k=11) + GT + BATCH +
poly(MAGE,2) + FAAR + AA87 + KJONN + MISD + PARITET_5,
data=ped, offset=offset, family=poisson())
mod.ph = coxph(Surv(GAc, hadevent) ~ GT + BATCH + poly(MAGE,2) + FAAR +
AA87 + KJONN + MISD + PARITET_5, data=merged)
# confirm that doesn't differ much
out.tmp = tidy(mod.pc,parametric=T)
out[snpnum,c("pc.beta", "pc.se")] = out.tmp[out.tmp$term=="GT", c("estimate", "std.error")]
out.tmp = tidy(mod.ph)
out[snpnum,c("cox.beta", "cox.se")] = out.tmp[out.tmp$term=="GT", c("estimate", "std.error")]
# do the schoenfeld residuals test with KM transform
out.tmp = cox.zph(mod.ph)$table
out[snpnum,"cox.sch.p"] = out.tmp[rownames(out.tmp)=="GT", "p"]
}
# merge w/ pheno data
merged = inner_join(gt, mfr_fid, by=c("SENTRIX_ID"))
nrow(merged) # all 25515 for autosomes
# create ped form for PAMs
ped = as_ped(merged, Surv(GAc, hadevent)~ X26 + X27 + X28 + X29 +
BATCH + MAGE + FAAR + AA87 + KJONN + MISD + PARITET_5,
id = "id", cut=c(0,seq(20, 137, by=7)))
nrow(ped) # ~380k
# -------------------------------------------
# Analysis loop FETAL
for(snpnum in 26:29){
print(paste("Working on SNP", snpnum))
# assign the right SNP to GT
merged$GT = merged[,paste0("X",snpnum)]
ped$GT = ped[,paste0("X",snpnum)]
# store marker info
out[snpnum,"rsid"] = gtinfo[snpnum, "ID"]
# NOTE: flipping alleles to effect=minor alignment,
# because the last (factor) analysis is sensitive to that.
if(mean(merged$GT)>1){
merged$GT = 2-merged$GT
ped$GT = 2-ped$GT
# store alleles
out[snpnum,c("ref", "eff")] = gtinfo[snpnum, c("ALT", "REF")]
} else {
out[snpnum,c("ref", "eff")] = gtinfo[snpnum, c("REF", "ALT")]
}
# run the PAM w/ linear interaction on GT dosage
print("PAM model w/ linear interaction...")
mod.tv.pc.int = bam(ped_status ~ ti(tend,bs='cr',k=11) + GT + GT:tend +
BATCH + poly(MAGE,2) + FAAR + AA87 + KJONN + MISD + PARITET_5,
data=ped, offset=offset, family=poisson())
# report the p-value and edf for the smooth
out.tmp = tidy(mod.tv.pc.int, parametric=T)
out[snpnum,c("pc.int.beta", "pc.int.p")] = out.tmp[out.tmp$term=="GT:tend",c("estimate", "p.value")]
# run PAM w/o smooth terms for comparison v Cox
print("no-interaction models...")
mod.pc = bam(ped_status ~ s(tend,bs='cr',k=11) + GT + BATCH +
poly(MAGE,2) + FAAR + AA87 + KJONN + MISD + PARITET_5,
data=ped, offset=offset, family=poisson())
mod.ph = coxph(Surv(GAc, hadevent) ~ GT + BATCH + poly(MAGE,2) + FAAR +
AA87 + KJONN + MISD + PARITET_5, data=merged)
# confirm that doesn't differ much
out.tmp = tidy(mod.pc,parametric=T)
out[snpnum,c("pc.beta", "pc.se")] = out.tmp[out.tmp$term=="GT", c("estimate", "std.error")]
out.tmp = tidy(mod.ph)
out[snpnum,c("cox.beta", "cox.se")] = out.tmp[out.tmp$term=="GT", c("estimate", "std.error")]
# do the schoenfeld residuals test with KM transform
out.tmp = cox.zph(mod.ph)$table
out[snpnum,"cox.sch.p"] = out.tmp[rownames(out.tmp)=="GT", "p"]
}
# attach locus and pval from the maintext PAM model
out = left_join(out, out_prev[,c("rsid", "locus", "chr", "pos", "ref", "eff", "EAF", "lin.beta", "pc.sm.p")],
by=c("rsid", "ref", "eff"))
out
write.table(out, snakemake@output$diagtable, sep="\t", quote=F, row.names = F)
ggplot(out, aes(x=cox.beta, y=pc.beta)) +
geom_vline(xintercept = 0, col="grey80") + geom_hline(yintercept = 0, col="grey80") +
geom_abline(slope = 1, col="grey70") +
geom_errorbar(aes(ymin=pc.beta-pc.se, ymax=pc.beta+pc.se), col="#FF410D", alpha=0.3) +
geom_errorbarh(aes(xmin=cox.beta-cox.se, xmax=cox.beta+cox.se), col="#FF410D", alpha=0.3) +
geom_point(col="grey20", pch=18) +
xlab("log hazard ratio, Cox regression") + ylab("log hazard ratio, PAM") +
coord_fixed() + theme_bw()
ggsave(snakemake@output$coxdiagplot, width=4, height=4, units="in")
out[,c("locus", "rsid", "chr", "pos", "ref", "eff", "EAF", "lin.beta", "pc.sm.p", "pc.int.p", "cox.sch.p")] %>%
arrange(pc.sm.p) %>%
mutate_at(c("EAF", "lin.beta", "pc.sm.p", "pc.int.p", "cox.sch.p"), prettyNum, digits=2) %>%
kable(format="simple")