forked from opain/GenoPred
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbsolute_Conversion.Rmd
More file actions
3339 lines (2531 loc) · 136 KB
/
Absolute_Conversion.Rmd
File metadata and controls
3339 lines (2531 loc) · 136 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Improving interpretability of polygenic scores using only summary statistics"
output:
html_document:
toc: true
theme: united
toc_depth: 3
number_sections: true
toc_float: true
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
<style>
p.caption {
font-size: 1.5em;
}
</style>
```{css, echo=F}
pre code, pre, code {
white-space: pre !important;
overflow-x: scroll !important;
word-break: keep-all !important;
word-wrap: initial !important;
}
```
***
This page describes a project investigatin approaches for converting polygenic scores into interpretable information.
Aims:
* Develop method for converting polygenic Z-scores into absolute estimates using summary statistics
* Develop figures to represent absolute risk
* Validate approach for estimating PRS R2/AUC from summary statistics
See [here](https://www.medrxiv.org/content/10.1101/2021.04.16.21255481v1) for a preprint describing this work.
***
# Converting polygenic Z-scores to absolute estimates
To enable correct intepretation of a polygenic score, the variance explained by the polygenic score must be considered. Furthermore, for binary outcomes the population prevelance must be considered, and for continuous outcomes the population mean and SD must be considered. It is possible to convert relative genetic risk into absolute esimates of an outcome when observed data is available, as 23andMe do, by splitting participants into genetic risk quantiles, and then estimating the mean outcome within each quantile. However, observed data is often not available. Here, we use an alternative approach based on summary statistics only alone.
***
## Describe conversion
***
### Binary outcomes
To convert a polygenic Z-score into an absolute estimate of risk, we must know the predicitve utility of the polygenic score (AUC), and the prevelance of the outcome in the general population. Then it is possible to estimate the proportion of cases within each polygenic score quantile using bivariate-normal distribution.
<details><summary>Show code</summary>
```{R, echo=T, eval=F}
# Thank you for Alex Gillet for her work developing this code.
ccprobs.f <- function(PRS_auc=0.641, prev=0.7463, n_quantile=20){
# Convert AUC into cohen's d
d <- sqrt(2)*qnorm(PRS_auc)
# Set mean difference between cases and control polygenic scores
mu_case <- d
mu_control <- 0
# Estimate mean and variance of polygenic scores across case and control
varPRS <- prev*(1+(d^2) - (d*prev)^2) + (1-prev)*(1 - (d*prev)^2)
E_PRS <- d*prev
# Estimate polygenic score quantiles
by_quant<-1/n_quantile
p_quant <- seq(by_quant, 1-by_quant, by=by_quant)
quant_vals_PRS <- rep(0, length(p_quant))
quant_f_solve <- function(x, prev, d, pq){prev*pnorm(x-d) + (1-prev)*pnorm(x) - pq}
for(i in 1:length(p_quant)){
quant_vals_PRS[i] <- unlist(uniroot(quant_f_solve, prev=prev, d=d, pq= p_quant[i], interval=c(-2.5, 2.5), extendInt = "yes", tol=6e-12)$root)
}
# Create a table for output
ul_qv_PRS <- matrix(0, ncol=2, nrow=n_quantile)
ul_qv_PRS[1,1] <- -Inf
ul_qv_PRS[2:n_quantile,1] <- quant_vals_PRS
ul_qv_PRS[1:(n_quantile-1),2] <- quant_vals_PRS
ul_qv_PRS[n_quantile,2] <- Inf
ul_qv_PRS<-cbind(ul_qv_PRS, (ul_qv_PRS[,1:2]-E_PRS)/sqrt(varPRS))
# Estimate case control proportion for each quantile
prob_quantile_case <- pnorm(ul_qv_PRS[,2], mean = mu_case) - pnorm(ul_qv_PRS[,1], mean = mu_case)
prob_quantile_control <- pnorm(ul_qv_PRS[,2], mean = mu_control) - pnorm(ul_qv_PRS[,1], mean = mu_control)
p_case_quantile <- (prob_quantile_case*prev)/by_quant
p_cont_quantile <- (prob_quantile_control*(1-prev))/by_quant
# Estimate OR comparing each quantile to bottom quantile
OR <- p_case_quantile/p_cont_quantile
OR <- OR/OR[1]
# Return output
out <- cbind(ul_qv_PRS[,3:4],p_cont_quantile, p_case_quantile, OR)
row.names(out) <- 1:n_quantile
colnames(out) <- c("q_min", "q_max","p_control", "p_case", "OR")
data.frame(out)
}
```
</details>
***
### Continuous outcomes
To convert a polygenic Z-score into an absolute estimate for a trait, we must know the predicitve utility of the polygenic score (R2), and the mean and SD of the outcome in the general population. Then it is possible to estimate the mean and SD of the trait within each polygenic score quantile using a truncated norm model (currently not theory based).
<details><summary>Show code</summary>
```{R, echo=T, eval=F}
# Thank you for Alex Gillet for her work developing this code.
mean_sd_quant.f <- function(PRS_R2=0.641, Outcome_mean=1, Outcome_sd=1, n_quantile=20){
### PRS quantiles with a continuous phenotype (Y)
library(tmvtnorm)
###
E_PRS = 0
SD_PRS = sqrt(1)
E_phenotype = Outcome_mean
SD_phenotype = Outcome_sd
by_quant<-1/(n_quantile)
PRS_quantile_bounds <- qnorm(p=seq(0, 1, by=by_quant), mean= E_PRS, sd= SD_PRS)
lower_PRS_vec <- PRS_quantile_bounds[1:n_quantile]
upper_PRS_vec <- PRS_quantile_bounds[2:(n_quantile+1)]
mean_vec <- c(E_phenotype, E_PRS)
sigma_mat <- matrix(sqrt(PRS_R2)*SD_phenotype*SD_PRS, nrow=2, ncol=2)
sigma_mat[1,1] <- SD_phenotype^2
sigma_mat[2,2] <- SD_PRS^2
### mean of phenotype within the truncated PRS distribution
out_mean_Y <- rep(0, n_quantile)
### SD of phenotype within the truncated PRS distribution
out_SD_Y <- rep(0, n_quantile)
### cov of Y and PRS given truncation on PRS
out_cov_Y_PRS <- rep(0, n_quantile)
### SD of PRS given truncation on PRS
out_SD_PRS <- rep(0, n_quantile)
### mean PRS given truncation on PRS
out_mean_PRS <- rep(0, n_quantile)
for(i in 1:n_quantile){
distribution_i <- mtmvnorm(mean = mean_vec,
sigma = sigma_mat,
lower = c(-Inf, lower_PRS_vec[i]),
upper = c(Inf, upper_PRS_vec[i]),
doComputeVariance=TRUE,
pmvnorm.algorithm=GenzBretz())
out_mean_Y[i] <- distribution_i$tmean[1]
out_mean_PRS[i] <- distribution_i$tmean[2]
out_SD_Y[i] <- sqrt(distribution_i$tvar[1,1])
out_SD_PRS[i] <- sqrt(distribution_i$tvar[2,2])
out_cov_Y_PRS[i] <- distribution_i$tvar[1,2]
}
out<-data.frame(q=1:n_quantile,
q_min=lower_PRS_vec,
q_max=upper_PRS_vec,
x_mean=out_mean_Y,
x_sd=out_SD_Y)
return(out)
out_mean_Y
out_SD_Y
out_mean_PRS
out_SD_PRS
out_cov_Y_PRS
}
library(tmvtnorm)
# Create alternative of script that doesn't require simulation
mean_sd_quant.f <- function(PRS_R2=0.641, Outcome_mean=1, Outcome_sd=1, n_quantile=20){
### PRS quantiles with a continuous phenotype (Y)
library(tmvtnorm)
###
E_PRS = 0
SD_PRS = sqrt(1)
E_phenotype = Outcome_mean
SD_phenotype = Outcome_sd
n_quantile=20
by_quant<-1/(n_quantile)
PRS_quantile_bounds <- qnorm(p=seq(0, 1, by=by_quant), mean= E_PRS, sd= SD_PRS)
lower_PRS_vec <- PRS_quantile_bounds[1:n_quantile]
upper_PRS_vec <- PRS_quantile_bounds[2:(n_quantile+1)]
mean_vec <- c(E_phenotype, E_PRS)
sigma_mat <- matrix(sqrt(PRS_R2)*SD_phenotype*SD_PRS, nrow=2, ncol=2)
sigma_mat[1,1] <- SD_phenotype^2
sigma_mat[2,2] <- SD_PRS^2
### mean of phenotype within the truncated PRS distribution
out_mean_Y <- rep(0, 20)
### SD of phenotype within the truncated PRS distribution
out_SD_Y <- rep(0, 20)
### cov of Y and PRS given truncation on PRS
out_cov_Y_PRS <- rep(0, 20)
### SD of PRS given truncation on PRS
out_SD_PRS <- rep(0, 20)
### mean PRS given truncation on PRS
out_mean_PRS <- rep(0, 20)
for(i in 1:n_quantile){
distribution_i <- mtmvnorm(mean = mean_vec,
sigma = sigma_mat,
lower = c(-Inf, lower_PRS_vec[i]),
upper = c(Inf, upper_PRS_vec[i]),
doComputeVariance=TRUE,
pmvnorm.algorithm=GenzBretz())
out_mean_Y[i] <- distribution_i$tmean[1]
out_mean_PRS[i] <- distribution_i$tmean[2]
out_SD_Y[i] <- sqrt(distribution_i$tvar[1,1])
out_SD_PRS[i] <- sqrt(distribution_i$tvar[2,2])
out_cov_Y_PRS[i] <- distribution_i$tvar[1,2]
}
out<-data.frame(q=1:n_quantile,
q_min=lower_PRS_vec,
q_max=upper_PRS_vec,
x_mean=out_mean_Y,
x_sd=out_SD_Y)
return(out)
out_mean_Y
out_SD_Y
out_mean_PRS
out_SD_PRS
out_cov_Y_PRS
}
pdf('~/comp_stand.pdf')
for(i in seq(0.05, 0.95, 0.05)){
sim_res<-PRS_abs_quant2(PRS_R2 = i, Outcome_mean = 0, Outcome_sd = 1, n_quantile = 20)
nosim_res<-mean_sd_quant.f(PRS_R2 = i, Outcome_mean = 0, Outcome_sd = 1, n_quantile = 20)
plot(sim_res$x_mean,nosim_res$x_mean, main=paste0('R2 = ',i))
abline(coef = c(0,1))
}
dev.off()
pdf('~/comp_centre.pdf')
for(i in seq(0.05, 0.95, 0.05)){
sim_res<-PRS_abs_quant2(PRS_R2 = i, Outcome_mean = 0, Outcome_sd = 2, n_quantile = 20)
nosim_res<-mean_sd_quant.f(PRS_R2 = i, Outcome_mean = 0, Outcome_sd = 2, n_quantile = 20)
plot(sim_res$x_mean,nosim_res$x_mean, main=paste0('R2 = ',i))
abline(coef = c(0,1))
plot(sim_res$x_sd,nosim_res$x_sd, main=paste0('R2 = ',i))
abline(coef = c(0,1))
}
dev.off()
pdf('~/comp_scaled.pdf')
for(i in seq(0.05, 0.95, 0.05)){
sim_res<-PRS_abs_quant2(PRS_R2 = i, Outcome_mean = 100, Outcome_sd = 1, n_quantile = 20)
nosim_res<-mean_sd_quant.f(PRS_R2 = i, Outcome_mean = 100, Outcome_sd = 1, n_quantile = 20)
plot(sim_res$x_mean,nosim_res$x_mean, main=paste0('R2 = ',i))
abline(coef = c(0,1))
}
dev.off()
```
</details>
***
## Validate conversion
***
### Calculate polygenic scores
<details><summary>pT + clump: Sparse</summary>
```{bash, echo=T, eval=F}
########
# 1KG ref
########
# Set required variables
. /users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Target_scoring.config
. /users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Pipeline_prep.config
pheno=$(echo Depression Intelligence BMI Height T2D CAD IBD MultiScler RheuArth Breast_Cancer Prostate_Cancer Depression)
gwas=$(echo DEPR07 COLL01 BODY04 HEIG03 DIAB05 COAD01 INFB01 SCLE03 RHEU02 BRCA01 PRCA01)
# Create directory
mkdir -p ${UKBB_output}/PRS_for_interpretation/1KG_ref/pt_clump
# Create file listing GWAS that haven't been processed.
> ${UKBB_output}/PRS_for_interpretation/1KG_ref/pt_clump/todo.txt
for i in $(seq 1 11);do
gwas_i=$(echo ${gwas} | cut -f ${i} -d ' ')
pheno_i=$(echo ${pheno} | cut -f ${i} -d ' ')
if [ ! -f ${UKBB_output}/PRS_for_interpretation/1KG_ref/pt_clump/${gwas_i}/UKBB.subset.w_hm3.${gwas_i}.profiles ]; then
echo ${gwas_i} ${pheno_i} >> ${UKBB_output}/PRS_for_interpretation/1KG_ref/pt_clump/todo.txt
fi
done
# Create shell script to run using sbatch
cat > ${UKBB_output}/PRS_for_interpretation/1KG_ref/pt_clump/sbatch.sh << 'EOF'
#!/bin/sh
#SBATCH -p shared,brc
#SBATCH --mem 5G
#SBATCH -J pt_clump
. /users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Target_scoring.config
. /users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Pipeline_prep.config
gwas=$(awk -v var="$SLURM_ARRAY_TASK_ID" 'NR == var {print $1}' ${UKBB_output}/PRS_for_interpretation/1KG_ref/pt_clump/todo.txt)
pheno=$(awk -v var="$SLURM_ARRAY_TASK_ID" 'NR == var {print $2}' ${UKBB_output}/PRS_for_interpretation/1KG_ref/pt_clump/todo.txt)
echo $gwas
echo $pheno
/users/k1806347/brc_scratch/Software/Rscript.sh /users/k1806347/brc_scratch/Software/MyGit/GenoPred/Scripts/Scaled_polygenic_scorer/Scaled_polygenic_scorer.R \
--target_plink_chr ${UKBB_output}/Genotype/Harmonised/UKBB.w_hm3.QCd.AllSNP.chr \
--target_keep ${UKBB_output}/Phenotype/PRS_comp_subset/UKBB.${pheno}.txt \
--ref_score ${Geno_1KG_dir}/Score_files_for_polygenic/pt_clump/${gwas}/1KGPhase3.w_hm3.${gwas} \
--ref_scale ${Geno_1KG_dir}/Score_files_for_polygenic/pt_clump/${gwas}/1KGPhase3.w_hm3.${gwas}.EUR.scale \
--ref_freq_chr ${Geno_1KG_dir}/freq_files/EUR/1KGPhase3.w_hm3.EUR.chr \
--plink ${plink1_9} \
--pheno_name ${gwas} \
--output ${UKBB_output}/PRS_for_interpretation/1KG_ref/pt_clump/${gwas}/UKBB.subset.w_hm3.${gwas}
EOF
sbatch --array 1-$(wc -l ${UKBB_output}/PRS_for_interpretation/1KG_ref/pt_clump/todo.txt | cut -d' ' -f1)%3 ${UKBB_output}/PRS_for_interpretation/1KG_ref/pt_clump/sbatch.sh
```
</details>
<details><summary>DBSLMM</summary>
```{bash, echo=T, eval=F}
########
# 1KG ref
########
# Set required variables
. /users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Target_scoring.config
. /users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Pipeline_prep.config
pheno=$(echo Depression Intelligence BMI Height T2D CAD IBD MultiScler RheuArth Breast_Cancer Prostate_Cancer Depression)
gwas=$(echo DEPR07 COLL01 BODY04 HEIG03 DIAB05 COAD01 INFB01 SCLE03 RHEU02 BRCA01 PRCA01)
# Create directory
mkdir -p ${UKBB_output}/PRS_for_interpretation/1KG_ref/DBSLMM
# Create file listing GWAS that haven't been processed.
> ${UKBB_output}/PRS_for_interpretation/1KG_ref/DBSLMM/todo.txt
for i in $(seq 1 11);do
gwas_i=$(echo ${gwas} | cut -f ${i} -d ' ')
pheno_i=$(echo ${pheno} | cut -f ${i} -d ' ')
if [ ! -f ${UKBB_output}/PRS_for_interpretation/1KG_ref/DBSLMM/${gwas_i}/UKBB.subset.w_hm3.${gwas_i}.DBSLMM_profiles ]; then
echo ${gwas_i} ${pheno_i} >> ${UKBB_output}/PRS_for_interpretation/1KG_ref/DBSLMM/todo.txt
fi
done
# Create shell script to run using sbatch
cat > ${UKBB_output}/PRS_for_interpretation/1KG_ref/DBSLMM/sbatch.sh << 'EOF'
#!/bin/sh
#SBATCH -p shared,brc
#SBATCH --mem 5G
#SBATCH -J DBSLMM
. /users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Target_scoring.config
. /users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Pipeline_prep.config
gwas=$(awk -v var="$SLURM_ARRAY_TASK_ID" 'NR == var {print $1}' ${UKBB_output}/PRS_for_interpretation/1KG_ref/DBSLMM/todo.txt)
pheno=$(awk -v var="$SLURM_ARRAY_TASK_ID" 'NR == var {print $2}' ${UKBB_output}/PRS_for_interpretation/1KG_ref/DBSLMM/todo.txt)
echo $gwas
echo $pheno
/users/k1806347/brc_scratch/Software/Rscript.sh /users/k1806347/brc_scratch/Software/MyGit/GenoPred/Scripts/Scaled_polygenic_scorer_DBSLMM/Scaled_polygenic_scorer_DBSLMM.R \
--target_plink_chr ${UKBB_output}/Genotype/Harmonised/UKBB.w_hm3.QCd.AllSNP.chr \
--target_keep ${UKBB_output}/Phenotype/PRS_comp_subset/UKBB.${pheno}.txt \
--ref_score ${Geno_1KG_dir}/Score_files_for_polygenic/DBSLMM/${gwas}/1KGPhase3.w_hm3.${gwas}.dbslmm.GW.txt \
--ref_scale ${Geno_1KG_dir}/Score_files_for_polygenic/DBSLMM/${gwas}/1KGPhase3.w_hm3.${gwas}.EUR.scale \
--ref_freq_chr ${Geno_1KG_dir}/freq_files/EUR/1KGPhase3.w_hm3.EUR.chr \
--plink ${plink1_9} \
--pheno_name ${gwas} \
--output ${UKBB_output}/PRS_for_interpretation/1KG_ref/DBSLMM/${gwas}/UKBB.subset.w_hm3.${gwas}
EOF
sbatch --array 1-$(wc -l ${UKBB_output}/PRS_for_interpretation/1KG_ref/DBSLMM/todo.txt | cut -d' ' -f1)%3 ${UKBB_output}/PRS_for_interpretation/1KG_ref/DBSLMM/sbatch.sh
```
</details>
***
### Evaluate polygenic scores
<details><summary>Compare all methods</summary>
```{R, echo=T, eval=F}
# Create a file listing the predictors files
source('/users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Target_scoring.config')
pheno<-c('Depression','Intelligence','BMI','Height','T2D','CAD','IBD','MultiScler','RheuArth','Breast_Cancer','Prostate_Cancer')
gwas<-c('DEPR07','COLL01','BODY04','HEIG03','DIAB05','COAD01','INFB01','SCLE03','RHEU02','BRCA01','PRCA01')
for(i in 1:length(pheno)){
pred_file<-NULL
# pT+clump (sparse)
pred_file<-rbind(pred_file,data.frame( predictors=paste0(UKBB_output,'/PRS_for_interpretation/1KG_ref/pt_clump/',gwas[i],'/UKBB.subset.w_hm3.',gwas[i],'.profiles'),
group='pT+clump'))
# DBSLMM
pred_file<-rbind(pred_file,data.frame( predictors=paste0(UKBB_output,'/PRS_for_interpretation/1KG_ref/DBSLMM/',gwas[i],'/UKBB.subset.w_hm3.',gwas[i],'.DBSLMM_profiles'),
group='DBSLMM'))
# Write out list of predictors with groups
dir.create(paste0('/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/',pheno[i]))
write.table(pred_file, paste0('/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/',pheno[i],'/UKBB.w_hm3.',gwas[i],'.EUR-PRSs.AllMethodComp.predictor_groups'), row.names=F, col.names=T, quote=F)
}
```
```{bash, eval=F, echo=T}
. /users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Target_scoring.config
# Run Model_builder_V2.R
pheno=$(echo Depression Intelligence BMI Height T2D CAD IBD MultiScler RheuArth Breast_Cancer Prostate_Cancer)
gwas=$(echo DEPR07 COLL01 BODY04 HEIG03 DIAB05 COAD01 INFB01 SCLE03 RHEU02 BRCA01 PRCA01)
prev=$(echo 0.15 NA NA NA 0.05 0.03 0.013 0.00164 0.005 0.125 0.125)
# 1KG reference
for i in $(seq 1 11);do
pheno_i=$(echo ${pheno} | cut -f ${i} -d ' ')
gwas_i=$(echo ${gwas} | cut -f ${i} -d ' ')
prev_i=$(echo ${prev} | cut -f ${i} -d ' ')
sbatch --mem 5G -n 1 -p brc,shared /users/k1806347/brc_scratch/Software/Rscript.sh /users/k1806347/brc_scratch/Software/MyGit/GenoPred/Scripts/Model_builder/Model_builder_V2.R \
--pheno ${UKBB_output}/Phenotype/PRS_comp_subset/UKBB.${pheno_i}.txt \
--keep /users/k1806347/brc_scratch/Analyses/PRS_comparison/UKBB_outcomes_for_prediction/ukb18177_glanville_post_qc_id_list.UpdateIDs.fam \
--out /scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/${pheno_i}/UKBB.w_hm3.${gwas_i}.EUR-PRSs.AllMethodComp \
--n_core 1 \
--compare_predictors F \
--assoc T \
--outcome_pop_prev ${prev_i} \
--predictors /scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/${pheno_i}/UKBB.w_hm3.${gwas_i}.EUR-PRSs.AllMethodComp.predictor_groups
done
```
</details>
***
### Binary outcomes
Calculate reference-standardised polygenic scores within UK Biobank for a range of dichotomous phenotypes. Estimate the AUC/R2 of the polygenic scores in UKB. Compare measured and estimated absolute risk per PRS quantile. Use the PRScs fully baysian (pseudovalidation) polygenic scores, as this method provides a single score with good relative performance compared to other approaches.
Reference-standardised polygenic scores have already been calculated in UKB for the PRS methods comparison study, and the AUC has already been estimated. Read in polygenic scores and observed phenotype for UKB, measure proportion of cases per PRS quantile, and then estimate proportion of cases per PRS quantile.
#### DBSLMM
<details><summary>Show code</summary>
```{R, eval=F, echo=T}
library(data.table)
source('/users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Target_scoring.config')
pheno=c('Depression','T2D','CAD','IBD','MultiScler','RheuArth','Breast_Cancer','Prostate_Cancer')
gwas=c('DEPR07','DIAB05','COAD01','INFB01','SCLE03','RHEU02','BRCA01','PRCA01')
n_quant<-20
files<-data.frame(pheno,gwas)
# Create function
ccprobs.f <- function(PRS_auc=0.641, prev=0.7463, n_quantile=20){
# Convert AUC into cohen's d
d <- sqrt(2)*qnorm(PRS_auc)
# Set mean difference between cases and control polygenic scores
mu_case <- d
mu_control <- 0
# Estimate mean and variance of polygenic scores across case and control
varPRS <- prev*(1+(d^2) - (d*prev)^2) + (1-prev)*(1 - (d*prev)^2)
E_PRS <- d*prev
# Estimate polygenic score quantiles
by_quant<-1/n_quantile
p_quant <- seq(by_quant, 1-by_quant, by=by_quant)
quant_vals_PRS <- rep(0, length(p_quant))
quant_f_solve <- function(x, prev, d, pq){prev*pnorm(x-d) + (1-prev)*pnorm(x) - pq}
for(i in 1:length(p_quant)){
quant_vals_PRS[i] <- unlist(uniroot(quant_f_solve, prev=prev, d=d, pq= p_quant[i], interval=c(-2.5, 2.5), extendInt = "yes", tol=6e-12)$root)
}
# Create a table for output
ul_qv_PRS <- matrix(0, ncol=2, nrow=n_quantile)
ul_qv_PRS[1,1] <- -Inf
ul_qv_PRS[2:n_quantile,1] <- quant_vals_PRS
ul_qv_PRS[1:(n_quantile-1),2] <- quant_vals_PRS
ul_qv_PRS[n_quantile,2] <- Inf
ul_qv_PRS<-cbind(ul_qv_PRS, (ul_qv_PRS[,1:2]-E_PRS)/sqrt(varPRS))
# Estimate case control proportion for each quantile
prob_quantile_case <- pnorm(ul_qv_PRS[,2], mean = mu_case) - pnorm(ul_qv_PRS[,1], mean = mu_case)
prob_quantile_control <- pnorm(ul_qv_PRS[,2], mean = mu_control) - pnorm(ul_qv_PRS[,1], mean = mu_control)
p_case_quantile <- (prob_quantile_case*prev)/by_quant
p_cont_quantile <- (prob_quantile_control*(1-prev))/by_quant
# Estimate OR comparing each quantile to bottom quantile
OR <- p_case_quantile/p_cont_quantile
OR <- OR/OR[1]
# Return output
out <- cbind(ul_qv_PRS[,3:4],p_cont_quantile, p_case_quantile, OR)
row.names(out) <- 1:n_quantile
colnames(out) <- c("q_min", "q_max","p_control", "p_case", "OR")
data.frame(out)
}
# Run analysis for each phenotype
res_all<-NULL
cor_res<-NULL
plots_all<-list()
prs_dist_all<-list()
mean_sd<-NULL
for(i in 1:dim(files)[1]){
# Read in pheno and prs data, and merge
pheno_i<-fread(paste0(UKBB_output,'/Phenotype/PRS_comp_subset/UKBB.',files$pheno[i],'.txt'))
names(pheno_i)[3]<-'pheno'
prs_i<-fread(paste0(UKBB_output,'/PRS_for_interpretation/1KG_ref/DBSLMM/',files$gwas[i],'/UKBB.subset.w_hm3.',files$gwas[i],'.DBSLMM_profiles'))
prs_i<-prs_i[,c('FID','IID',paste0(files$gwas[i], '_DBSLMM')), with=F]
names(prs_i)[3]<-'prs'
pheno_prs<-merge(pheno_i, prs_i, by=c('FID','IID'))
mean_sd<-rbind(mean_sd,data.frame(Phenotype=files$pheno[i],
Mean_all=mean(pheno_prs$prs),
SD_all=sd(pheno_prs$prs),
Mean_con=mean(pheno_prs$prs[pheno_prs$pheno == 0]),
SD_con=sd(pheno_prs$prs[pheno_prs$pheno == 0]),
Mean_cas=mean(pheno_prs$prs[pheno_prs$pheno == 1]),
SD_cas=sd(pheno_prs$prs[pheno_prs$pheno == 1]),
f_test_pval=var.test(prs ~ pheno, pheno_prs, alternative = "two.sided")$p.value))
# Plot DBSLMM PRS distribution
library(ggplot2)
library(cowplot)
prs_dist_all[[i]]<-ggplot(pheno_prs, aes(x=prs)) +
geom_histogram() +
labs(y="Count", x='Polygenic Score', title=files$pheno[i]) +
theme_cowplot(12)
# Read in AUC for PRS
assoc<-fread(paste0('/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/',files$pheno[i],'/UKBB.w_hm3.',files$gwas[i],'.EUR-PRSs.AllMethodComp.assoc.txt'))
prs_auc<-assoc[grepl('DBSLMM', assoc$Predictor),]$AUC
# Assign individuals to observed PRS quantiles
obs_quant<-quantile(pheno_prs$prs, prob = seq(0, 1, length = n_quant+1))
pheno_prs$obs_quant<-as.numeric(cut( pheno_prs$prs, obs_quant, include.lowest = T))
# Calculate proportion of each quantile that are cases
obs_cc<-NULL
for(k in 1:n_quant){
obs_cc<-rbind(obs_cc, data.frame(Phenotype=files$pheno[i],
Type='Observed',
Quantile=k,
q_min=obs_quant[k],
q_max=obs_quant[k+1],
p_control=1-mean(pheno_prs$pheno[pheno_prs$obs_quant == k]),
p_case=mean(pheno_prs$pheno[pheno_prs$obs_quant == k])))
}
# Assign individuals to estimated PRS quantiles
est_cc<-ccprobs.f(PRS_auc = prs_auc, prev=mean(pheno_prs$pheno), n_quantile = n_quant)
est_cc$OR<-NULL
est_cc<-data.frame(Phenotype=files$pheno[i],Type="\nEstimated\n(Observed AUC)",Quantile=1:n_quant, est_cc)
est_quant<-sort(unique(c(est_cc$q_min, est_cc$q_max)))
pheno_prs$est_quant<-as.numeric(cut( pheno_prs$prs, est_quant, include.lowest = T))
tmp<-cor.test(obs_cc$p_case, est_cc$p_case)
tmp2<-abs(est_cc$p_case-obs_cc$p_case)/obs_cc$p_case
# Estimate correlation between observed and expected
cor_res<-rbind(cor_res,data.frame(Phenotype=files$pheno[i],
Cor=tmp$estimate,
Low95CI=tmp$conf.int[1],
High95CI=tmp$conf.int[2],
Mean_perc_diff=mean(tmp2),
N=length(pheno_prs$pheno),
Ncas=sum(pheno_prs$pheno == 1),
Ncon=sum(pheno_prs$pheno == 0)))
quant_comp<-rbind(obs_cc, est_cc)
res_all<-rbind(res_all, quant_comp)
library(ggplot2)
library(cowplot)
plots_all[[i]]<-ggplot(quant_comp, aes(x=Quantile, y=p_case, colour=Type)) +
geom_point(alpha=0.8) +
geom_line(alpha=0.8) +
labs(y="p(case)", title=files$pheno[i], colour='Method') +
theme_cowplot(12)
}
png(paste0('/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/PRS_dist_binary.png'), units='px', res=300, width=2000, height=2800)
plot_grid(plotlist=prs_dist_all, ncol = 2)
dev.off()
png(paste0('/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/PropCC_Comp.png'), units='px', res=300, width=2000, height=2800)
plot_grid(plotlist=plots_all, ncol = 2)
dev.off()
write.csv(cor_res, '/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/PropCC_Comp.csv', row.names=F, quote=F)
write.csv(mean_sd, '/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/PRS_Mean_SD.csv', row.names=F, quote=F)
```
</details>
```{bash, eval=T, echo=F}
mkdir -p /users/k1806347/brc_scratch/Software/MyGit/GenoPred/Images/Validating_ImputeMe_Ideas
cp /scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/PropCC_Comp.png /users/k1806347/brc_scratch/Software/MyGit/GenoPred/Images/Validating_ImputeMe_Ideas/
```
<details><summary>Show results</summary>

```{r, echo=F, eval=T, results='asis'}
res<-read.csv("/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/PropCC_Comp.csv")
res_tab<-res
res_tab[,2:5]<-round(res_tab[,2:5], 3)
res_tab$R<-paste0(res_tab$Cor, " (",res_tab$Low95CI,"-",res_tab$High95CI,")")
res_tab$Diff<-paste0(round(res_tab$Mean_perc_diff*100,1),"%")
res_tab<-res_tab[,c('Phenotype','R','Diff','N','Ncas','Ncon')]
names(res_tab)<-c('Phenotype',"Correlation (95%CI)","Mean Abs. Diff.",'N','Ncas','Ncon')
library(knitr)
kable(res_tab, rownames = FALSE, caption='Correlation between observed and estimated proportion of cases within polygenic score quantiles')
cat('Median Cor. = ',median(res$Cor),'; Mean Cor. = ', mean(res$Cor),'; Min. Cor. = ',min(res$Cor),'; Max. Cor. = ',max(res$Cor),'\n', sep='')
cat('Mean Abs. Diff = ',mean(res$Mean_perc_diff),'; Min. Mean Abs. Diff. = ',min(res$Mean_perc_diff),'; Max. Mean Abs. Diff. = ',max(res$Mean_perc_diff),'\n', sep='')
```
</details>
***
#### pT+clump
<details><summary>Show code</summary>
```{R, eval=F, echo=T}
library(data.table)
source('/users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Target_scoring.config')
pheno=c('Depression','T2D','CAD','IBD','MultiScler','RheuArth','Breast_Cancer','Prostate_Cancer')
gwas=c('DEPR07','DIAB05','COAD01','INFB01','SCLE03','RHEU02','BRCA01','PRCA01')
n_quant<-20
files<-data.frame(pheno,gwas)
# Create function
ccprobs.f <- function(PRS_auc=0.641, prev=0.7463, n_quantile=20){
# Convert AUC into cohen's d
d <- sqrt(2)*qnorm(PRS_auc)
# Set mean difference between cases and control polygenic scores
mu_case <- d
mu_control <- 0
# Estimate mean and variance of polygenic scores across case and control
varPRS <- prev*(1+(d^2) - (d*prev)^2) + (1-prev)*(1 - (d*prev)^2)
E_PRS <- d*prev
# Estimate polygenic score quantiles
by_quant<-1/n_quantile
p_quant <- seq(by_quant, 1-by_quant, by=by_quant)
quant_vals_PRS <- rep(0, length(p_quant))
quant_f_solve <- function(x, prev, d, pq){prev*pnorm(x-d) + (1-prev)*pnorm(x) - pq}
for(i in 1:length(p_quant)){
quant_vals_PRS[i] <- unlist(uniroot(quant_f_solve, prev=prev, d=d, pq= p_quant[i], interval=c(-2.5, 2.5), extendInt = "yes", tol=6e-12)$root)
}
# Create a table for output
ul_qv_PRS <- matrix(0, ncol=2, nrow=n_quantile)
ul_qv_PRS[1,1] <- -Inf
ul_qv_PRS[2:n_quantile,1] <- quant_vals_PRS
ul_qv_PRS[1:(n_quantile-1),2] <- quant_vals_PRS
ul_qv_PRS[n_quantile,2] <- Inf
ul_qv_PRS<-cbind(ul_qv_PRS, (ul_qv_PRS[,1:2]-E_PRS)/sqrt(varPRS))
# Estimate case control proportion for each quantile
prob_quantile_case <- pnorm(ul_qv_PRS[,2], mean = mu_case) - pnorm(ul_qv_PRS[,1], mean = mu_case)
prob_quantile_control <- pnorm(ul_qv_PRS[,2], mean = mu_control) - pnorm(ul_qv_PRS[,1], mean = mu_control)
p_case_quantile <- (prob_quantile_case*prev)/by_quant
p_cont_quantile <- (prob_quantile_control*(1-prev))/by_quant
# Estimate OR comparing each quantile to bottom quantile
OR <- p_case_quantile/p_cont_quantile
OR <- OR/OR[1]
# Return output
out <- cbind(ul_qv_PRS[,3:4],p_cont_quantile, p_case_quantile, OR)
row.names(out) <- 1:n_quantile
colnames(out) <- c("q_min", "q_max","p_control", "p_case", "OR")
data.frame(out)
}
# Run analysis for each phenotype
res_all<-NULL
cor_res<-NULL
plots_all<-list()
prs_dist_all<-list()
for(i in 1:dim(files)[1]){
# Read in pheno and prs data, and merge
pheno_i<-fread(paste0(UKBB_output,'/Phenotype/PRS_comp_subset/UKBB.',files$pheno[i],'.txt'))
names(pheno_i)[3]<-'pheno'
prs_i<-fread(paste0(UKBB_output,'/PRS_for_interpretation/1KG_ref/pt_clump/',files$gwas[i],'/UKBB.subset.w_hm3.',files$gwas[i],'.profiles'))
# Extract PRS with the most stringent p-value threshold
score_nsnp<-fread(paste0('/users/k1806347/brc_scratch/Data/1KG/Phase3/Score_files_for_polygenic/pt_clump/',gwas[i],'/1KGPhase3.w_hm3.',gwas[i],'.NSNP_per_pT'))
score_nsnp<-score_nsnp[score_nsnp$NSNP >= 5,]
nsnp<-score_nsnp$NSNP[score_nsnp$pT1 == min(score_nsnp$pT1)]
pT<-min(score_nsnp$pT1)
prs_i<-prs_i[,c('FID','IID',paste0(files$gwas[i], '_',pT)), with=F]
names(prs_i)[3]<-'prs'
pheno_prs<-merge(pheno_i, prs_i, by=c('FID','IID'))
# Plot DBSLMM PRS distribution
library(ggplot2)
library(cowplot)
prs_dist_all[[i]]<-ggplot(pheno_prs, aes(x=prs)) +
geom_histogram() +
labs(y="Count", x='Polygenic Score', title=paste0(files$pheno[i],': ',nsnp,' SNPs')) +
theme_cowplot(12)
# Read in AUC for PRS
assoc<-fread(paste0('/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/',files$pheno[i],'/UKBB.w_hm3.',files$gwas[i],'.EUR-PRSs.AllMethodComp.assoc.txt'))
prs_auc<-assoc[grepl(paste0(gwas[i],'_',gsub('-','.',pT)), assoc$Predictor),]$AUC
# Assign individuals to observed PRS quantiles
obs_quant<-quantile(pheno_prs$prs, prob = seq(0, 1, length = n_quant+1))
pheno_prs$obs_quant<-as.numeric(cut( pheno_prs$prs, obs_quant, include.lowest = T))
# Calculate proportion of each quantile that are cases
obs_cc<-NULL
for(k in 1:n_quant){
obs_cc<-rbind(obs_cc, data.frame(Phenotype=files$pheno[i],
Type='Observed',
Quantile=k,
q_min=obs_quant[k],
q_max=obs_quant[k+1],
p_control=1-mean(pheno_prs$pheno[pheno_prs$obs_quant == k]),
p_case=mean(pheno_prs$pheno[pheno_prs$obs_quant == k])))
}
# Assign individuals to estimated PRS quantiles
est_cc<-ccprobs.f(PRS_auc = prs_auc, prev=mean(pheno_prs$pheno), n_quantile = n_quant)
est_cc$OR<-NULL
est_cc<-data.frame(Phenotype=files$pheno[i],Type="\nEstimated\n(Observed AUC)",Quantile=1:n_quant, est_cc)
est_quant<-sort(unique(c(est_cc$q_min, est_cc$q_max)))
pheno_prs$est_quant<-as.numeric(cut( pheno_prs$prs, est_quant, include.lowest = T))
tmp<-cor.test(obs_cc$p_case, est_cc$p_case)
tmp2<-abs(est_cc$p_case-obs_cc$p_case)/obs_cc$p_case
# Estimate correlation between observed and expected
cor_res<-rbind(cor_res,data.frame(Phenotype=files$pheno[i],
Cor=tmp$estimate,
Low95CI=tmp$conf.int[1],
High95CI=tmp$conf.int[2],
Mean_perc_diff=mean(tmp2),
N=length(pheno_prs$pheno),
Ncas=sum(pheno_prs$pheno == 1),
Ncon=sum(pheno_prs$pheno == 0)))
quant_comp<-rbind(obs_cc, est_cc)
res_all<-rbind(res_all, quant_comp)
library(ggplot2)
library(cowplot)
plots_all[[i]]<-ggplot(quant_comp, aes(x=Quantile, y=p_case, colour=Type)) +
geom_point(alpha=0.8) +
geom_line(alpha=0.8) +
labs(y="p(case)", title=paste0(files$pheno[i],': \n',nsnp,' SNPs; AUC=',round(prs_auc,3)), colour='Method') +
theme_cowplot(12)
}
png(paste0('/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/pt_clump_PRS_dist_binary.png'), units='px', res=300, width=2000, height=2800)
plot_grid(plotlist=prs_dist_all, ncol = 2)
dev.off()
png(paste0('/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/pt_clump_PropCC_Comp.png'), units='px', res=300, width=2000, height=2800)
plot_grid(plotlist=plots_all, ncol = 2)
dev.off()
write.csv(cor_res, '/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/pt_clump_PropCC_Comp.csv', row.names=F, quote=F)
```
</details>
```{bash, eval=T, echo=F}
mkdir -p /users/k1806347/brc_scratch/Software/MyGit/GenoPred/Images/Validating_ImputeMe_Ideas
cp /scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/pt_clump_PropCC_Comp.png /users/k1806347/brc_scratch/Software/MyGit/GenoPred/Images/Validating_ImputeMe_Ideas/
```
<details><summary>Show results</summary>

```{r, echo=F, eval=T, results='asis'}
res<-read.csv("/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/pt_clump_PropCC_Comp.csv")
res_tab<-res
res_tab[,2:5]<-round(res_tab[,2:5], 3)
res_tab$R<-paste0(res_tab$Cor, " (",res_tab$Low95CI,"-",res_tab$High95CI,")")
res_tab$Diff<-paste0(round(res_tab$Mean_perc_diff*100,1),"%")
res_tab<-res_tab[,c('Phenotype','R','Diff','N','Ncas','Ncon')]
names(res_tab)<-c('Phenotype',"Correlation (95%CI)","Mean Abs. Diff.",'N','Ncas','Ncon')
library(knitr)
kable(res_tab, rownames = FALSE, caption='Correlation between observed and estimated proportion of cases within polygenic score quantiles')
cat('Median Cor. = ',median(res$Cor),'; Mean Cor. = ', mean(res$Cor),'; Min. Cor. = ',min(res$Cor),'; Max. Cor. = ',max(res$Cor),'\n', sep='')
```
</details>
***
### Continuous outcomes
Calculate reference-standardised polygenic scores within UK Biobank for a range of continuous phenotypes. Estimate the R2 of the polygenic scores in UKB. Compare measured and estimated absolute meana and sd per PRS quantile. Use the DBSLMM fully baysian (pseudovalidation) polygenic scores, as this method provides a single score with good relative performance compared to other approaches.
Reference-standardised polygenic scores have already been calculated in UKB for the PRS methods comparison study, and the R2 has already been estimated. Read in polygenic scores and observed phenotype for UKB, measure phenotype mean and sd per PRS quantile, and then estimate measure phenotype mean and sd per PRS quantile.
#### DBSLMM
<details><summary>Show code</summary>
```{R, eval=F, echo=T}
library(data.table)
library(e1071)
source('/users/k1806347/brc_scratch/Software/MyGit/GenoPred/config_used/Target_scoring.config')
gwas<-c('COLL01','HEIG03','BODY04')
pheno<-c('Intelligence','Height','BMI')
n_quant<-20
files<-data.frame(pheno,gwas)
# Create function
mean_sd_quant.f <- function(PRS_R2=0.641, Outcome_mean=1, Outcome_sd=1, n_quantile=20){
### PRS quantiles with a continuous phenotype (Y)
library(tmvtnorm)
###
E_PRS = 0
SD_PRS = sqrt(1)
E_phenotype = Outcome_mean
SD_phenotype = Outcome_sd
by_quant<-1/(n_quantile)
PRS_quantile_bounds <- qnorm(p=seq(0, 1, by=by_quant), mean= E_PRS, sd= SD_PRS)
lower_PRS_vec <- PRS_quantile_bounds[1:n_quantile]
upper_PRS_vec <- PRS_quantile_bounds[2:(n_quantile+1)]
mean_vec <- c(E_phenotype, E_PRS)
sigma_mat <- matrix(sqrt(PRS_R2)*SD_phenotype*SD_PRS, nrow=2, ncol=2)
sigma_mat[1,1] <- SD_phenotype^2
sigma_mat[2,2] <- SD_PRS^2
### mean of phenotype within the truncated PRS distribution
out_mean_Y <- rep(0, n_quantile)
### SD of phenotype within the truncated PRS distribution
out_SD_Y <- rep(0, n_quantile)
### cov of Y and PRS given truncation on PRS
out_cov_Y_PRS <- rep(0, n_quantile)
### SD of PRS given truncation on PRS
out_SD_PRS <- rep(0, n_quantile)
### mean PRS given truncation on PRS
out_mean_PRS <- rep(0, n_quantile)
for(i in 1:n_quantile){
distribution_i <- mtmvnorm(mean = mean_vec,
sigma = sigma_mat,
lower = c(-Inf, lower_PRS_vec[i]),
upper = c(Inf, upper_PRS_vec[i]),
doComputeVariance=TRUE,
pmvnorm.algorithm=GenzBretz())
out_mean_Y[i] <- distribution_i$tmean[1]
out_mean_PRS[i] <- distribution_i$tmean[2]
out_SD_Y[i] <- sqrt(distribution_i$tvar[1,1])
out_SD_PRS[i] <- sqrt(distribution_i$tvar[2,2])
out_cov_Y_PRS[i] <- distribution_i$tvar[1,2]
}
out<-data.frame(q=1:n_quantile,
q_min=lower_PRS_vec,
q_max=upper_PRS_vec,
x_mean=out_mean_Y,
x_sd=out_SD_Y)
return(out)
out_mean_Y
out_SD_Y
out_mean_PRS
out_SD_PRS
out_cov_Y_PRS
}
# Run analysis for each phenotype
res_all<-NULL
plots_all<-list()
cor_res<-NULL
prs_dist_all<-list()
for(i in 1:dim(files)[1]){
# Read in pheno and prs data, and merge
pheno_i<-fread(paste0(UKBB_output,'/Phenotype/PRS_comp_subset/UKBB.',files$pheno[i],'.txt'))
names(pheno_i)[3]<-'pheno'
prs_i<-fread(paste0(UKBB_output,'/PRS_for_interpretation/1KG_ref/DBSLMM/',files$gwas[i],'/UKBB.subset.w_hm3.',files$gwas[i],'.DBSLMM_profiles'))
prs_i<-prs_i[,c('FID','IID',paste0(files$gwas[i], '_DBSLMM')), with=F]
names(prs_i)[3]<-'prs'
pheno_prs<-merge(pheno_i, prs_i, by=c('FID','IID'))
# Plot DBSLMM PRS distribution
library(ggplot2)
library(cowplot)
prs_dist_all[[i]]<-ggplot(pheno_prs, aes(x=prs)) +
geom_histogram() +
labs(y="Count", x='Polygenic Score', title=files$pheno[i]) +
theme_cowplot(12)
# Read in AUC for PRS
assoc<-fread(paste0('/scratch/users/k1806347/Analyses/AbsoluteRisk/Measured_AUC_R2/',files$pheno[i],'/UKBB.w_hm3.',files$gwas[i],'.EUR-PRSs.AllMethodComp.assoc.txt'))
prs_r2<-assoc[grepl('DBSLMM', assoc$Predictor),]$Obs_R2
# Assign individuals to observed PRS quantiles
obs_quant<-quantile(pheno_prs$prs, prob = seq(0, 1, length = n_quant+1))
pheno_prs$obs_quant<-as.numeric(cut( pheno_prs$prs, obs_quant, include.lowest = T))
# Calculate mean and SD of each quantile that are cases
obs_dist<-NULL
for(k in 1:n_quant){
obs_dist<-rbind(obs_dist, data.frame(Phenotype=files$pheno[i],
Type='Observed',
Quantile=k,
q_min=obs_quant[k],
q_max=obs_quant[k+1],
x_mean=mean(pheno_prs$pheno[pheno_prs$obs_quant == k]),
x_sd=sd(pheno_prs$pheno[pheno_prs$obs_quant == k])))
}
# Assign individuals to estimated PRS quantiles
est_dist<-mean_sd_quant.f(PRS_R2 = prs_r2, Outcome_mean=mean(pheno_prs$pheno), Outcome_sd=sd(pheno_prs$pheno), n_quantile = n_quant)
est_dist$q<-NULL
est_dist<-data.frame(Phenotype=files$pheno[i],Type="\nEstimated\n(Observed R2)",Quantile=1:n_quant, est_dist)
est_quant<-sort(unique(c(est_dist$q_min, est_dist$q_max)))
pheno_prs$est_quant<-as.numeric(cut( pheno_prs$prs, est_quant, include.lowest = T))
quant_comp<-rbind(obs_dist, est_dist)
tmp<-cor.test(obs_dist$x_mean, est_dist$x_mean)