Last updated: 2020-07-19

Checks: 7 0

Knit directory: psychencode/

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(20200622) 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 1e04578. 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:    analysis/.DS_Store
    Ignored:    output/.DS_Store

Untracked files:
    Untracked:  models/
    Untracked:  output/test_results/

Unstaged changes:
    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.

These are the previous versions of the repository in which changes were made to the R Markdown (analysis/calculate_covariances.Rmd) and HTML (docs/calculate_covariances.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 1e04578 sabrina-mi 2020-07-19 updated links
html 1e04578 sabrina-mi 2020-07-19 updated links
Rmd d0856a5 sabrina-mi 2020-07-19 added covariances job script links
Rmd 1487151 sabrina-mi 2020-07-19 add covariance scripts


Linkage disequilibrium between snps should be considered when estimating their effects. An external reference panel with individual-level genotypic data is needed to infer LD among genetic variants, in this case, we used 1000 Genomes Project Europeans. The covariance matrix captures LD structure. Weights are computed for each variant to predict expression of a gene. Together, they can be used in S-PrediXcan to study association between predicted gene expression and a phenotype. Alvaro’s script for generating covariances takes in a predicted transciptome model and dosage information from the reference set.


conda activate imlabtools

Download Data

The data for each chromosome contains a VCF file with nucleotide variants, indexes, copy number variants and structural variants information, along with its tabix index file. To download from the command line, run the chunk below. Each file will take at least half an hour, so if possible, submit it as a job in CRI: (Run qsub

cd $DATA
for I in {1..22}

Convert Genotypes to PrediXcan Format

The VCFs need to be converted to simpler text files in “PrediXcan format”, with columns chromosome rsid position allele1 allele2 MAF id1 ….. idn. More info here: The output should be a gzipped text file for each chromosome. The conversion takes about 2 hours for each chromosome, so it’s best to submit the job in CRI: However, if you run the chunk on a laptop, you may need to install bcftools beforehand. Run brew install bcftools or follow instructions here:

filter_and_convert ()
#echo -ne "varID\t" | gzip > $3
#bcftools view $1 -S $2 --force-samples -Ou |  bcftools query -l | tr '\n' '\t' | sed 's/\t$/\n/' | gzip >> $3

#The first python inline script will check if a variant is blacklisted
#[ -f $3 ] && rm $3
NOW=$(date +%Y-%m-%d/%H:%M:%S)
echo "Starting at $NOW"
bcftools +fill-tags $1 -Ou | bcftools query -f '%CHROM\t%ID\t%POS\t%REF\t%ALT\t%MAF[\t%GT]\n' | \
awk '
for (i = 1; i <=6; i ++) {
    if (i < 6) {

for (i = 7; i <= NF; i++) {
    if ( substr($i, 0, 1) == ".") {
    } else if ($i ~ "[0-9]|[0-9]") {
        n = split($i, array, "|")
    } else {
        printf("Unexpected: %s",$i)
        exit 1
' | gzip > $2

NOW=$(date +%Y-%m-%d/%H:%M:%S)
echo "Ending at $NOW"

[ -d /scratch/ ] || mkdir -p /scratch/

for I in {1..22}
    filter_and_convert $INPUT $OUTPUT

Run Covariances Script

The script to generate covariances is here: More info: It takes the PrediXcan format dosages and a transcriptome prediction model, in this case, a database derived from a TWAS made available at: The output will be a gzipped text file with covariance between two snps. The script below can also be submitted as a job in CRI:

python3 $METAXCAN/ \
--weight_db $MODEL/psychencode_model/psychencode.db \
--input_folder /scratch/ \
--delimiter $'\t' \
--covariance_output $MODEL/psychencode_model/psychencode.txt.gz

R version 3.6.2 (2019-12-12)
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.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

[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     

loaded via a namespace (and not attached):
 [1] workflowr_1.6.2 Rcpp_1.0.3      rprojroot_1.3-2 digest_0.6.23  
 [5] later_1.0.0     R6_2.4.1        backports_1.1.5 git2r_0.27.1   
 [9] magrittr_1.5    evaluate_0.14   stringi_1.4.5   rlang_0.4.2    
[13] fs_1.3.1        promises_1.1.0  whisker_0.4     rmarkdown_2.1  
[17] tools_3.6.2     stringr_1.4.0   glue_1.3.1      httpuv_1.5.3.1 
[21] xfun_0.12       yaml_2.2.0      compiler_3.6.2  htmltools_0.4.0
[25] knitr_1.27