diff --git a/NAMESPACE b/NAMESPACE index d392d1279..6914af3d3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,6 +29,10 @@ export(calculateOverlap) export(calculateRDA) export(calculateUnifrac) export(cluster) +export(convertFromBIOM) +export(convertFromDADA2) +export(convertFromPhyloseq) +export(convertToPhyloseq) export(countDominantFeatures) export(countDominantTaxa) export(estimateDivergence) @@ -158,6 +162,7 @@ exportMethods(calculateOverlap) exportMethods(calculateUnifrac) exportMethods(checkTaxonomy) exportMethods(cluster) +exportMethods(convertToPhyloseq) exportMethods(countDominantFeatures) exportMethods(countDominantTaxa) exportMethods(estimateDivergence) diff --git a/NEWS b/NEWS index 13f930df9..2fe5f540c 100644 --- a/NEWS +++ b/NEWS @@ -135,7 +135,10 @@ Changes in version 1.13.x includes the newly added subsampled assay. + Fix bug in mergeFeaturesByPrevalence + new aliases calculateDPCoA to getDPCoA, calculateNMDS to getNMDS, calculateRDA to getRDA, -calculateCCA to getCCA + calculateCCA to getCCA ++ add informative error message in rarefyAssay on assays with strictly-negative values + add informative error message in rarefyAssay on assays with strictly-negative values + Use rbiom package in unifrac implementation + Updated parameter names to follow naming convention "parameter.name" ++ rename makeTreeSEFrom* and makePhyloseqFromTreeSE functions and combine their + documentation under new manual page convert diff --git a/R/makeTreeSummarizedExperimentFromBiom.R b/R/convertFromBIOM.R similarity index 84% rename from R/makeTreeSummarizedExperimentFromBiom.R rename to R/convertFromBIOM.R index 0f385ae33..7003c32bf 100644 --- a/R/makeTreeSummarizedExperimentFromBiom.R +++ b/R/convertFromBIOM.R @@ -1,10 +1,16 @@ -#' Loading a biom file +#' Converters #' -#' For convenience a few functions are available to convert data from a -#' \sQuote{biom} file or object into a +#' For convenience a few functions are available to convert BIOM, DADA2 and +#' phyloseq objects to #' \code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +#' objects and +#' \code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +#' objects to phyloseq objects. #' -#' @param file biom file location +#' @param file BIOM file location +#' +#' @param obj BIOM object to be converted to a +#' \code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} #' #' @param prefix.rm \code{TRUE} or \code{FALSE}: Should #' taxonomic prefixes be removed? The prefixes is removed only from detected @@ -27,22 +33,31 @@ #' #' @param ... additional arguments #' \itemize{ -#' \item \code{patter}: \code{character} value specifying artifacts +#' \item{\code{pattern}}{\code{character} value specifying artifacts #' to be removed. If \code{patterns = "auto"}, special characters -#' are removed. (default: \code{pattern = "auto"}) +#' are removed. (default: \code{pattern = "auto"}) +#' } #' } #' -#' @return An object of class +#' @details +#' \code{convertFromBIOM} coerces a BIOM object to a +#' \code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +#' object. +#' +#' \code{importBIOM} loads a BIOM file and creates a +#' \code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +#' object from the BIOM object contained in the loaded file +#' @return +#' \code{importBIOM} returns an object of class +#' \code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +#' +#' \code{convertFromBIOM} returns an object of class #' \code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} #' -#' @name makeTreeSEFromBiom -#' @seealso -#' \code{\link[=makeTreeSEFromPhyloseq]{makeTreeSEFromPhyloseq}} -#' \code{\link[=makeTreeSEFromDADA2]{makeTreeSEFromDADA2}} -#' \code{\link[=importQIIME2]{importQIIME2}} -#' \code{\link[=importMothur]{importMothur}} +#' @name convert #' #' @examples +#' ### Load and convert BIOM results to a TreeSE #' # Load biom file #' library(biomformat) #' biom_file <- system.file("extdata", "rich_dense_otu_table.biom", @@ -53,7 +68,7 @@ #' #' # Make TreeSE from biom object #' biom_object <- biomformat::read_biom(biom_file) -#' tse <- makeTreeSEFromBiom(biom_object) +#' tse <- convertFromBIOM(biom_object) #' #' # Get taxonomyRanks from prefixes and remove prefixes #' tse <- importBIOM(biom_file, @@ -69,31 +84,30 @@ #' artifact.rm = TRUE) NULL -#' @rdname makeTreeSEFromBiom -#' +#' \code{importBIOM} loads a BIOM file and creates a TreeSE from the BIOM object +#' contained in the loaded file. +#' @rdname convert #' @export importBIOM <- function(file, ...) { .require_package("biomformat") biom <- biomformat::read_biom(file) - makeTreeSEFromBiom(biom, ...) + convertFromBIOM(biom,...) } -#' @rdname makeTreeSEFromBiom -#' -#' @param x object of type \code{\link[biomformat:read_biom]{biom}} -#' -#' @export +#' \code{convertFromBIOM} creates a TreeSE object from a BIOM object. #' @importFrom S4Vectors make_zero_col_DFrame DataFrame #' @importFrom dplyr %>% bind_rows -makeTreeSEFromBiom <- function( - x, prefix.rm = removeTaxaPrefixes, +#' @rdname convert +#' @export +convertFromBIOM <- function( + obj, prefix.rm = removeTaxaPrefixes, removeTaxaPrefixes = FALSE, rank.from.prefix = rankFromPrefix, rankFromPrefix = FALSE, artifact.rm = remove.artifacts, remove.artifacts = FALSE, ...){ # input check .require_package("biomformat") - if(!is(x,"biom")){ - stop("'x' must be a 'biom' object", call. = FALSE) + if(!is(obj,"biom")){ + stop("'obj' must be a 'biom' object", call. = FALSE) } if( !.is_a_bool(prefix.rm) ){ stop("'prefix.rm' must be TRUE or FALSE.", call. = FALSE) @@ -105,9 +119,9 @@ makeTreeSEFromBiom <- function( stop("'artifact.rm' must be TRUE or FALSE.", call. = FALSE) } # - counts <- as(biomformat::biom_data(x), "matrix") - sample_data <- biomformat::sample_metadata(x) - feature_data <- biomformat::observation_metadata(x) + counts <- as(biomformat::biom_data(obj), "matrix") + sample_data <- biomformat::sample_metadata(obj) + feature_data <- biomformat::observation_metadata(obj) # colData is initialized with empty tables with rownames if it is NULL if( is.null(sample_data) ){ @@ -185,8 +199,8 @@ makeTreeSEFromBiom <- function( } # Adjust row and colnames - rownames(counts) <- rownames(feature_data) <- biomformat::rownames(x) - colnames(counts) <- rownames(sample_data) <- biomformat::colnames(x) + rownames(counts) <- rownames(feature_data) <- biomformat::rownames(obj) + colnames(counts) <- rownames(sample_data) <- biomformat::colnames(obj) # Convert into DataFrame sample_data <- DataFrame(sample_data) @@ -202,14 +216,6 @@ makeTreeSEFromBiom <- function( return(tse) } -####################### makeTreeSummarizedExperimentFromBiom ################### -#' @param x object of type \code{\link[biomformat:read_biom]{biom}} -#' @rdname makeTreeSEFromBiom -#' @export -makeTreeSummarizedExperimentFromBiom <- function(x, ...){ - makeTreeSEFromBiom(x, ...) -} - ################################ HELP FUNCTIONS ################################ # This function removes prefixes from taxonomy names .remove_prefixes_from_taxa <- function( diff --git a/R/makeTreeSummarizedExperimentFromDADA2.R b/R/convertFromDADA2.R similarity index 53% rename from R/makeTreeSummarizedExperimentFromDADA2.R rename to R/convertFromDADA2.R index c026c5d24..bd39333d1 100644 --- a/R/makeTreeSummarizedExperimentFromDADA2.R +++ b/R/convertFromDADA2.R @@ -1,44 +1,41 @@ #' Coerce \sQuote{DADA2} results to \code{TreeSummarizedExperiment} #' -#' \code{makeTreeSEFromDADA2} is a wrapper for the -#' \code{mergePairs} function from the \code{dada2} package. -#' -#' @param ... See \code{mergePairs} function for -#' more details. +#' @param ... Addional arguments. For \code{convertFromDADA2}, see +#' \code{mergePairs} function for more details. #' #' @details -#' A count matrix is constructed via \code{makeSequenceTable(mergePairs(...))} -#' and rownames are dynamically created as \code{ASV(N)} with \code{N} from -#' 1 to \code{nrow} of the count tables. The colnames and rownames from the -#' output of \code{makeSequenceTable} are stored as \code{colnames} and in the -#' \code{referenceSeq} slot of the \code{TreeSummarizedExperiment}, -#' respectively. +#' \code{convertFromDADA2} is a wrapper for the +#' \code{mergePairs} function from the \code{dada2} package. +#' A count matrix is constructed via +#' \code{makeSequenceTable(mergePairs(...))} and rownames are dynamically +#' created as \code{ASV(N)} with \code{N} from 1 to \code{nrow} of the count +#' tables. The colnames and rownames from the output of \code{makeSequenceTable} +#' are stored as \code{colnames} and in the \code{referenceSeq} slot of the +#' \code{TreeSummarizedExperiment},respectively. #' -#' @return An object of class \code{TreeSummarizedExperiment} +#' @return \code{convertFromDADA2} returns an object of class +#' \code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} #' #' @importFrom S4Vectors SimpleList #' @importFrom Biostrings DNAStringSet #' -#' @name makeTreeSEFromDADA2 -#' @seealso -#' \code{\link[=makeTreeSEFromPhyloseq]{makeTreeSEFromPhyloseq}} -#' \code{\link[=makeTreeSEFromBiom]{makeTreeSEFromBiom}} -#' \code{\link[=importQIIME2]{importQIIME2}} -#' \code{\link[=importMothur]{importMothur}} +#' @rdname convert #' #' @export #' #' @examples +#' +#' ### Coerce DADA2 results to a TreeSE object #' if(requireNamespace("dada2")) { #' fnF <- system.file("extdata", "sam1F.fastq.gz", package="dada2") #' fnR = system.file("extdata", "sam1R.fastq.gz", package="dada2") #' dadaF <- dada2::dada(fnF, selfConsist=TRUE) #' dadaR <- dada2::dada(fnR, selfConsist=TRUE) #' -#' tse <- makeTreeSEFromDADA2(dadaF, fnF, dadaR, fnR) +#' tse <- convertFromDADA2(dadaF, fnF, dadaR, fnR) #' tse #' } -makeTreeSEFromDADA2 <- function(...) { +convertFromDADA2 <- function(...) { # input checks .require_package("dada2") .require_package("stringr") @@ -62,12 +59,3 @@ makeTreeSEFromDADA2 <- function(...) { rownames(output) <- rName output } - -#################### makeTreeSummarizedExperimentFromDADA2 ##################### -#' @param ... See \code{mergePairs} function for -#' more details. -#' @name makeTreeSEFromDADA2 -#' @export -makeTreeSummarizedExperimentFromDADA2 <- function(...) { - makeTreeSEFromDADA2(...) -} diff --git a/R/convertFromPhyloseq.R b/R/convertFromPhyloseq.R new file mode 100644 index 000000000..1753798a5 --- /dev/null +++ b/R/convertFromPhyloseq.R @@ -0,0 +1,77 @@ +#' @param phy a \code{phyloseq} object +#' +#' @details +#' \code{convertFromPhyloseq} converts \code{phyloseq} +#' objects into +#' \code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} objects. +#' All data stored in a \code{phyloseq} object is transferred. +#' +#' @return +#' \code{convertFromPhyloseq} returns an object of class +#' \code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +#' +#' @importFrom S4Vectors SimpleList DataFrame make_zero_col_DFrame +#' @importFrom SummarizedExperiment colData colData<- +#' +#' @export +#' +#' @rdname convert +#' @seealso +#' \code{\link[=importQIIME2]{importQIIME2}} +#' \code{\link[=importMothur]{importMothur}} +#' +#' @examples +#' +#' ### Coerce a phyloseq object to a TreeSE object +#' if (requireNamespace("phyloseq")) { +#' data(GlobalPatterns, package="phyloseq") +#' convertFromPhyloseq(GlobalPatterns) +#' data(enterotype, package="phyloseq") +#' convertFromPhyloseq(enterotype) +#' data(esophagus, package="phyloseq") +#' convertFromPhyloseq(esophagus) +#' } +convertFromPhyloseq <- function(phy, ...) { + # input check + .require_package("phyloseq") + if(!is(phy,"phyloseq")){ + stop("'phy' must be a 'phyloseq' object") + } + # + # Get the assay + counts <- phy@otu_table@.Data + # Check the orientation, and transpose if necessary + if( !phy@otu_table@taxa_are_rows ){ + counts <- t(counts) + } + # Create a list of assays + assays <- SimpleList(counts = counts) + + if(!is.null(phy@tax_table@.Data)){ + rowData <- DataFrame(data.frame(phy@tax_table@.Data)) + } else{ + rowData <- S4Vectors::make_zero_col_DFrame(nrow(assays$counts)) + rownames(rowData) <- rownames(assays$counts) + } + if(!is.null(phy@sam_data)){ + colData <- DataFrame(data.frame(phy@sam_data)) + } else{ + colData <- S4Vectors::make_zero_col_DFrame(ncol(assays$counts)) + rownames(colData) <- colnames(assays$counts) + } + if(!is.null(phy@phy_tree)){ + rowTree <- phy@phy_tree + } else { + rowTree <- NULL + } + if (!is.null(phy@refseq)) { + referenceSeq <- phy@refseq + } else { + referenceSeq <- NULL + } + TreeSummarizedExperiment(assays = assays, + rowData = rowData, + colData = colData, + rowTree = rowTree, + referenceSeq = referenceSeq) +} diff --git a/R/convertToPhyloseq.R b/R/convertToPhyloseq.R new file mode 100644 index 000000000..1240140ff --- /dev/null +++ b/R/convertToPhyloseq.R @@ -0,0 +1,264 @@ +#' Create a phyloseq object from a TreeSummarizedExperiment object +#' +#' @param x a +#' \code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +#' object +#' +#' @param assay.type A single character value for selecting the +#' \code{\link[SummarizedExperiment:SummarizedExperiment-class]{assay}} to be +#' included in the phyloseq object that is created. +#' (By default: \code{assay.type = "counts"}) +#' +#' @param assay_name a single \code{character} value for specifying which +#' assay to use for calculation. +#' (Please use \code{assay.type} instead. At some point \code{assay_name} +#' will be disabled.) +#' +#' @param tree.name a single \code{character} value for specifying which +#' tree will be included in the phyloseq object that is created, +#' (By default: \code{tree.name = "phylo"}) +#' +#' @param tree_name a single \code{character} value for specifying which +#' tree will be included in the phyloseq object that is created, +#' (By default: \code{tree_name = "phylo"}) +#' \code{tree_name} will be deprecated. Use \code{tree.name} instead. +#' +#' @details +#' \code{convertToPhyloseq} creates a phyloseq object from a +#' \code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +#' object. By using \code{assay.type}, it is possible to specify which table +#' from \code{assay} is added to the phyloseq object. +#' +#' @return +#' \code{convertToPhyloseq} returns an object of class +#' \code{\link[phyloseq:phyloseq-class]{phyloseq}} +#' +#' @author Leo Lahti and Tuomas Borman. Contact: \url{microbiome.github.io} +#' +#' @examples +#' +#' ### Coerce a TreeSE object to a phyloseq object +#' # Get tse object +#' data(GlobalPatterns) +#' tse <- GlobalPatterns +#' +#' # Create a phyloseq object from it +#' phy <- convertToPhyloseq(tse) +#' phy +#' +#' # By default the chosen table is counts, but if there are other tables, +#' # they can be chosen with assay.type. +#' +#' # Counts relative abundances table +#' tse <- transformAssay(tse, method = "relabundance") +#' phy2 <- convertToPhyloseq(tse, assay.type = "relabundance") +#' phy2 +#' @rdname convert +#' @export +setGeneric("convertToPhyloseq", signature = c("x"), + function(x, ...) + standardGeneric("convertToPhyloseq")) + +#' @rdname convert +#' @export +setMethod("convertToPhyloseq", + signature = c(x = "SummarizedExperiment"), + function(x, assay.type = "counts", assay_name = NULL, ...){ + # Input check + .require_package("phyloseq") + # Check that tse do not have zero rows + if(!all(dim(x) > 0)){ + stop("'x' contains zero rows. 'x' can not be converted + to a phyloseq object.", + call. = FALSE) + } + + if (!is.null(assay_name)) { + .Deprecated(old="assay_name", new="assay.type", "Now assay_name is deprecated. Use assay.type instead.") + } + + # Check assay.type + .check_assay_present(assay.type, x) + + # phyloseq object requires nonduplicated rownames. If there are + # duplicated rownames, they are converted so that they are unique + if( any(duplicated(rownames(x))) ){ + rownames(x) <- getTaxonomyLabels(x) + } + # List of arguments + args = list() + # Gets the abundance data from assay, and converts it to otu_table + otu_table <- as.matrix(assay(x, assay.type)) + otu_table <- phyloseq::otu_table(otu_table, taxa_are_rows = TRUE) + # Adds to the list + args[["otu_table"]] <- otu_table + + # If rowData includes information + if(!( length(rowData(x)[,taxonomyRanks(x)]) == 0 || + is.null((rowData(x)[,taxonomyRanks(x)])) )){ + # Converts taxonomy table to characters if it's not already + rowData(x) <- DataFrame(lapply(rowData(x), as.character)) + # Gets the taxonomic data from rowData, and converts it to tax_table + tax_table <- as.matrix(rowData(x)[,taxonomyRanks(x),drop=FALSE]) + tax_table <- phyloseq::tax_table(tax_table) + # Adds to the list + args[["tax_table"]] <- tax_table + } + + # If colData includes information + if(!( length(colData(x)) == 0 || is.null(ncol(colData(x))) )){ + # Gets the feature_data from colData and converts it to sample_data + sample_data <- as.data.frame(colData(x)) + sample_data <- phyloseq::sample_data(sample_data) + # Adds to the list + args[["sample_data"]] <- sample_data + } + + # Creates a phyloseq object + phyloseq <- do.call(phyloseq::phyloseq, args) + return(phyloseq) + } +) + +#' @rdname convert +#' @export +setMethod("convertToPhyloseq", + signature = c(x = "TreeSummarizedExperiment"), + function(x, tree.name = tree_name, tree_name = "phylo", ...){ + # If rowTrees exist, check tree.name + if( length(x@rowTree) > 0 ){ + .check_rowTree_present(tree.name, x) + # Subset the data based on the tree + x <- x[ rowLinks(x)$whichTree == tree.name, ] + add_phy_tree <- TRUE + } else{ + add_phy_tree <- FALSE + } + # + + # phyloseq and tree objects require nonduplicated rownames. If there are + # duplicated rownames, they are converted so that they are unique + if( any(duplicated(rownames(x))) ){ + rownames(x) <- getTaxonomyLabels(x) + } + # Gets otu_table object from the function above this, if tse contains + # only abundance table. + # Otherwise, gets a phyloseq object with otu_table, and tax_table + # and/or sample_data + obj <- callNextMethod() + # List of arguments + args = list() + # Adds to the list of arguments, if 'obj' is not a phyloseq object + # i.e. is an otu_table + if(!is(obj,"phyloseq")){ + # Adds otu_table to the list + args[["otu_table"]] <- obj + } + + # Add phylogenetic tree + if( add_phy_tree ){ + phy_tree <- .get_rowTree_for_phyloseq(x, tree.name) + # If the object is a phyloseq object, adds phy_tree to it + if(is(obj,"phyloseq")){ + phyloseq::phy_tree(obj) <- phy_tree + } else{ + # Adds to the list + args[["phy_tree"]] <- phy_tree + } + } + + # If referenceSeq has information, stores it to refseq and converts is + # to phyloseq's refseq. + if( !is.null(referenceSeq(x)) ){ + # Get referenceSeqs + refseq <- .get_referenceSeq_for_phyloseq(x, ...) + # IF refSeq passed the test, add it + if( !is.null(refseq) ){ + # Convert it to phyloseq object + refseq <- phyloseq::refseq(refseq) + # If the object is a phyloseq object, adds refseq to it + if(is(obj,"phyloseq")){ + obj <- phyloseq::merge_phyloseq(obj, refseq) + } else{ + # Adds to the list + args[["refseq"]] <- refseq + } + } + } + + # If 'obj' is not a phyloseq object, creates one. + if(!is(obj,"phyloseq")){ + # Creates a phyloseq object + phyloseq <- do.call(phyloseq::phyloseq, args) + } else{ + phyloseq <- obj + } + phyloseq + } +) + + +################################ HELP FUNCTIONS ################################ +# If tips do not match with rownames, prune the tree +.get_x_with_pruned_tree <- function(x, tree.name){ + # Get rowLinks + row_links <- rowLinks(x) + # Gets node labels + node_labs <- row_links[ , "nodeLab"] + # Prunes the tree + tree_pruned <- ape::keep.tip(rowTree(x), node_labs) + # Replace tip labels with corresponding rownames + tree_pruned$tip.label <- rownames(x) + # Assigns the pruned tree back to TSE object + rowTree(x) <- tree_pruned + warning("Tips of rowTree are renamed to match rownames.", call. = FALSE) + return(x) +} + +# In phyloseq, tips and rownames must match +.get_rowTree_for_phyloseq <- function(x, tree.name){ + # Check if the rowTree's tips match with rownames: + # tips labels are found from rownames + if( any(!( rowTree(x, tree.name)$tip.label) %in% rownames(x)) ){ + # If rowtree do not match, tree is pruned + x <- .get_x_with_pruned_tree(x, tree.name) + } + # Get rowTree + phy_tree <- rowTree(x, tree.name) + # Convert rowTree to phyloseq object + phy_tree <- phyloseq::phy_tree(phy_tree) + + return(phy_tree) +} + +.get_referenceSeq_for_phyloseq <- function(x, referenceSeq = 1, ...){ + # Get reference seqs + refSeqs <- referenceSeq(x) + # Is referenceSeq a list / does it contain multiple DNA sets + is_list <- is(refSeqs, "DNAStringSetList") + + # Take only one set, if it is a list + if( is_list ){ + # Check referenceSeq + if( !( (.is_non_empty_string(referenceSeq) && referenceSeq %in% names(refSeqs)) || + (.is_an_integer(referenceSeq) && (referenceSeq>0 && referenceSeq<=length(refSeqs))) ) + ){ + stop("'referenceSeq' must be a non-empty single character value or an integer ", + "specifying the DNAStringSet from DNAStringSetList.", + call. = FALSE) + } + # Get specified referenceSeq + refSeqs <- refSeqs[[referenceSeq]] + warning("Use 'referenceSeq' to specify DNA set from DNAStringSetList. ", + "Current choice is '", referenceSeq, "'.", + call. = FALSE) + } + # Check if all rownames have referenceSeqs + if( !(all(rownames(x) %in% names(refSeqs)) && + all(names(refSeqs) %in% rownames(x) )) ){ + warning("referenceSeq does not match with rownames so they are discarded.", + call. = FALSE) + refSeqs <- NULL + } + return(refSeqs) +} diff --git a/R/deprecate.R b/R/deprecate.R index 520620661..e1312dd30 100644 --- a/R/deprecate.R +++ b/R/deprecate.R @@ -1,8 +1,6 @@ #' These functions will be deprecated. Please use other functions instead. #' -#' @param x A -#' \code{\link[SummarizedExperiment:SummarizedExperiment-class]{SummarizedExperiment}} -#' object. +#' @param x an object #' #' @param ... Additional parameters. See dedicated function. #' @@ -59,6 +57,23 @@ setMethod("taxonomyTree", signature = c(x = "SummarizedExperiment"), } ) +#' @rdname deprecate +#' @export +setGeneric("makeTreeSummarizedExperimentFromPhyloseq", signature = c("x"), + function(x) + standardGeneric("makeTreeSummarizedExperimentFromPhyloseq")) + +#' @rdname deprecate +#' @export +setMethod("makeTreeSummarizedExperimentFromPhyloseq", + signature = c(x = "ANY"), function(x){ + .Deprecated(msg = paste0( + "'makeTreeSummarizedExperimentFromPhyloseq' is deprecated.", + " Use 'convertFromPhyloseq' instead.")) + convertFromPhyloseq(x) + } +) + #' @rdname deprecate #' @export setGeneric("mergeRows", @@ -79,6 +94,40 @@ setMethod("mergeRows", signature = c(x = "SummarizedExperiment"), #' @rdname deprecate #' @export +setGeneric("makePhyloseqFromTreeSummarizedExperiment", signature = c("x"), + function(x, ...) + standardGeneric("makePhyloseqFromTreeSummarizedExperiment")) + +#' @rdname deprecate +#' @export +setMethod("makePhyloseqFromTreeSummarizedExperiment", + signature = c(x = "ANY"), function(x, ...){ + .Deprecated(msg = paste0( + "'makePhyloseqFromTreeSummarizedExperiment' is deprecated.", + " Use 'convertToPhyloseq' instead.")) + convertToPhyloseq(x, ...) + } +) + +#' @rdname deprecate +#' @export +setGeneric("makePhyloseqFromTreeSE", signature = c("x"), + function(x, ...) + standardGeneric("makePhyloseqFromTreeSE")) + +#' @rdname deprecate +#' @export +setMethod("makePhyloseqFromTreeSE", + signature = c(x = "ANY"), function(x, ...){ + .Deprecated(msg = paste0( + "'makePhyloseqFromTreeSE' is deprecated.", + " Use 'convertToPhyloseq' instead.")) + convertToPhyloseq(x, ...) + } +) + +#' @rdname deprecate +#' @export setMethod("mergeRows", signature = c(x = "TreeSummarizedExperiment"), function(x, ...){ .Deprecated(msg = paste0("'mergeRows' is deprecated. ", @@ -222,6 +271,51 @@ setMethod("mergeFeaturesByPrevalence", signature = c(x = "SummarizedExperiment") } ) +#' @rdname deprecate +#' @export +makeTreeSEFromBiom <- function(...){ + .Deprecated(msg = paste0( + "'makeTreeSEFromBiom' is deprecated.", + " Use 'convertFromBIOM' instead.")) + convertFromBIOM(...) +} + +#' @rdname deprecate +#' @export +makeTreeSummarizedExperimentFromBiom <- function(...){ + .Deprecated(msg = paste0( + "'makeTreeSummarizedExperimentFromBiom' is deprecated.", + " Use 'convertFromBIOM' instead.")) + convertFromBIOM(...) +} + +#' @rdname deprecate +#' @export +makeTreeSEFromDADA2 <- function(...) { + .Deprecated(msg = paste0( + "'makeTreeSEFromDADA2' is deprecated.", + " Use 'convertFromDADA2' instead.")) + convertFromDADA2(...) +} + +#' @rdname deprecate +#' @export +makeTreeSummarizedExperimentFromDADA2 <- function(...) { + .Deprecated(msg = paste0( + "'makeTreeSummarizedExperimentFromDADA2' is deprecated.", + " Use 'convertFromDADA2' instead.")) + convertFromDADA2(...) +} + +#' @rdname deprecate +#' @export +makeTreeSEFromPhyloseq <- function(x) { + .Deprecated(msg = paste0( + "'makeTreeSEFromPhyloseq' is deprecated.", + " Use 'convertFromPhyloseq' instead.")) + convertFromPhyloseq(...) +} + #' @rdname deprecate #' @export setGeneric("getExperimentCrossAssociation", signature = c("x"), diff --git a/R/importHumann.R b/R/importHumann.R index a35e2c1a1..3b1173f4c 100644 --- a/R/importHumann.R +++ b/R/importHumann.R @@ -47,11 +47,11 @@ #' #' @seealso #' \code{\link[=importMetaPhlAn]{importMetaPhlAn}} -#' \code{\link[=makeTreeSEFromPhyloseq]{makeTreeSEFromPhyloseq}} -#' \code{\link[=makeTreeSEFromBiom]{makeTreeSEFromBiom}} -#' \code{\link[=makeTreeSEFromDADA2]{makeTreeSEFromDADA2}} #' \code{\link[=importQIIME2]{importQIIME2}} #' \code{\link[=importMothur]{importMothur}} +#' \code{\link[=convert]{convertFromDADA2}} +#' \code{\link[=convert]{convertFromPhyloseq}} +#' \code{\link[=convert]{convertFromBIOM}} #' #' @export #' @author Leo Lahti and Tuomas Borman. Contact: \url{microbiome.github.io} diff --git a/R/importMetaphlan.R b/R/importMetaphlan.R index cd95b14fb..a3aca5022 100644 --- a/R/importMetaphlan.R +++ b/R/importMetaphlan.R @@ -60,11 +60,11 @@ #' @name importMetaPhlAn #' @seealso #' \code{\link[=importHUMAnN]{importHUMAnN}} -#' \code{\link[=makeTreeSEFromPhyloseq]{makeTreeSEFromPhyloseq}} -#' \code{\link[=makeTreeSEFromBiom]{makeTreeSEFromBiom}} -#' \code{\link[=makeTreeSEFromDADA2]{makeTreeSEFromDADA2}} #' \code{\link[=importQIIME2]{importQIIME2}} #' \code{\link[=importMothur]{importMothur}} +#' \code{\link[=convert]{convertFromDADA2}} +#' \code{\link[=convert]{convertFromPhyloseq}} +#' \code{\link[=convert]{convertFromBIOM}} #' #' @export #' @author Leo Lahti and Tuomas Borman. Contact: \url{microbiome.github.io} diff --git a/R/importMothur.R b/R/importMothur.R index 83e4256f2..f3c993a63 100644 --- a/R/importMothur.R +++ b/R/importMothur.R @@ -35,9 +35,9 @@ #' #' @name importMothur #' @seealso -#' \code{\link[=makeTreeSEFromPhyloseq]{makeTreeSEFromPhyloseq}} -#' \code{\link[=makeTreeSEFromBiom]{makeTreeSEFromBiom}} -#' \code{\link[=makeTreeSEFromDADA2]{makeTreeSEFromDADA2}} +#' \code{\link[=convert]{convertFromDADA2}} +#' \code{\link[=convert]{convertFromPhyloseq}} +#' \code{\link[=convert]{convertFromBIOM}} #' \code{\link[=importQIIME2]{importQIIME2}} #' #' @author Leo Lahti and Tuomas Borman. Contact: \url{microbiome.github.io} diff --git a/R/importQIIME2.R b/R/importQIIME2.R index 2f65728c3..64a11e3a0 100644 --- a/R/importQIIME2.R +++ b/R/importQIIME2.R @@ -64,9 +64,9 @@ #' #' @name importQIIME2 #' @seealso -#' \code{\link[=makeTreeSEFromPhyloseq]{makeTreeSEFromPhyloseq}} -#' \code{\link[=makeTreeSEFromBiom]{makeTreeSEFromBiom}} -#' \code{\link[=makeTreeSEFromDADA2]{makeTreeSEFromDADA2}} +#' \code{\link[=convert]{convertFromDADA2}} +#' \code{\link[=convert]{convertFromPhyloseq}} +#' \code{\link[=convert]{convertFromBIOM}} #' \code{\link[=importMothur]{importMothur}} #' #' @export diff --git a/R/makeTreeSummarizedExperimentFromPhyloseq.R b/R/makeTreeSummarizedExperimentFromPhyloseq.R deleted file mode 100644 index 15e64b916..000000000 --- a/R/makeTreeSummarizedExperimentFromPhyloseq.R +++ /dev/null @@ -1,90 +0,0 @@ -#' Coerce a \code{phyloseq} object to a \code{TreeSummarizedExperiment} -#' -#' \code{makeTreeSEFromPhyloseq} converts \code{phyloseq} -#' objects into \code{TreeSummarizedExperiment} objects. -#' -#' All data stored in a \code{phyloseq} object is transferred. -#' -#' @param x a \code{phyloseq} object -#' -#' @return An object of class \code{TreeSummarizedExperiment} -#' -#' @importFrom S4Vectors SimpleList DataFrame make_zero_col_DFrame -#' @importFrom SummarizedExperiment colData colData<- -#' -#' @export -#' -#' @name makeTreeSEFromPhyloseq -#' @seealso -#' \code{\link[=makeTreeSEFromBiom]{makeTreeSEFromBiom}} -#' \code{\link[=makeTreeSEFromDADA2]{makeTreeSEFromDADA2}} -#' \code{\link[=importQIIME2]{importQIIME2}} -#' \code{\link[=importMothur]{importMothur}} -#' -#' @examples -#' if (requireNamespace("phyloseq")) { -#' data(GlobalPatterns, package="phyloseq") -#' makeTreeSEFromPhyloseq(GlobalPatterns) -#' data(enterotype, package="phyloseq") -#' makeTreeSEFromPhyloseq(enterotype) -#' data(esophagus, package="phyloseq") -#' makeTreeSEFromPhyloseq(esophagus) -#' } -makeTreeSEFromPhyloseq <- function(x) { - # input check - .require_package("phyloseq") - if(!is(x,"phyloseq")){ - stop("'x' must be a 'phyloseq' object") - } - # - # Get the assay - counts <- x@otu_table@.Data - # Check the orientation, and transpose if necessary - if( !x@otu_table@taxa_are_rows ){ - counts <- t(counts) - } - # Create a list of assays - assays <- SimpleList(counts = counts) - - if(!is.null(x@tax_table@.Data)){ - rowData <- DataFrame(data.frame(x@tax_table@.Data)) - } else{ - rowData <- S4Vectors::make_zero_col_DFrame(nrow(assays$counts)) - rownames(rowData) <- rownames(assays$counts) - } - if(!is.null(x@sam_data)){ - colData <- DataFrame(data.frame(x@sam_data)) - } else{ - colData <- S4Vectors::make_zero_col_DFrame(ncol(assays$counts)) - rownames(colData) <- colnames(assays$counts) - } - if(!is.null(x@phy_tree)){ - rowTree <- x@phy_tree - } else { - rowTree <- NULL - } - if (!is.null(x@refseq)) { - referenceSeq <- x@refseq - } else { - referenceSeq <- NULL - } - TreeSummarizedExperiment(assays = assays, - rowData = rowData, - colData = colData, - rowTree = rowTree, - referenceSeq = referenceSeq) -} - -####################### makeTreeSummarizedExperimentFromPhyloseq ####################### -#' @rdname makeTreeSEFromPhyloseq -#' @export -setGeneric("makeTreeSummarizedExperimentFromPhyloseq", signature = c("x"), - function(x) - standardGeneric("makeTreeSummarizedExperimentFromPhyloseq")) - -#' @rdname makeTreeSEFromPhyloseq -#' @export -setMethod("makeTreeSummarizedExperimentFromPhyloseq", signature = c(x = "ANY"), - function(x){ - makeTreeSEFromPhyloseq(x) - }) diff --git a/R/makephyloseqFromTreeSummarizedExperiment.R b/R/makephyloseqFromTreeSummarizedExperiment.R deleted file mode 100644 index 908a7052b..000000000 --- a/R/makephyloseqFromTreeSummarizedExperiment.R +++ /dev/null @@ -1,279 +0,0 @@ -#' Create a phyloseq object from a TreeSummarizedExperiment object -#' -#' This function creates a phyloseq object from a TreeSummarizedExperiment -#' object. By using \code{assay.type}, it is possible to specify which table -#' from \code{assay} is added to the phyloseq object. -#' -#' @param x a \code{TreeSummarizedExperiment} object -#' -#' @param assay.type A single character value for selecting the -#' \code{\link[SummarizedExperiment:SummarizedExperiment-class]{assay}} to be -#' included in the phyloseq object that is created. -#' (By default: \code{assay.type = "counts"}) -#' -#' @param assay_name a single \code{character} value for specifying which -#' assay to use for calculation. -#' (Please use \code{assay.type} instead. At some point \code{assay_name} -#' will be disabled.) -#' -#' @param tree.name a single \code{character} value for specifying which -#' tree will be included in the phyloseq object that is created, -#' (By default: \code{tree.name = "phylo"}) -#' -#' @param tree_name Deprecated. Use \code{tree.name} instead. -#' -#' @param ... additional arguments -#' -#' @details -#' \code{makePhyloseqFromTreeSE} is used for creating a -#' phyloseq object from TreeSummarizedExperiment object. -#' -#' @return -#' An object of class \code{Phyloseq} object. -#' -#' @name makePhyloseqFromTreeSE -#' @export -#' -#' @author Leo Lahti and Tuomas Borman. Contact: \url{microbiome.github.io} -#' -#' @examples -#' # Get tse object -#' data(GlobalPatterns) -#' tse <- GlobalPatterns -#' -#' # Create a phyloseq object from it -#' phy <- makePhyloseqFromTreeSE(tse) -#' phy -#' -#' # By default the chosen table is counts, but if there are other tables, -#' # they can be chosen with assay.type. -#' -#' # Counts relative abundances table -#' tse <- transformAssay(tse, method = "relabundance") -#' phy2 <- makePhyloseqFromTreeSE(tse, assay.type = "relabundance") -#' phy2 -NULL - -#' @rdname makePhyloseqFromTreeSE -#' @export -setGeneric("makePhyloseqFromTreeSE", signature = c("x"), - function(x, ...) - standardGeneric("makePhyloseqFromTreeSE")) - - -#' @rdname makePhyloseqFromTreeSE -#' @export -setMethod("makePhyloseqFromTreeSE", - signature = c(x = "SummarizedExperiment"), - function(x, assay.type = "counts", assay_name = NULL, ...){ - # Input check - .require_package("phyloseq") - # Check that tse do not have zero rows - if(!all(dim(x) > 0)){ - stop("'x' contains zero rows. 'x' can not be converted - to a phyloseq object.", - call. = FALSE) - } - - if (!is.null(assay_name)) { - .Deprecated(old="assay_name", new="assay.type", "Now assay_name is deprecated. Use assay.type instead.") - } - - # Check assay.type - .check_assay_present(assay.type, x) - - # phyloseq object requires nonduplicated rownames. If there are - # duplicated rownames, they are converted so that they are unique - if( any(duplicated(rownames(x))) ){ - rownames(x) <- getTaxonomyLabels(x) - } - # List of arguments - args = list() - # Gets the abundance data from assay, and converts it to otu_table - otu_table <- as.matrix(assay(x, assay.type)) - otu_table <- phyloseq::otu_table(otu_table, taxa_are_rows = TRUE) - # Adds to the list - args[["otu_table"]] <- otu_table - - # If rowData includes information - if(!( length(rowData(x)[,taxonomyRanks(x)]) == 0 || - is.null((rowData(x)[,taxonomyRanks(x)])) )){ - # Converts taxonomy table to characters if it's not already - rowData(x) <- DataFrame(lapply(rowData(x), as.character)) - # Gets the taxonomic data from rowData, and converts it to tax_table - tax_table <- as.matrix(rowData(x)[,taxonomyRanks(x),drop=FALSE]) - tax_table <- phyloseq::tax_table(tax_table) - # Adds to the list - args[["tax_table"]] <- tax_table - } - - # If colData includes information - if(!( length(colData(x)) == 0 || is.null(ncol(colData(x))) )){ - # Gets the feature_data from colData and converts it to sample_data - sample_data <- as.data.frame(colData(x)) - sample_data <- phyloseq::sample_data(sample_data) - # Adds to the list - args[["sample_data"]] <- sample_data - } - - # Creates a phyloseq object - phyloseq <- do.call(phyloseq::phyloseq, args) - return(phyloseq) - } -) - -#' @rdname makePhyloseqFromTreeSE -#' @export -setMethod("makePhyloseqFromTreeSE", - signature = c(x = "TreeSummarizedExperiment"), - function(x, tree.name = tree_name, tree_name = "phylo", ...){ - # If rowTrees exist, check tree.name - if( length(x@rowTree) > 0 ){ - .check_rowTree_present(tree.name, x) - # Subset the data based on the tree - x <- x[ rowLinks(x)$whichTree == tree.name, ] - add_phy_tree <- TRUE - } else{ - add_phy_tree <- FALSE - } - # - - # phyloseq and tree objects require nonduplicated rownames. If there are - # duplicated rownames, they are converted so that they are unique - if( any(duplicated(rownames(x))) ){ - rownames(x) <- getTaxonomyLabels(x) - } - # Gets otu_table object from the function above this, if tse contains - # only abundance table. - # Otherwise, gets a phyloseq object with otu_table, and tax_table - # and/or sample_data - obj <- callNextMethod() - # List of arguments - args = list() - # Adds to the list of arguments, if 'obj' is not a phyloseq object - # i.e. is an otu_table - if(!is(obj,"phyloseq")){ - # Adds otu_table to the list - args[["otu_table"]] <- obj - } - - # Add phylogenetic tree - if( add_phy_tree ){ - phy_tree <- .get_rowTree_for_phyloseq(x, tree.name) - # If the object is a phyloseq object, adds phy_tree to it - if(is(obj,"phyloseq")){ - phyloseq::phy_tree(obj) <- phy_tree - } else{ - # Adds to the list - args[["phy_tree"]] <- phy_tree - } - } - - # If referenceSeq has information, stores it to refseq and converts is - # to phyloseq's refseq. - if( !is.null(referenceSeq(x)) ){ - # Get referenceSeqs - refseq <- .get_referenceSeq_for_phyloseq(x, ...) - # IF refSeq passed the test, add it - if( !is.null(refseq) ){ - # Convert it to phyloseq object - refseq <- phyloseq::refseq(refseq) - # If the object is a phyloseq object, adds refseq to it - if(is(obj,"phyloseq")){ - obj <- phyloseq::merge_phyloseq(obj, refseq) - } else{ - # Adds to the list - args[["refseq"]] <- refseq - } - } - } - - # If 'obj' is not a phyloseq object, creates one. - if(!is(obj,"phyloseq")){ - # Creates a phyloseq object - phyloseq <- do.call(phyloseq::phyloseq, args) - } else{ - phyloseq <- obj - } - phyloseq - } -) - -################### makePhyloseqFromTreeSummarizedExperiment ################### -#' @rdname makePhyloseqFromTreeSE -#' @export -setGeneric("makePhyloseqFromTreeSummarizedExperiment", signature = c("x"), - function(x, ...) - standardGeneric("makePhyloseqFromTreeSummarizedExperiment")) - -#' @rdname makePhyloseqFromTreeSE -#' @export -setMethod("makePhyloseqFromTreeSummarizedExperiment", signature = c(x = "ANY"), - function(x, ...){ - makePhyloseqFromTreeSE(x, ...) - }) - -################################ HELP FUNCTIONS ################################ -# If tips do not match with rownames, prune the tree -.get_x_with_pruned_tree <- function(x, tree.name){ - # Get rowLinks - row_links <- rowLinks(x) - # Gets node labels - node_labs <- row_links[ , "nodeLab"] - # Prunes the tree - tree_pruned <- ape::keep.tip(rowTree(x), node_labs) - # Replace tip labels with corresponding rownames - tree_pruned$tip.label <- rownames(x) - # Assigns the pruned tree back to TSE object - rowTree(x) <- tree_pruned - warning("Tips of rowTree are renamed to match rownames.", call. = FALSE) - return(x) -} - -# In phyloseq, tips and rownames must match -.get_rowTree_for_phyloseq <- function(x, tree.name){ - # Check if the rowTree's tips match with rownames: - # tips labels are found from rownames - if( any(!( rowTree(x, tree.name)$tip.label) %in% rownames(x)) ){ - # If rowtree do not match, tree is pruned - x <- .get_x_with_pruned_tree(x, tree.name) - } - # Get rowTree - phy_tree <- rowTree(x, tree.name) - # Convert rowTree to phyloseq object - phy_tree <- phyloseq::phy_tree(phy_tree) - - return(phy_tree) -} - -.get_referenceSeq_for_phyloseq <- function(x, referenceSeq = 1, ...){ - # Get reference seqs - refSeqs <- referenceSeq(x) - # Is referenceSeq a list / does it contain multiple DNA sets - is_list <- is(refSeqs, "DNAStringSetList") - - # Take only one set, if it is a list - if( is_list ){ - # Check referenceSeq - if( !( (.is_non_empty_string(referenceSeq) && referenceSeq %in% names(refSeqs)) || - (.is_an_integer(referenceSeq) && (referenceSeq>0 && referenceSeq<=length(refSeqs))) ) - ){ - stop("'referenceSeq' must be a non-empty single character value or an integer ", - "specifying the DNAStringSet from DNAStringSetList.", - call. = FALSE) - } - # Get specified referenceSeq - refSeqs <- refSeqs[[referenceSeq]] - warning("Use 'referenceSeq' to specify DNA set from DNAStringSetList. ", - "Current choice is '", referenceSeq, "'.", - call. = FALSE) - } - # Check if all rownames have referenceSeqs - if( !(all(rownames(x) %in% names(refSeqs)) && - all(names(refSeqs) %in% rownames(x) )) ){ - warning("referenceSeq does not match with rownames so they are discarded.", - call. = FALSE) - refSeqs <- NULL - } - return(refSeqs) -} \ No newline at end of file diff --git a/man/convert.Rd b/man/convert.Rd new file mode 100644 index 000000000..96c2627ae --- /dev/null +++ b/man/convert.Rd @@ -0,0 +1,214 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/convertFromBIOM.R, R/convertFromDADA2.R, +% R/convertFromPhyloseq.R, R/convertToPhyloseq.R +\name{convert} +\alias{convert} +\alias{importBIOM} +\alias{convertFromBIOM} +\alias{convertFromDADA2} +\alias{convertFromPhyloseq} +\alias{convertToPhyloseq} +\alias{convertToPhyloseq,SummarizedExperiment-method} +\alias{convertToPhyloseq,TreeSummarizedExperiment-method} +\title{Converters} +\usage{ +importBIOM(file, ...) + +convertFromBIOM( + obj, + prefix.rm = removeTaxaPrefixes, + removeTaxaPrefixes = FALSE, + rank.from.prefix = rankFromPrefix, + rankFromPrefix = FALSE, + artifact.rm = remove.artifacts, + remove.artifacts = FALSE, + ... +) + +convertFromDADA2(...) + +convertFromPhyloseq(phy, ...) + +convertToPhyloseq(x, ...) + +\S4method{convertToPhyloseq}{SummarizedExperiment}(x, assay.type = "counts", assay_name = NULL, ...) + +\S4method{convertToPhyloseq}{TreeSummarizedExperiment}(x, tree.name = tree_name, tree_name = "phylo", ...) +} +\arguments{ +\item{file}{BIOM file location} + +\item{...}{Addional arguments. For \code{convertFromDADA2}, see +\code{mergePairs} function for more details.} + +\item{obj}{BIOM object to be converted to a +\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}}} + +\item{prefix.rm}{\code{TRUE} or \code{FALSE}: Should +taxonomic prefixes be removed? The prefixes is removed only from detected +taxa columns meaning that \code{rank.from.prefix} should be enabled in the most cases. +(default \code{prefix.rm = FALSE})} + +\item{removeTaxaPrefixes}{Deprecated. Use \code{prefix.rm} instead.} + +\item{rank.from.prefix}{\code{TRUE} or \code{FALSE}: If file does not have +taxonomic ranks on feature table, should they be scraped from prefixes? +(default \code{rank.from.prefix = FALSE})} + +\item{rankFromPrefix}{Deprecated.Use \code{rank.from.prefix} instead.} + +\item{artifact.rm}{\code{TRUE} or \code{FALSE}: If file have +some taxonomic character naming artifacts, should they be removed. +(default \code{artifact.rm = FALSE})} + +\item{remove.artifacts}{Deprecated. Use \code{artifact.rm} instead.} + +\item{phy}{a \code{phyloseq} object} + +\item{x}{a +\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +object} + +\item{assay.type}{A single character value for selecting the +\code{\link[SummarizedExperiment:SummarizedExperiment-class]{assay}} to be +included in the phyloseq object that is created. +(By default: \code{assay.type = "counts"})} + +\item{assay_name}{a single \code{character} value for specifying which +assay to use for calculation. +(Please use \code{assay.type} instead. At some point \code{assay_name} +will be disabled.)} + +\item{tree.name}{a single \code{character} value for specifying which +tree will be included in the phyloseq object that is created, +(By default: \code{tree.name = "phylo"})} + +\item{tree_name}{a single \code{character} value for specifying which +tree will be included in the phyloseq object that is created, +(By default: \code{tree_name = "phylo"}) +\code{tree_name} will be deprecated. Use \code{tree.name} instead.} +} +\value{ +\code{importBIOM} returns an object of class +\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} + +\code{convertFromBIOM} returns an object of class +\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} + +\code{convertFromDADA2} returns an object of class +\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} + +\code{convertFromPhyloseq} returns an object of class +\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} + +\code{convertToPhyloseq} returns an object of class +\code{\link[phyloseq:phyloseq-class]{phyloseq}} +} +\description{ +For convenience a few functions are available to convert BIOM, DADA2 and +phyloseq objects to +\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +objects and +\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +objects to phyloseq objects. +} +\details{ +\code{convertFromBIOM} coerces a BIOM object to a +\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +object. + +\code{importBIOM} loads a BIOM file and creates a +\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +object from the BIOM object contained in the loaded file + +\code{convertFromDADA2} is a wrapper for the +\code{mergePairs} function from the \code{dada2} package. +A count matrix is constructed via +\code{makeSequenceTable(mergePairs(...))} and rownames are dynamically +created as \code{ASV(N)} with \code{N} from 1 to \code{nrow} of the count +tables. The colnames and rownames from the output of \code{makeSequenceTable} +are stored as \code{colnames} and in the \code{referenceSeq} slot of the +\code{TreeSummarizedExperiment},respectively. + +\code{convertFromPhyloseq} converts \code{phyloseq} +objects into +\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} objects. +All data stored in a \code{phyloseq} object is transferred. + +\code{convertToPhyloseq} creates a phyloseq object from a +\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} +object. By using \code{assay.type}, it is possible to specify which table +from \code{assay} is added to the phyloseq object. +} +\examples{ +### Load and convert BIOM results to a TreeSE +# Load biom file +library(biomformat) +biom_file <- system.file("extdata", "rich_dense_otu_table.biom", + package = "biomformat") + +# Make TreeSE from biom file +tse <- importBIOM(biom_file) + +# Make TreeSE from biom object +biom_object <- biomformat::read_biom(biom_file) +tse <- convertFromBIOM(biom_object) + +# Get taxonomyRanks from prefixes and remove prefixes +tse <- importBIOM(biom_file, + rank.from.prefix = TRUE, + prefix.rm = TRUE) + +# Load another biom file +biom_file <- system.file("extdata/testdata", "Aggregated_humanization2.biom", + package = "mia") + +# Clean artifacts from taxonomic data +tse <- importBIOM(biom_file, + artifact.rm = TRUE) + +### Coerce DADA2 results to a TreeSE object +if(requireNamespace("dada2")) { + fnF <- system.file("extdata", "sam1F.fastq.gz", package="dada2") + fnR = system.file("extdata", "sam1R.fastq.gz", package="dada2") + dadaF <- dada2::dada(fnF, selfConsist=TRUE) + dadaR <- dada2::dada(fnR, selfConsist=TRUE) + + tse <- convertFromDADA2(dadaF, fnF, dadaR, fnR) + tse +} + +### Coerce a phyloseq object to a TreeSE object +if (requireNamespace("phyloseq")) { + data(GlobalPatterns, package="phyloseq") + convertFromPhyloseq(GlobalPatterns) + data(enterotype, package="phyloseq") + convertFromPhyloseq(enterotype) + data(esophagus, package="phyloseq") + convertFromPhyloseq(esophagus) +} + +### Coerce a TreeSE object to a phyloseq object +# Get tse object +data(GlobalPatterns) +tse <- GlobalPatterns + +# Create a phyloseq object from it +phy <- convertToPhyloseq(tse) +phy + +# By default the chosen table is counts, but if there are other tables, +# they can be chosen with assay.type. + +# Counts relative abundances table +tse <- transformAssay(tse, method = "relabundance") +phy2 <- convertToPhyloseq(tse, assay.type = "relabundance") +phy2 +} +\seealso{ +\code{\link[=importQIIME2]{importQIIME2}} +\code{\link[=importMothur]{importMothur}} +} +\author{ +Leo Lahti and Tuomas Borman. Contact: \url{microbiome.github.io} +} diff --git a/man/deprecate.Rd b/man/deprecate.Rd index 0518a4b51..a79180850 100644 --- a/man/deprecate.Rd +++ b/man/deprecate.Rd @@ -8,8 +8,14 @@ \alias{addTaxonomyTree,SummarizedExperiment-method} \alias{taxonomyTree} \alias{taxonomyTree,SummarizedExperiment-method} +\alias{makeTreeSummarizedExperimentFromPhyloseq} +\alias{makeTreeSummarizedExperimentFromPhyloseq,ANY-method} \alias{mergeRows} \alias{mergeRows,SummarizedExperiment-method} +\alias{makePhyloseqFromTreeSummarizedExperiment} +\alias{makePhyloseqFromTreeSummarizedExperiment,ANY-method} +\alias{makePhyloseqFromTreeSE} +\alias{makePhyloseqFromTreeSE,ANY-method} \alias{mergeRows,TreeSummarizedExperiment-method} \alias{mergeCols} \alias{mergeCols,SummarizedExperiment-method} @@ -25,6 +31,11 @@ \alias{mergeFeaturesByRank,SingleCellExperiment-method} \alias{mergeFeaturesByPrevalence} \alias{mergeFeaturesByPrevalence,SummarizedExperiment-method} +\alias{makeTreeSEFromBiom} +\alias{makeTreeSummarizedExperimentFromBiom} +\alias{makeTreeSEFromDADA2} +\alias{makeTreeSummarizedExperimentFromDADA2} +\alias{makeTreeSEFromPhyloseq} \alias{getExperimentCrossAssociation} \alias{getExperimentCrossAssociation,MultiAssayExperiment-method} \alias{getExperimentCrossAssociation,SummarizedExperiment-method} @@ -119,10 +130,22 @@ taxonomyTree(x, ...) \S4method{taxonomyTree}{SummarizedExperiment}(x, ...) +makeTreeSummarizedExperimentFromPhyloseq(x) + +\S4method{makeTreeSummarizedExperimentFromPhyloseq}{ANY}(x) + mergeRows(x, ...) \S4method{mergeRows}{SummarizedExperiment}(x, ...) +makePhyloseqFromTreeSummarizedExperiment(x, ...) + +\S4method{makePhyloseqFromTreeSummarizedExperiment}{ANY}(x, ...) + +makePhyloseqFromTreeSE(x, ...) + +\S4method{makePhyloseqFromTreeSE}{ANY}(x, ...) + \S4method{mergeRows}{TreeSummarizedExperiment}(x, ...) mergeCols(x, ...) @@ -153,6 +176,16 @@ mergeFeaturesByPrevalence(x, ...) \S4method{mergeFeaturesByPrevalence}{SummarizedExperiment}(x, ...) +makeTreeSEFromBiom(...) + +makeTreeSummarizedExperimentFromBiom(...) + +makeTreeSEFromDADA2(...) + +makeTreeSummarizedExperimentFromDADA2(...) + +makeTreeSEFromPhyloseq(x) + getExperimentCrossAssociation(x, ...) \S4method{getExperimentCrossAssociation}{MultiAssayExperiment}(x, ...) @@ -314,9 +347,7 @@ perSampleDominantTaxa(x, ...) \S4method{perSampleDominantTaxa}{SummarizedExperiment}(x, ...) } \arguments{ -\item{x}{A -\code{\link[SummarizedExperiment:SummarizedExperiment-class]{SummarizedExperiment}} -object.} +\item{x}{an object} \item{...}{Additional parameters. See dedicated function.} } diff --git a/man/importHUMAnN.Rd b/man/importHUMAnN.Rd index 603953cc3..331d722fd 100644 --- a/man/importHUMAnN.Rd +++ b/man/importHUMAnN.Rd @@ -68,11 +68,11 @@ microbial communities with bioBakery 3. \emph{eLife}. 10:e65088. } \seealso{ \code{\link[=importMetaPhlAn]{importMetaPhlAn}} -\code{\link[=makeTreeSEFromPhyloseq]{makeTreeSEFromPhyloseq}} -\code{\link[=makeTreeSEFromBiom]{makeTreeSEFromBiom}} -\code{\link[=makeTreeSEFromDADA2]{makeTreeSEFromDADA2}} \code{\link[=importQIIME2]{importQIIME2}} \code{\link[=importMothur]{importMothur}} +\code{\link[=convert]{convertFromDADA2}} +\code{\link[=convert]{convertFromPhyloseq}} +\code{\link[=convert]{convertFromBIOM}} } \author{ Leo Lahti and Tuomas Borman. Contact: \url{microbiome.github.io} diff --git a/man/importMetaPhlAn.Rd b/man/importMetaPhlAn.Rd index 5b7c1f0d8..d4cd6bf70 100644 --- a/man/importMetaPhlAn.Rd +++ b/man/importMetaPhlAn.Rd @@ -90,11 +90,11 @@ and strain-level profiling of diverse microbial communities with bioBakery 3. } \seealso{ \code{\link[=importHUMAnN]{importHUMAnN}} -\code{\link[=makeTreeSEFromPhyloseq]{makeTreeSEFromPhyloseq}} -\code{\link[=makeTreeSEFromBiom]{makeTreeSEFromBiom}} -\code{\link[=makeTreeSEFromDADA2]{makeTreeSEFromDADA2}} \code{\link[=importQIIME2]{importQIIME2}} \code{\link[=importMothur]{importMothur}} +\code{\link[=convert]{convertFromDADA2}} +\code{\link[=convert]{convertFromPhyloseq}} +\code{\link[=convert]{convertFromBIOM}} } \author{ Leo Lahti and Tuomas Borman. Contact: \url{microbiome.github.io} diff --git a/man/importMothur.Rd b/man/importMothur.Rd index 5e8561b02..e1ecdc7ea 100644 --- a/man/importMothur.Rd +++ b/man/importMothur.Rd @@ -72,9 +72,9 @@ tse \url{https://mothur.org/wiki/design_file/} } \seealso{ -\code{\link[=makeTreeSEFromPhyloseq]{makeTreeSEFromPhyloseq}} -\code{\link[=makeTreeSEFromBiom]{makeTreeSEFromBiom}} -\code{\link[=makeTreeSEFromDADA2]{makeTreeSEFromDADA2}} +\code{\link[=convert]{convertFromDADA2}} +\code{\link[=convert]{convertFromPhyloseq}} +\code{\link[=convert]{convertFromBIOM}} \code{\link[=importQIIME2]{importQIIME2}} } \author{ diff --git a/man/importQIIME2.Rd b/man/importQIIME2.Rd index eb63b21e8..b3658160b 100644 --- a/man/importQIIME2.Rd +++ b/man/importQIIME2.Rd @@ -149,9 +149,9 @@ microbiome data science using QIIME 2. Nature Biotechnology 37: 852–857. \url{https://qiime2.org} } \seealso{ -\code{\link[=makeTreeSEFromPhyloseq]{makeTreeSEFromPhyloseq}} -\code{\link[=makeTreeSEFromBiom]{makeTreeSEFromBiom}} -\code{\link[=makeTreeSEFromDADA2]{makeTreeSEFromDADA2}} +\code{\link[=convert]{convertFromDADA2}} +\code{\link[=convert]{convertFromPhyloseq}} +\code{\link[=convert]{convertFromBIOM}} \code{\link[=importMothur]{importMothur}} } \author{ diff --git a/man/makePhyloseqFromTreeSE.Rd b/man/makePhyloseqFromTreeSE.Rd deleted file mode 100644 index 37f472be3..000000000 --- a/man/makePhyloseqFromTreeSE.Rd +++ /dev/null @@ -1,73 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/makephyloseqFromTreeSummarizedExperiment.R -\name{makePhyloseqFromTreeSE} -\alias{makePhyloseqFromTreeSE} -\alias{makePhyloseqFromTreeSE,SummarizedExperiment-method} -\alias{makePhyloseqFromTreeSE,TreeSummarizedExperiment-method} -\alias{makePhyloseqFromTreeSummarizedExperiment} -\alias{makePhyloseqFromTreeSummarizedExperiment,ANY-method} -\title{Create a phyloseq object from a TreeSummarizedExperiment object} -\usage{ -makePhyloseqFromTreeSE(x, ...) - -\S4method{makePhyloseqFromTreeSE}{SummarizedExperiment}(x, assay.type = "counts", assay_name = NULL, ...) - -\S4method{makePhyloseqFromTreeSE}{TreeSummarizedExperiment}(x, tree.name = tree_name, tree_name = "phylo", ...) - -makePhyloseqFromTreeSummarizedExperiment(x, ...) - -\S4method{makePhyloseqFromTreeSummarizedExperiment}{ANY}(x, ...) -} -\arguments{ -\item{x}{a \code{TreeSummarizedExperiment} object} - -\item{...}{additional arguments} - -\item{assay.type}{A single character value for selecting the -\code{\link[SummarizedExperiment:SummarizedExperiment-class]{assay}} to be -included in the phyloseq object that is created. -(By default: \code{assay.type = "counts"})} - -\item{assay_name}{a single \code{character} value for specifying which -assay to use for calculation. -(Please use \code{assay.type} instead. At some point \code{assay_name} -will be disabled.)} - -\item{tree.name}{a single \code{character} value for specifying which -tree will be included in the phyloseq object that is created, -(By default: \code{tree.name = "phylo"})} - -\item{tree_name}{Deprecated. Use \code{tree.name} instead.} -} -\value{ -An object of class \code{Phyloseq} object. -} -\description{ -This function creates a phyloseq object from a TreeSummarizedExperiment -object. By using \code{assay.type}, it is possible to specify which table -from \code{assay} is added to the phyloseq object. -} -\details{ -\code{makePhyloseqFromTreeSE} is used for creating a -phyloseq object from TreeSummarizedExperiment object. -} -\examples{ -# Get tse object -data(GlobalPatterns) -tse <- GlobalPatterns - -# Create a phyloseq object from it -phy <- makePhyloseqFromTreeSE(tse) -phy - -# By default the chosen table is counts, but if there are other tables, -# they can be chosen with assay.type. - -# Counts relative abundances table -tse <- transformAssay(tse, method = "relabundance") -phy2 <- makePhyloseqFromTreeSE(tse, assay.type = "relabundance") -phy2 -} -\author{ -Leo Lahti and Tuomas Borman. Contact: \url{microbiome.github.io} -} diff --git a/man/makeTreeSEFromBiom.Rd b/man/makeTreeSEFromBiom.Rd deleted file mode 100644 index 6fb71bc8f..000000000 --- a/man/makeTreeSEFromBiom.Rd +++ /dev/null @@ -1,95 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/makeTreeSummarizedExperimentFromBiom.R -\name{makeTreeSEFromBiom} -\alias{makeTreeSEFromBiom} -\alias{importBIOM} -\alias{makeTreeSummarizedExperimentFromBiom} -\title{Loading a biom file} -\usage{ -importBIOM(file, ...) - -makeTreeSEFromBiom( - x, - prefix.rm = removeTaxaPrefixes, - removeTaxaPrefixes = FALSE, - rank.from.prefix = rankFromPrefix, - rankFromPrefix = FALSE, - artifact.rm = remove.artifacts, - remove.artifacts = FALSE, - ... -) - -makeTreeSummarizedExperimentFromBiom(x, ...) -} -\arguments{ -\item{file}{biom file location} - -\item{...}{additional arguments -\itemize{ -\item \code{patter}: \code{character} value specifying artifacts -to be removed. If \code{patterns = "auto"}, special characters -are removed. (default: \code{pattern = "auto"}) -}} - -\item{x}{object of type \code{\link[biomformat:read_biom]{biom}}} - -\item{prefix.rm}{\code{TRUE} or \code{FALSE}: Should -taxonomic prefixes be removed? The prefixes is removed only from detected -taxa columns meaning that \code{rank.from.prefix} should be enabled in the most cases. -(default \code{prefix.rm = FALSE})} - -\item{removeTaxaPrefixes}{Deprecated. Use \code{prefix.rm} instead.} - -\item{rank.from.prefix}{\code{TRUE} or \code{FALSE}: If file does not have -taxonomic ranks on feature table, should they be scraped from prefixes? -(default \code{rank.from.prefix = FALSE})} - -\item{rankFromPrefix}{Deprecated.Use \code{rank.from.prefix} instead.} - -\item{artifact.rm}{\code{TRUE} or \code{FALSE}: If file have -some taxonomic character naming artifacts, should they be removed. -(default \code{artifact.rm = FALSE})} - -\item{remove.artifacts}{Deprecated. Use \code{artifact.rm} instead.} -} -\value{ -An object of class -\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} -} -\description{ -For convenience a few functions are available to convert data from a -\sQuote{biom} file or object into a -\code{\link[TreeSummarizedExperiment:TreeSummarizedExperiment-class]{TreeSummarizedExperiment}} -} -\examples{ -# Load biom file -library(biomformat) -biom_file <- system.file("extdata", "rich_dense_otu_table.biom", - package = "biomformat") - -# Make TreeSE from biom file -tse <- importBIOM(biom_file) - -# Make TreeSE from biom object -biom_object <- biomformat::read_biom(biom_file) -tse <- makeTreeSEFromBiom(biom_object) - -# Get taxonomyRanks from prefixes and remove prefixes -tse <- importBIOM(biom_file, - rank.from.prefix = TRUE, - prefix.rm = TRUE) - -# Load another biom file -biom_file <- system.file("extdata/testdata", "Aggregated_humanization2.biom", - package = "mia") - -# Clean artifacts from taxonomic data -tse <- importBIOM(biom_file, - artifact.rm = TRUE) -} -\seealso{ -\code{\link[=makeTreeSEFromPhyloseq]{makeTreeSEFromPhyloseq}} -\code{\link[=makeTreeSEFromDADA2]{makeTreeSEFromDADA2}} -\code{\link[=importQIIME2]{importQIIME2}} -\code{\link[=importMothur]{importMothur}} -} diff --git a/man/makeTreeSEFromDADA2.Rd b/man/makeTreeSEFromDADA2.Rd deleted file mode 100644 index f01459426..000000000 --- a/man/makeTreeSEFromDADA2.Rd +++ /dev/null @@ -1,47 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/makeTreeSummarizedExperimentFromDADA2.R -\name{makeTreeSEFromDADA2} -\alias{makeTreeSEFromDADA2} -\alias{makeTreeSummarizedExperimentFromDADA2} -\title{Coerce \sQuote{DADA2} results to \code{TreeSummarizedExperiment}} -\usage{ -makeTreeSEFromDADA2(...) - -makeTreeSummarizedExperimentFromDADA2(...) -} -\arguments{ -\item{...}{See \code{mergePairs} function for -more details.} -} -\value{ -An object of class \code{TreeSummarizedExperiment} -} -\description{ -\code{makeTreeSEFromDADA2} is a wrapper for the -\code{mergePairs} function from the \code{dada2} package. -} -\details{ -A count matrix is constructed via \code{makeSequenceTable(mergePairs(...))} -and rownames are dynamically created as \code{ASV(N)} with \code{N} from -1 to \code{nrow} of the count tables. The colnames and rownames from the -output of \code{makeSequenceTable} are stored as \code{colnames} and in the -\code{referenceSeq} slot of the \code{TreeSummarizedExperiment}, -respectively. -} -\examples{ -if(requireNamespace("dada2")) { - fnF <- system.file("extdata", "sam1F.fastq.gz", package="dada2") - fnR = system.file("extdata", "sam1R.fastq.gz", package="dada2") - dadaF <- dada2::dada(fnF, selfConsist=TRUE) - dadaR <- dada2::dada(fnR, selfConsist=TRUE) - - tse <- makeTreeSEFromDADA2(dadaF, fnF, dadaR, fnR) - tse -} -} -\seealso{ -\code{\link[=makeTreeSEFromPhyloseq]{makeTreeSEFromPhyloseq}} -\code{\link[=makeTreeSEFromBiom]{makeTreeSEFromBiom}} -\code{\link[=importQIIME2]{importQIIME2}} -\code{\link[=importMothur]{importMothur}} -} diff --git a/man/makeTreeSEFromPhyloseq.Rd b/man/makeTreeSEFromPhyloseq.Rd deleted file mode 100644 index f629642e7..000000000 --- a/man/makeTreeSEFromPhyloseq.Rd +++ /dev/null @@ -1,43 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/makeTreeSummarizedExperimentFromPhyloseq.R -\name{makeTreeSEFromPhyloseq} -\alias{makeTreeSEFromPhyloseq} -\alias{makeTreeSummarizedExperimentFromPhyloseq} -\alias{makeTreeSummarizedExperimentFromPhyloseq,ANY-method} -\title{Coerce a \code{phyloseq} object to a \code{TreeSummarizedExperiment}} -\usage{ -makeTreeSEFromPhyloseq(x) - -makeTreeSummarizedExperimentFromPhyloseq(x) - -\S4method{makeTreeSummarizedExperimentFromPhyloseq}{ANY}(x) -} -\arguments{ -\item{x}{a \code{phyloseq} object} -} -\value{ -An object of class \code{TreeSummarizedExperiment} -} -\description{ -\code{makeTreeSEFromPhyloseq} converts \code{phyloseq} -objects into \code{TreeSummarizedExperiment} objects. -} -\details{ -All data stored in a \code{phyloseq} object is transferred. -} -\examples{ -if (requireNamespace("phyloseq")) { - data(GlobalPatterns, package="phyloseq") - makeTreeSEFromPhyloseq(GlobalPatterns) - data(enterotype, package="phyloseq") - makeTreeSEFromPhyloseq(enterotype) - data(esophagus, package="phyloseq") - makeTreeSEFromPhyloseq(esophagus) -} -} -\seealso{ -\code{\link[=makeTreeSEFromBiom]{makeTreeSEFromBiom}} -\code{\link[=makeTreeSEFromDADA2]{makeTreeSEFromDADA2}} -\code{\link[=importQIIME2]{importQIIME2}} -\code{\link[=importMothur]{importMothur}} -} diff --git a/pkgdown/_pkgdown.yml b/pkgdown/_pkgdown.yml index 06be0afd4..dc286b536 100644 --- a/pkgdown/_pkgdown.yml +++ b/pkgdown/_pkgdown.yml @@ -12,14 +12,11 @@ reference: - transformAssay - subtitle: Data loading - contents: - - makeTreeSummarizedExperimentFromPhyloseq - - makeTreeSummarizedExperimentFromDADA2 - - importBIOM + - convert - importQIIME2 - importMothur - importMetaPhlAn - importHUMAnN - - makePhyloseqFromTreeSummarizedExperiment - title: Taxonomic data contents: - taxonomyRanks diff --git a/tests/testthat/test-4IO.R b/tests/testthat/test-4IO.R index 3f1be3da0..ce37e2b81 100644 --- a/tests/testthat/test-4IO.R +++ b/tests/testthat/test-4IO.R @@ -7,7 +7,7 @@ test_that("Importing biom files yield SummarizedExperiment objects", { expect_s4_class(me, "SummarizedExperiment") # load from object x1 <- biomformat::read_biom(rich_dense_file) - me2 <- makeTreeSEFromBiom(x1) + me2 <- convertFromBIOM(x1) expect_s4_class(me2, "SummarizedExperiment") expect_equal(dim(me), dim(me2)) expect_equal(rowData(me), rowData(me2)) @@ -16,11 +16,11 @@ test_that("Importing biom files yield SummarizedExperiment objects", { system.file("extdata/testdata/Aggregated_humanization2.biom", package="mia") ) - tse <- makeTreeSEFromBiom(biom_object, - prefix.rm = FALSE, - rank.from.prefix = FALSE, - artifact.rm = TRUE, - pattern = "\"") + tse <- convertFromBIOM(biom_object, + prefix.rm = FALSE, + rank.from.prefix = FALSE, + artifact.rm = TRUE, + pattern = "\"") # Testing no prefixes removed expect_true(rowData(tse) %>% apply(2,grepl,pattern="sk__|([dkpcofgs]+)__") %>% @@ -34,53 +34,57 @@ test_that("Importing biom files yield SummarizedExperiment objects", { expect_false(apply(rowData(tse), 2, grepl, pattern="^\"") %>% all()) # Testing prefixes removed - tse <- makeTreeSEFromBiom(biom_object, - prefix.rm=TRUE, - rank.from.prefix=TRUE, - artifact.rm = TRUE, - pattern = "\"") + tse <- convertFromBIOM(biom_object, + prefix.rm=TRUE, + rank.from.prefix=TRUE, + artifact.rm = TRUE, + pattern = "\"") expect_false(rowData(tse) %>% apply(2,grepl,pattern="sk__|([dkpcofgs]+)__") %>% all()) # Testing parsing taxonomy ranks from prefixes - tse <- makeTreeSEFromBiom(biom_object, - prefix.rm=FALSE, - rank.from.prefix=TRUE, - artifact.rm = TRUE, - pattern = "\"") + tse <- convertFromBIOM(biom_object, + prefix.rm=FALSE, + rank.from.prefix=TRUE, + artifact.rm = TRUE, + pattern = "\"") expect_true( sapply(tolower(colnames(rowData(tse))), function(x) x %in% TAXONOMY_RANKS) %>% all()) # Testing the artifact.rm, the original artifact in the biom file # is '\"', as a test we rather try remove a non existing pattern. - tse <- makeTreeSEFromBiom(biom_object, - prefix.rm=FALSE, - rank.from.prefix=FALSE, - artifact.rm = TRUE, - pattern = "\\*|\\?") + tse <- convertFromBIOM(biom_object, + prefix.rm=FALSE, + rank.from.prefix=FALSE, + artifact.rm = TRUE, + pattern = "\\*|\\?") # with wrong pattern artifact not cleaned expect_true(apply(rowData(tse), 2, grepl, pattern="\"") %>% any()) # Testing the artifact.rm, with the value 'auto' to automatically # detect the artifact and remove it (in our case the artifact is '\"'). - tse <- makeTreeSEFromBiom(biom_object, - prefix.rm=FALSE, - rank.from.prefix=FALSE, - artifact.rm = TRUE) + tse <- convertFromBIOM(biom_object, + prefix.rm=FALSE, + rank.from.prefix=FALSE, + artifact.rm = TRUE) # Checking if 'auto' has detected and cleaned the artifact expect_false(apply(rowData(tse), 2, grepl, pattern="\"") %>% any()) # Testing the artifact.rm, with the value NULL to not detect or clean # anything. - tse <- makeTreeSEFromBiom(biom_object, - prefix.rm=FALSE, - rank.from.prefix=FALSE, - artifact.rm = FALSE) + tse <- convertFromBIOM(biom_object, + prefix.rm=FALSE, + rank.from.prefix=FALSE, + artifact.rm = FALSE) # Checking if the '\"' artifact still exists. expect_true(apply(rowData(tse), 2, grepl, pattern="\"") %>% any()) # General final test - tse <- makeTreeSEFromBiom(biom_object, + tse <- convertFromBIOM(biom_object, + prefix.rm=TRUE, + rank.from.prefix=TRUE, + artifact.rm = TRUE) + tse <- convertFromBIOM(biom_object, prefix.rm=TRUE, rank.from.prefix=TRUE, artifact.rm = TRUE) @@ -100,10 +104,10 @@ test_that("Importing biom files yield SummarizedExperiment objects", { system.file("extdata", "rich_dense_otu_table.biom", package = "biomformat") ) - tse <- makeTreeSEFromBiom(biom_object, - prefix.rm=TRUE, - rank.from.prefix=TRUE, - artifact.rm = TRUE) + tse <- convertFromBIOM(biom_object, + prefix.rm=TRUE, + rank.from.prefix=TRUE, + artifact.rm = TRUE) # check if taxa prefixes removed expect_false(rowData(tse) %>% apply(2,grepl,pattern="sk__|([dkpcofgs]+)__") %>% @@ -117,20 +121,20 @@ test_that("Importing biom files yield SummarizedExperiment objects", { test_that("Importing phyloseq objects yield TreeSummarizedExperiment objects", { skip_if_not_installed("phyloseq") data(GlobalPatterns, package="phyloseq") - me <- makeTreeSEFromPhyloseq(GlobalPatterns) + me <- convertFromPhyloseq(GlobalPatterns) expect_s4_class(me, "TreeSummarizedExperiment") expect_equal(dim(me),c(19216,26)) data(enterotype, package="phyloseq") - me <- makeTreeSEFromPhyloseq(enterotype) + me <- convertFromPhyloseq(enterotype) expect_s4_class(me, "TreeSummarizedExperiment") expect_equal(dim(me),c(553,280)) data(esophagus, package="phyloseq") - me <- makeTreeSEFromPhyloseq(esophagus) + me <- convertFromPhyloseq(esophagus) expect_s4_class(me, "TreeSummarizedExperiment") expect_equal(dim(me),c(58,3)) esophagus2 <- esophagus phyloseq::otu_table(esophagus2) <- t(phyloseq::otu_table(esophagus)) - me2 <- makeTreeSEFromPhyloseq(esophagus2) + me2 <- convertFromPhyloseq(esophagus2) expect_equal(me, me2) }) @@ -141,7 +145,7 @@ test_that("Importing dada2 objects yield TreeSummarizedExperiment objects", { dadaF <- dada2::dada(fnF, selfConsist=TRUE) dadaR <- dada2::dada(fnR, selfConsist=TRUE) - me <- makeTreeSEFromDADA2(dadaF, fnF, dadaR, fnR) + me <- convertFromDADA2(dadaF, fnF, dadaR, fnR) expect_s4_class(me, "TreeSummarizedExperiment") }) @@ -414,7 +418,7 @@ test_that("dimnames of feature table is identicle with meta data", { }) -test_that("makePhyloseqFromTreeSE", { +test_that("convert TreeSE objects to phyloseq objects", { skip_if_not_installed("phyloseq") @@ -422,7 +426,7 @@ test_that("makePhyloseqFromTreeSE", { data(GlobalPatterns, package="mia") tse <- GlobalPatterns - phy <- makePhyloseqFromTreeSE(GlobalPatterns) + phy <- convertToPhyloseq(GlobalPatterns) # Test that assay is in otu_table expect_equal(as.data.frame(phyloseq::otu_table(phy)@.Data), as.data.frame(assays(tse)$counts)) @@ -444,8 +448,8 @@ test_that("makePhyloseqFromTreeSE", { # Test with agglomeration that that pruning is done internally test1 <- agglomerateByRank(tse, rank = "Phylum") test2 <- agglomerateByRank(tse, rank = "Phylum", update.tree = TRUE) - test1_phy <- expect_warning(makePhyloseqFromTreeSE(test1)) - test2_phy <- makePhyloseqFromTreeSE(test2) + test1_phy <- expect_warning(convertToPhyloseq(test1)) + test2_phy <- convertToPhyloseq(test2) expect_equal(length(phyloseq::phy_tree(test1_phy)$node), length(ape::keep.tip(rowTree(test1), rowLinks(test1)$nodeLab)$node)) @@ -456,21 +460,21 @@ test_that("makePhyloseqFromTreeSE", { # Check that everything works also with agglomerated data for (level in colnames(rowData(tse)) ){ temp <- agglomerateByRank(tse, rank = level) - expect_warning(makePhyloseqFromTreeSE(temp)) + expect_warning(convertToPhyloseq(temp)) } tse2 <- tse # Concerts data frame to factors rowData(tse2) <- DataFrame(lapply(rowData(tse2), as.factor)) - phy <- makePhyloseqFromTreeSE(tse) - phy2 <- makePhyloseqFromTreeSE(tse2) + phy <- convertToPhyloseq(tse) + phy2 <- convertToPhyloseq(tse2) expect_equal(phyloseq::tax_table(phy2), phyloseq::tax_table(phy)) # TSE object data(esophagus, package="mia") tse <- esophagus - phy <- makePhyloseqFromTreeSE(tse, assay.type="counts") + phy <- convertToPhyloseq(tse, assay.type="counts") # Test that assay is in otu_table expect_equal(as.data.frame(phyloseq::otu_table(phy)@.Data), as.data.frame(assays(tse)$counts)) @@ -480,10 +484,10 @@ test_that("makePhyloseqFromTreeSE", { # Test that merging objects lead to correct phyloseq tse <- mergeSEs(GlobalPatterns, esophagus, assay.type="counts", missing.values = 0) - pseq <- makePhyloseqFromTreeSE(tse, assay.type="counts") + pseq <- convertToPhyloseq(tse, assay.type="counts") tse_compare <- tse[ rownames(GlobalPatterns), ] - pseq_compare <- makePhyloseqFromTreeSE(tse_compare, assay.type="counts") + pseq_compare <- convertToPhyloseq(tse_compare, assay.type="counts") expect_equal(phyloseq::otu_table(pseq), phyloseq::otu_table(pseq_compare)) }) diff --git a/tests/testthat/test-5Unifrac.R b/tests/testthat/test-5Unifrac.R index bf86df8b1..126c7cac0 100644 --- a/tests/testthat/test-5Unifrac.R +++ b/tests/testthat/test-5Unifrac.R @@ -47,7 +47,6 @@ test_that("Unifrac beta diversity", { unifrac_rbiom <- as.matrix(rbiom::unifrac(assay(tse), weighted = TRUE, rowTree(tse))) expect_equal(unifrac_mia, unifrac_rbiom, tolerance = 1e-3) - # Test with merged object with multiple trees. runUnifrac takes subset of # data based on provided tree. tse <- mergeSEs(GlobalPatterns, esophagus, assay.type="counts", missing.values = 0) @@ -63,7 +62,6 @@ test_that("Unifrac beta diversity", { unifrac_rbiom <- as.matrix(rbiom::unifrac(assay(tse_ref), weighted = TRUE, rowTree(tse_ref))) expect_equal(unifrac_mia, unifrac_rbiom, tolerance = 1e-3) - # Test the function with agglomerated data. runUnifrac renames rownames # based on tips and links to them. Then it also prunes the tree so that # rows are in tips. diff --git a/vignettes/mia.Rmd b/vignettes/mia.Rmd index 687f0794d..8c2da84af 100644 --- a/vignettes/mia.Rmd +++ b/vignettes/mia.Rmd @@ -280,12 +280,12 @@ data(esophagus, package = "phyloseq") ```{r} esophagus -esophagus <- makeTreeSEFromPhyloseq(esophagus) +esophagus <- convertFromPhyloseq(esophagus) esophagus ``` For more details have a look at the man page, for examples -`?makeTreeSEFromPhyloseq`. +`?convert`. ## General wrapper functions