Last updated: 2021-01-26

Checks: 5 2

Knit directory: causal-TWAS/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


The R Markdown is untracked by Git. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20191103) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Using absolute paths to the files within your workflowr project makes it difficult for you and others to run your code on a different machine. Change the absolute path(s) below to the suggested relative path(s) to make your code more reproducible.

absolute relative
~/causalTWAS/causal-TWAS/analysis/summarize_ctwas_plots.R analysis/summarize_ctwas_plots.R
~/causalTWAS/causal-TWAS/code/qqplot.R code/qqplot.R

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 717669c. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    .ipynb_checkpoints/
    Ignored:    analysis/.ipynb_checkpoints/
    Ignored:    code/.ipynb_checkpoints/
    Ignored:    code/before_package/.ipynb_checkpoints/
    Ignored:    code/workflow/.ipynb_checkpoints/
    Ignored:    data/

Untracked files:
    Untracked:  analysis/simulation-ctwas-ukbWG-gtex.adipose_s200.113.Rmd
    Untracked:  analysis/simulation-ctwas-ukbWG-gtex.adipose_s40.22.Rmd
    Untracked:  analysis/simulation-ctwas-ukbWG-gtex.adipose_s40.22_2.Rmd
    Untracked:  analysis/simulation-ctwas-ukbWG-gtex.adipose_s80.45.Rmd
    Untracked:  analysis/simulation-ctwas-ukbWG-gtex.adipose_visual.Rmd
    Untracked:  analysis/summarize_ctwas_plots.R
    Untracked:  code/workflow/Snakefile-simu_20210113

Unstaged changes:
    Deleted:    analysis/simulation-susieI-ukbWG-gtex.adipose_s80.45.Rmd
    Modified:   analysis/visual_twas_plots.R
    Modified:   code/qqplot.R
    Modified:   code/run_ctwas_rss.R
    Modified:   code/run_ctwas_rss2.R
    Modified:   code/simulate_phenotype.R
    Modified:   code/workflow/Snakefile-simu_20210112
    Modified:   code/workflow/Snakefile-simu_20210117
    Modified:   code/workflow/Snakefile-simu_20210118
    Modified:   code/workflow/workflow-data.ipynb

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


There are no past versions. Publish this analysis with wflow_publish() to start tracking its development.


library(ctwas)
library(data.table)
suppressMessages({library(plotly)})
library(tidyr)
library(plyr)
library(stringr)
source("~/causalTWAS/causal-TWAS/analysis/summarize_ctwas_plots.R")
source('~/causalTWAS/causal-TWAS/code/qqplot.R')
pgenfn = "/home/simingz/causalTWAS/ukbiobank/ukb_pgen_s40.22/ukb-s40.22_pgenfs.txt"
ld_pgenfn = "/home/simingz/causalTWAS/ukbiobank/ukb_pgen_s40.22/ukb-s40.22_pgenfs.txt"
outputdir = "/home/simingz/causalTWAS/simulations/simulation_ctwas_rss_20210112/" # /
runtag = "ukb-s40.22-adi"
configtags = 1
simutags = paste(rep(1:2, each = length(1:5)), 1:5, sep = "-")

pgenfs <- read.table(pgenfn, header = F, stringsAsFactors = F)[,1]
pvarfs <- sapply(pgenfs, prep_pvar, outputdir = outputdir)

ld_pgenfs <- read.table(ld_pgenfn, header = F, stringsAsFactors = F)[,1]
ld_pvarfs <- sapply(ld_pgenfs, prep_pvar, outputdir = outputdir)

pgens <- lapply(1:length(pgenfs), function(x) prep_pgen(pgenf = pgenfs[x],pvarf = pvarfs[x]))

Analysis description

n.ori <- 40000 # number of samples
n <- pgenlibr::GetRawSampleCt(pgens[[1]])
p <- sum(unlist(lapply(pgens, pgenlibr::GetVariantCt))) # number of SNPs
J <- 8021 # number of genes

