annotate phyloseq_from_dada2.R @ 2:b6a38f6222af draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 4dba8c02f5b33b425d4ecd9c9f0d0eaa0a74f9bf
author iuc
date Thu, 26 Dec 2024 18:34:16 +0000
parents c0101c72b8af
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
1 #!/usr/bin/env Rscript
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
2
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
3 suppressPackageStartupMessages(library("optparse"))
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
4 suppressPackageStartupMessages(library("phyloseq"))
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
5 suppressPackageStartupMessages(library("tidyverse"))
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
6
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
7 option_list <- list(
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
8 make_option(c("--sequence_table"), action = "store", dest = "sequence_table", help = "Input sequence table"),
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
9 make_option(c("--taxonomy_table"), action = "store", dest = "taxonomy_table", help = "Input taxonomy table"),
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
10 make_option(c("--sample_table"), action = "store", default = NULL, dest = "sample_table", help = "Input sample table"),
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
11 make_option(c("--output"), action = "store", dest = "output", help = "RDS output")
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
12 )
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
13
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
14 parser <- OptionParser(usage = "%prog [options] file", option_list = option_list)
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
15 args <- parse_args(parser, positional_arguments = TRUE)
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
16 opt <- args$options
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
17 # The input sequence_table is an integer matrix
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
18 # stored as tabular (rows = samples, columns = ASVs).
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
19 seq_table_numeric_matrix <- data.matrix(read.table(opt$sequence_table, header = T, sep = "\t", row.names = 1, check.names = FALSE))
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
20 # The input taxonomy_table is a table containing
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
21 # the assigned taxonomies exceeding the minBoot
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
22 # level of bootstrapping confidence. Rows correspond
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
23 # to sequences, columns to taxonomic levels. NA
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
24 # indicates that the sequence was not consistently
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
25 # classified at that level at the minBoot threshold.
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
26 tax_table_matrix <- as.matrix(read.table(opt$taxonomy_table, header = T, sep = "\t", row.names = 1, check.names = FALSE))
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
27 # Construct a tax_table object. The rownames of
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
28 # tax_tab must match the OTU names (taxa_names)
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
29 # of the otu_table defined below.
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
30 tax_tab <- tax_table(tax_table_matrix)
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
31
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
32 # Construct an otu_table object.
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
33 otu_tab <- otu_table(seq_table_numeric_matrix, taxa_are_rows = TRUE)
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
34
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
35 # Construct a phyloseq object.
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
36 phyloseq_obj <- phyloseq(otu_tab, tax_tab)
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
37 if (!is.null(opt$sample_table)) {
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
38 sample_tab <- sample_data(
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
39 read.table(opt$sample_table, header = T, sep = "\t", row.names = 1, check.names = FALSE)
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
40 )
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
41 phyloseq_obj <- merge_phyloseq(phyloseq_obj, sample_tab)
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
42 }
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
43
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
44 # use short names for our ASVs and save the ASV sequences
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
45 # refseq slot of the phyloseq object as described in
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
46 # https://benjjneb.github.io/dada2/tutorial.html
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
47 dna <- Biostrings::DNAStringSet(taxa_names(phyloseq_obj))
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
48 names(dna) <- taxa_names(phyloseq_obj)
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
49 phyloseq_obj <- merge_phyloseq(phyloseq_obj, dna)
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
50 taxa_names(phyloseq_obj) <- paste0("ASV", seq(ntaxa(phyloseq_obj)))
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
51
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
52 print(phyloseq_obj)
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
53
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
54 # save R object to file
c0101c72b8af planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 5ec9f9e81bb9a42dec5c331dd23215ca0b027b2b
iuc
parents:
diff changeset
55 saveRDS(phyloseq_obj, file = opt$output, compress = TRUE)