-
Notifications
You must be signed in to change notification settings - Fork 2
/
_targets.R
1492 lines (1375 loc) · 70.2 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
library(targets)
library(tarchetypes)
library(crew)
source("R/functions.R")
options(tidyverse.quiet = TRUE)
# Set up local controller if threads > 1
nthreads <- max(as.numeric(readr::read_csv("sample_data/loci_params.csv", show_col_types = FALSE)$threads))
if(is.null(nthreads) | is.infinite(nthreads)){
local_controller <- NULL
} else if ( nthreads ==1 ){
local_controller <- NULL
} else if (is.numeric(nthreads) & nthreads > 1){
message(paste0("Running pipeline across ", nthreads, " parallel threads"))
local_controller <- crew::crew_controller_local(
name="local",
workers = nthreads,
host = "127.0.0.1",
port = NULL,
tls = crew::crew_tls(mode="none"), #No transport layer security enabled
seconds_interval = 0.25,
seconds_timeout = 10,
seconds_launch = 30,
seconds_idle = Inf,
seconds_wall = Inf,
tasks_max = Inf,
tasks_timers = 0L,
reset_globals = TRUE,
reset_packages = FALSE,
reset_options = FALSE,
garbage_collection = FALSE,
launch_max = 5L)
} else {
stop("threads parameter must be numeric")
}
# Load packages -----------------------------------------------------------
tar_option_set(packages = c(
"ggplot2",
"scales",
"gridExtra",
"tibble",
"dplyr",
"stringr",
"tidyr",
"purrr",
"rlang",
"readr",
"magrittr",
"patchwork",
"vegan",
"patchwork",
"phyloseq",
"DECIPHER",
"Biostrings",
"ShortRead",
"savR",
"dada2",
"ngsReports",
"taxreturn",
"seqateurs",
"mirai",
"crew",
"nanonext"
),
imports =c(
"taxreturn",
"seqateurs"
),
controller = local_controller,
workspace_on_error = TRUE)
# Source script files in R directory
tar_source()
# Targets pipeline
list(
# Parameter setup ---------------------------------------------------------
# Track input files
tar_file(samdf_file, "sample_data/Sample_info.csv"),
tar_file(params_file, "sample_data/loci_params.csv"),
# Load input tracking files
tar_target(samdf, {
# define default fields
default_samdf <- tibble::tibble(
sample_id = NA_character_,
sample_name = NA_character_,
extraction_rep = NA_integer_,
amp_rep = NA_integer_,
client_name = NA_character_,
experiment_name = NA_character_,
sample_type = NA_character_,
collection_method = NA_character_,
collection_location = NA_character_,
lat_lon = NA_character_,
environment = NA_character_,
collection_date = NA_character_,
operator_name = NA_character_,
description = NA_character_,
assay = NA_character_,
extraction_method = NA_character_,
amp_method = NA_character_,
target_gene = NA_character_,
pcr_primers = NA_character_,
for_primer_seq = NA_character_,
rev_primer_seq = NA_character_,
index_plate = NA_character_,
index_well = NA_character_,
i7_index_id = NA_character_,
i7_index = NA_character_,
i5_index_id = NA_character_,
i5_index = NA_character_,
seq_platform = NA_character_,
fcid = NA_character_,
for_read_length = NA_integer_,
rev_read_length = NA_integer_,
seq_run_id = NA_character_,
seq_id = NA_character_,
seq_date = NA_character_,
analysis_method = NA_character_,
notes = NA_character_
)
# Read in input samdf
input_samdf <- readr::read_csv(samdf_file, show_col_types = FALSE, col_types = cols(.default = "c"))
# Make sure all columns are present and same type using a special bind operation
samdf_checked <- new_bind(default_samdf %>% filter(FALSE), input_samdf)
# Check essential parameters are present
assertthat::assert_that(all(is.character(samdf_checked$sample_id)) & all(!is.na(samdf_checked$sample_id)),
msg = "All samples must have a sample_id in the sample_info.csv file")
assertthat::assert_that(all(is.character(samdf_checked$pcr_primers)) & all(!is.na(samdf_checked$pcr_primers)),
msg = "All samples must have pcr_primers in the sample_info.csv file")
assertthat::assert_that(all(is.character(samdf_checked$for_primer_seq)) & all(!is.na(samdf_checked$for_primer_seq)),
msg = "All samples must have a for_primer_seq in the sample_info.csv file")
assertthat::assert_that(all(is.character(samdf_checked$rev_primer_seq)) & all(!is.na(samdf_checked$rev_primer_seq)),
msg = "All samples must have a rev_primer_seq in the sample_info.csv file")
# Check that there are no duplicated sample_ids
assertthat::assert_that(all(!duplicated(samdf_checked$sample_id)),
msg = paste0("Duplicate sample ids: ", paste(samdf_checked$sample_id[duplicated(samdf_checked$sample_id)], collapse=" "), " in samplesheet"))
return(samdf_checked)
}),
tar_target(params, {
# define default params
default_params <- tibble::tibble(pcr_primers = NA_character_,
target_gene = NA_character_,
max_primer_mismatch = 0,
read_min_length = 20,
read_max_length = Inf,
read_max_ee = 1,
read_trunc_length = 0,
read_trim_left = 0,
read_trim_right = 0,
high_sensitivity = TRUE,
asv_min_length = 0,
asv_max_length = Inf,
concat_unmerged = FALSE,
genetic_code = NA_character_,
coding = FALSE,
phmm = NA_character_,
idtaxa_db = NA_character_,
ref_fasta = NA_character_,
idtaxa_confidence = 60,
run_blast = FALSE,
blast_min_identity = 97,
blast_min_coverage = 90,
target_kingdom = NA_character_,
target_phylum = NA_character_,
target_class = NA_character_,
target_order = NA_character_,
target_family = NA_character_,
target_genus = NA_character_,
target_species = NA_character_,
min_sample_reads = 0,
min_taxa_reads = 0,
min_taxa_ra = 0,
threads = 1
)
# Read in params file
input_params <- readr::read_csv(params_file, show_col_types = FALSE, col_types = cols(.default = "c"))
# Make sure all columns are present and same type using a special bind operation
params_df <- new_bind(default_params %>% filter(FALSE), input_params)
# Check columns arent NA
for(i in 1:ncol(default_params)){
param_to_check <- colnames(default_params)[i]
if(all(is.na(params_df %>% dplyr::pull(!!param_to_check))) & !param_to_check %in% colnames(input_params)){
warning(paste0("Parameter: ", param_to_check, " is NA, using default: ", default_params %>% dplyr::pull(!!param_to_check)))
params_df <- params_df %>%
dplyr::mutate(!!param_to_check := default_params %>% dplyr::pull(!!param_to_check))
}
}
# Check class of all columns
for(i in 1:ncol(default_params)){
param_to_check <- colnames(default_params)[i]
if(!class(default_params %>% dplyr::pull(!!param_to_check)) == class(params_df %>% dplyr::pull(!!param_to_check))){
stop(paste0("The column ", param_to_check, " in loci_params.csv file must be of class ", class(default_params %>% dplyr::pull(!!param_to_check))))
}
}
# Check idtaxa db exists - Needs to handle multiple dbs
check_paths <- params_df$idtaxa_db[!is.na(params_df$idtaxa_db)]%>%
stringr::str_split(pattern=";", n=Inf) %>%
unlist()
for(i in seq_along(check_paths)){
assertthat::is.readable(check_paths[i])
}
# Check fasta exists
check_paths <- params_df$ref_fasta[!is.na(params_df$ref_fasta)] %>%
stringr::str_split(pattern=";", n=Inf) %>%
unlist()
for(i in seq_along(check_paths)){
assertthat::is.readable(check_paths[i])
}
# Check phmm exists
check_paths <- params_df$phmm[!is.na(params_df$phmm)] %>%
stringr::str_split(pattern=";", n=Inf) %>%
unlist()
for(i in seq_along(check_paths)){
assertthat::is.readable(check_paths[i])
}
return(params_df)
}, tidy_eval = FALSE
),
# Create temporary params_primer file for tracking
tar_file(params_primer_path,{
out <- "output/temp/params_primer.csv"
params %>%
dplyr::select(pcr_primers, target_gene, max_primer_mismatch) %>%
write_csv(out)
return(out)
}),
tar_target(params_primer,read_csv(params_primer_path, show_col_types = FALSE)),
# Create temporary params_readfilter file for tracking
tar_file(params_readfilter_path,{
out <- "output/temp/params_readfilter.csv"
params %>%
dplyr::select(pcr_primers, target_gene, read_min_length, read_max_length, read_max_ee,
read_trunc_length, read_trim_left, read_trim_right) %>%
write_csv(out)
return(out)
}),
tar_target(params_readfilter, read_csv(params_readfilter_path, show_col_types = FALSE)),
# Create temporary params_dada file for tracking
tar_file(params_dada_path,{
out <- "output/temp/params_dada.csv"
params %>%
dplyr::select(pcr_primers, target_gene, concat_unmerged, high_sensitivity) %>%
write_csv(out)
return(out)
}),
tar_target(params_dada, read_csv(params_dada_path, show_col_types = FALSE)),
# Create temporary params_asvfilter file for tracking
tar_file(params_asvfilter_path,{
out <- "output/temp/params_asvfilter.csv"
params %>%
dplyr::select(pcr_primers, target_gene, asv_min_length, asv_max_length,
phmm, coding, genetic_code) %>%
write_csv(out)
return(out)
}),
tar_target(params_asvfilter, read_csv(params_asvfilter_path, show_col_types = FALSE)),
# Create temporary params_database file for tracking
tar_file(params_database_path,{
out <- "output/temp/params_database.csv"
params %>%
dplyr::select(pcr_primers, target_gene, idtaxa_db, idtaxa_confidence,
ref_fasta, blast_min_identity, blast_min_coverage, run_blast) %>%
write_csv(out)
return(out)
}),
tar_target(params_database, read_csv(params_database_path, show_col_types = FALSE)),
# Create temporary params_ps file for tracking
tar_file(params_ps_path,{
out <- "output/temp/params_ps.csv"
params %>%
dplyr::select(pcr_primers, target_gene, target_kingdom, target_phylum, target_class,
target_order, target_family, target_genus, target_species,
min_sample_reads, min_taxa_reads, min_taxa_ra) %>%
write_csv(out)
return(out)
}),
tar_target(params_ps, read_csv(params_ps_path, show_col_types = FALSE)),
# Create directories
tar_target(create_dirs, step_validate_folders(getwd())),
# Look for sequencing reads
tar_files(
fastq_path,
purrr::map(list.dirs("data", recursive=FALSE),
list.files, pattern="_R[12]_", full.names = TRUE) %>%
unlist()
),
tar_target(temp_samdf1, step_check_files(samdf, fastq_path, col_name="starting")),
tar_group_by(temp_samdf1_grouped, temp_samdf1, fcid),
# Sequencing QC -------------------------------------------------------
tar_target(seq_qc, {
process <- temp_samdf1_grouped %>%
dplyr::group_by(fcid) %>%
tidyr::nest() %>%
dplyr::mutate(seq_qc = purrr::map(fcid, step_seq_qc, quiet=FALSE, write_all=FALSE))
out <- paste0("output/logs/", unique(process$fcid),"/",unique(process$fcid),"_flowcell_qc.pdf")
if(is.na(process$seq_qc[[1]]$reads_pf)){
pdf(file=out, paper="A4")
plot.new()
text(x=.5, y=.5, "ERROR: InterOp folder or RunInfo.xml not present")
try(dev.off(), silent=TRUE)
}
return(out)
}, pattern = map(temp_samdf1_grouped), format="file", iteration = "vector"),
tar_target(switching_qc,{
process <- temp_samdf1_grouped %>%
dplyr::group_by(fcid) %>%
tidyr::nest() %>%
dplyr::mutate(switching_qc = purrr::map(fcid, step_switching_calc, quiet=TRUE))
out <- paste0("output/logs/",unique(process$fcid),"/",unique(process$fcid),"_index_switching.pdf")
if(is.na(process$switching_qc[[1]]$switch_rate)){
pdf(file=out, paper="A4")
plot.new()
text(x=.5, y=.5, "ERROR: Undetermined reads file not found")
try(dev.off(), silent=TRUE)
}
return(out)
},pattern = map(temp_samdf1_grouped), format="file", iteration = "vector"),
# Demultiplex and trim primers --------------------------------------------
tar_target(primer_trim,
{
temp_samdf1 %>%
dplyr::left_join(params_primer, by = "pcr_primers") %>%
dplyr::mutate(primer_trim = purrr::pmap(dplyr::select(., sample_id, for_primer_seq, rev_primer_seq, pcr_primers, fcid, max_primer_mismatch),
.f = ~step_primer_trim(sample_id = ..1, for_primer_seq=..2, rev_primer_seq=..3, pcr_primers = ..4,
input_dir = paste0("data/",..5), output_dir = paste0("data/",..5,"/01_trimmed"),
qc_dir=paste0("output/logs/",..5),
max_mismatch=..6,
quiet = FALSE)))%>%
dplyr::select(sample_id, sample_name, fcid, primer_trim)
},
pattern = map(temp_samdf1), iteration = "vector"),
# Return filepath for tracking
tar_target(primer_trim_path,
{
outF <- primer_trim$primer_trim %>%
dplyr::bind_rows()%>%
dplyr::pull(fwd_out)
outR <- primer_trim$primer_trim %>%
dplyr::bind_rows()%>%
dplyr::pull(rev_out)
# Check for empty files
outF <- outF[file.exists(outF)]
outR <- outR[file.exists(outR)]
# Return list of completed path
return(c(outF,outR))
},
pattern = map(primer_trim), format="file", iteration = "vector"),
## Make temporary samdf
tar_target(temp_samdf2, {
temp_samdf1 %>%
dplyr::select(-where(is.list)) %>%
step_demux_samdf() %>%
step_check_files(primer_trim_path, col_name="trimmed")
}),
# Filter reads ------------------------------------------------------------
tar_target(read_filter,
{
temp_samdf2 %>%
dplyr::select(sample_id, sample_name, pcr_primers, fcid) %>%
dplyr::left_join(params_readfilter, by = "pcr_primers") %>%
dplyr::mutate(read_filter = purrr::pmap(dplyr::select(., sample_id, fcid, read_min_length, read_max_length, read_max_ee,
read_trunc_length, read_trim_left, read_trim_right),
.f = ~step_filter_reads(
sample_id = ..1,
input_dir = paste0("data/",..2,"/01_trimmed/"),
output_dir = paste0("data/",..2,"/02_filtered"),
min_length = ..3,
max_length = ..4,
max_ee = ..5,
trunc_length = ..6,
trim_left = ..7,
trim_right = ..8,
rm.lowcomplex = 0,
quiet = FALSE)))%>%
dplyr::select(sample_id, sample_name, fcid, read_filter)
},
pattern = map(temp_samdf2), iteration = "vector"),
# Return filepath for tracking
tar_target(read_filter_path,
{
outF <- read_filter$read_filter %>%
dplyr::bind_rows()%>%
dplyr::pull(fwd_out)
outR <- read_filter$read_filter %>%
dplyr::bind_rows()%>%
dplyr::pull(rev_out)
# Check for empty files
outF <- outF[file.exists(outF)]
outR <- outR[file.exists(outR)]
# Return list of completed path
return(c(outF,outR))
},
pattern = map(read_filter), format="file", iteration = "vector"),
# Make temporary samdf
tar_target(temp_samdf3, {
temp_samdf2 %>%
dplyr::select(-where(is.list)) %>%
step_check_files(read_filter_path, col_name="filtered")
}),
## Pre-filtering quality plots ---------------------------------------------
# Sample a random set of 5 samples for read quality plotting
#tar_target(prefilt_read_samples,{
# group_sizes <- temp_samdf2 %>%
# dplyr::group_by(fcid, pcr_primers) %>%
# dplyr::group_size()
# if(all(group_sizes > 5)){
# n_samples <- 5
# } else {
# n_samples = min(group_sizes)
# }
# out <- temp_samdf2 %>%
# dplyr::group_by(fcid, pcr_primers) %>%
# dplyr::slice_sample(n=n_samples)
#}),
tar_target(prefilt_qualplots,
temp_samdf2 %>%
dplyr::mutate(prefilt_qualplots = purrr::pmap(list(sample_id, fcid),
.f = ~plot_read_quals(sample_id = ..1,
input_dir = paste0("data/",..2,"/01_trimmed/"),
truncLen=NULL, quiet = FALSE, n = 10000)
)),
pattern = map(temp_samdf2), iteration = "vector"),
## Write out prefilt qualplots
tar_target(write_prefilt_qualplots, {
prefilt_qualplots %>%
dplyr::group_by(fcid) %>%
tidyr::nest() %>%
purrr::pwalk(list(fcid, data),
.f = ~{
pdf(file=paste0("output/logs/",..1,"/", ..1,"_prefilt_qualplots.pdf"), width = 11, height = 8 , paper="a4r")
print(..2$prefilt_qualplots)
try(dev.off(), silent=TRUE)
})
out <- paste0("output/logs/",unique(prefilt_qualplots$fcid),"/", unique(prefilt_qualplots$fcid),"_prefilt_qualplots.pdf")
return(out)
}, format="file", iteration = "vector"),
## Post-filtering quality plots --------------------------------------------
# Get the same samples as the prefilt quality plots
#tar_target(postfilt_read_samples,{
# out <- temp_samdf3 %>%
# dplyr::group_by(fcid, pcr_primers) %>%
# filter(sample_id %in% prefilt_read_samples$sample_id)
#}),
tar_target(postfilt_qualplots,
temp_samdf3 %>%
dplyr::mutate(postfilt_qualplots = purrr::pmap(list(sample_id, fcid),
.f = ~plot_read_quals(sample_id = ..1,
input_dir = paste0("data/",..2,"/02_filtered/"), truncLen=NULL, quiet = FALSE, n = 10000)
)),
pattern = map(temp_samdf3), iteration = "vector"),
# Write out postfilt qualplots
tar_target(write_postfilt_qualplots, {
postfilt_qualplots %>%
dplyr::group_by(fcid) %>%
tidyr::nest() %>%
purrr::pwalk(list(fcid, data),
.f = ~{
pdf(file=paste0("output/logs/",..1,"/", ..1, "_postfilt_qualplots.pdf"), width = 11, height = 8 , paper="a4r")
print(..2$postfilt_qualplots)
try(dev.off(), silent=TRUE)
})
out <- paste0("output/logs/",unique(postfilt_qualplots$fcid),"/", unique(postfilt_qualplots$fcid),"_postfilt_qualplots.pdf")
return(out)
}, format="file", iteration = "vector"),
# Infer sequence variants with DADA2 --------------------------------------
# Group temporary samdf by fcid
tar_group_by(temp_samdf3_grouped, temp_samdf3, fcid, pcr_primers),
# Group temporary samdf by sample_ids
tar_group_by(temp_samdf3_grouped_sample, temp_samdf3, fcid, pcr_primers, sample_id),
# Error model for forward reads
tar_target(error_model_fwd,{
process <- temp_samdf3_grouped %>%
dplyr::group_by(fcid, pcr_primers) %>%
tidyr::nest() %>%
dplyr::mutate(error_model = purrr::pmap(dplyr::select(.,fcid, pcr_primers),
.f = ~step_errormodel(fcid = ..1,
input_dir = paste0("data/",..1,"/02_filtered"),
pcr_primers = ..2,
output = paste0("output/rds/",..1,"_",..2,"_errormodelF.rds"),
qc_dir = paste0("output/logs/",..1),
read="F",
nbases=1e+08,
randomize=FALSE,
multithread=FALSE,
quiet = FALSE,
write_all = FALSE)
))
return(paste0("output/rds/",unique(process$fcid),"_", unique(process$pcr_primers),"_errormodelF.rds"))
}, format="file", pattern = map(temp_samdf3_grouped), iteration = "vector"),
# Error model for reverse reads
tar_target(error_model_rev,{
process <- temp_samdf3_grouped %>%
dplyr::group_by(fcid, pcr_primers) %>%
tidyr::nest() %>%
dplyr::mutate(error_model = purrr::pmap(dplyr::select(.,fcid, pcr_primers),
.f = ~step_errormodel(fcid = ..1,
input_dir = paste0("data/",..1,"/02_filtered"),
pcr_primers = ..2,
output = paste0("output/rds/",..1,"_",..2,"_errormodelR.rds"),
qc_dir = paste0("output/logs/",..1),
read="R",
nbases=1e+08,
randomize=FALSE,
multithread=FALSE,
quiet = FALSE,
write_all = FALSE)
))
return(paste0("output/rds/",unique(process$fcid),"_", unique(process$pcr_primers),"_errormodelR.rds"))
}, format="file", pattern = map(temp_samdf3_grouped), iteration = "vector"),
# TODO:How to make it just redo one of the dadas if only one runs filtered file changed changed?
# first round denoising of forward reads
tar_target(denoise_fwd,{
process <- temp_samdf3_grouped_sample %>%
dplyr::group_by(fcid, pcr_primers, sample_id) %>%
tidyr::nest() %>%
dplyr::mutate(error_model = purrr::map2(fcid,pcr_primers, ~{
readRDS(error_model_fwd[stringr::str_detect(error_model_fwd, paste0(.x,"_",.y, "_errormodelF.rds"))])
})) %>%
dplyr::mutate(dada2 = purrr::pmap(dplyr::select(.,fcid, pcr_primers, error_model, sample_id),
.f = ~step_dada2_single2(fcid = ..1,
input_dir = paste0("data/",..1,"/02_filtered"),
sample_id= ..4,
pcr_primers = ..2,
output = paste0("output/rds/", ..4,"_dada1F.rds"),
qc_dir = paste0("output/logs/",..1),
read = "F",
error_model = ..3,
multithread = FALSE,
quiet = FALSE)
))
return(paste0("output/rds/", unique(process$sample_id),"_dada1F.rds"))
}, format="file", pattern = map(temp_samdf3_grouped_sample), iteration = "vector"),
# first round denoising of reverse reads
tar_target(denoise_rev,{
process <- temp_samdf3_grouped_sample %>%
dplyr::group_by(fcid, pcr_primers, sample_id) %>%
tidyr::nest() %>%
dplyr::mutate(error_model = purrr::map2(fcid,pcr_primers, ~{
readRDS(error_model_rev[stringr::str_detect(error_model_rev, paste0(.x,"_",.y, "_errormodelR.rds"))])
})) %>%
dplyr::mutate(dada2 = purrr::pmap(dplyr::select(.,fcid, pcr_primers, error_model, sample_id),
.f = ~step_dada2_single2(fcid = ..1,
input_dir = paste0("data/",..1,"/02_filtered"),
sample_id= ..4,
pcr_primers = ..2,
output = paste0("output/rds/", ..4,"_dada1R.rds"),
qc_dir = paste0("output/logs/",..1),
read = "R",
error_model = ..3,
multithread = FALSE,
quiet = FALSE)
))
return(paste0("output/rds/", unique(process$sample_id),"_dada1R.rds"))
}, format="file", pattern = map(temp_samdf3_grouped_sample), iteration = "vector"),
# Extract priors from forward reads
tar_target(priors_fwd,{
process <- temp_samdf3_grouped_sample %>%
dplyr::group_by(fcid, pcr_primers, sample_id) %>%
tidyr::nest() %>%
dplyr::mutate(priors = purrr::map(sample_id, ~{
readRDS(denoise_fwd[stringr::str_detect(denoise_fwd, paste0(.x,"_dada1F.rds"))])$sequence
}))
# Only keep the ones that appear across more than one sample
priors <- unlist(process$priors)
priors <- names(table(priors))[table(priors) > 1]
# TODO: Merge with any input_priors
saveRDS(priors, "output/rds/priorsF.rds")
return("output/rds/priorsF.rds")
}, format="file"),
# Extract priors from reverse reads
tar_target(priors_rev,{
process <- temp_samdf3_grouped_sample %>%
dplyr::group_by(fcid, pcr_primers, sample_id) %>%
tidyr::nest() %>%
dplyr::mutate(priors = purrr::map(sample_id, ~{
readRDS(denoise_rev[stringr::str_detect(denoise_rev, paste0(.x,"_dada1R.rds"))])$sequence
}))
# Only keep the ones that appear across more than one sample
priors <- unlist(process$priors)
priors <- names(table(priors))[table(priors) > 1]
# TODO: Merge with any input_priors
saveRDS(priors, "output/rds/priorsR.rds")
return("output/rds/priorsR.rds")
}, format="file"),
# Run second round of forward read denoising using priors
tar_target(denoise2_fwd,{
process <- temp_samdf3_grouped_sample %>%
dplyr::select(-one_of("high_sensitivity"))%>%
dplyr::left_join(params_dada, by="pcr_primers") %>%
dplyr::group_by(fcid, pcr_primers, sample_id, high_sensitivity) %>%
tidyr::nest() %>%
dplyr::mutate(error_model = purrr::map2(fcid,pcr_primers, ~{
readRDS(error_model_fwd[stringr::str_detect(error_model_fwd, paste0(.x,"_",.y, "_errormodelF.rds"))])
})) %>%
dplyr::mutate(priors = list(readRDS(priors_fwd))) %>%
dplyr::mutate(dada2 = purrr::pmap(dplyr::select(.,fcid, pcr_primers, error_model, sample_id, priors, high_sensitivity),
.f = ~{
if(..6){
step_dada2_single2(fcid = ..1,
input_dir = paste0("data/",..1,"/02_filtered"),
sample_id= ..4,
pcr_primers = ..2,
output = paste0("output/rds/", ..4,"_dada2F.rds"),
qc_dir = paste0("output/logs/",..1),
read = "F",
priors = ..5,
error_model = ..3,
multithread = FALSE,
quiet = FALSE)
} else {
# Just copy over
saveRDS(readRDS(denoise_fwd[stringr::str_detect(denoise_fwd, paste0("output/rds/",..4,"_dada1F.rds"))]), paste0("output/rds/",..4,"_dada2F.rds"))
}
}
))
return(paste0("output/rds/", unique(process$sample_id),"_dada2F.rds"))
}, format="file", pattern = map(temp_samdf3_grouped_sample), iteration = "vector"),
# Run second round of reverse read denoising using priors
tar_target(denoise2_rev,{
process <- temp_samdf3_grouped_sample %>%
dplyr::select(-one_of("high_sensitivity"))%>%
dplyr::left_join(params_dada, by="pcr_primers") %>%
dplyr::group_by(fcid, pcr_primers, sample_id, high_sensitivity) %>%
tidyr::nest() %>%
dplyr::mutate(error_model = purrr::map2(fcid,pcr_primers, ~{
readRDS(error_model_rev[stringr::str_detect(error_model_rev, paste0(.x,"_",.y, "_errormodelR.rds"))])
})) %>%
dplyr::mutate(priors = list(readRDS(priors_rev))) %>%
dplyr::mutate(dada2 = purrr::pmap(dplyr::select(.,fcid, pcr_primers, error_model, sample_id, priors, high_sensitivity),
.f = ~{
if(..6){
step_dada2_single2(fcid = ..1,
input_dir = paste0("data/",..1,"/02_filtered"),
sample_id= ..4,
pcr_primers = ..2,
output = paste0("output/rds/", ..4,"_dada2R.rds"),
qc_dir = paste0("output/logs/",..1),
read = "R",
priors = ..5,
error_model = ..3,
multithread = FALSE,
quiet = FALSE)
} else {
# Just copy over
saveRDS(readRDS(denoise_rev[stringr::str_detect(denoise_rev, paste0("output/rds/",..4,"_dada1R.rds"))]), paste0("output/rds/",..4,"_dada2R.rds"))
}
}
))
return(paste0("output/rds/", unique(process$sample_id),"_dada2R.rds"))
}, format="file", pattern = map(temp_samdf3_grouped_sample), iteration = "vector"),
# Merge reads and create seqtab by pcr_primers
tar_target(dada,{
process <- temp_samdf3_grouped %>%
dplyr::select(-one_of("concat_unmerged"))%>%
dplyr::left_join(params_dada, by="pcr_primers") %>%
dplyr::group_by(fcid, pcr_primers, concat_unmerged) %>%
tidyr::nest() %>%
dplyr::mutate(dada = purrr::map2(fcid, pcr_primers, ~{
fwd_to_read <- denoise2_fwd[str_detect(denoise2_fwd, paste0(.x, ".*",.y))]
dada_fwd <- fwd_to_read %>%
purrr::map(function(x){readRDS(x)})
names(dada_fwd) <- basename(fwd_to_read) %>% stringr::str_remove("_dada2F.rds")
rev_to_read <- denoise2_rev[str_detect(denoise2_rev, paste0(.x, ".*",.y))]
dada_rev <- rev_to_read %>%
purrr::map(function(x){readRDS(x)})
names(dada_rev) <- basename(rev_to_read) %>% stringr::str_remove("_dada2R.rds")
return(list(dada_fwd =dada_fwd, dada_rev = dada_rev))
})) %>%
dplyr::mutate(dada2 = purrr::pmap(dplyr::select(.,fcid, pcr_primers, concat_unmerged, dada),
.f = ~step_mergereads(fcid = ..1,
input_dir = paste0("data/",..1,"/02_filtered"),
pcr_primers = ..2,
output = paste0("output/rds/",..1,"_", ..2,"_seqtab.rds"),
qc_dir = paste0("output/logs/",..1),
quiet = FALSE,
write_all = FALSE,
concat_unmerged=..3,
dada = ..4)
))
},
pattern = map(temp_samdf3_grouped), iteration = "vector"),
# Return filepath for tracking
tar_target(dada_path,
{
return(paste0("output/rds/",unique(dada$fcid), "_", unique(dada$pcr_primers),"_seqtab.rds"))
},
pattern = map(dada), format="file", iteration = "vector"),
# Filter ASVs -------------------------------------------------
# Filter by primer
tar_target(filtered_seqtab, {
temp_samdf3_grouped %>%
dplyr::select(-one_of("asv_min_length", "asv_max_length", "phmm", "coding", "genetic_code"))%>%
dplyr::left_join(params_asvfilter, by="pcr_primers") %>%
dplyr::group_by(fcid, pcr_primers, asv_min_length, asv_max_length, phmm, coding, genetic_code, for_primer_seq, rev_primer_seq) %>%
tidyr::nest() %>%
dplyr::mutate(subset_seqtab = purrr::map2(fcid, pcr_primers, ~{
readRDS(dada_path[stringr::str_detect(dada_path, paste0(.x, ".*",.y, "_seqtab.rds"))])
})) %>%
dplyr::ungroup()%>%
dplyr::mutate(filtered_seqtab = purrr::pmap(dplyr::select(., fcid, pcr_primers, subset_seqtab, asv_min_length, asv_max_length, phmm, coding, genetic_code, for_primer_seq, rev_primer_seq),
.f = ~step_filter_asvs(
seqtab = ..3,
pcr_primers = ..2,
output = paste0("output/rds/",..1,"_", ..2,"_seqtab.cleaned.rds"),
qc_dir = "output/logs/",
min_length = ..4,
max_length = ..5,
phmm = ..6,
check_frame = ..7,
genetic_code = ..8,
primers = c(..9, ..10),
multithread = FALSE,
quiet = FALSE)
)) %>%
unnest_wider(filtered_seqtab) %>%
dplyr::mutate(filtered_asvs = purrr::map(filtered_asvs, ~{
.x %>%
dplyr::select(-sample_id) # remove sample_id from the nested column to avoid duplicate name columns
})) %>%
tidyr::unnest(c(data, filtered_asvs))%>%
dplyr::select(sample_id, sample_name, fcid, reads_starting, reads_chimerafilt, pcr_primers, reads_lengthfilt,
reads_phmmfilt, reads_framefilt, reads_final, plot, cleanup_summary)%>%
dplyr::mutate(path = paste0("output/rds/",fcid, "_", pcr_primers,"_seqtab.cleaned.rds"))
}, pattern = map(temp_samdf3_grouped), iteration = "vector"),
# Return filepath for tracking
tar_target(filtered_seqtab_path,
{
return(unique(filtered_seqtab$path))
}, format="file"),
# Write out seqtab filtering summary csv
tar_target(write_seqtab_summary, {
bind_rows(unique(filtered_seqtab$cleanup_summary)) %>%
write_csv("output/logs/ASV_cleanup_summary.csv")
out <- "output/logs/ASV_cleanup_summary.csv"
return(out)
}, format="file", iteration = "vector"),
# Write out seqtab filtering plots
tar_target(write_seqtab_qualplots, {
pdf("output/logs/ASV_cleanup_summary.pdf", width = 11, height = 8 , paper="a4r")
print(unique(filtered_seqtab$plot))
try(dev.off(), silent=TRUE)
out <- "output/logs/ASV_cleanup_summary.pdf"
return(out)
}, format="file", iteration = "vector"),
## Merge all loci into final seqtab ----------------------------------------
tar_target(merged_seqtab_path, {
process <- temp_samdf3 %>%
tidyr::nest(data=everything()) %>%
dplyr::mutate(final_seqtab = purrr::map(data, ~{
seqtabs <- filtered_seqtab_path
seqtabs <- seqtabs[seqtabs %>%
purrr::map_lgl(function(y){
any(stringr::str_detect(y, paste0(unique(.x$pcr_primers), "_seqtab.cleaned.rds")))
})]
# Remove empty seqtabs
empty <- seqtabs[purrr::map_lgl(seqtabs, function(z){
!ncol(readRDS(z)) > 0
})]
if(length(empty) > 0 ){
warning(paste0("No sequences in: ", empty, " skipping"))
}
if(length(seqtabs[!seqtabs %in% empty]) > 1){
st.all <- dada2::mergeSequenceTables(tables=seqtabs[!seqtabs %in% empty])
} else if(length(seqtabs[!seqtabs %in% empty]) == 1) {
st.all <- readRDS(seqtabs[!seqtabs %in% empty])
}
saveRDS(st.all, "output/rds/seqtab_final.rds")
return(TRUE)
})) %>%
tidyr::unnest(data)
out <- paste0("output/rds/seqtab_final.rds")
return(out)
}, format="file", iteration = "vector"),
# Assign taxonomy ---------------------------------------------------------
tar_file(idtaxa_db_tracked,{
out <- params_database %>%
dplyr::pull(idtaxa_db) %>%
unique()%>%
stringr::str_split(pattern=";", n=Inf) %>%
unlist()
return(out)
}
),
tar_file(ref_fasta_tracked,{
out <- params_database %>%
dplyr::pull(ref_fasta) %>%
unique() %>%
stringr::str_split(pattern=";", n=Inf) %>%
unlist()
return(out)
}
),
## IDTAXA -------------------------------------------------------------------
tar_target(tax_idtaxa,{
temp_samdf3_grouped %>%
dplyr::select(-one_of("target_gene", "idtaxa_db"))%>%
dplyr::left_join(params_database %>% dplyr::select(pcr_primers, target_gene, idtaxa_db, idtaxa_confidence)) %>%
tidyr::separate_rows(idtaxa_db, sep=";") %>%
dplyr::group_by(fcid, target_gene, pcr_primers, idtaxa_db, idtaxa_confidence) %>%
tidyr::nest() %>%
dplyr::mutate(idtaxa_db2 = purrr::map(idtaxa_db, ~{
idtaxa_db_tracked[stringr::str_detect(idtaxa_db_tracked, .x)]
})) %>%
unnest(idtaxa_db2)%>%
dplyr::mutate(filtered_seqtab = purrr::map2(fcid, pcr_primers, ~{
readRDS(filtered_seqtab_path[stringr::str_detect(filtered_seqtab_path, paste0(.x,"_", .y, "_seqtab.cleaned.rds"))])
})) %>%
dplyr::mutate(idtaxa = purrr::pmap(list(target_gene, pcr_primers, filtered_seqtab, idtaxa_db2, idtaxa_confidence),
.f = ~step_idtaxa(
seqtab = ..3,
database = ..4,
ranks = c("Root","Kingdom", "Phylum","Class", "Order", "Family", "Genus","Species"),
qc_dir = "output/logs/",
threshold = ..5,
multithread = FALSE,
quiet = FALSE,
return_ids = TRUE)
)) %>%
mutate(idtaxa_ids = purrr::map(idtaxa,~{
.x$ids
})) %>%
mutate(idtaxa = purrr::map(idtaxa,~{
.x$tax
}))
}, pattern = map(temp_samdf3_grouped), iteration = "vector"),
# Write out idtaxa objects
tar_target(idtaxa_path, {
process <- tax_idtaxa %>%
mutate(output = purrr::pmap(list(fcid, pcr_primers, idtaxa_db, idtaxa), ~{
# Write out RDS of the tax table
saveRDS(..4, paste0("output/rds/",..1,"_",..2,"_",basename(..3) %>% stringr::str_remove("\\..*$"),"_taxtab.rds"))
}))
out <- unique(paste0("output/rds/",process$fcid,"_",process$pcr_primers,"_",basename(process$idtaxa_db %>% stringr::str_remove("\\..*$")),"_taxtab.rds"))
return(out)
}, format="file", iteration = "vector"),
## BLAST -------------------------------------------------------------------
tar_target(tax_blast_path,
{
process <- temp_samdf3_grouped %>%
dplyr::select(-one_of("target_gene", "ref_fasta"))%>%
dplyr::left_join(params_database %>% dplyr::select(pcr_primers, target_gene, ref_fasta, blast_min_identity, blast_min_coverage, run_blast)) %>%
tidyr::separate_rows(ref_fasta, sep=";") %>%
dplyr::group_by(fcid, target_gene, pcr_primers, blast_min_identity, blast_min_coverage, ref_fasta, run_blast) %>%
tidyr::nest() %>%
dplyr::mutate(ref_fasta2 = purrr::map(ref_fasta, ~{
ref_fasta_tracked[stringr::str_detect(ref_fasta_tracked, .x)]
})) %>%
unnest(ref_fasta2) %>%
dplyr::mutate(filtered_seqtab = purrr::map2(fcid, pcr_primers, ~{
readRDS(filtered_seqtab_path[stringr::str_detect(filtered_seqtab_path, paste0(.x,"_", .y, "_seqtab.cleaned.rds"))])
})) %>%
dplyr::mutate(blast = purrr::pmap(list(fcid, target_gene, pcr_primers, filtered_seqtab, ref_fasta2, blast_min_identity, blast_min_coverage, run_blast),
.f = ~{
if(isTRUE(..8)){
blast_res <- step_blast_tophit(
seqtab = ..4,
database = ..5,
ranks = c("Root","Kingdom", "Phylum","Class", "Order", "Family", "Genus","Species") ,
output = paste0("output/rds/",..1,"_",..3,"_",basename(..5) %>% stringr::str_remove("\\..*$"),"_blast.rds"),
qc_dir = "output/logs/",
identity = ..6,
coverage=..7,
evalue=1e06,
max_target_seqs=5,
max_hsp=5,
multithread = FALSE,
quiet = FALSE)
return(blast_res)
} else {
blast_res <- tibble::enframe(getSequences(..4), name=NULL, value="OTU") %>%
dplyr::mutate(Genus = NA_character_, Species = NA_character_) %>%
column_to_rownames("OTU") %>%
as.matrix()
saveRDS(blast_res, paste0("output/rds/",..1,"_",..3,"_",basename(..5) %>% stringr::str_remove("\\..*$"),"_blast.rds"))
return(blast_res)
}
}
))
out <- unique(paste0("output/rds/",process$fcid, "_",process$pcr_primers, "_",basename(process$ref_fasta) %>% stringr::str_remove("\\..*$"),"_blast.rds"))
return(out)
}, pattern = map(temp_samdf3_grouped), format="file", iteration = "vector"),
## Aggregate taxonomic assignment methods-----------------------------------------------
tar_target(joint_tax, {
process <- temp_samdf3_grouped %>%
dplyr::select(-one_of("target_gene"))%>%
dplyr::left_join(params_database %>% dplyr::select(pcr_primers, target_gene, idtaxa_db, ref_fasta)) %>%
dplyr::group_by(fcid, pcr_primers) %>%
tidyr::nest() %>%
dplyr::mutate(filtered_seqtab = purrr::map2(fcid, pcr_primers, ~{
readRDS(filtered_seqtab_path[stringr::str_detect(filtered_seqtab_path, paste0(.x,"_", .y, "_seqtab.cleaned.rds"))])
}))%>%
dplyr::mutate(idtaxa = purrr::pmap(list(data, fcid, pcr_primers, filtered_seqtab),
.f = ~{
idtaxa_dbs <- ..1 %>%
tidyr::separate_rows(idtaxa_db, sep=";") %>%
dplyr::pull(idtaxa_db) %>%
unique() %>%
basename() %>%
stringr::str_remove("\\..*$")
# Read in idtaxa assignments - Handle multiple input dbs
taxtabs <- purrr::map(idtaxa_dbs, function(x){
idtaxa_path[stringr::str_detect(idtaxa_path, paste0(..2, "_",..3,"_", x))] %>%
readRDS()
})
# Merge multiple taxtabs if multiple databases were used - preffering assignments in the order the databases were input
# Handles maximum 3 databases currently
if(length(taxtabs) == 1){
out <- taxtabs[[1]]
} else if(length(taxtabs) == 2){
out <- coalesce_tax(taxtabs[[1]], taxtabs[[2]], prefer="left")
} else if(length(taxtabs) == 3){
temptax <- coalesce_tax(taxtabs[[1]], taxtabs[[2]], prefer="left")
out <- coalesce_tax(temptax, taxtabs[[3]], prefer="left")
}
# Check that output dimensions match input
if(!all(rownames(out) %in% colnames(..4))){
stop("Number of ASVs classified does not match the number of input ASVs")
}
return(out)
}),