Last updated: 2023-03-21
Checks: 7 0
Knit directory: fitnessGWAS/
This reproducible R Markdown analysis was created with workflowr (version 1.7.0.4). 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(20180914)
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 5204ef7. 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: .Rapp.history
Ignored: .Rhistory
Ignored: .Rproj.user/
Ignored: .httr-oauth
Ignored: .pversion
Ignored: analysis/.DS_Store
Ignored: code/.DS_Store
Ignored: code/Drosophila_GWAS.Rmd
Ignored: data/.DS_Store
Ignored: data/derived/
Ignored: data/input/.DS_Store
Ignored: data/input/.pversion
Ignored: data/input/dgrp.fb557.annot.txt
Ignored: data/input/dgrp2.bed
Ignored: data/input/dgrp2.bim
Ignored: data/input/dgrp2.fam
Ignored: data/input/huang_transcriptome/
Ignored: figures/.DS_Store
Ignored: old_analyses/.DS_Store
Untracked files:
Untracked: old_analyses/Data for old analyses/
Untracked: old_analyses/eQTL_analysis.Rmd
Untracked: old_analyses/fitness_data.csv
Untracked: old_analyses/gcta_quant_genetics_OLD.Rmd
Untracked: old_analyses/quantitative_genetics_OLD_brms.Rmd
Unstaged changes:
Modified: figures/fig3_boyle_plot.pdf
Modified: figures/fig4_mutation_load.pdf
Modified: figures/fig5_quartiles_plot.pdf
Modified: figures/fig6_antagonism_ratios.pdf
Modified: figures/fig7_models.pdf
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/TWAS.Rmd
) and HTML
(docs/TWAS.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 | 5204ef7 | lukeholman | 2023-03-21 | wflow_publish("analysis/TWAS.Rmd") |
html | 7449a90 | lukeholman | 2021-10-01 | Build site. |
Rmd | 01226ab | lukeholman | 2021-10-01 | wflow_publish("analysis/*") |
html | 8d14298 | lukeholman | 2021-09-26 | Build site. |
Rmd | af15dd6 | lukeholman | 2021-09-26 | Commit Sept 2021 |
html | 3124aad | lukeholman | 2021-03-05 | Build site. |
Rmd | 6704dc8 | lukeholman | 2021-03-05 | wflow_publish("analysis/TWAS.Rmd") |
html | c18c925 | lukeholman | 2021-03-05 | Build site. |
Rmd | 382f3a3 | lukeholman | 2021-03-05 | wflow_publish("analysis/TWAS.Rmd") |
Rmd | 3855d33 | lukeholman | 2021-03-04 | big fist commit 2021 |
html | 3855d33 | lukeholman | 2021-03-04 | big fist commit 2021 |
Rmd | 8d54ea5 | Luke Holman | 2018-12-23 | Initial commit |
library(edgeR) # BiocManager::install("edgeR")
library(tidyverse)
library(glue)
library(future)
library(future.apply)
library(parallel)
library(kableExtra)
library(DT)
library(ashr)
library(mashr)
options(stringsAsFactors = FALSE)
# Connect to the database of annotations
db <- DBI::dbConnect(RSQLite::SQLite(), "data/derived/annotations.sqlite3")
# Helper to run shell commands
run_command <- function(shell_command, wd = getwd(), path = ""){
cat(system(glue("cd ", wd, path, "\n",shell_command), intern = TRUE), sep = '\n')
}
kable_table <- function(df) { # cool tables
kable(df, "html") %>%
kable_styling() %>%
scroll_box(height = "300px")
}
my_data_table <- function(df){ # Make html tables:
datatable(
df, rownames=FALSE,
autoHideNavigation = TRUE,
extensions = c("Scroller", "Buttons"),
options = list(
dom = 'Bfrtip',
deferRender=TRUE,
scrollX=TRUE, scrollY=400,
scrollCollapse=TRUE,
buttons =
list('pageLength', 'colvis', 'csv', list(
extend = 'pdf',
pageSize = 'A4',
orientation = 'landscape',
filename = 'TWAS_enrichment')),
columnDefs = list(list(targets = c(8,10), visible = FALSE)),
pageLength = 50
)
)
}
# Helper to load Huang et al.'s data
load_expression_data <- function(sex, both_sex_chromosomes = TRUE){
# Note: Huang et al's data contains weird stuff like supposedly Y-linked genes that have
# higher/equal expression in *females* in all lines, presumably microarray issues/errors.
# To be conservative, we restrict our analyses to genes that are known to be on a
# chromosomes that is present in both sexes
if(both_sex_chromosomes){
genes_allowed <- tbl(db, "genes") %>%
filter(chromosome %in% c("2L", "2R", "3L", "3R", "4", "X")) %>%
pull(FBID)
} else {
genes_allowed <- tbl(db, "genes") %>%
pull(FBID)
}
if(sex != "both"){
expression <- glue("data/input/huang_transcriptome/dgrp.array.exp.{sex}.txt") %>%
read_delim(delim = " ") %>%
filter(gene %in% genes_allowed)
sample_names <- names(expression)[names(expression) != "gene"] %>% str_remove(":[12]")
gene_names <- expression$gene
expression <- expression %>% select(-gene) %>% as.matrix() %>% t()
rownames(expression) <- sample_names # rows are samples, columns are genes
colnames(expression) <- gene_names
return(expression %>% as.data.frame() %>%
tibble::rownames_to_column("line") %>%
as_tibble() %>%
mutate(line = str_remove_all(line, "[.]1")))
}
females <- read_delim("data/input/huang_transcriptome/dgrp.array.exp.female.txt", delim = " ") %>%
filter(gene %in% genes_allowed)
names(females)[-1] <- paste("F_", names(females)[-1],sep="") #%>% str_remove(":[12]")
females <- females %>%
left_join(read_delim("data/input/huang_transcriptome/dgrp.array.exp.male.txt", delim = " "), by = "gene")
sample_names <- names(females)[names(females) != "gene"] %>% str_remove(":[12]")
gene_names <- females$gene
sex <- ifelse(str_detect(sample_names, "F_"), "female", "male")
line <- str_remove_all(sample_names, "F_")
females <- females %>% select(-gene) %>% t()
colnames(females) <- gene_names
list(
sampleIDs = tibble(sex, line),
expression = females
)
}
# Table S5: heritability of the expression level of each transcript (as measured in males, or females)
# huang_heritability <- read_csv("data/input/huang_2015_tableS5_transcript_heritability.csv")
# Load the predicted line means from present study, as calculated in get_predicted_line_means.Rmd
predicted_line_means <- read_csv("data/derived/predicted_line_means.csv")
The following takes the 368 female samples and the 369 male samples,
and finds the log fold difference in expression between sexes, and the
average expression across both sexes, using the edgeR
package.
if(!file.exists("data/derived/gene_expression_by_sex.csv")){
expression_data_both_sexes <- load_expression_data("both") %>% unname()
voom_gene_data <- calcNormFactors(DGEList(t(expression_data_both_sexes[[2]])))
mm <- model.matrix(~ sex, data = expression_data_both_sexes[[1]])
colnames(mm) <- gsub("sex", "", colnames(mm))
sex_bias_in_expression <- voom_gene_data %>%
voom(mm, plot = FALSE) %>%
lmFit(mm) %>%
eBayes() %>%
topTable(n = Inf) %>%
rownames_to_column("FBID") %>%
select(FBID, logFC, AveExpr) %>%
rename(male_bias_in_expression = logFC) %>%
as_tibble() %>% arrange(male_bias_in_expression)
write_csv(sex_bias_in_expression, "data/derived/gene_expression_by_sex.csv")
} else {
sex_bias_in_expression <- read_csv("data/derived/gene_expression_by_sex.csv")
}
This analysis uses the expression data from Huang et al. (2015), which was downloaded from the DGRP website.
Here, we perform a large number of simple linear regressions, and obtain the slope (beta or \(\beta\)), and the associated standard error, from a regression of transcript \(i\) on fitness trait \(j\). The number of regressions run was 57,148, i.e. 4 fitness traits \(\times\) 14,287 transcripts. This approach is often called a ‘TWAS’, i.e. transcriptome-wide association study.
transcript_selection_analysis <- function(expression_data, phenotypes){
if("block" %in% names(phenotypes)) phenotypes <- phenotypes %>% select(-block)
expression_data <- expression_data %>%
filter(line %in% phenotypes$line)
# Find line mean expression for each gene (average across the c. 2 replicate samples per line)
chunk_cols <- split(2:ncol(expression_data),
ceiling(seq_along(2:ncol(expression_data)) / 500))
mean_expression_data <- mclapply(1:2, function(i){
expression_data[, c(1, chunk_cols[[i]])] %>%
group_by(line) %>%
summarise_all(mean) %>%
ungroup()
}) %>% bind_rows()
# Scale each transcript's expression level so that mean is 0,
# and the variance is 1, across all the lines measured by Huang et al.
for(i in 2:ncol(mean_expression_data)) mean_expression_data[,i] <- as.numeric(scale(mean_expression_data[,i]))
# Join the microarray data with the phenotypes (i.e. our fitness data), and
# keep only the lines where we have both sets of measurements
expression_data <- phenotypes %>% left_join(expression_data, by = "line")
expression_data <- expression_data[complete.cases(expression_data), ] %>% select(-line)
print("Data ready for analysis. Starting TWAS...")
# Create chunks of transcript names, which will be used to facilitate parallel processing
transcripts <- names(expression_data)[-c(1:4)]
transcripts <- split(transcripts, ceiling(seq_along(transcripts) / 100))
# Define a function to run 4 linear models, and get the beta and SE
# for regressions of expression level on the 4 fitness traits
do_one_transcript <- function(transcript){
expression_level <- expression_data %>% pull(!!transcript)
FE <- summary(lm(female.fitness.early ~ expression_level, data = expression_data))$coefficients
FL <- summary(lm(female.fitness.late ~ expression_level, data = expression_data))$coefficients
ME <- summary(lm(male.fitness.early ~ expression_level, data = expression_data))$coefficients
ML <- summary(lm(male.fitness.late ~ expression_level, data = expression_data))$coefficients
c(FE[2,1], FL[2,1], ME[2,1], ML[2,1], # effect size
FE[2,2], FL[2,2], ME[2,2], ML[2,2], # SE
FE[2,4], FL[2,4], ME[2,4], ML[2,4]) # p-value
}
# Runs do_one_transcript() on all the transcripts listed in the vector 'transcripts'
do_chunk_of_transcripts <- function(transcripts){
output <- data.frame(transcripts, lapply(transcripts, do_one_transcript) %>% do.call("rbind", .))
names(output) <- c("gene", "beta_FE", "beta_FL", "beta_ME", "beta_ML",
"SE_FE", "SE_FL", "SE_ME", "SE_ML",
"pval_FE", "pval_FL", "pval_ME", "pval_ML")
output
}
# Run it all, in parallel
transcripts %>%
mclapply(do_chunk_of_transcripts) %>%
do.call("rbind", .) %>% as_tibble() %>% mutate(gene = as.character(gene))
}
if(!file.exists("data/derived/TWAS/TWAS_result_males.csv")){
TWAS_result_females <- load_expression_data("female") %>%
transcript_selection_analysis(predicted_line_means)
TWAS_result_females %>% write_csv("data/derived/TWAS/TWAS_result_females.csv")
TWAS_result_males <- load_expression_data("male") %>%
transcript_selection_analysis(predicted_line_means)
TWAS_result_males %>% write_csv("data/derived/TWAS/TWAS_result_males.csv")
} else {
TWAS_result_females <- read_csv("data/derived/TWAS/TWAS_result_females.csv")
TWAS_result_males <- read_csv("data/derived/TWAS/TWAS_result_males.csv")
}
mashr
to adjust the TWAS resultsThis section re-uses the custom function run_mashr()
.
See the earlier script (where mashr
was applied to the GWAS
data) for the function definition. As the GWAS data, we use
mashr
’s data-driven mode to derive adjusted (shrinked)
estimates of beta (i.e. the slope of the regression of transcript
abundance on the phenotype of interest), which are sensitive to the
covariance structure in the data (which is estimated from the data). As
well as producing these adjusted estimates, we derive the local false
sign rate for each combination of transcript and fitness trait (used
later to calculate evidence ratios).
if(!file.exists("data/derived/TWAS/TWAS_ED.rds")){
input_data <- data.frame(TWAS_result_females[,1:3],
TWAS_result_males[,4:5],
TWAS_result_females[,6:7],
TWAS_result_males[,8:9])
TWAS_ED <- input_data %>%
run_mashr(mashr_mode = "ED",
ED_p_cutoff = 0.4)
saveRDS(TWAS_ED, "data/derived/TWAS/TWAS_ED.rds")
} else {
TWAS_ED <- readRDS("data/derived/TWAS/TWAS_ED.rds")
}
TWAS_results <- data.frame(
FBID = TWAS_result_females$gene,
as.data.frame(get_pm(TWAS_ED)),
as.data.frame(get_lfsr(TWAS_ED))) %>%
as_tibble()
names(TWAS_results)[6:9] <- paste(
"LFSR", c("FE", "FL", "ME", "ML"), sep = "_")
TWAS_results <- TWAS_results %>%
left_join(tbl(db, "genes") %>%
select(FBID, gene_name, chromosome) %>%
collect(), by = "FBID") %>%
left_join(sex_bias_in_expression, by = "FBID") %>%
mutate(across(where(is.numeric), ~ round(.x, 2))) %>%
as_tibble() %>%
distinct()
TWAS_results %>%
write_csv("data/derived/TWAS/TWAS_results.csv")
sessionInfo()
R version 4.2.2 (2022-10-31)
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.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/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] parallel stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] mashr_0.2.69 ashr_2.2-54 DT_0.27
[4] kableExtra_1.3.4 future.apply_1.10.0 future_1.30.0
[7] glue_1.6.2 lubridate_1.9.2 forcats_1.0.0
[10] stringr_1.5.0 dplyr_1.1.0 purrr_1.0.1
[13] readr_2.1.4 tidyr_1.3.0 tibble_3.1.8
[16] ggplot2_3.4.1 tidyverse_2.0.0 edgeR_3.40.2
[19] limma_3.54.2 workflowr_1.7.0.4
loaded via a namespace (and not attached):
[1] fs_1.6.1 bit64_4.0.5 webshot_0.5.4 httr_1.4.5
[5] rprojroot_2.0.3 tools_4.2.2 bslib_0.4.2 utf8_1.2.2
[9] R6_2.5.1 irlba_2.3.5.1 DBI_1.1.3 colorspace_2.1-0
[13] rmeta_3.0 withr_2.5.0 tidyselect_1.2.0 processx_3.8.0
[17] bit_4.0.5 compiler_4.2.2 git2r_0.31.0 cli_3.6.0
[21] rvest_1.0.3 xml2_1.3.3 sass_0.4.5 scales_1.2.1
[25] SQUAREM_2021.1 mvtnorm_1.1-3 callr_3.7.3 mixsqp_0.3-48
[29] systemfonts_1.0.4 digest_0.6.31 rmarkdown_2.20 svglite_2.1.1
[33] pkgconfig_2.0.3 htmltools_0.5.4 parallelly_1.34.0 dbplyr_2.3.0
[37] fastmap_1.1.1 invgamma_1.1 htmlwidgets_1.6.1 rlang_1.0.6
[41] rstudioapi_0.14 RSQLite_2.3.0 jquerylib_0.1.4 generics_0.1.3
[45] jsonlite_1.8.4 vroom_1.6.1 magrittr_2.0.3 Matrix_1.5-1
[49] Rcpp_1.0.10 munsell_0.5.0 fansi_1.0.4 abind_1.4-5
[53] lifecycle_1.0.3 stringi_1.7.12 whisker_0.4.1 yaml_2.3.7
[57] plyr_1.8.8 blob_1.2.3 grid_4.2.2 listenv_0.9.0
[61] promises_1.2.0.1 crayon_1.5.2 lattice_0.20-45 hms_1.1.2
[65] locfit_1.5-9.7 knitr_1.42 ps_1.7.2 pillar_1.8.1
[69] codetools_0.2-18 evaluate_0.20 getPass_0.2-2 vctrs_0.5.2
[73] tzdb_0.3.0 httpuv_1.6.9 gtable_0.3.1 assertthat_0.2.1
[77] cachem_1.0.7 xfun_0.37 later_1.3.0 viridisLite_0.4.1
[81] truncnorm_1.0-8 memoise_2.0.1 timechange_0.2.0 globals_0.16.2
[85] ellipsis_0.3.2