Last updated: 2020-11-06

Checks: 7 0

Knit directory: mmbr-rss-dsc/

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.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

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(20200227) 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 c6470aa. 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:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    output/.DS_Store

Untracked files:
    Untracked:  analysis/GTExprofile2.Rmd
    Untracked:  data/ENSG00000140265.12.Multi_Tissues.rds
    Untracked:  data/FastQTLSumStats.mash.FL_PC3.rds
    Untracked:  output/.mmbr_gtex_res.Rprof.swp
    Untracked:  output/GTExprofile_res.rds
    Untracked:  output/GTExprofile_resL1.rds
    Untracked:  output/GTExprofile_resL1_elbo.rds
    Untracked:  output/GTExprofile_resL3.rds
    Untracked:  output/GTExprofile_resL3_elbo.rds
    Untracked:  output/GTExprofile_res_elbo.rds
    Untracked:  output/GTExprofile_resapprox.rds
    Untracked:  output/GTExprofile_resapproxL1.rds
    Untracked:  output/GTExprofile_resapproxL1_elbo.rds
    Untracked:  output/GTExprofile_resapproxL3.rds
    Untracked:  output/GTExprofile_resapproxL3_elbo.rds
    Untracked:  output/GTExprofile_resapprox_elbo.rds
    Untracked:  output/GTExprofile_resapproxdiag.rds
    Untracked:  output/GTExprofile_resapproxdiagL1.rds
    Untracked:  output/GTExprofile_resapproxdiagL1_elbo.rds
    Untracked:  output/GTExprofile_resapproxdiagL3.rds
    Untracked:  output/GTExprofile_resapproxdiagL3_elbo.rds
    Untracked:  output/GTExprofile_resapproxdiag_elbo.rds
    Untracked:  output/GTExprofile_resdiag.rds
    Untracked:  output/mmbr_gtex_res.Rprof
    Untracked:  output/mmbr_gtex_res_approx.Rprof
    Untracked:  output/mmbr_gtex_res_approx_diag.Rprof
    Untracked:  output/mmbr_gtex_res_diag.Rprof
    Untracked:  output/mnm_missing_output.20200527.rds
    Untracked:  output/test
    Untracked:  output/tiny_data_211_cond2L2.gif
    Untracked:  output/tiny_data_211_cond2L2.pdf
    Untracked:  output/tiny_data_211_cond2L3.gif
    Untracked:  output/tiny_data_211_cond2L3.pdf
    Untracked:  output/tiny_data_211_cond2initL3.gif
    Untracked:  output/tiny_data_211_cond2initL3.pdf

