Last updated: 2020-08-12

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 file has unstaged changes. 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/code/fit_mr.ash.R code/fit_mr.ash.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 1d3e1ed. 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:    code/workflow/.ipynb_checkpoints/
    Ignored:    data/

Untracked files:
    Untracked:  analysis/visual_twas_plots.R
    Untracked:  code/run_test_mr.ash_gene.R
    Untracked:  code/run_test_susieI.R

Unstaged changes:
    Modified:   analysis/simulation-mr.ash2s-ukbchr17to22-gtex.adipose.Rmd
    Modified:   analysis/simulation-mr.ash2s-ukbchr22-gtex.adipose.Rmd
    Modified:   code/run_ld.R
    Modified:   code/workflow/workflow-ashtest3.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.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/simulation-mr.ash2s-ukbchr22-gtex.adipose.Rmd) and HTML (docs/simulation-mr.ash2s-ukbchr22-gtex.adipose.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd ca1d8bc simingz 2020-07-02 mr.ash2s lasso beta init
Rmd ed258e4 simingz 2020-06-22 fix sa2 bug; locus zoom
Rmd d9c73d0 simingz 2020-06-18 logging and more weights for chr22

We test the performance of mr.ash2s on ukbiobank data (chr22, n sample = 20,000). We use weights from Fusion website, using Adipose (subcutaneous) p < 0.01. Total features > 8,000. On chr22: 240.

library(mr.ash.alpha)
library(data.table)
suppressMessages({library(plotly)})

source("~/causalTWAS/causal-TWAS/code/fit_mr.ash.R")

summary_mr.ash <- function(fit){
  cat("pi1 = ", 1-fit$pi[[1]], "\n")
  pve <- get_pve(fit)
  cat("pve = ", pve, "\n")
}

summary_mr.ash2 <- function(g.fit, s.fit, phenores, pipcut = 0.1){
  e.b <- rep(0, length(g.fit$beta))
  e.b[phenores$param$idx.cgene] <- phenores$param$e.beta
  cat("gene expression effect: \n")
  summary_mr.ash(g.fit)
  
  s.b <- rep(0, length(s.fit$beta))
  s.b[phenores$param$idx.cSNP] <- phenores$param$s.theta
  cat("snp effect: \n")
  summary_mr.ash(s.fit)
}

plot_pip <- function(dt, ld = F){
  
  if (isTRUE(ld)) {
    fig <- plot_ly(dt, x = ~p0, y = ~ PIP, type = 'scatter', 
                   mode = 'markers', color = ~ max_r2, 
                   text = ~ paste("Name: ", name, 
                                  "\n", ifcausal,
                                  "\nmax R^2: ", round(max_r2, digits = 2), 
                                  "\nMax pair: ", max_pair))
  } else {
    pal <- c("salmon", "darkgreen")
    pal <- setNames(pal, c("Causal", "Non causal"))
  
    fig <- plot_ly(dt, x = ~p0, y = ~ PIP, type = 'scatter', mode = 'markers', color = ~ ifcausal, colors = pal, text = ~paste("Name: ", name))
  }

  fig <- fig %>% layout(yaxis = list(title = "PIP"))
  return(fig)
}

plot_beta <- function(dt){
  fig <- plot_ly(dt, x = ~p0, y = ~ TrueBeta, type = 'scatter', mode = 'markers', name ="Truth", text = ~paste("Name: ", name))
  fig <- fig %>% add_trace(y = ~ EstBeta, name = 'Estimated',mode = 'markers')

  fig <- fig %>% layout(yaxis = list(title = "beta"))
  return(fig)
}

plot_p <- function(dt){
  pal <- c("salmon", "darkgreen")
  pal <- setNames(pal, c("Causal", "Non causal"))
  
  fig <- plot_ly(dt, x = ~p0, y = ~ PVALUE , type = 'scatter', mode = 'markers', color = ~ ifcausal, colors = pal, text = ~paste("Name: ", name))

  fig <- fig %>% layout(yaxis = list(title = "p value"))
  return(fig)
}

plot_susie_pip <- function(dt) {
   pal <- c("salmon", "darkgreen")
   pal <- setNames(pal, c("Causal", "Non causal"))
   
   fig <- plot_ly(dt, x = ~ p0, y = ~ susiePIP , type = 'scatter', mode = 'markers', color = ~ ifcausal, colors = pal, text = ~paste("Name: ", name))

   fig <- fig %>% layout(yaxis = list(title = "SUSIE PIP"))
}

plot_all <- function(mrashres.gene, gwasres.gene, mrashres.snp, gwasres.snp, susieres, ldres){
  a <- read.table(mrashres.gene , header =T)
  a[a$ifcausal == 1, "ifcausal"] = "Causal"
  a[a$ifcausal == 0, "ifcausal"] = "Non causal"
  
  ld <- read.table(ldres, header =T)
  a <- merge(a, ld, by = "name", all.x = T)
  
  su <- read.table(susieres, header =T)
  colnames(su)[colnames(su) == "pip"] <-  "susiePIP"
  a <- merge(a, su, by = "name", all.x = T)
  
  p <- fread(gwasres.gene, header =T)
  p[,"PVALUE"] <- -log10(p[,"PVALUE"])
  colnames(p)[colnames(p) == "MARKER_ID"] <-  "name"
  dt.gene <- merge(a, p, by = "name", all.x = T)

  a <- read.table(mrashres.snp , header =T)
  a[a$ifcausal == 1, "ifcausal"] = "Causal"
  a[a$ifcausal == 0, "ifcausal"] = "Non causal"

  p <- fread(gwasres.snp, header =T)
  p[,"PVALUE"] <- -log10(p[,"PVALUE"])
  colnames(p)[colnames(p) == "MARKER_ID"] <-  "name"
  dt.snp <- merge(a, p, by = "name", all.x = T)
  
  fig1 <- plot_pip(dt.gene, ld = T)  
  fig2 <- plot_beta(dt.gene) 
  fig3 <- plot_p(dt.gene) 
  fig4 <- plot_susie_pip(dt.gene) 
  fig5 <- plot_pip(dt.snp) 
  fig6 <- plot_beta(dt.snp) 
  fig7 <- plot_p(dt.snp) %>% layout( plot_bgcolor='#EFF6FC')
  
  fig <- subplot(fig1, fig2, fig3, fig4, fig5, fig6, fig7, nrows=7, shareX = T, titleY = T)
  
  fig <- fig %>% layout(autosize = F, width = 600, height = 900, margin = 0.02,
    xaxis = list(title = "Genomic position"))
  
  fig <- fig %>% toWebGL()
  fig
}
simdatadir <- "~/causalTWAS/simulations/simulation_ashtest_20200616/"
outputdir <- "~/causalTWAS/simulations/simulation_ashtest_20200616/"
susiedir <- "~/causalTWAS/simulations/simulation_susietest_20200616/"
get_files <- function(tag, tag2){
  par <- paste0(outputdir, tag, "-mr.ash2s.", tag2, ".param.txt")
  gmrash <- paste0(outputdir, tag, "-mr.ash2s.", tag2, ".expr.txt")
  ggwas <- paste0(outputdir, tag, ".exprgwas.txt.gz")
  smrash <- paste0(outputdir, tag, "-mr.ash2s.", tag2, ".snp.txt")
  sgwas <- paste0(outputdir, tag, ".snpgwas.txt.gz")
  susie <- paste0(susiedir, tag, ".", tag2, ".L3-susieres.txt")
  rsq <- paste0(outputdir, tag, ".LD.txt")
  return(tibble::lst(par, gmrash, ggwas, smrash, sgwas, susie, rsq))
}

Simulation 1

MR.ASH2s results (start from gene):

tag <- "20200616-1"
tag2 <- "expr-res"

param <- read.table(paste0(outputdir, tag, "-mr.ash2s.", tag2, ".param.txt"), header = T, row.names = 1)
print(param)
            estimated       truth
gene.pi1 0.0426885773 0.050000000
gene.pve 0.0122476815 0.009728774
snp.pi1  0.0004598143 0.004995857
snp.pve  0.0434112482 0.052606374

MR.ASH results (start from SNP):

tag <- "20200616-1"
tag2 <- "snp-res"

param <- read.table(paste0(outputdir, tag, "-mr.ash2s.", tag2, ".param.txt"), header = T, row.names = 1)
print(param)

Simulation 2

MR.ASH results (start from gene):

tag <- "20200616-2"
tag2 <- "expr-res"

param <- read.table(paste0(outputdir, tag, "-mr.ash2s.", tag2, ".param.txt"), header = T, row.names = 1)
print(param)
           estimated        truth
gene.pi1 0.004914075 0.0083333333
gene.pve 0.025797069 0.0259632167
snp.pi1  0.000478632 0.0009991714
snp.pve  0.046614513 0.0537683179

MR.ASH results (start from snp):

tag <- "20200616-2"
tag2 <- "snp-res"

param <- read.table(paste0(outputdir, tag, "-mr.ash2s.", tag2, ".param.txt"), header = T, row.names = 1)
print(param)
            estimated        truth
gene.pi1 0.0049690283 0.0083333333
gene.pve 0.0238702087 0.0259632167
snp.pi1  0.0004787319 0.0009991714
snp.pve  0.0465108737 0.0537683179

Simulation 3

MR.ASH results (start from gene):

tag <- "20200616-3"
tag2 <- "expr-res"

param <- read.table(paste0(outputdir, tag, "-mr.ash2s.", tag2, ".param.txt"), header = T, row.names = 1)
print(param)
           estimated        truth
gene.pi1 0.024185757 0.0500000000
gene.pve 0.007153070 0.0037961081
snp.pi1  0.000027828 0.0009991714
snp.pve  0.002831605 0.0070873214

MR.ASH results (start from snp):

tag <- "20200616-3"
tag2 <- "snp-res"

param <- read.table(paste0(outputdir, tag, "-mr.ash2s.", tag2, ".param.txt"), header = T, row.names = 1)
print(param)
            estimated        truth
gene.pi1 2.418579e-02 0.0500000000
gene.pve 7.153081e-03 0.0037961081
snp.pi1  2.782795e-05 0.0009991714
snp.pve  2.831600e-03 0.0070873214

Simulation 4

MR.ASH results (start from gene):

tag <- "20200616-4"
tag2 <- "expr-res"

param <- read.table(paste0(outputdir, tag, "-mr.ash2s.", tag2, ".param.txt"), header = T, row.names = 1)
print(param)
            estimated        truth
gene.pi1 1.347012e-05 0.0083333333
gene.pve 3.927603e-06 0.0006724397
snp.pi1  3.837943e-05 0.0049958571
snp.pve  3.802902e-03 0.0104491656

MR.ASH results (start from snp):

tag <- "20200616-4"
tag2 <- "snp-res"

param <- read.table(paste0(outputdir, tag, "-mr.ash2s.", tag2, ".param.txt"), header = T, row.names = 1)
print(param)
            estimated        truth
gene.pi1 1.118801e-05 0.0083333333
gene.pve 3.263589e-06 0.0006724397
snp.pi1  3.838511e-05 0.0049958571
snp.pve  3.803461e-03 0.0104491656

Simulation 5

MR.ASH results (start from gene):

tag <- "20200616-5"
tag2 <- "expr-res"

f <- get_files(tag= tag, tag2 = tag2)
param <- read.table(f$par, header = T, row.names = 1)
print(param)
plot_all(f$gmrash, f$ggwas, f$smrash, f$sgwas, f$susie, f$rsq)

MR.ASH results (start from SNP): very similar to start from gene.

tag <- "20200616-5"
tag2 <- "snp-res"

f <- get_files(tag= tag, tag2 = tag2)
param <- read.table(f$par, header = T, row.names = 1)
print(param)
plot_all(f$gmrash, f$ggwas, f$smrash, f$sgwas, f$susie, f$rsq)

Simulation 6

MR.ASH results (start from gene):

tag <- "20200616-6"
tag2 <- "expr-res"

f <- get_files(tag= tag, tag2 = tag2)
param <- read.table(f$par, header = T, row.names = 1)
print(param)
plot_all(f$gmrash, f$ggwas, f$smrash, f$sgwas, f$susie, f$rsq)

MR.ASH results (start from SNP): very similar to start from gene

tag <- "20200616-6"
tag2 <- "snp-res"

f <- get_files(tag= tag, tag2 = tag2)
param <- read.table(f$par, header = T, row.names = 1)
print(param)
plot_all(f$gmrash, f$ggwas, f$smrash, f$sgwas, f$susie, f$rsq)

Simulation 7

Use lasso trained weight model from FUSION twas. This makes eQTLs (SNP weights != 0) more sparse (<20/gene) compared to BSLMM/BLUP (a few hundred/per gene).

Use beta.init = lasso for mr.ash.

MR.ASH results (use lasso for all):

tag <- "20200616-7"
tag2 <- "lasso"

f <- get_files(tag= tag, tag2 = tag2)
param <- read.table(f$par, header = T, row.names = 1)
print(param)
plot_all(f$gmrash, f$ggwas, f$smrash, f$sgwas, f$susie, f$rsq)

MR.ASH results (use lasso for just for SNPs):

tag <- "20200616-7"
tag2 <- "lassoSNP"

f <- get_files(tag= tag, tag2 = tag2)
param <- read.table(f$par, header = T, row.names = 1)
print(param)
plot_all(f$gmrash, f$ggwas, f$smrash, f$sgwas, f$susie, f$rsq)

Simulation 8

Use lasso trained weight model from FUSION twas. This makes eQTLs (SNP weights != 0) more sparse (<20/gene) compared to BSLMM/BLUP (a few hundred/per gene).

Use beta.init = lasso for mr.ash.

MR.ASH results (use lasso for all):

tag <- "20200616-8"
tag2 <- "lasso"

f <- get_files(tag= tag, tag2 = tag2)
param <- read.table(f$par, header = T, row.names = 1)
print(param)
plot_all(f$gmrash, f$ggwas, f$smrash, f$sgwas, f$susie, f$rsq)

MR.ASH results (use lasso for just for SNPs):

tag <- "20200616-8"
tag2 <- "lassoSNP"

f <- get_files(tag= tag, tag2 = tag2)
param <- read.table(f$par, header = T, row.names = 1)
print(param)
plot_all(f$gmrash, f$ggwas, f$smrash, f$sgwas, f$susie, f$rsq)

More examples from separate runs:

source("analysis/visual_twas_plots.R")
tag <- "20200616-8-7"
tag2 <- "lassoes-es"
f <- get_files(tag= tag, tag2 = tag2)
param <- read.table(f$par, header = T, row.names = 1)
print(param)
            estimated        truth
gene.pi1 0.0122711089 0.0209205021
gene.pve 0.0120372617 0.0075310692
snp.pi1  0.0002556609 0.0002558854
snp.pve  0.0965410043 0.0742511618
plot_all(f$gmrash, f$ggwas, f$smrash, f$sgwas, f$gsusie, f$ssusie, f$rsq)

sessionInfo()
R version 3.5.1 (2018-07-02)
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] plotly_4.9.2.9000   ggplot2_3.3.1       data.table_1.12.7  
[4] mr.ash.alpha_0.1-34

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0  purrr_0.3.4       lattice_0.20-38  
 [4] colorspace_1.3-2  vctrs_0.3.1       generics_0.0.2   
 [7] htmltools_0.3.6   viridisLite_0.3.0 yaml_2.2.0       
