Last updated: 2024-02-13

Checks: 6 1

Knit directory: QBS-statsgen/

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(20231230) 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.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

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 2a4438c. 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:    .Rproj.user/B81CBE6F/bibliography-index/
    Ignored:    .Rproj.user/B81CBE6F/ctx/
    Ignored:    .Rproj.user/B81CBE6F/pcs/
    Ignored:    .Rproj.user/B81CBE6F/presentation/
    Ignored:    .Rproj.user/B81CBE6F/profiles-cache/
    Ignored:    .Rproj.user/B81CBE6F/sources/per/
    Ignored:    .Rproj.user/B81CBE6F/sources/s-4dd76fec/
    Ignored:    .Rproj.user/B81CBE6F/tutorial/
    Ignored:    .Rproj.user/shared/notebooks/1C2AC29C-e1-gwas-power/
    Ignored:    .Rproj.user/shared/notebooks/1EB0B2DC-e1-gwas/1/s/ce0r78nx8keuu/
    Ignored:    .Rproj.user/shared/notebooks/1EB0B2DC-e1-gwas/1/s/csetup_chunk/
    Ignored:    .Rproj.user/shared/notebooks/1EB0B2DC-e1-gwas/1/s/czxn6jf8lsykc/
    Ignored:    .Rproj.user/shared/notebooks/201A6972-e3-mr/
    Ignored:    .Rproj.user/shared/notebooks/26ED8139-e2-prs/
    Ignored:    .Rproj.user/shared/notebooks/2EB8AA83-index/1/B81CBE6Fc2354472/
    Ignored:    .Rproj.user/shared/notebooks/6834E257-e3-qtlmapping/
    Ignored:    .Rproj.user/shared/notebooks/BC66D613-e2-lmm/
    Ignored:    .Rproj.user/shared/notebooks/F12E549C-e3-gwasqtl/
    Ignored:    .Rproj.user/shared/notebooks/FCFC3BD0-e2-finemapping/
    Ignored:    data/e2/
    Ignored:    data/e3/
    Ignored:    output/

Untracked files:
    Untracked:  analysis/e3-gwasqtl.Rmd
    Untracked:  analysis/e3-mr.Rmd
    Untracked:  analysis/e3-qtlmapping.Rmd

Unstaged changes:
    Modified:   .Rhistory
    Modified:   .Rproj.user/B81CBE6F/persistent-state
    Modified:   .Rproj.user/B81CBE6F/sources/prop/4C8B7780
    Modified:   .Rproj.user/B81CBE6F/sources/prop/INDEX
    Deleted:    .Rproj.user/B81CBE6F/sources/s-e0e7218a/27031BD8-contents
    Deleted:    .Rproj.user/B81CBE6F/sources/s-e0e7218a/34A40D3B-contents
    Deleted:    .Rproj.user/B81CBE6F/sources/s-e0e7218a/6C1FFABC-contents
    Deleted:    .Rproj.user/B81CBE6F/sources/s-e0e7218a/73BDB580-contents
    Deleted:    .Rproj.user/B81CBE6F/sources/s-e0e7218a/lock_file
    Modified:   .Rproj.user/shared/notebooks/2EB8AA83-index/1/s/chunks.json
    Modified:   .Rproj.user/shared/notebooks/paths
    Modified:   analysis/index.Rmd

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.


Before the class

Install R packages.

Read in genotype data

We use the genotype data provided by the SuSiE package. In this demo we assume these SNPs are close to a gene, i.e. would be included in a cis-EQTL mapping analysis.

library(susieR)
data(N2finemapping)
attach(N2finemapping)
G <- scale(X)
N <- nrow(G)
M <- ncol(G)

set.seed(123)

Simulate phenotype and expression