Unstaged changes:
    Modified:   analysis/GTExprofileProblem.Rmd
    Modified:   analysis/mmbr_missing_rss_problem1.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.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/GTExprofile.Rmd) and HTML (docs/GTExprofile.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 c6470aa zouyuxin 2020-11-06 wflow_publish("analysis/GTExprofile.Rmd")
html 4f613d3 zouyuxin 2020-11-05 Build site.
Rmd 9c86ea4 zouyuxin 2020-11-05 wflow_publish("analysis/GTExprofile.Rmd")
html 04c585c zouyuxin 2020-10-26 Build site.
Rmd f2dbdb1 zouyuxin 2020-10-26 wflow_publish("analysis/GTExprofile.Rmd")
html f647b2e zouyuxin 2020-10-25 Build site.
html 2c1cd2e zouyuxin 2020-10-25 Build site.
Rmd 409a2bd zouyuxin 2020-10-25 wflow_publish("analysis/GTExprofile.Rmd")
html 6c7b7d2 zouyuxin 2020-10-25 Build site.
Rmd e1d8a6c zouyuxin 2020-10-25 wflow_publish("analysis/GTExprofile.Rmd")
html c935589 zouyuxin 2020-10-25 Build site.
Rmd c70e15b zouyuxin 2020-10-25 wflow_publish("analysis/GTExprofile.Rmd")
html 048c109 zouyuxin 2020-10-25 Build site.
Rmd 0b6e539 zouyuxin 2020-10-25 wflow_publish("analysis/GTExprofile.Rmd")
html d73c578 zouyuxin 2020-10-22 Build site.
Rmd 8a5817b zouyuxin 2020-10-22 wflow_publish("analysis/GTExprofile.Rmd")
html 766fa18 zouyuxin 2020-10-22 Build site.
Rmd 92878b8 zouyuxin 2020-10-22 wflow_publish("analysis/GTExprofile.Rmd")
html 6bd95e3 zouyuxin 2020-10-22 Build site.
Rmd edc73ea zouyuxin 2020-10-22 wflow_publish("analysis/GTExprofile.Rmd")
html b88f57a zouyuxin 2020-10-22 Build site.
Rmd 6bc1773 zouyuxin 2020-10-22 wflow_publish("analysis/GTExprofile.Rmd")
html 59e8414 zouyuxin 2020-10-21 Build site.
Rmd 720a822 zouyuxin 2020-10-21 wflow_publish("analysis/GTExprofile.Rmd")
html 2ad4ce9 zouyuxin 2020-10-21 Build site.
Rmd 5991fb8 zouyuxin 2020-10-21 wflow_publish("analysis/GTExprofile.Rmd")
html e1c109a zouyuxin 2020-10-21 Build site.
Rmd 91d1294 zouyuxin 2020-10-21 wflow_publish("analysis/GTExprofile.Rmd")
html b71d58b zouyuxin 2020-10-21 Build site.
Rmd 40a3869 zouyuxin 2020-10-21 wflow_publish("analysis/GTExprofile.Rmd")
html 16ee453 zouyuxin 2020-10-21 Build site.
Rmd d1ea2ba zouyuxin 2020-10-21 wflow_publish("analysis/GTExprofile.Rmd")
html d9c78d7 zouyuxin 2020-10-21 Build site.
Rmd eb60182 zouyuxin 2020-10-21 wflow_publish("analysis/GTExprofile.Rmd")
html 8b335f6 zouyuxin 2020-10-21 Build site.
Rmd 5bbf780 zouyuxin 2020-10-21 wflow_publish("analysis/GTExprofile.Rmd")
html 1ff70cd zouyuxin 2020-10-21 Build site.
Rmd 1872f83 zouyuxin 2020-10-21 wflow_publish("analysis/GTExprofile.Rmd")

Here is one gene identified in MASH paper that have different signs for brain vs non brain tissues.

# processing code
compute_maf <- function(geno){
  f <- mean(geno,na.rm = TRUE)/2
  return(min(f, 1-f))
}

compute_missing <- function(geno){
  miss <- sum(is.na(geno))/length(geno)
  return(miss)
}

mean_impute <- function(geno){
  f <- apply(geno, 2, function(x) mean(x,na.rm = TRUE))
  for (i in 1:length(f)) geno[,i][which(is.na(geno[,i]))] <- f[i]
  return(geno)
}

is_zero_variance <- function(x) {
  if (length(unique(x))==1) return(T)
  else return(F)
}

filter_X <- function(X, missing_rate_thresh, maf_thresh) {
  rm_col <- which(apply(X, 2, compute_missing) > missing_rate_thresh)
  if (length(rm_col)) X <- X[, -rm_col]
  rm_col <- which(apply(X, 2, compute_maf) < maf_thresh)
  if (length(rm_col)) X <- X[, -rm_col]
  rm_col <- which(apply(X, 2, is_zero_variance))
  if (length(rm_col)) X <- X[, -rm_col]
  return(mean_impute(X))
}

compute_cov_flash <- function(Y, error_cache = NULL){
  covar <- diag(ncol(Y))
  tryCatch({
    fl <- flashier::flash(Y, var.type = 2, prior.family = c(flashier::prior.normal(), flashier::prior.normal.scale.mix()), backfit = TRUE, verbose.lvl=0)
    if(fl$n.factors==0){
      covar <- diag(fl$residuals.sd^2)
    } else {
      fsd <- sapply(fl$fitted.g[[1]], '[[', "sd")
      covar <- diag(fl$residuals.sd^2) + crossprod(t(fl$flash.fit$EF[[2]]) * fsd)
    }
    if (nrow(covar) == 0) {
      covar <- diag(ncol(Y))
      stop("Computed covariance matrix has zero rows")
    }
  }, error = function(e) {
    if (!is.null(error_cache)) {
      saveRDS(list(data=Y, message=warning(e)), error_cache)
      warning("FLASH failed. Using Identity matrix instead.")
      warning(e)
    } else {
      stop(e)
    }
  })
  s <- apply(Y, 2, sd, na.rm=T)
  if (length(s)>1) s = diag(s)
  else s = matrix(s,1,1)
  covar <- s%*%cov2cor(covar)%*%s
  return(covar)
}

get_center <- function(k,n) {
  ## For given number k, get the range k surrounding n/2
  ## but have to make sure it does not go over the bounds
  if (is.null(k)) {
    return(1:n)
  }
  start = floor(n/2 - k/2)
  end = floor(n/2 + k/2)
  if (start<1) start = 1
  if (end>n) end = n
  return(start:end)
}
dat = readRDS('data/ENSG00000140265.12.Multi_Tissues.rds')
prior = 'data/FastQTLSumStats.mash.FL_PC3.rds'
cis = 500
U = readRDS(prior)$Ulist
weights = rep(1/length(U), length(U))
prior = mmbr::create_mash_prior(mixture_prior=list(weights=weights, matrices=U))
resid_Y = compute_cov_flash(dat$y_res)
X = filter_X(dat$X, 0.1, 0.05)
X = X[,get_center(cis, ncol(X))]
Y = dat$y_res

The covariance/correlation matrix of Y using pairwise complete observations:

library(corrplot)
corrplot 0.84 loaded
par(mfrow=c(1,2))
corrplot(cov(Y, use='pairwise.complete.obs'), method='color', type='upper', tl.col="black", tl.srt=45, is.corr = FALSE)
corrplot(cor(Y, use='pairwise.complete.obs'), method='color', type='upper', tl.col="black", tl.srt=45, is.corr = TRUE)

Version Author Date
2c1cd2e zouyuxin 2020-10-25
c935589 zouyuxin 2020-10-25
048c109 zouyuxin 2020-10-25

The covarince/correlation matrix of Y using FLASH:

colnames(resid_Y) = rownames(resid_Y) = colnames(Y)
par(mfrow=c(1,2))
corrplot(resid_Y, method='color', type='upper', tl.col="black", tl.srt=45, is.corr = FALSE)
corrplot(cov2cor(resid_Y), method='color', type='upper', tl.col="black", tl.srt=45, is.corr = TRUE)

Version Author Date
2c1cd2e zouyuxin 2020-10-25
c935589 zouyuxin 2020-10-25
048c109 zouyuxin 2020-10-25

Models with L = 10

We fit 4 models with L = 10:

  1. model with exact computation

  2. model with exact computation using diagonal residual variance

  3. model with approximate computation

  4. model with approximate computation using diagonal residual variance

We expect model 2 and 4 have same results.

library(profvis)
prof1 = profvis({
  stime <- proc.time()
  res <- mmbr::msusie(X, Y, prior_variance=prior, residual_variance=resid_Y, approximate=FALSE)
  etime <- proc.time()
  time_res <- etime - stime
  },prof_output='output/mmbr_gtex_res.Rprof')
saveRDS(list(result = res, result_time = time_res, result_profile = prof1), 'output/GTExprofile_res.rds')
rm(res)
rm(prof1)

prof2 = profvis({
  stime <- proc.time()
  res_diag <- mmbr::msusie(X, Y, prior_variance=prior, residual_variance=diag(diag(resid_Y)), approximate=FALSE)
  etime <- proc.time()
  time_res_diag <- etime - stime
  },prof_output='output/mmbr_gtex_res_diag.Rprof')
saveRDS(list(result = res_diag, result_time = time_res_diag, result_profile = prof2), 'output/GTExprofile_resdiag.rds')
rm(res_diag)
rm(prof2)

prof3 = profvis({
  stime <- proc.time()
  res_approx <- mmbr::msusie(X, Y, prior_variance=prior, residual_variance=resid_Y, approximate=TRUE,, L=3)
  etime <- proc.time()
  time_res_approx <- etime - stime
  },prof_output='output/mmbr_gtex_res_approx.Rprof')
saveRDS(list(result = res_approx, result_time = time_res_approx, result_profile = prof3), 'output/GTExprofile_resapprox.rds')
rm(res_approx)
rm(prof3)

prof4 = profvis({
  stime <- proc.time()
  res_approx_diag <- mmbr::msusie(X, Y, prior_variance=prior, residual_variance=diag(diag(resid_Y)), approximate=TRUE, L=3)
  etime <- proc.time()
  time_res_approx_diag <- etime - stime
  },prof_output='output/mmbr_gtex_res_approx_diag.Rprof')
saveRDS(list(result = res_approx_diag, result_time = time_res_approx_diag, result_profile = prof4), 'output/GTExprofile_resapproxdiag.rds')
rm(res_approx_diag)
rm(prof4)

Load models:

library(mmbr)
Loading required package: mashr
Loading required package: ashr
Loading required package: susieR
res1 = readRDS('output/GTExprofile_res.rds')
res2 = readRDS('output/GTExprofile_resdiag.rds')
res3 = readRDS('output/GTExprofile_resapprox.rds')
res4 = readRDS('output/GTExprofile_resapproxdiag.rds')

As we expected, model 2 and 4 are same.

all.equal(res2$result$pip, res4$result$pip)
[1] TRUE

We check results from model 1, 3 and 4.

Model 1 credible sets:

susie_plot(res1$result, y='PIP')

Version Author Date
048c109 zouyuxin 2020-10-25
1ff70cd zouyuxin 2020-10-21

Model 3 credible sets:

susie_plot(res3$result, y='PIP')

Version Author Date
048c109 zouyuxin 2020-10-25

Model 4 credible sets:

susie_plot(res4$result, y='PIP')

Version Author Date
048c109 zouyuxin 2020-10-25

Model 1 and 3 both give 5 same Credible Sets. There are 3 CSs have overlapped SNPs.

Total Time Algorithm Time # iterations
model 1 23097.391 22887.729 10
model 2 39748.754 39524.412 18
model 3 2116.505 2077.43 10
model 4 3165.6 3129.651 18
univariate_res = lapply(1:ncol(Y), function(i) susieR:::univariate_regression(X,Y[,i]))
res1$result$bhat = do.call(cbind, lapply(1:ncol(Y), function(i) univariate_res[[i]]$betahat))
res1$result$shat = do.call(cbind, lapply(1:ncol(Y), function(i) univariate_res[[i]]$sebetahat))
rownames(res1$result$bhat) = 1:501
p = mmbr::mmbr_plot(res1$result, original_sumstat = TRUE, cs_only = FALSE)
pdf('docs/assets/GRExProfile/GTExprofile_univ.pdf', width = 100, height = 15)
print(p$plot)
dev.off()

Univariate Effects

p = mmbr::mmbr_plot(res1$result)
pdf('docs/assets/GRExProfile/GTExprofile_res.pdf', width = 60, height = 15)
print(p$plot)
dev.off()

Model 1 Effects

p = mmbr::mmbr_plot(res3$result)
pdf('docs/assets/GRExProfile/GTExprofile_resapprox.pdf', width = 60, height = 15)
print(p$plot)
dev.off()

Model 3 Effects

p = mmbr::mmbr_plot(res4$result)
pdf('docs/assets/GRExProfile/GTExprofile_resapproxdiag.pdf', width = 5, height = 15)
print(p$plot)
dev.off()

Model 4 result

Models with L = 3

stime <- proc.time()
res <- mmbr::msusie(X, Y, prior_variance=prior, residual_variance=resid_Y, approximate=FALSE, L=3)
etime <- proc.time()
time_res <- etime - stime
saveRDS(list(result = res, result_time = time_res), 'output/GTExprofile_resL3.rds')

stime <- proc.time()
res_approx <- mmbr::msusie(X, Y, prior_variance=prior, residual_variance=resid_Y, approximate=TRUE, L=3)
etime <- proc.time()
time_res_approx <- etime - stime
saveRDS(list(result = res_approx, result_time = time_res_approx), 'output/GTExprofile_resapproxL3.rds')

stime <- proc.time()
res_approx_diag <- mmbr::msusie(X, Y, prior_variance=prior, residual_variance=diag(diag(resid_Y)), approximate=TRUE, L=3)
etime <- proc.time()
time_res_approx_diag <- etime - stime
saveRDS(list(result = res_approx_diag, result_time = time_res_approx_diag), 'output/GTExprofile_resapproxdiagL3.rds')
res1_L3 = readRDS('output/GTExprofile_resL3.rds')
res3_L3 = readRDS('output/GTExprofile_resapproxL3.rds')
res4_L3 = readRDS('output/GTExprofile_resapproxdiagL3.rds')

Model 1 credible sets:

susie_plot(res1_L3$result, y='PIP')

Version Author Date
048c109 zouyuxin 2020-10-25
b88f57a zouyuxin 2020-10-22

Model 3 credible sets:

susie_plot(res3_L3$result, y='PIP')

Version Author Date
048c109 zouyuxin 2020-10-25

Model 4 credible sets:

susie_plot(res4_L3$result, y='PIP')

Version Author Date
048c109 zouyuxin 2020-10-25
Total Time Algorithm Time # iterations
model 1 5793.07 5558.434 10
model 3 484.594 447.362 10
model 4 1232.319 1196.183 18
p = mmbr::mmbr_plot(res1_L3$result)
pdf('docs/assets/GRExProfile/GTExprofile_resL3.pdf', width = 17, height = 15)
print(p$plot)
dev.off()

Model 1 L3 Effects

Models with L = 1

stime <- proc.time()
res <- mmbr::msusie(X, Y, prior_variance=prior, residual_variance=resid_Y, approximate=FALSE, L=1)
etime <- proc.time()
time_res <- etime - stime
saveRDS(list(result = res, result_time = time_res), 'output/GTExprofile_resL1.rds')

stime <- proc.time()
res_approx <- mmbr::msusie(X, Y, prior_variance=prior, residual_variance=resid_Y, approximate=TRUE, L=1)
etime <- proc.time()
time_res_approx <- etime - stime
saveRDS(list(result = res_approx, result_time = time_res_approx), 'output/GTExprofile_resapproxL1.rds')

stime <- proc.time()
res_approx_diag <- mmbr::msusie(X, Y, prior_variance=prior, residual_variance=diag(diag(resid_Y)), approximate=TRUE, L=1)
etime <- proc.time()
time_res_approx_diag <- etime - stime
saveRDS(list(result = res_approx_diag, result_time = time_res_approx_diag), 'output/GTExprofile_resapproxdiagL1.rds')
res1_L1 = readRDS('output/GTExprofile_resL1.rds')
res3_L1 = readRDS('output/GTExprofile_resapproxL1.rds')
res4_L1 = readRDS('output/GTExprofile_resapproxdiagL1.rds')

Model 1 credible sets:

susie_plot(res1_L1$result, y='PIP')

Version Author Date
04c585c zouyuxin 2020-10-26

Model 3 credible sets:

susie_plot(res3_L1$result, y='PIP')

Version Author Date
04c585c zouyuxin 2020-10-26

Model 4 credible sets:

susie_plot(res4_L1$result, y='PIP')

Version Author Date
04c585c zouyuxin 2020-10-26

sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] mmbr_0.0.1.0305 susieR_0.9.26   mashr_0.2.40    ashr_2.2-51    
[5] corrplot_0.84   workflowr_1.6.2