Data

  • GWAS summary statistics we simulated summary statistics data with different causal gene/SNP proportion and PVE. To simulate this data, we need the following:
    • genotype data we used is from UKB biobank, randomly selecting 40000 samples. We then filtered samples based on relatedness, ethics and other qc metrics, that ended up with n = 22542 samples. We use SNP genotype data from chr 1 to chr 22 combined from UKB. There are total = 6229504 SNPs.

    • Expression models The one we used in this analysis is GTEx Adipose tissue v7 dataset. This dataset contains ~ 380 samples, 8021 genes with expression model. FUSION/TWAS were used to train expression model and we used their lasso results. SNPs included in eQTL anlaysis are restricted to cis-locus 500kb on either side of the gene boundary. eQTLs are defined as SNPs with abs(effectize) > 1e-8 in lasso results.

    To simulate phenotype data, first we impute gene expression based on expression models, then we set gene/SNP pi1 and PVE, get rough effect size for causal SNPs and genes and simulate phenotype under the sparse model with spike and slab prior. Then we performed GWAS for all SNPs and get z scores for each by univariate linear regression.

  • LD genotype reference We used the same genotype as in simulation to serve as the LD reference.

  • Expression models
    We used GTEx Adipose tissue v7 dataset, the same as used for simulating phenotypes.

Analysis

  1. Get z scores for gene expression. We used expression models and LD reference to get z scores for gene expression.

  2. Run ctwas_rss We used LDetect to define regions. ctwas_rss algorithm first runs on all regions to get rough estimate for gene and SNP prior. Then run on small regions (having small probablities of having > 1 causal signals based on rough estimates) to get more accurate estimate. To lower computational burden, we downsampled SNPs (0.1), estimate parameters and convert back to orginal scale. Lastly, run susie with given L for all regions and for all genes and SNPs using estimated prior and prior variance.

Power estimation

simutag <- "1-1"
niter <- 1000
snp.p <- 5e-8
gene.p <- 1e-5
source(paste0(outputdir, "simu", simutag, "_param.R"))
load(paste0(outputdir, runtag, "_simu", simutag, "-pheno.Rd"))

We select run 1-1 as an example.

  • For SNPs. \(\pi_1 =\) 2.510^{-4} , effect size = 0.0283342, PVE = 0.5066594. Power at 5e-08 p value cutoff:
load("data/power_s40.22.Rd")
# p1 <- pow(niter, n, phenores[["batch"]][[1]][["sigma_theta"]], snp.p)
print(p1)
[1] 0.056
  • For genes. \(\pi_1 = 0.05\) , effect size = 0.0248146, PVE = 0.0998065. Power at 1e-05 p value cutoff:
# p2 <- pow(niter, n, phenores[["batch"]][[1]][["sigma_beta"]], gene.p)
print(p2)
[1] 0.079
# save(p1,p2, file = "data/power_s80.45.Rd")

GWAS/TWAS p value distribution

simutag <- "1-1"
chrom <- 1
source(paste0(outputdir, "simu", simutag, "_param.R"))
load(paste0(outputdir, runtag, "_simu", simutag, "-pheno.Rd"))

We select run 1-1 as an example.

  • For genes. \(\pi_1 = 0.05\) , effect size = 0.0248146, PVE = 0.0998065. TWAS p values and qqplot:
exprgwasf <- paste0(outputdir, runtag, "_simu", simutag, ".exprgwas.txt.gz")

exprvarf <- paste0(outputdir, runtag, "_chr", chrom, ".exprvar")
exprid <- read_exprvar(exprvarf)[, "id"]
cau <- as.matrix(exprid[phenores[["batch"]][[chrom]][["idx.cgene"]]])
pdist_plot(exprgwasf, chrom, cau)

exprgwas <- fread(exprgwasf, header =T)
gg_qqplot(exprgwas$PVALUE)

  • For SNPs. \(\pi_1 =\) 2.510^{-4} , effect size = 0.0283342, PVE = 0.5066594. GWAS p values and qqplot:
snpgwasf <-  paste0(outputdir, runtag, "_simu", simutag, ".snpgwas.txt.gz")

pvarf <- pvarfs[chrom]
snpid <- read_pvar(pvarf)[, "id"]
cau <- as.matrix(snpid[phenores[["batch"]][[chrom]][["idx.cSNP"]]])
pdist_plot(snpgwasf, chrom, cau, thin = 0.1)

snpgwas <- fread(snpgwasf, header =T)
gg_qqplot(snpgwas$PVALUE, thin = 0.1)

ctwas results

