Last updated: 2022-08-23

Checks: 6 1

Knit directory: RatXcan_Training/

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 file has unstaged changes. 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(20220711) 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 e8ceeb9. 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:    .Rproj.user/
    Ignored:    analysis/.Rhistory
    Ignored:    output/
    Ignored:    scripts/.Rhistory

Untracked files:
    Untracked:  .DS_Store
    Untracked:  .gitignore
    Untracked:  analysis/Plot_Predictability_Heritability.Rmd
    Untracked:  code/helpers.R

Unstaged changes:
    Modified:   analysis/.DS_Store
    Modified:   analysis/Analyze_PrediXcan_Results.Rmd
    Modified:   analysis/EN_Prediction_Model.Rmd
    Modified:   analysis/Run_PrediXcan.Rmd
    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/EN_Prediction_Model.Rmd) and HTML (docs/EN_Prediction_Model.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 28edc16 sabrina-mi 2022-08-22 add link and change titles
html 28edc16 sabrina-mi 2022-08-22 add link and change titles
Rmd 3902557 sabrina-mi 2022-08-22 add analyze predixcan result
html 3902557 sabrina-mi 2022-08-22 add analyze predixcan result
Rmd 3dc873d sabrina-mi 2022-08-20 copy over documentation from Rat Genomics Paper Pipeline
html 3dc873d sabrina-mi 2022-08-20 copy over documentation from Rat Genomics Paper Pipeline

Definitions

library(tidyverse)
library(devtools)
# library(broom)
library(data.table)

base.dir = "/Users/sabrinami/Github/Rat_Genomics_Paper_Pipeline/"

Process Genotype and Gene Expression data

We first wrangle Palmer Lab data to fit as inputs to the prediction model pipeline. The steps are documented in RatXcan_Training/analysis/Process_Geno_Gex_Data.Rmd. The following sections assume the genotype and gene expression data are in correct format.

The genotype file should be a matrix with individuals in the column and snps in each row.

              0007A0008B 0007A00024 0007A000DB 0007A001C5 0007A0059F
1_55365_A_T            2          2          2          2          2
1_759319_T_C           1          0          1          0          1
1_1134030_A_G          2          2          2          1          2
              0007A00263
1_55365_A_T            2
1_759319_T_C           1
1_1134030_A_G          2

The gene expression file for each tissue should be a matrix file with genes in the columns and individuals for the rows.

           ENSRNOG00000000007 ENSRNOG00000000010 ENSRNOG00000000012
00077E67B5         -0.5135095          0.1112824          0.3733866
00077E8336         -0.4776331          1.9545779          2.2365395
00077EA7E6          1.0283240          1.7746883          0.8783629
           ENSRNOG00000000017 ENSRNOG00000000021 ENSRNOG00000000024
00077E67B5         -0.7251591          0.9759002         -0.4423612
00077E8336          0.0317348          0.2730561          1.9545779
00077EA7E6         -0.8552639         -0.2730561          0.2730561

PEER analysis

Now we are performing PEER factor analysis on each tissue, choosing 7 factors.

DIR=/Users/sabrinami/Github/Rat_Genomics_Paper_Pipeline
cd $DIR
peertool -f data/gexAc.csv -n 7 -o peer_Ac --has_header
peertool -f data/gexIl.csv -n 7 -o peer_Il --has_header
peertool -f data/gexLh.csv -n 7 -o peer_Lh --has_header
peertool -f data/gexPl.csv -n 7 -o peer_Pl --has_header
peertool -f data/gexVo.csv -n 7 -o peer_Vo --has_header

Later on, we examine these 7 factors, as well as other covariates, to interpret expression variability.

This python script separates the genotype file by chromosome.

#Splitting the genotype file by chromosome - run this from the rat_genomic_alaysis directory
python scripts/split_genotype_by_chr.py data/genotype.txt data/geno_by_chr/genotype

Covariate Files

We analyze sex, batch number, and batch center, as possible covariates, along with the 7 PEER factors. Start by loading gene expression data and phenotype data.

load("~/Box/imlab-data/Projects/PTRS-PGRS-Rosetta/Data-From-Abe-Palmer-Lab/Final_P50_traits/P50_raw_trait_values.RData")

gexAc_transpose <- read.table( base.dir %&% "data/gexAc.csv", sep = ",", col.names = TRUE, row.names = FALSE)
gexIl_transpose <- read.table( base.dir %&% "data/gexIl.csv", sep = ",", col.names = TRUE, row.names = FALSE)
gexLh_transpose <- read.table( base.dir %&% "data/gexLh.csv", sep = ",", col.names = TRUE, row.names = FALSE)
gexPl_transpose <- read.table(base.dir %&% "data/gexPl.csv", sep = ",", col.names = TRUE, row.names = FALSE)
gexVo_transpose <- read.table(base.dir %&% "data/gexVo.csv", sep = ",", col.names = TRUE, row.names = FALSE)

We selecting sex, batch number, and batch center from the phenotype file as covariates along with the 7 PEER factors.

covariatesAc = raw_traits[match(rownames(gexAc_transpose), raw_traits$rfid), ]
covariatesAc = subset(covariatesAc, select = c(rfid, sex, batchnumber, center))
covariatesIl = raw_traits[match(rownames(gexIl_transpose), raw_traits$rfid), ]
covariatesIl = subset(covariatesIl, select = c(rfid, sex, batchnumber, center))
covariatesLh = raw_traits[match(rownames(gexLh_transpose), raw_traits$rfid), ]
covariatesLh = subset(covariatesLh, select = c(rfid, sex, batchnumber, center))
covariatesPl = raw_traits[match(rownames(gexPl_transpose), raw_traits$rfid), ]
covariatesPl = subset(covariatesPl, select = c(rfid, sex, batchnumber, center))
covariatesVo = raw_traits[match(rownames(gexVo_transpose), raw_traits$rfid), ]
covariatesVo = subset(covariatesVo, select = c(rfid, sex, batchnumber, center))

Now we read the PEER factor output files to be appended to the covariate file and eventually regressed out of the expression files.

peer_factorsAc = read.csv(file = base.dir %&% "peer_Ac/X.csv", header = FALSE)
peer_factorsIl = read.csv(file = base.dir %&% "peer_Il/X.csv", header = FALSE)
peer_factorsLh = read.csv(file = base.dir %&% "peer_Lh/X.csv", header = FALSE)
peer_factorsPl = read.csv(file = base.dir %&% "peer_Pl/X.csv", header = FALSE)
peer_factorsVo = read.csv(file = base.dir %&% "peer_Vo/X.csv", header = FALSE)

For each tissue’s PEER factor output, set individual IDs as rownames and enumerate the PEER factors in the columns.

# Manipulating the PEER factor files so we can append to covariate file

colnames(peer_factorsAc) = c('PF1', 'PF2', 'PF3', 'PF4', 'PF5', 'PF6', 'PF7')
rownames(peer_factorsAc) = rownames(gexAc_transpose)

colnames(peer_factorsIl) = c('PF1', 'PF2', 'PF3', 'PF4', 'PF5', 'PF6', 'PF7')
rownames(peer_factorsIl) = rownames(gexIl_transpose)

colnames(peer_factorsLh) = c('PF1', 'PF2', 'PF3', 'PF4', 'PF5', 'PF6', 'PF7')
rownames(peer_factorsLh) = rownames(gexLh_transpose)

colnames(peer_factorsPl) = c('PF1', 'PF2', 'PF3', 'PF4', 'PF5', 'PF6', 'PF7')
rownames(peer_factorsPl) = rownames(gexPl_transpose)

colnames(peer_factorsVo) = c('PF1', 'PF2', 'PF3', 'PF4', 'PF5', 'PF6', 'PF7')
rownames(peer_factorsVo) = rownames(gexVo_transpose)
for (i in 1:length(colnames(gexAc_transpose))) {
    fit = lm(gexAc_transpose[,i] ~ covariatesAc$sex + covariatesAc$batchnumber + peer_factorsAc$PF1 + peer_factorsAc$PF2 + peer_factorsAc$PF3 + peer_factorsAc$PF4 + peer_factorsAc$PF5 + peer_factorsAc$PF6 + peer_factorsAc$PF7)
    gexAc_transpose[,i] <- fit$residuals
    #t_statsAc[,i] <- tidy(fit)$statistic
    #p_valsAc[,i] <- tidy(fit)$p.value
}

for (i in 1:length(colnames(gexIl_transpose))) {
    fit = lm(gexIl_transpose[,i] ~ covariatesIl$sex + covariatesIl$batchnumber + peer_factorsIl$PF1 + peer_factorsIl$PF2 + peer_factorsIl$PF3 + peer_factorsIl$PF4 + peer_factorsIl$PF5 + peer_factorsIl$PF6 + peer_factorsIl$PF7)
    gexIl_transpose[,i] <- fit$residuals
    #t_statsAc[,i] <- tidy(fit)$statistic
    #p_valsAc[,i] <- tidy(fit)$p.value
}

for (i in 1:length(colnames(gexLh_transpose))) {
    fit = lm(gexLh_transpose[,i] ~ covariatesLh$sex + covariatesLh$batchnumber + peer_factorsLh$PF1 + peer_factorsLh$PF2 + peer_factorsLh$PF3 + peer_factorsLh$PF4 + peer_factorsLh$PF5 + peer_factorsLh$PF6 + peer_factorsLh$PF7)
    gexLh_transpose[,i] <- fit$residuals
    #t_statsAc[,i] <- tidy(fit)$statistic
    #p_valsAc[,i] <- tidy(fit)$p.value
}

for (i in 1:length(colnames(gexPl_transpose))) {
    fit = lm(gexPl_transpose[,i] ~ covariatesPl$sex + covariatesPl$batchnumber + peer_factorsPl$PF1 + peer_factorsPl$PF2 + peer_factorsPl$PF3 + peer_factorsPl$PF4 + peer_factorsPl$PF5 + peer_factorsPl$PF6 + peer_factorsPl$PF7)
    gexPl_transpose[,i] <- fit$residuals
    #t_statsAc[,i] <- tidy(fit)$statistic
    #p_valsAc[,i] <- tidy(fit)$p.value
}

for (i in 1:length(colnames(gexVo_transpose))) {
    fit = lm(gexVo_transpose[,i] ~ covariatesVo$sex + covariatesVo$batchnumber + peer_factorsVo$PF1 + peer_factorsVo$PF2 + peer_factorsVo$PF3 + peer_factorsVo$PF4 + peer_factorsVo$PF5 + peer_factorsVo$PF6 + peer_factorsVo$PF7)
    gexVo_transpose[,i] <- fit$residuals
    #t_statsAc[,i] <- tidy(fit)$statistic
    #p_valsAc[,i] <- tidy(fit)$p.value
  }

Write the processed expression data to file.

# Save expression as tsv
Ac_expr <- as.data.frame(gexAc_transpose) %>% mutate(ID = rownames(gexAc_transpose), .before = colnames(gexAc_transpose))

Il_expr <- as.data.frame(gexIl_transpose) %>% mutate(ID = rownames(gexIl_transpose), .before = colnames(gexIl_transpose))

Lh_expr <- as.data.frame(gexLh_transpose) %>% mutate(ID = rownames(gexLh_transpose), .before = colnames(gexLh_transpose))

Pl_expr <- as.data.frame(gexPl_transpose) %>% mutate(ID = rownames(gexPl_transpose), .before = colnames(gexPl_transpose))

Vo_expr <- as.data.frame(gexVo_transpose) %>% mutate(ID = rownames(gexVo_transpose), .before = colnames(gexVo_transpose))

"%&%" = function(a,b) paste(a,b,sep="")
exprlist <- list(Ac_expr, Il_expr, Lh_expr, Pl_expr, Vo_expr)
tis <- c("Ac", "Il", "Lh", "Pl", "Vo")
i = 1
for(l in exprlist) {
write_tsv(l, base.dir
%&% tis[i] %&% "_expression_transformed.tsv", col_names = TRUE)
  i <- i+1
}

Save the expression RDS objects to be used as arguments in the script.

saveRDS(as.matrix(gexAc_transpose), base.dir %&% "/Ac_expression_transformed.RDS")

saveRDS(as.matrix(gexIl_transpose), base.dir %&% "/Il_expression_transformed.RDS")

saveRDS(as.matrix(gexLh_transpose), base.dir %&% "Lh_expression_transformed.RDS")

saveRDS(as.matrix(gexPl_transpose), base.dir %&% "Pl_expression_transformed.RDS")

saveRDS(as.matrix(gexVo_transpose), base.dir %&% "/Vo_expression_transformed.RDS")

Save the gene and SNP annotation as RDS objects to be used as arguments in the script.

# 
snp_files <- list(snp_annot.chr1, snp_annot.chr2, snp_annot.chr3, snp_annot.chr4, snp_annot.chr5, snp_annot.chr6, snp_annot.chr7, snp_annot.chr8, snp_annot.chr9, snp_annot.chr10, snp_annot.chr11, snp_annot.chr12, snp_annot.chr13, snp_annot.chr14, snp_annot.chr15, snp_annot.chr16, snp_annot.chr17, snp_annot.chr18, snp_annot.chr19, snp_annot.chr20)
i = 1
for(l in snp_files) {
  saveRDS(l, base.dir %&% "data/snp_annot/" %&% "snp_annot.chr" %&% i %&% ".RDS")
  i <- i+1
}
# Saving the gene annotation RDS object to be used as an argument in the script
saveRDS(gene_annotation, base.dir %&% "data/gene_annotation.RDS")

Metadata Files

# Creating the meta data file for each tissue 
cd $DIR
python scripts/create_meta_data.py --geno "data/genotype.txt" --expr "Ac_expression_transformed.tsv" --alpha 1 --snpset "1KG" --rsid_label 1 --window 1000000 --out_prefix "Results/allMetaData/Ac"

python scripts/create_meta_data.py --geno "data/genotype.txt" --expr "Il_expression_transformed.tsv" --alpha 1 --snpset "1KG" --rsid_label 1 --window 1000000 --out_prefix "Results/allMetaData/Il"

python scripts/create_meta_data.py --geno "data/genotype.txt" --expr "Lh_expression_transformed.tsv" --alpha 1 --snpset "1KG" --rsid_label 1 --window 1000000 --out_prefix "Results/allMetaData/Lh"

python scripts/create_meta_data.py --geno "data/genotype.txt" --expr "Pl_expression_transformed.tsv" --alpha 1 --snpset "1KG" --rsid_label 1 --window 1000000 --out_prefix "Results/allMetaData/Pl"

python scripts/create_meta_data.py --geno "data/genotype.txt" --expr "Vo_expression_transformed.tsv" --alpha 1 --snpset "1KG" --rsid_label 1 --window 1000000 --out_prefix "Results/allMetaData/Vo"
# Running the model training script for each tissue/chromosome pair
cd $DIR
for i in {1..20}
do
  Rscript scripts/create_model.R 'Ac' $i 0.5 1000000
  Rscript scripts/create_model.R 'Il' $i 0.5 1000000
  Rscript scripts/create_model.R 'Lh' $i 0.5 1000000
  Rscript scripts/create_model.R 'Pl' $i 0.5 1000000
  Rscript scripts/create_model.R 'Vo' $i 0.5 1000000
done
  
# Concatenating all of the results files for each tissue
bash scripts/make_all_results.sh 'Ac' 'Results/all_results_Ac' 0.5 '1KG_snps'
bash scripts/make_all_betas.sh 'Ac' 'Results/all_betas_Ac' 0.5 '1KG_snps'
bash scripts/make_all_logs.sh 'Ac' 'Results/all_logs_Ac'
bash scripts/make_all_covariances.sh 'Ac' 'Results/all_covariances_Ac' 0.5 '1KG_snps'

bash scripts/make_all_results.sh 'Il' 'Results/all_results_Il' 0.5 '1KG_snps'
bash scripts/make_all_betas.sh 'Il' 'Results/all_betas_Il' 0.5 '1KG_snps'
bash scripts/make_all_logs.sh 'Il' 'Results/all_logs_Il'
bash scripts/make_all_covariances.sh 'Il' 'Results/all_covariances_Il' 0.5 '1KG_snps' 

bash scripts/make_all_results.sh 'Lh' 'Results/all_results_Lh' 0.5 '1KG_snps'
bash scripts/make_all_betas.sh 'Lh' 'Results/all_betas_Lh' 0.5 '1KG_snps'
bash scripts/make_all_logs.sh 'Lh' 'Results/all_logs_Lh'
bash scripts/make_all_covariances.sh 'Lh' 'Results/all_covariances_Lh' 0.5 '1KG_snps'

bash scripts/make_all_results.sh 'Pl' 'Results/all_results_Pl' 0.5 '1KG_snps'
bash scripts/make_all_betas.sh 'Pl' 'Results/all_betas_Pl' 0.5 '1KG_snps'
bash scripts/make_all_logs.sh 'Pl' 'Results/all_logs_Pl'
bash scripts/make_all_covariances.sh 'Pl' 'Results/all_covariances_Pl' 0.5 '1KG_snps'

bash scripts/make_all_results.sh 'Vo' 'Results/all_results_Vo' 0.5 '1KG_snps'
bash scripts/make_all_betas.sh 'Vo' 'Results/all_betas_Vo' 0.5 '1KG_snps'
bash scripts/make_all_logs.sh 'Vo' 'Results/all_logs_Vo'
bash scripts/make_all_covariances.sh 'Vo' 'Results/all_covariances_Vo' 0.5 '1KG_snps'
# Putting these into sql lite databases
python scripts/make_sqlite_db.py --output "Results/sql/Ac_output_db.db" --results "Results/all_results_Ac" --construction "Results/all_logs_Ac" --betas "Results/all_betas_Ac" --meta "Results/allMetaData/Ac.allMetaData.txt"

python scripts/make_sqlite_db.py --output "Results/sql/Il_output_db.db" --results "Results/all_results_Il" --construction "Results/all_logs_Il" --betas "Results/all_betas_Il" --meta "Results/allMetaData/Il.allMetaData.txt"

python scripts/make_sqlite_db.py --output "Results/sql/Lh_output_db.db" --results "Results/all_results_Lh" --construction "Results/all_logs_Lh" --betas "Results/all_betas_Lh" --meta "Results/allMetaData/Lh.allMetaData.txt"

python scripts/make_sqlite_db.py --output "Results/sql/Pl_output_db.db" --results "Results/all_results_Pl" --construction "Results/all_logs_Pl" --betas "Results/all_betas_Pl" --meta "Results/allMetaData/Pl.allMetaData.txt"

python scripts/make_sqlite_db.py --output "Results/sql/Vo_output_db.db" --results "Results/all_results_Vo" --construction "Results/all_logs_Vo" --betas "Results/all_betas_Vo" --meta "Results/allMetaData/Vo.allMetaData.txt"

sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3     rstudioapi_0.11  whisker_0.4      knitr_1.39      
 [5] magrittr_1.5     workflowr_1.6.2  R6_2.4.1         rlang_1.0.2     
 [9] fastmap_1.1.0    stringr_1.4.0    tools_4.0.3      xfun_0.31       
[13] cli_3.3.0        git2r_0.27.1     jquerylib_0.1.4  htmltools_0.5.2 
[17] ellipsis_0.3.2   rprojroot_1.3-2  yaml_2.2.1       digest_0.6.27   
[21] tibble_3.0.4     lifecycle_0.2.0  crayon_1.3.4     later_1.1.0.1   
[25] sass_0.4.1       vctrs_0.4.1      fs_1.5.0         promises_1.1.1  
[29] glue_1.6.2       evaluate_0.15    rmarkdown_2.14   stringi_1.5.3   
[33] compiler_4.0.3   bslib_0.3.1      pillar_1.4.6     backports_1.1.10
[37] jsonlite_1.7.1   httpuv_1.5.4     pkgconfig_2.0.3