Last updated: 2019-10-03

Checks: 4 3

Knit directory: lipids_mvp/

This reproducible R Markdown analysis was created with workflowr (version 1.4.0). 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(20190925) 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.

The following chunks had caches available:
  • create table
  • session-info-chunk-inserted-by-workflowr
  • unnamed-chunk-1
  • unnamed-chunk-2
  • unnamed-chunk-3
  • unnamed-chunk-4
  • unnamed-chunk-5

To ensure reproducibility of the results, delete the cache directory workingwithdata_cache and re-run the analysis. To have workflowr automatically delete the cache directory prior to building the file, set delete_cache = TRUE when running wflow_build() or wflow_publish().

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
~/lipids_mvp/merged_z.txt merged_z.txt
~/lipids_mvp/max_ld_block.txt max_ld_block.txt
~/lipids_mvp/EDcov.Rds EDcov.Rds

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.

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:    analysis/.DS_Store
    Ignored:    analysis/workingwithdata_cache/
    Ignored:    data/Archive.zip.gz
    Ignored:    docs/.DS_Store

Untracked files:
    Untracked:  EDcov.Rds
    Untracked:  analysis/EDcov.Rds
    Untracked:  analysis/merged_betas.txt
    Untracked:  analysis/merged_se.txt
    Untracked:  analysis/merged_z.txt
    Untracked:  analysis/workingwithdata.Rmd
    Untracked:  docs/figure/
    Untracked:  max_ld_block.txt
    Untracked:  merged_betas.txt
    Untracked:  merged_se.txt
    Untracked:  merged_z.txt

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.


R Markdown

Here we will try to merge based on common chr.position (per Hg18) names.

We recognize that the alleles have been flipped to make all effects positive Let’s use HDL as reference

ref=m[,3]

for(x in seq(1:3)){
  betaindex=4+2*x
  beta=m[,betaindex]
  allele=betaindex-1
  a=m[,allele]
  test=a==ref;test[test=="FALSE"]=-1
  beta=beta*test
  m[,betaindex]=beta}

head(m)
write.table(m,"merged_betas.txt")

## Creating SE Table
n=merge(hdl[,c(1,3,7)],ldl[,c(1,7)],by=1);names(n)[3]="hdl";names(n)[4]="ldl"
n=merge(n,tg[,c(1,7)],by=1);names(n)[5]="tg"
n=merge(n,tc[,c(1,7)],by=1);names(n)[6]="tc"
write.table(n,"merged_se.txt")

z=m[,c(4,6,8,10)]/n[,c(3:6)]
z=cbind(m[,c(1,2)],z)
write.table(z,"merged_z.txt")

To run mashr, we need a matrix of maxes. The best way to do this is to choose the max effect across conditions per LD block, as described in Pickrell et al. Only then can we assume the maxes used to create covariance matrices are truly linearly independent. We will select SNPS falling within each of the 1700 LD blocks and choose SNP with maximum absolute effect acrtoss conditions.

z=read.table("~/lipids_mvp/merged_z.txt")

bed=read.table("~/Downloads/ld_chunk.bed")
head(bed)
    V1      V2      V3
1 chr1   10583 1892607
2 chr1 1892607 3582736
3 chr1 3582736 4380811
4 chr1 4380811 5913893
5 chr1 5913893 7247335
6 chr1 7247335 9365199
z=z[1:2437099,]### last 3 are rsIDs from mislabeled columns
library("reshape")
df=transform(z, foo = colsplit(z$SNP_hg18, split = "\\:", names = c('Chr', 'Pos')))

znew=cbind(df$foo.Chr,df$foo.Pos,z[,-1])
maxes=apply(znew[,c("hdl","ldl","tg","tc")],1,function(x){max(abs(x))})
znew=cbind(znew,maxes)
max_block=data.frame(matrix(ncol = ncol(znew), nrow = nrow(bed)))
colnames(max_block)=colnames(znew)
for(i in 1:nrow(bed)){
  chr=bed[i,1]
  start=bed[i,2]
  stop=bed[i,3]
  in_chrom=znew[znew$`df$foo.Chr`==chr,]
  goodguys=in_chrom[in_chrom$`df$foo.Pos`>start&in_chrom$`df$foo.Pos`<stop,]
 if(nrow(goodguys)>0) {
    z.max=which.max(goodguys[,"maxes"])
    z_good=goodguys[z.max,]
    } else {
      z_good=rep(0,ncol(max_block))
    }
  z_good=data.table(z_good,stringsAsFactors = T)
  z_good$`df$foo.Chr`=as.character(z_good$`df$foo.Chr`)
  z_good$rsid=as.character(z_good$rsid)
  max_block[i,]=z_good
  #print(i)
}