loaded via a namespace (and not attached):
 [1] progress_1.2.2     tidyselect_1.1.0   xfun_0.19          purrr_0.3.4       
 [5] lattice_0.20-41    colorspace_1.4-1   vctrs_0.3.4        generics_0.1.0    
 [9] htmltools_0.5.0    yaml_2.2.1         rlang_0.4.8        mixsqp_0.3-46     
[13] later_1.1.0.1      pillar_1.4.6       glue_1.4.2         plyr_1.8.6        
[17] matrixStats_0.57.0 lifecycle_0.2.0    stringr_1.4.0      munsell_0.5.0     
[21] gtable_0.3.0       flashier_0.2.7     mvtnorm_1.1-1      evaluate_0.14     
[25] knitr_1.30         httpuv_1.5.4       invgamma_1.1       parallel_3.6.3    
[29] irlba_2.3.3        Rcpp_1.0.5         promises_1.1.1     backports_1.2.0   
[33] scales_1.1.1       rmeta_3.0          truncnorm_1.0-8    abind_1.4-5       
[37] fs_1.5.0           ggplot2_3.3.2      hms_0.5.3          digest_0.6.27     
[41] stringi_1.5.3      dplyr_1.0.2        ebnm_0.1-24        grid_3.6.3        
[45] rprojroot_1.3-2    tools_3.6.3        magrittr_1.5       tibble_3.0.4      
[49] crayon_1.3.4       whisker_0.4        pkgconfig_2.0.3    ellipsis_0.3.1    
[53] Matrix_1.2-18      SQUAREM_2020.5     prettyunits_1.1.1  assertthat_0.2.1  
[57] reshape_0.8.8      rmarkdown_2.5      rstudioapi_0.11    R6_2.5.0          
[61] git2r_0.27.1       compiler_3.6.3