|
| 1 | +#!/usr/bin/env Rscript |
| 2 | +suppressPackageStartupMessages(library(dada2)) |
| 3 | +suppressPackageStartupMessages(library(optparse)) |
| 4 | + |
| 5 | +option_list <- list( |
| 6 | + make_option("--seqtab", type = "character", help = "Input readmap RDS (with id and seq columns)"), |
| 7 | + make_option("--ref", type = "character", help = "Reference FASTA for assignTaxonomy"), |
| 8 | + make_option("--species_ref", type = "character", default = "null", |
| 9 | + help = "Reference FASTA for addSpecies(); omit or 'null' to skip species assignment"), |
| 10 | + make_option("--tax_batch", type = "integer", default = 0, |
| 11 | + help = "Batch size for taxonomy assignment (0 = no batching) [default %default]"), |
| 12 | + make_option("--min_boot", type = "integer", default = 50, |
| 13 | + help = "Minimum bootstrap confidence for taxonomy assignment [default %default]"), |
| 14 | + make_option("--ncpus", type = "integer", default = 1, |
| 15 | + help = "Number of processors to use [default %default]") |
| 16 | +) |
| 17 | + |
| 18 | +opt <- parse_args(OptionParser(option_list = option_list)) |
| 19 | +for (arg in c("seqtab", "ref")) { |
| 20 | + if (is.null(opt[[arg]])) stop(paste("--", arg, " is required", sep = "")) |
| 21 | +} |
| 22 | + |
| 23 | +runSpecies <- !is.null(opt$species_ref) && opt$species_ref != "null" |
| 24 | + |
| 25 | +seqs <- readRDS(opt$seqtab) |
| 26 | +seqtab <- seqs$seq |
| 27 | + |
| 28 | +# Assign taxonomy |
| 29 | +tax <- NULL |
| 30 | +boots <- NULL |
| 31 | + |
| 32 | +if (opt$tax_batch == 0 | length(seqtab) < opt$tax_batch) { # no batch, run normally |
| 33 | + cat("Running all samples\n") |
| 34 | + tax <- assignTaxonomy(seqtab, opt$ref, |
| 35 | + multithread = opt$ncpus, |
| 36 | + tryRC = TRUE, |
| 37 | + outputBootstraps = TRUE, |
| 38 | + minBoot = opt$min_boot, |
| 39 | + verbose = TRUE) |
| 40 | + boots <- tax$boot |
| 41 | + if (runSpecies) { |
| 42 | + tax <- addSpecies(tax$tax, opt$species_ref, tryRC = TRUE, verbose = TRUE) |
| 43 | + } else { |
| 44 | + tax <- tax$tax |
| 45 | + } |
| 46 | +} else { |
| 47 | + # see https://github.com/benjjneb/dada2/issues/1429 for this |
| 48 | + to_split <- seq(1, length(seqtab), by = opt$tax_batch) |
| 49 | + to_split2 <- c(to_split[2:length(to_split)] - 1, length(seqtab)) |
| 50 | + |
| 51 | + for (i in 1:length(to_split)) { |
| 52 | + cat(paste("Running all samples from", to_split[i], "to", to_split2[i], "\n")) |
| 53 | + seqtab2 <- seqtab[to_split[i]:to_split2[i]] |
| 54 | + tax2 <- assignTaxonomy(seqtab2, opt$ref, |
| 55 | + multithread = opt$ncpus, |
| 56 | + tryRC = TRUE, |
| 57 | + outputBootstraps = TRUE, |
| 58 | + minBoot = opt$min_boot, |
| 59 | + verbose = TRUE) |
| 60 | + |
| 61 | + if (is.null(boots)) { |
| 62 | + boots <- tax2$boot |
| 63 | + } else { |
| 64 | + boots <- rbind(boots, tax2$boot) |
| 65 | + } |
| 66 | + |
| 67 | + if (runSpecies) { |
| 68 | + tax2 <- addSpecies(tax2$tax, |
| 69 | + refFasta = opt$species_ref, |
| 70 | + tryRC = TRUE, |
| 71 | + verbose = TRUE) |
| 72 | + } else { |
| 73 | + tax2 <- tax2$tax |
| 74 | + } |
| 75 | + if (is.null(tax)) { |
| 76 | + tax <- tax2 |
| 77 | + } else { |
| 78 | + tax <- rbind(tax, tax2) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +# make sure these are the same order |
| 84 | +rownames(tax) <- seqs[rownames(tax), ]$id |
| 85 | +rownames(boots) <- seqs[rownames(boots), ]$id |
| 86 | + |
| 87 | +# Write original data |
| 88 | +saveRDS(tax, "taxtab.original.RDS") |
| 89 | +saveRDS(boots, "bootstraps.original.RDS") |
0 commit comments