[10] rlang_0.4.6       R.oo_1.22.0       later_0.7.5      
[13] pillar_1.4.4      glue_1.4.1        withr_2.1.2      
[16] R.utils_2.7.0     lifecycle_0.2.0   stringr_1.4.0    
[19] munsell_0.5.0     gtable_0.2.0      workflowr_1.6.2  
[22] R.methodsS3_1.7.1 htmlwidgets_1.3   evaluate_0.12    
[25] knitr_1.20        httpuv_1.4.5      crosstalk_1.0.0  
[28] highr_0.7         Rcpp_1.0.4.6      xtable_1.8-3     
[31] promises_1.0.1    scales_1.0.0      backports_1.1.2  
[34] jsonlite_1.6.1    mime_0.6          fs_1.3.1         
[37] digest_0.6.25     stringi_1.3.1     dplyr_1.0.0      
[40] shiny_1.2.0       grid_3.5.1        rprojroot_1.3-2  
[43] tools_3.5.1       magrittr_1.5      lazyeval_0.2.1   
[46] tibble_3.0.1      crayon_1.3.4      whisker_0.3-2    
[49] tidyr_0.8.3       pkgconfig_2.0.2   ellipsis_0.3.1   
[52] Matrix_1.2-15     rmarkdown_1.10    httr_1.4.1       
[55] R6_2.3.0          git2r_0.26.1      compiler_3.5.1