Last updated: 2022-05-12

Checks: 5 2

Knit directory: cTWAS_analysis/

This reproducible R Markdown analysis was created with workflowr (version 1.7.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(20211220) 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
/project2/xinhe/shengqian/cTWAS/cTWAS_analysis/data/ data
/project2/xinhe/shengqian/cTWAS/cTWAS_analysis/code/ctwas_config.R code/ctwas_config.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 687aaea. 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:    .ipynb_checkpoints/
    Ignored:    data/AF/

Untracked files:
    Untracked:  G_list.RData
    Untracked:  Rplot.png
    Untracked:  SCZ_annotation.xlsx
    Untracked:  analysis/.ipynb_checkpoints/
    Untracked:  analysis/SCZ_2018_Brain_Amygdala_S.Rmd
    Untracked:  analysis/SCZ_2018_Brain_Anterior_cingulate_cortex_BA24_S.Rmd
    Untracked:  analysis/SCZ_2018_Brain_Caudate_basal_ganglia_S.Rmd
    Untracked:  analysis/SCZ_2018_Brain_Cerebellar_Hemisphere_S.Rmd
    Untracked:  analysis/SCZ_2018_Brain_Cerebellum_S.Rmd
    Untracked:  analysis/SCZ_2018_Brain_Cortex_S.Rmd
    Untracked:  analysis/SCZ_2018_Brain_Frontal_Cortex_BA9_S.Rmd
    Untracked:  analysis/SCZ_2018_Brain_Hippocampus_S.Rmd
    Untracked:  analysis/SCZ_2018_Brain_Hypothalamus_S.Rmd
    Untracked:  analysis/SCZ_2018_Brain_Nucleus_accumbens_basal_ganglia_S.Rmd
    Untracked:  analysis/SCZ_2018_Brain_Putamen_basal_ganglia_S.Rmd
    Untracked:  analysis/SCZ_2018_Brain_Spinal_cord_cervical_c-1_S.Rmd
    Untracked:  analysis/SCZ_2018_Brain_Substantia_nigra_S.Rmd
    Untracked:  code/.ipynb_checkpoints/
    Untracked:  code/AF_out/
    Untracked:  code/Autism_out/
    Untracked:  code/BMI_S_out/
    Untracked:  code/BMI_out/
    Untracked:  code/Glucose_out/
    Untracked:  code/LDL_S_out/
    Untracked:  code/SCZ_2014_EUR_out/
    Untracked:  code/SCZ_2018_S_out/
    Untracked:  code/SCZ_2018_out/
    Untracked:  code/SCZ_2020_Single_out/
    Untracked:  code/SCZ_2020_out/
    Untracked:  code/SCZ_S_out/
    Untracked:  code/SCZ_out/
    Untracked:  code/T2D_out/
    Untracked:  code/ctwas_config.R
    Untracked:  code/mapping.R
    Untracked:  code/out/
    Untracked:  code/process_scz_2018_snps.R
    Untracked:  code/run_AF_analysis.sbatch
    Untracked:  code/run_AF_analysis.sh
    Untracked:  code/run_AF_ctwas_rss_LDR.R
    Untracked:  code/run_Autism_analysis.sbatch
    Untracked:  code/run_Autism_analysis.sh
    Untracked:  code/run_Autism_ctwas_rss_LDR.R
    Untracked:  code/run_BMI_analysis.sbatch
    Untracked:  code/run_BMI_analysis.sh
    Untracked:  code/run_BMI_analysis_S.sbatch
    Untracked:  code/run_BMI_analysis_S.sh
    Untracked:  code/run_BMI_ctwas_rss_LDR.R
    Untracked:  code/run_BMI_ctwas_rss_LDR_S.R
    Untracked:  code/run_Glucose_analysis.sbatch
    Untracked:  code/run_Glucose_analysis.sh
    Untracked:  code/run_Glucose_ctwas_rss_LDR.R
    Untracked:  code/run_LDL_analysis_S.sbatch
    Untracked:  code/run_LDL_analysis_S.sh
    Untracked:  code/run_LDL_ctwas_rss_LDR_S.R
    Untracked:  code/run_SCZ_2014_EUR_analysis.sbatch
    Untracked:  code/run_SCZ_2014_EUR_analysis.sh
    Untracked:  code/run_SCZ_2014_EUR_ctwas_rss_LDR.R
    Untracked:  code/run_SCZ_2018_analysis.sbatch
    Untracked:  code/run_SCZ_2018_analysis.sh
    Untracked:  code/run_SCZ_2018_analysis_S.sbatch
    Untracked:  code/run_SCZ_2018_analysis_S.sh
    Untracked:  code/run_SCZ_2018_ctwas_rss_LDR.R
    Untracked:  code/run_SCZ_2018_ctwas_rss_LDR_S.R
    Untracked:  code/run_SCZ_2020_Single_analysis.sbatch
    Untracked:  code/run_SCZ_2020_Single_analysis.sh
    Untracked:  code/run_SCZ_2020_Single_ctwas_rss_LDR.R
    Untracked:  code/run_SCZ_2020_analysis.sbatch
    Untracked:  code/run_SCZ_2020_analysis.sh
    Untracked:  code/run_SCZ_2020_ctwas_rss_LDR.R
    Untracked:  code/run_SCZ_analysis.sbatch
    Untracked:  code/run_SCZ_analysis.sh
    Untracked:  code/run_SCZ_analysis_S.sbatch
    Untracked:  code/run_SCZ_analysis_S.sh
    Untracked:  code/run_SCZ_ctwas_rss_LDR.R
    Untracked:  code/run_SCZ_ctwas_rss_LDR_S.R
    Untracked:  code/run_T2D_analysis.sbatch
    Untracked:  code/run_T2D_analysis.sh
    Untracked:  code/run_T2D_ctwas_rss_LDR.R
    Untracked:  code/wflow_build.R
    Untracked:  code/wflow_build.sbatch
    Untracked:  data/.ipynb_checkpoints/
    Untracked:  data/BMI/
    Untracked:  data/GO_Terms/
    Untracked:  data/PGC3_SCZ_wave3_public.v2.tsv
    Untracked:  data/SCZ/
    Untracked:  data/SCZ_2014_EUR/
    Untracked:  data/SCZ_2018/
    Untracked:  data/SCZ_2018_S/
    Untracked:  data/SCZ_2020/
    Untracked:  data/SCZ_2020_Single/
    Untracked:  data/SCZ_S/
    Untracked:  data/Supplementary Table 15 - MAGMA.xlsx
    Untracked:  data/Supplementary Table 20 - Prioritised Genes.xlsx
    Untracked:  data/T2D/
    Untracked:  data/UKBB/
    Untracked:  data/UKBB_SNPs_Info.text
    Untracked:  data/gene_OMIM.txt
    Untracked:  data/gene_pip_0.8.txt
    Untracked:  data/mashr_Heart_Atrial_Appendage.db
    Untracked:  data/mashr_sqtl/
    Untracked:  data/scz_2018.RDS
    Untracked:  data/summary_known_genes_annotations.xlsx
    Untracked:  data/untitled.txt
    Untracked:  top_genes_32.txt
    Untracked:  top_genes_37.txt
    Untracked:  top_genes_43.txt
    Untracked:  top_genes_81.txt
    Untracked:  z_snp_pos_SCZ.RData
    Untracked:  z_snp_pos_SCZ_2014_EUR.RData
    Untracked:  z_snp_pos_SCZ_2018.RData
    Untracked:  z_snp_pos_SCZ_2020.RData

Unstaged changes:
    Modified:   analysis/BMI_Brain_Cerebellum_S.Rmd
    Modified:   analysis/SCZ_Brain_Cerebellum_S.Rmd
    Modified:   analysis/SCZ_Brain_Cortex.Rmd
    Modified:   analysis/SCZ_Brain_Cortex_S.Rmd
    Modified:   analysis/SCZ_Brain_Hypothalamus_S.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.


library(reticulate)
use_python("/scratch/midway2/shengqian/miniconda3/envs/PythonForR/bin/python",required=T)

Weight QC

#number of imputed weights
nrow(qclist_all)
[1] 27353
#number of imputed weights by chromosome
table(qclist_all$chr)

   1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16 
2535 1830 1661  982 1135 1371 1536  916 1175 1171 1678 1471  543  971  987 1200 
  17   18   19   20   21   22 
1981  337 2002  917   48  906 
#number of imputed weights without missing variants
sum(qclist_all$nmiss==0)
[1] 23734
#proportion of imputed weights without missing variants
mean(qclist_all$nmiss==0)
[1] 0.8677
from pyensembl import EnsemblRelease #repl_python()
import math
import time
import numpy as np
import pandas as pd

data = EnsemblRelease(104)
ctwas_gene_res_df = r.ctwas_gene_res
ctwas_gene_res_df['intron_pos'] = ctwas_gene_res_df['id'].apply(lambda x: math.floor((int(x.split("_")[2])+int(x.split("_")[3]))/2))
ctwas_gene_res_df['genename'] = np.nan
ctwas_gene_res_df['intron_id'] = ctwas_gene_res_df['id']
for index, row in ctwas_gene_res_df.iterrows():
  gene_names = data.gene_names_at_locus(contig=int(row["chrom"]), position=int(row["intron_pos"]))
  if len(gene_names)!=0:
    for i in gene_names:
      if i != '':
        gene_info = data.genes_by_name(i)[0]
        ctwas_gene_res_df.loc[index,'genename'] = gene_info.gene_name
        ctwas_gene_res_df.loc[index,'id'] = gene_info.gene_id
        ctwas_gene_res_df.loc[index,'type'] = gene_info.biotype
        break
      
ctwas_gene_res_df = ctwas_gene_res_df.dropna(subset=['genename'])
print("finish")
finish
#add z scores to results
load(paste0(results_dir, "/", analysis_id, "_expr_z_gene.Rd"))
ctwas_gene_res <- py$ctwas_gene_res_df
ctwas_gene_res$z <- z_gene[ctwas_gene_res$intron_id,]$z

z_snp <- z_snp[z_snp$id %in% ctwas_snp_res$id,]
ctwas_snp_res$z <- z_snp$z[match(ctwas_snp_res$id, z_snp$id)]

#merge gene and snp results with added information
ctwas_snp_res$genename=NA
ctwas_snp_res$gene_type=NA
ctwas_snp_res$intron_id=NA
ctwas_snp_res$intron_pos=NA

saveRDS(ctwas_gene_res, file = paste0(results_dir,"/",analysis_id,"_ctwas_gene_res.RDS"))
saveRDS(ctwas_snp_res, file =  paste0(results_dir,"/",analysis_id,"_ctwas_snp_res.RDS"))
ctwas_intron_res <- readRDS(paste0(results_dir,"/",analysis_id,"_ctwas_gene_res.RDS"))
ctwas_intron_res <- ctwas_intron_res[!is.na(ctwas_intron_res$genename),]
ctwas_snp_res <- readRDS(paste0(results_dir,"/",analysis_id,"_ctwas_snp_res.RDS"))

#get number of eQTL for inrtons
num_sqtl <- c()
for (i in 1:22){
  load(paste0(results_dir, "/", analysis_id, "_expr_chr", i, ".exprqc.Rd"))
  num_sqtl <- c(num_sqtl, unlist(lapply(wgtlist, nrow)))
}
ctwas_intron_res$num_sqtl <- num_sqtl[ctwas_intron_res$intron_id]
library(dplyr)
by_genename <- ctwas_intron_res %>% group_by(genename)
ctwas_gene_res <- data.frame(by_genename %>% summarise(
  chrom = chrom[1],
  id = id[1],
  pos = min(pos),
  type = type[1],
  region_tag1 = region_tag1[1],
  region_tag2 = region_tag2[1],
  cs_index = cs_index[1],
  susie_pip = sum(susie_pip),
  mu2 = mu2[which.max(abs(mu2))],
  region_tag = region_tag[1],
  PVE = sum(susie_pip*PVE),
  z = z[which.max(abs(z))],
  gene_type = type[1],
  num_intron = length(intron_id),
  num_sqtl = sum(num_sqtl)))

ctwas_snp_res$num_intron <- NA
ctwas_snp_res$num_sqtl <- NA
ctwas_res <- rbind(ctwas_gene_res,
                   ctwas_snp_res[,colnames(ctwas_gene_res)])

#store columns to report
report_cols <- colnames(ctwas_gene_res)[!(colnames(ctwas_gene_res) %in% c("type", "region_tag1", "region_tag2", "cs_index", "gene_type", "z_flag", "id", "chrom", "pos"))]
first_cols <- c("genename", "region_tag")
report_cols <- c(first_cols, report_cols[!(report_cols %in% first_cols)])

report_cols_snps <- c("id", report_cols[-1])
report_cols_snps <- report_cols_snps[!(report_cols_snps %in% "num_sqtl")]

#get number of SNPs from s1 results; adjust for thin argument
ctwas_res_s1 <- data.table::fread(paste0(results_dir, "/", analysis_id, "_ctwas.s1.susieIrss.txt"))
n_snps <- sum(ctwas_res_s1$type=="SNP")/thin
rm(ctwas_res_s1)

Check convergence of parameters

#estimated group prior
estimated_group_prior <- group_prior_rec[,ncol(group_prior_rec)]
names(estimated_group_prior) <- c("gene", "snp")
estimated_group_prior["snp"] <- estimated_group_prior["snp"]*thin #adjust parameter to account for thin argument
print(estimated_group_prior)

#estimated group prior variance
estimated_group_prior_var <- group_prior_var_rec[,ncol(group_prior_var_rec)]
names(estimated_group_prior_var) <- c("gene", "snp")
print(estimated_group_prior_var)

#report sample size
print(sample_size)

#report group size
group_size <- c(nrow(ctwas_gene_res), n_snps)
print(group_size)

#estimated group PVE
estimated_group_pve <- estimated_group_prior_var*estimated_group_prior*group_size/sample_size #check PVE calculation
names(estimated_group_pve) <- c("gene", "snp")
print(estimated_group_pve)

#compare sum(PIP*mu2/sample_size) with above PVE calculation
c(sum(ctwas_gene_res$PVE),sum(ctwas_snp_res$PVE))

Genes with highest PIPs

Genes with highest PVE

Comparing z scores and PIPs

GO enrichment analysis for genes with PIP>0.5

#number of genes for gene set enrichment
length(genes)

DisGeNET enrichment analysis for genes with PIP>0.5

WebGestalt enrichment analysis for genes with PIP>0.5

PIP Manhattan Plot

Sensitivity, specificity and precision for silver standard genes

#number of genes in known annotations
print(length(known_annotations))

#number of genes in known annotations with imputed expression
print(sum(known_annotations %in% ctwas_gene_res$genename))

#significance threshold for TWAS
print(sig_thresh)

#number of ctwas genes
length(ctwas_genes)

#number of TWAS genes
length(twas_genes)

#show novel genes (ctwas genes with not in TWAS genes)
ctwas_gene_res[ctwas_gene_res$genename %in% novel_genes,report_cols]

#sensitivity / recall
print(sensitivity)

#specificity
print(specificity)

#precision / PPV
print(precision)

sessionInfo()
R version 3.6.1 (2019-07-05)
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] reticulate_1.16 workflowr_1.7.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8        highr_0.9         jquerylib_0.1.4   compiler_3.6.1   
 [5] pillar_1.6.4      later_0.8.0       git2r_0.26.1      tools_3.6.1      
 [9] getPass_0.2-2     digest_0.6.29     jsonlite_1.7.2    lattice_0.20-38  
[13] evaluate_0.14     tibble_3.1.6      lifecycle_1.0.1   pkgconfig_2.0.3  
[17] rlang_1.0.1       Matrix_1.2-18     cli_3.1.0         rstudioapi_0.13  
[21] yaml_2.2.1        xfun_0.29         fastmap_1.1.0     httr_1.4.2       
[25] stringr_1.4.0     knitr_1.36        fs_1.5.2          vctrs_0.3.8      
[29] grid_3.6.1        rprojroot_2.0.2   data.table_1.14.2 glue_1.6.2       
[33] R6_2.5.1          processx_3.5.2    fansi_1.0.2       rmarkdown_2.11   
[37] callr_3.7.0       magrittr_2.0.2    whisker_0.3-2     ps_1.6.0         
[41] promises_1.0.1    htmltools_0.5.2   ellipsis_0.3.2    httpuv_1.5.1     
[45] utf8_1.2.2        stringi_1.7.6     crayon_1.5.0