This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tar_twas.R
137 lines (128 loc) · 4.27 KB
/
tar_twas.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
### global libraries ===============================================================================
library(targets)
library(tarchetypes)
library(here)
library(data.table)
# library(future)
# library(future.callr)
# targets::tar_renv(extras = "visNetwork", path = "scripts/_dependencies.R")
# renv::install("gabraham/flashpca/flashpcaR")
# renv::install(c("bioc::S4Vectors", "bioc::biomaRt", "bioc::tximport", "bioc::DESeq2", "bioc::MatrixGenerics"))
# renv::hydrate()
### project setup ==================================================================================
# Functions/scripts required: tar-twas.R, tar-biomart.R, tar-pval_trans.R
invisible(sapply(
X = list.files(here("scripts", "tar-utils"), pattern = "^tar-.*R$", full.names = TRUE),
FUN = source, echo = FALSE
))
# plan(future.callr::callr, workers = 40)
# plan(multicore, workers = 40)
# message(sprintf("Number of workers: %d", future::nbrOfWorkers()))
# setDTthreads(threads = 1)
### targets setup ==================================================================================
tar_setup <- list(
tar_target(project, sub("(.*)_[^_]*\\.Rproj$", "\\1", list.files(here(), pattern = ".Rproj$")), packages = "here"),
tar_target(author, "Mickaël Canouil, *Ph.D*"),
tar_target(output_directory, here::here("outputs"), packages = "here")
)
### targets ========================================================================================
tar_sample_sheet_qc <- list(
tar_target(twas_sample_sheet_qc,
command = qc_sample_sheet_twas(
phenotype = NULL,
run_path = "/disks/RUN/Run_42/"
),
packages = "data.table"
)
)
tar_twas <- list(
tar_target(twas_rna_level, c("ensembl_gene_id", "ensembl_transcript_id")[1]),
tar_target(twas_models,
command = tar_group(dplyr::group_by(
data.frame(
pretty_trait = c("Case/Control"),
raw_trait = c("group"),
covariates = c("")
),
pretty_trait, raw_trait, covariates
)),
packages = "dplyr",
iteration = "group"
),
tar_target(twas_tximport,
command = read_rsem(
sample_sheet = twas_sample_sheet_qc,
rna_level = twas_rna_level
),
pattern = map(twas_rna_level),
iteration = "list",
packages = c("tximport", "readr")
),
tar_target(twas_pca_plots,
command = plot_pca_twas(
txi = twas_tximport,
sample_sheet = twas_sample_sheet_qc,
pca_vars = c("group"),
n_comp = 10,
fig_n_comp = 3
),
pattern = map(twas_tximport, twas_rna_level),
iteration = "list",
packages = c(
"flashpcaR", "data.table", "ggplot2", "ggtext", "patchwork",
"scales", "stats", "utils",
"DESeq2", "MatrixGenerics"
)
),
tar_target(twas_biomart,
command = get_biomart_information(
ensembl_id = rownames(twas_tximport[["counts"]]),
rna_level = twas_rna_level,
organism = "hsapiens_gene_ensembl",
version = unique(sub(
pattern = ".*Ensembl-([0-9]+)\\.genes\\.results$",
replacement = "\\1",
x = basename(twas_sample_sheet_qc[["rnaseq_path"]])
))
),
pattern = map(twas_rna_level, twas_tximport),
iteration = "list",
packages = c("biomaRt", "httr", "data.table")
),
tar_target(twas_results_file,
command = do_twas(
txi = twas_tximport,
sample_sheet = twas_sample_sheet_qc,
model = twas_models,
path = file.path(output_directory, "twas"),
rna_level = twas_rna_level,
biomart = twas_biomart
),
pattern = cross(twas_models, map(twas_rna_level, twas_tximport, twas_biomart)),
iteration = "list",
packages = c("data.table", "DESeq2", "S4Vectors", "MatrixGenerics", "utils", "stats"),
format = "file"
),
tar_target(twas_results_volcano,
command = plot_volcano_twas(file = twas_results_file, model = twas_models),
pattern = map(twas_models, twas_results_file),
iteration = "list",
packages = c("ggplot2", "ggtext", "data.table", "ggrepel", "scales")
),
tar_render(twas_report,
path = here("slides/twas_report.Rmd"),
output_dir = here("reports"),
packages = c(
"xaringan",
"here", "knitr", "ragg", "ggplot2", "ggtext",
"patchwork", "data.table", "gt", "scales",
"showtext", "svglite", "katex",
"targets", "bacon", "utils"
)
)
)
list(
tar_setup,
tar_sample_sheet_qc,
tar_twas
)