Results: Each row shows parameter estimation results from 5 simulation runs with similar settings (i.e. pi1 and PVE for genes and SNPs). Results from each run were represented by one dot, dots with the same color come from the same run. truth: the true parameters, selected_truth: the truth in selected regions that were used to estimate parameters, ctwas: ctwas estimated parameters (using summary statistics as input).

plot_par <- function(configtag, tags){
  source(paste0(outputdir, "config", configtag, ".R"))
  phenofs <- paste0(outputdir, runtag, "_simu", simutags, "-pheno.Rd")
  susieIfs <- paste0(outputdir, runtag, "_simu", simutags, "_config", configtag, ".s2.susieIrssres.Rd")
  susieIfs2 <- paste0(outputdir, runtag, "_simu",simutags, "_config", configtag,".s2.susieIrss.txt")

  mtx <- show_param(phenofs, susieIfs, susieIfs2, thin = thin)
  par(mfrow=c(1,3))
  cat("simulations ", paste(tags, sep=",") , ": ")
  cat("mean gene PVE:", mean(mtx[, "PVE.gene_truth"]), ",", "mean SNP PVE:", mean(mtx[, "PVE.SNP_truth"]), "\n")
  plot_param(mtx)
}

plot_PIP <- function(configtag, tags){
   phenofs <- paste0(outputdir, runtag, "_simu", simutags, "-pheno.Rd")
   susieIfs <- paste0(outputdir, runtag, "_simu",simutags, "_config", configtag,".susieIrss.txt")

   f1 <- caliPIP_plot(phenofs, susieIfs)
   f2 <- ncausal_plot(phenofs, susieIfs) 
   gridExtra::grid.arrange(f1, f2, ncol =2)
}

ctwas_rss parameter estimation

configtag <- 1

simutags <- paste(1, 1:5, sep = "-")
plot_par(configtag, simutags)
simulations  1-1 1-2 1-3 1-4 1-5 : mean gene PVE: 0.09425993 , mean SNP PVE: 0.5068263 

# plot_PIP(configtag, simutags)

simutags <- paste(2, 1:5, sep = "-")
plot_par(configtag, simutags)
simulations  2-1 2-2 2-3 2-4 2-5 : mean gene PVE: 0.1114992 , mean SNP PVE: 0.5008008 

# plot_PIP(configtag, simutags)

sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Scientific Linux 7.4 (Nitrogen)

Matrix products: default
BLAS/LAPACK: /software/openblas-0.2.19-el7-x86_64/lib/libopenblas_haswellp-r0.2.19.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plotrix_3.7-6     cowplot_1.0.0     stringr_1.4.0     plyr_1.8.4       
[5] tidyr_1.1.0       plotly_4.9.0      ggplot2_3.2.1     data.table_1.13.2
[9] ctwas_0.1.5      

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0  xfun_0.8          purrr_0.3.4      
 [4] lattice_0.20-38   pgenlibr_0.2      colorspace_1.4-1 
 [7] vctrs_0.3.1       htmltools_0.3.6   viridisLite_0.3.0
[10] yaml_2.2.0        rlang_0.4.6       R.oo_1.22.0      
[13] later_0.8.0       pillar_1.4.2      R.utils_2.9.0    
[16] glue_1.3.1        withr_2.1.2       foreach_1.4.4    
[19] lifecycle_0.1.0   munsell_0.5.0     gtable_0.3.0     
[22] workflowr_1.6.2   R.methodsS3_1.7.1 htmlwidgets_1.3  
[25] codetools_0.2-16  evaluate_0.14     labeling_0.3     
[28] knitr_1.23        httpuv_1.5.1      highr_0.8        
[31] logging_0.10-108  Rcpp_1.0.5        promises_1.0.1   
[34] scales_1.1.0      backports_1.1.4   jsonlite_1.6     
[37] farver_2.0.1      fs_1.3.1          digest_0.6.20    
[40] stringi_1.4.3     dplyr_0.8.3       grid_3.6.1       
[43] rprojroot_1.3-2   tools_3.6.1       magrittr_1.5     
[46] lazyeval_0.2.2    tibble_2.1.3      crayon_1.3.4     
[49] pkgconfig_2.0.2   Matrix_1.2-18     assertthat_0.2.1 
[52] rmarkdown_1.13    httr_1.4.2        iterators_1.0.10 
[55] R6_2.4.0          git2r_0.26.1      compiler_3.6.1