-
Notifications
You must be signed in to change notification settings - Fork 0
/
_targets.R
executable file
·218 lines (201 loc) · 4.88 KB
/
_targets.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# _targets.R
library(targets)
library(tarchetypes)
library(future)
library(future.batchtools)
library(batchtools)
library(here)
source(here("code", "targets_functions.R"))
# @@@@@@@@@@@@
# Set Up -----
# @@@@@@@@@@@
options(tidyverse.quiet = TRUE)
tar_option_set(
packages = c(
"data.table",
"here",
"fs",
"condathis",
"janitor",
"tidyverse",
"vroom"
),
workspace_on_error = TRUE,
garbage_collection = TRUE,
memory = "transient"
)
list(
# @@@@@@@@@@@@
# Set up -----
# @@@@@@@@@@@@
# folder with the FASTQ files
tar_target(
fastq_path,
here::here("data", "raw", "fastq")
),
# This is the file that maps subj to fastq file
tar_file_read(
rna_files,
here("config", "mapping.csv"),
read_csv(file = !!.x, col_types = cols()) |>
# add absolute path to file name
mutate(across(
starts_with("r"),
\(x) fs::path(fastq_path, x)
))
),
# Make it iterable for branching
tar_target(
rna_mapping,
rna_files |>
group_by(subject_id) |>
tar_group(),
iteration = "group"
),
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# ___check fastq and trimmed them ----
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# Create environment and install fastp
tar_target(
fastp_env,
{
condathis::create_env(
"fastp=0.23.4",
env_name = "fastp-env",
overwrite = TRUE
)
"fastp-env"
}
),
# use a wrapper function in here("code", "targets_functions.R")
# to run fastp
tar_target(
trimmed_fastq_qc,
{
fastp(
r1 = rna_mapping$r1,
r2 = rna_mapping$r2,
path_folder_out_trimmed = here::here("data", "outputs", "trimmed"),
path_folder_out_qc = here::here("data", "outputs", "fastp_qc"),
conda_env = fastp_env
)
},
iteration = "list",
format = "file",
pattern = map(rna_mapping)
),
# @@@@@@@@@@@@
# Align -----
# @@@@@@@@@@@@
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# ___Prepare Micromamba Envs for Download and Align -----
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# Create environment and install gsutils
tar_target(
wget_env,
{
if (.Platform$OS.type %in% c("windows")) {
package_string <- "m2-wget"
} else {
package_string <- "wget"
}
condathis::create_env(
packages = package_string,
env_name = "wget-env",
overwrite = TRUE
)
"wget-env"
}
),
# Create environment and install gsutils
tar_target(
gsutil_env,
{
condathis::create_env("gsutil=5.33", env_name = "gsutil-env", overwrite = TRUE)
"gsutil-env"
}
),
# samtools
tar_target(
samtools_env,
{
condathis::create_env("samtools=1.17", env_name = "samtools-env", overwrite = TRUE)
"samtools-env"
}
),
# minimap2
tar_target(
minimap_env,
{
condathis::create_env("minimap2=2.26", env_name = "minimap-env", overwrite = TRUE)
"minimap-env"
}
),
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@
# ___Download References -----
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@
# use a wrapper function in here("code", "targets_functions.R")
tar_target(
reference_files,
download_references_hg19(
path_download = here::here("data", "outputs", "reference"),
gsutil_conda_env = gsutil_env,
wget_conda_env = wget_env
),
format = "file"
),
# the minimap2 indexed reference
tar_file(
reference_mmi,
minimap2_index(
reference_files = reference_files,
threads = 2,
path_folder_out = here::here("data", "outputs", "reference"),
conda_env = minimap_env
)
),
# @@@@@@@@@@@@@@@@
# ___mapping -----
# @@@@@@@@@@@@@@@@
# use a wrapper function in here("code", "targets_functions.R")
tar_target(
mapped_sams,
minimap2_align(
reference_mmi = reference_mmi,
r1 = trimmed_fastq_qc[1],
r2 = trimmed_fastq_qc[2],
threads = 2,
path_folder_out = here::here("data", "outputs", "aligned_sam"),
conda_env = minimap_env
),
pattern = map(trimmed_fastq_qc),
format = "file"
),
# @@@@@@@@@@@@@@@@@@@@@@@@@
# SAM to BAM and Sort -----
# @@@@@@@@@@@@@@@@@@@@@@@@@
# use a wrapper function in here("code", "targets_functions.R")
tar_target(
bam_files,
sam_to_bam(mapped_sams,
path_tmp = here::here("data", "tmp"),
path_folder_out = here::here("data", "outputs", "bams"),
threads = 2,
conda_env = samtools_env
),
pattern = map(mapped_sams),
format = "file"
),
# use a wrapper function in here("code", "targets_functions.R")
tar_target(
sorted_bams, # Sorted and Indexed
sort_index(bam_files,
path_tmp = here::here("data", "tmp"),
path_folder_out = here::here("data", "outputs", "sorted_bams"),
threads = 2,
conda_env = samtools_env
),
pattern = map(bam_files),
format = "file"
)
)