library(MASS)
library(glmnet)
Loading required package: Matrix
Loaded glmnet 4.1-2
# Simulation function
# G: genotype matrix. h2.eQTL: h2 of eQTL. n.eQTL: number of eQTL SNPs. gamma: effect size of gene. theta: effect size of SNP.
simulate <- function(G, h2.eQTL, n.eQTL, gamma, theta) {
  N <- dim(G)[1]
  M <- dim(G)[2]
  
  idx.eQTL <- sample(1:M, n.eQTL)
  alpha <- rep(sqrt(h2.eQTL/n.eQTL), n.eQTL)
  
  # simulate gene expression data
  X <- G[, idx.eQTL] %*% alpha + rnorm(N, sd=1)
  
  # simulate phenotype data
  idx.SNP <- idx.eQTL[1] # choose a SNP that is one eQTL
  Y <- X * gamma + as.matrix(G[, idx.SNP]) %*% theta + rnorm(N)
  
  # return results
  return (list(X=X, Y=Y, idx.eQTL=idx.eQTL, idx.SNP))
}

Run a gene-level association analysis

  • Simulate X and Y
h2.eQTL <- 0.3
n.eQTL <- 2
gamma <- 0.18
theta <- 0

data1 <- simulate(G, h2.eQTL, n.eQTL, gamma, theta)
  • Predict gene expression
cvfit <- cv.glmnet(G, data1$X)
X.tilde <- predict(cvfit, G, s = "lambda.min")
plot(data1$X, X.tilde )

  • Run association analysis between predicted expression and phenotype
fit <- lm(data1$Y ~ X.tilde)
pvalue <- summary(fit)$coefficients["X.tilde",4]
print(pvalue)
[1] 0.001428478

TWAS is confounded by variant pleiotropic effect.

  • Simulate X and Y
h2.eQTL <- 0.3
n.eQTL <- 2
gamma <- 0
theta <- 0.18

data2 <- simulate(G, h2.eQTL, n.eQTL, gamma, theta)
  • Predict gene expression
cvfit <- cv.glmnet(G, data2$X)
X.tilde <- predict(cvfit, G, s = "lambda.min")
plot(data2$X, X.tilde) 

  • Run association analysis between predicted expression and phenotype
fit <- lm(data2$Y ~ X.tilde)
pvalue <- summary(fit)$coefficients["X.tilde",4]
print(pvalue)
[1] 0.000167917

sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS:   /software/R-4.1.0-no-openblas-el7-x86_64/lib64/R/lib/libRblas.so
LAPACK: /software/R-4.1.0-no-openblas-el7-x86_64/lib64/R/lib/libRlapack.so

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

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

other attached packages:
[1] glmnet_4.1-2   Matrix_1.3-3   MASS_7.3-54    susieR_0.12.35

loaded via a namespace (and not attached):
 [1] shape_1.4.6        tidyselect_1.1.1   xfun_0.38          bslib_0.4.2       
 [5] purrr_0.3.4        splines_4.1.0      lattice_0.20-44    colorspace_2.0-2  
 [9] vctrs_0.3.8        generics_0.1.0     htmltools_0.5.5    yaml_2.2.1        
[13] survival_3.2-11    utf8_1.2.1         rlang_1.1.0        mixsqp_0.3-48     
[17] jquerylib_0.1.4    later_1.2.0        pillar_1.6.1       glue_1.4.2        
[21] DBI_1.1.1          foreach_1.5.1      plyr_1.8.6         matrixStats_0.59.0
[25] lifecycle_1.0.3    stringr_1.4.0      munsell_0.5.0      gtable_0.3.0      
[29] workflowr_1.6.2    codetools_0.2-18   evaluate_0.20      knitr_1.42        
[33] fastmap_1.1.0      httpuv_1.6.1       irlba_2.3.3        fansi_0.5.0       
[37] highr_0.9          Rcpp_1.0.9         promises_1.2.0.1   scales_1.1.1      
[41] cachem_1.0.5       jsonlite_1.7.2     fs_1.6.1           ggplot2_3.3.5     
[45] digest_0.6.27      stringi_1.6.2      dplyr_1.0.7        rprojroot_2.0.2   
[49] grid_4.1.0         cli_3.6.1          tools_4.1.0        magrittr_2.0.1    
[53] sass_0.4.0         tibble_3.1.2       crayon_1.5.2       pkgconfig_2.0.3   
[57] ellipsis_0.3.2     iterators_1.0.13   reshape_0.8.8      assertthat_0.2.1  
[61] rmarkdown_2.21     rstudioapi_0.13    R6_2.5.0           git2r_0.28.0      
[65] compiler_4.1.0