max_block=na.omit(max_block)
write.table(max_block,"max_ld_block.txt")

Now, we’re ready to mash!

library("mashr")
library("flashr")
max_block=read.table("~/lipids_mvp/max_ld_block.txt")
## source('~/Dropbox/jointData/flashscript.R')
# identify a random subset of 20000 tests
random.subset = sample(1:nrow(znew),20000)
zmash=as.matrix(znew[,c("hdl","ldl","tg","tc")]);rownames(zmash)=znew$rsid
data.temp = mash_set_data(zmash[random.subset,],alpha = 1)

Vhat = estimate_null_correlation_simple(data.temp)
rm(data.temp)
data.random = mash_set_data(zmash[random.subset,],alpha = 1,V=Vhat)

zmax=apply(max_block[,c(4:7)],2,function(x){as.numeric(x)});rownames(zmax)=max_block$rsid
data.strong = mash_set_data(zmax,alpha = 1,V=Vhat)

U.pca = cov_pca(data.strong,3)

# U.flash=cov_flash(data.strong, non_canonical = TRUE)
# X.center = apply(data.strong$Bhat, 2, function(x) x - mean(x))
# U.ed = cov_ed(data.strong, c(U.flash, U.pca, list("XX" = t(X.center) %*% X.center / nrow(X.center))))
# saveRDS(U.ed,"EDcov.Rds")

U.ed=readRDS("~/lipids_mvp/EDcov.Rds")
U.c = cov_canonical(data.random)
m = mash(data.random, Ulist = c(U.ed,U.c),outputlevel = 1)
 - Computing 20000 x 321 likelihood matrix.
 - Likelihood calculations took 0.70 seconds.
 - Fitting model with 321 mixture components.
 - Model fitting took 33.73 seconds.
k=length(m$fitted_g$Ulist)
l=length(m$fitted_g$grid)
pimat=matrix(m$fitted_g$pi[-1],nrow=l,byrow=T)
colnames(pimat)=names(m$fitted_g$pi)[2:(k+1)]
barplot(colSums(pimat),las=2)

Now, let’s plot the patterns of sharing:

library("lattice")
for(i in 1:k){
  z.num=as.matrix(m$fitted_g$Ulist[[i]])
  colnames(z.num)=row.names(z.num)=colnames(zmash)
clrs = colorRampPalette((c("#D73027","#FC8D59","#FEE090","#FFFFBF", "#E0F3F8","#91BFDB","#4575B4")))(64)
z.num[lower.tri(z.num)] = NA
print(levelplot(z.num,col.regions = clrs,xlab = "",ylab = "",colorkey = TRUE,main=paste0(names(m$fitted_g$Ulist)[[i]])))
}


sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/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] lattice_0.20-38   flashr_0.6-3      mashr_0.2.21.0631 ashr_2.2-37      
[5] reshape_0.8.8    

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1        pillar_1.4.2      compiler_3.5.2   
 [4] git2r_0.26.1      plyr_1.8.4        highr_0.8        
 [7] workflowr_1.4.0   iterators_1.0.10  tools_3.5.2      
[10] digest_0.6.20     tibble_2.1.3      gtable_0.3.0     
[13] evaluate_0.14     pkgconfig_2.0.2   rlang_0.4.0      
[16] Matrix_1.2-17     foreach_1.4.4     rstudioapi_0.10  
[19] yaml_2.2.0        parallel_3.5.2    mvtnorm_1.0-11   
[22] xfun_0.8          dplyr_0.8.3       stringr_1.4.0    
[25] knitr_1.23        fs_1.3.1          tidyselect_0.2.5 
[28] rprojroot_1.3-2   grid_3.5.2        glue_1.3.1       
[31] R6_2.4.0          rmarkdown_1.14    mixsqp_0.1-97    
[34] rmeta_3.0         reshape2_1.4.3    purrr_0.3.2      
[37] ggplot2_3.2.0     magrittr_1.5      scales_1.0.0     
[40] backports_1.1.4   codetools_0.2-16  htmltools_0.3.6  
[43] MASS_7.3-51.4     abind_1.4-5       assertthat_0.2.1 
[46] softImpute_1.4    colorspace_1.4-1  stringi_1.4.3    
[49] lazyeval_0.2.2    munsell_0.5.0     doParallel_1.0.14
[52] pscl_1.5.2        truncnorm_1.0-8   SQUAREM_2017.10-1
[55] crayon_1.3.4