-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipes_targets.qmd
362 lines (279 loc) · 9.99 KB
/
recipes_targets.qmd
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
---
title: "Recipes for some analyses modifications"
subtitle: "Where we present some piece of codes to modify the bioinformatic pipeline"
author: Adrien Taudière
date: last-modified
execute:
eval: false
format:
html:
code-fold: false
# bibliography: bibliography.bib
# link-citations: true
---
See also [Easy16S](https://github.com/YongxinLiu/EasyAmplicon/blob/master/pipeline_en.sh) for inspiration in bash.
## Change the parameter to merged forward and reverse sequences
1. Play with option min Overlap (default to 12) and maxMismach (default to 0). For example, replace the `merged_seq`
targets with the following code to allow 1 mismatch and a minimum overlap of 8.
```{r, eval=FALSE}
tar_target(
merged_seq,
mergePairs(
dadaF = ddF,
dadaR = ddR,
derepF = derep_fs,
derepR = derep_rs,
minOverlap = 8,
maxMismatch = 1)
),
format = "qs"
)
)
```
## Forward only pipeline
1. Replace all the "paired end" area with the following code
```{r, eval=FALSE}
##> Remove primers
tar_target(
cutadapt,
cutadapt_remove_primers(
path_to_fastq = here("data/data_raw/rawseq/"),
primer_fw = fw_primer_sequences,
folder_output = here("data/data_intermediate/seq_wo_primers/"),
args_before_cutadapt = "source ~/miniforge3/etc/profile.d/conda.sh && conda activate cutadaptenv && "
)
),
tar_target(data_raw, {
cutadapt
list_fastq_files(path = here::here("data/data_intermediate/seq_wo_primers/"),
paired_end = FALSE)
}),
##> Classical dada2 pipeline
tar_target(data_fnfs, data_raw$fnfs),
### Pre-filtered data with low stringency
tar_target(
filtered,
filter_trim(
output_fw = paste(
getwd(),
here("/data/data_intermediate/filterAndTrim_fwd"),
sep = ""
),
rev = data_fnrs,
multithread = n_threads,
compress = TRUE
)
),
### Dereplicate fastq files
tar_target(derep_fs, derepFastq(filtered[[1]]), format = "qs"),
tar_target(derep_rs, derepFastq(filtered[[2]]), format = "qs"),
### Learns the error rates
tar_target(err_fs, learnErrors(derep_fs, multithread = 4), format = "qs"),
tar_target(err_rs, learnErrors(derep_rs, multithread = 4), format = "qs"),
### Make amplicon sequence variants
tar_target(ddF, dada(derep_fs, err_fs, multithread = 4), format = "qs"),
tar_target(ddR, dada(derep_rs, err_rs, multithread = 4), format = "qs"),
### Build a a table of ASV x Samples
tar_target(seq_tab, makeSequenceTable(ddF)),
```
## Add a second taxonomic assignation using a different database or algorithm
1. Add a new database file (*fasta*) in `data/data_raw/refseq`
1. Copy and complete with good names the two targets below
1. Rename the targets by using the new name (e.g. `data_phyloseq_newDB`) instead of `data_phyloseq` in the subsequent targets
```{r, eval=FALSE}
[...]
tar_target(
name = file_refseq_taxo2,
command = "data/data_raw/refseq/XXX",
format = "file"
)
[...]
tar_target(
data_phyloseq_newDB,
add_new_taxonomy_pq(
data_phyloseq,
file_refseq_taxo2,
suffix = "PR2",
taxLevels = c(
"Kingdom",
"Supergroup",
"Division",
"Subdivision",
"Class",
"Order",
"Family",
"Genus",
"Species"
)
)
)
```
## Filter by % sequence (whith blast)
```{r, eval=FALSE}
tar_target(d_blast,
filter_taxa_blast(
data_phyloseq,
fasta_for_db = paste0(here::here(), "/", file_refseq_taxo),
nproc = 4
)
)
```
## Add funguild informations for Fungi
```{r, eval=FALSE}
tar_target(d_funguild,
MiscMetabar::add_funguild_info(data_phyloseq)
)
```
## Add Protax informations for Bacteria
::: {.content-hidden}
## FAPROTAX analysis
### Assign functionnality
```{r}
# Inspiration from https://forum.qiime2.org/t/exporting-otu-table-from-phyloseq-into-either-biom-or-text-format/19103/7
write_biom_csv <- function(physeq, file, sep = "; ") {
ps <- phyloseq::taxa_sums(physeq) |>
as.data.frame()
ps |> tibble::rownames_to_column("#OTU ID") |>
left_join(phyloseq::tax_table(physeq) |>
as.data.frame() |>
tibble::rownames_to_column("#OTU ID") |>
tidyr::unite("taxonomy", !`#OTU ID`, sep = sep)) -> phyloseq_biom
write.table(phyloseq_biom, file = file, sep = "\t", quote = FALSE, row.names = FALSE)
}
```
```{r}
here::i_am("report_16S.qmd")
tax_tab_path <- here::here("output/faprotax_tax_table.csv")
FAPROTAX_path <- here::here("code/FAPROTAX_1.2.7")
d_vs@tax_table <- tax_table(cbind(d_vs@tax_table, "Genus_species" = paste(d_vs@tax_table[,"Genus"],
d_vs@tax_table[,"Species"]) ))
write.table(subset_taxa(d_vs, !is.na(Species))@tax_table, tax_tab_path, sep = ";")
write_biom_csv(d_vs, tax_tab_path)
dir.create("output/FAPROTAX")
# system(pip install biom-format)
system(paste0("python3 ", FAPROTAX_path, "/collapse_table.py", " -i ", tax_tab_path, " -o output/FAPROTAX/func_table.csv -g ", FAPROTAX_path, '/FAPROTAX.txt -r output/FAPROTAX/report.txt -s output/FAPROTAX/output_subtables/ -f -c "#" -d "taxonomy" --omit_columns 0 --column_names_are_in last_comment_line -n columns_after_collapsing -v --out_groups2records_table output/FAPROTAX/faprotax_result_OTU.tsv'))
func_tab <- read.table("output/FAPROTAX/faprotax_result_OTU.tsv", sep="\t", header = TRUE)
colnames(func_tab) <- gsub("FPTax_record", "record_faprotax", paste0("FPTax_", colnames(func_tab)))
tax_tab <-
as.matrix(cbind(
tax_table(d_vs),
func_tab
))
d_vs@tax_table <- tax_table(tax_tab)
```
### Functionnal analysis
```{r}
func_group <- colSums(apply(d_vs@tax_table[,grepl("FPTax",colnames(d_vs@tax_table))], 2, as.numeric))
func_group <- func_group[func_group>0]
DT::datatable(as.data.frame(sort(func_group, decreasing = T)))
func_group_more_than20 <- func_group[func_group>20]
d_vs_Treatment <- speedyseq::merge_samples2(d_vs, "Treatment")
```
```{r}
psm <- psmelt(d_vs) |>
filter(Abundance > 0) |>
mutate(across(all_of(starts_with("FPTax_")), as.numeric))
psm |>
group_by(Sample) |>
summarise("sumFPTax_chemoheterotrophy" = sum(FPTax_chemoheterotrophy))
ggplot(psm, aes(x = Treatment, y = FPTax_chemoheterotrophy, fill = Sample)) +
geom_bar(stat = "identity")
ggplot(psm, aes(x = Treatment, y = FPTax_chemoheterotrophy)) + geom_boxplot()
psm2 <- psm |>
group_by(Sample) |>
summarise(across(all_of(starts_with("FPTax_")), sum)) |>
tidyr::pivot_longer(all_of(starts_with("FPTax_")),
names_to = "Function",
values_to = "Nb_ASV")
psm2_na <- psm |>
group_by(Sample) |>
summarise(across(all_of(starts_with("FPTax_")), function(x) {
sum(x == 0)
})) |>
tidyr::pivot_longer(all_of(starts_with("FPTax_")),
names_to = "Function",
values_to = "Nb_ASV_NA")
psm2_nbseq <- psm |>
group_by(Sample) |>
summarise(across(all_of(starts_with("FPTax_")), function(x) {
sum(x * Abundance)
})) |>
tidyr::pivot_longer(all_of(starts_with("FPTax_")),
names_to = "Function",
values_to = "Nb_seq")
psm3 <- psm2 |>
filter(Function %in% names(func_group_more_than20))
psm3_na <- psm2_na |>
filter(Function %in% names(func_group_more_than20))
psm3_nbseq <- psm2_nbseq |>
filter(Function %in% names(func_group_more_than20))
psm_4 <- cbind(psm3,
"Nb_ASV_NA"=psm3_na$Nb_ASV_NA,
"Nb_seq"=psm3_nbseq$Nb_seq)
psm_4 <- psm_4 |>
mutate("Prop_ASV" = Nb_ASV/(Nb_ASV + Nb_ASV_NA))
psm_4$Treatment <- d_vs@sam_data$Treatment[match(psm_4$Sample, sample_names(d_vs))]
ggplot(psm_4, aes(y = Function, x = Nb_ASV, fill= Treatment)) +
geom_boxplot()
ggplot(psm_4, aes(y = Function, x = Nb_seq, fill= Treatment)) +
geom_boxplot()
ggstatsplot::ggbetweenstats(psm_4 |> filter(Function == "FPTax_chemoheterotrophy"),
Treatment, Nb_ASV)
```
```{r}
psm_treat <- psmelt(d_vs_Treatment) |>
filter(Abundance > 0) |>
mutate(across(all_of(starts_with("FPTax_")), as.numeric))
psm_treat2 <- psm_treat |>
group_by(Treatment) |>
summarise(across(all_of(starts_with("FPTax_")), sum)) |>
group_by(Treatment) |>
tidyr::pivot_longer(all_of(starts_with("FPTax_")),
names_to = "Function",
values_to = "Nb_ASV")
psm_treat2_na <- psm_treat |>
group_by(Treatment) |>
summarise(across(all_of(starts_with("FPTax_")), function(x){
sum(x == 0)
})) |>
group_by(Treatment) |>
tidyr::pivot_longer(all_of(starts_with("FPTax_")),
names_to = "Function",
values_to = "Nb_ASV_NA")
psm_treat2_seq <- psm_treat |>
group_by(Treatment) |>
summarise(across(all_of(starts_with("FPTax_")), function(x){
sum(x*Abundance)
})) |>
group_by(Treatment) |>
tidyr::pivot_longer(all_of(starts_with("FPTax_")),
names_to = "Function",
values_to = "Nb_seq")
psm_treat3 <- psm_treat2 |>
filter(Function %in% names(func_group_more_than20))
psm_treat3_na <- psm_treat2_na |>
filter(Function %in% names(func_group_more_than20))
psm_treat3_seq <- psm_treat2_seq |>
filter(Function %in% names(func_group_more_than20))
psm_treat4 <- cbind(psm_treat3,
"Nb_ASV_NA"=psm_treat3_na$Nb_ASV_NA,
"Nb_seq"=psm_treat3_seq$Nb_seq)
psm_treat4 <- psm_treat4 |>
mutate("Prop_ASV" = Nb_ASV/(Nb_ASV + Nb_ASV_NA))
ggplot(psm_treat4, aes(y = Function, x = Nb_ASV, fill = Treatment)) +
geom_bar(stat = "identity", position = "Fill")
ggplot(psm_treat4, aes(y = Function, x = Nb_seq, fill = Treatment)) +
geom_bar(stat = "identity", position = "Fill")
ggplot(psm_treat4, aes(y = Treatment, x = Prop_ASV, fill = Treatment)) +
geom_bar(stat = "identity") + facet_wrap(~Function)
```
:::
## Remove n nucleotides at the left or right end
You may want to use the trimLeft and/or trimRight arguments of filter_trim function.
```{r}
filter_trim(
# [...],
trimLeft = 1,
trimRight = 1
)
```