From daad0485d3c84cdae0cf1756b02886a19749b0da Mon Sep 17 00:00:00 2001 From: Jeff Kimbrel Date: Fri, 29 Sep 2023 07:54:38 -0700 Subject: [PATCH] moved add_taxonomy from method to function --- DESCRIPTION | 2 +- R/add_taxonomy.R | 31 +++++++++++++++++++++++++++++++ R/qsip_feature_data.R | 35 +++-------------------------------- man/add_taxonomy.Rd | 6 +++--- 4 files changed, 38 insertions(+), 36 deletions(-) create mode 100644 R/add_taxonomy.R diff --git a/DESCRIPTION b/DESCRIPTION index 2200910b..2f7b71f2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: qSIP2 Title: qSIP Analysis -Version: 0.4.5.9004 +Version: 0.4.5.9005 Authors@R: person("Jeff", "Kimbrel", , "kimbrel1@llnl.gov", role = c("aut", "cre"), comment = c(ORCID = "YOUR-ORCID-ID")) diff --git a/R/add_taxonomy.R b/R/add_taxonomy.R new file mode 100644 index 00000000..a62e483d --- /dev/null +++ b/R/add_taxonomy.R @@ -0,0 +1,31 @@ +#' Add a taxonomy table to qSIP abundance data +#' +#' @param feature_object An object of `qsip_feature_data` class +#' @param taxa A taxa table +#' @param id The column name for the taxa ids that match the ids in the +#' abundance table +#' +#' @export +#' +#' @keywords abundance + +add_taxonomy <- function(feature_object, taxa, feature_id) { + + taxa = taxa |> + dplyr::rename("feature_id" := feature_id) + + + feature_object_ids = feature_object@data['feature_id'] + taxa_ids = taxa['feature_id'] + + if (length(setdiff(feature_object_ids, taxa_ids)) > 0) { + stop("Some ids found in the abundance object are not found in the taxa table") + } else if (length(setdiff(taxa_ids, feature_object_ids)) > 0) { + setdiff(taxa_ids, feature_object_ids) + stop("Some ids found in the taxa table are not found in the abundance object") + } else { + + feature_object@taxonomy = taxa + feature_object + } +} diff --git a/R/qsip_feature_data.R b/R/qsip_feature_data.R index 6ed58936..7e9ce386 100644 --- a/R/qsip_feature_data.R +++ b/R/qsip_feature_data.R @@ -16,7 +16,9 @@ qsip_feature_data <- S7::new_class( feature_id = S7::class_character, taxonomy = S7::class_data.frame ), - constructor = function(data, feature_id, taxonomy = data.frame()) { + constructor = function(data, + feature_id, + taxonomy = data.frame()) { # rename columns to standardized names data = data |> @@ -41,35 +43,4 @@ qsip_feature_data <- S7::new_class( -#' Add a taxonomy table to qSIP abundance data -#' -#' @param x An object of `qsip_feature_data` class -#' @param taxa A taxa table -#' @param id The column name for the taxa ids that match the ids in the -#' abundance table -#' -#' @export -#' -#' @keywords abundance - -add_taxonomy <- S7::new_generic("add_taxonomy", "x") - -S7::method(add_taxonomy, qsip_feature_data) <- function(x, taxa, feature_id) { - - x_ids = x@data |> dplyr::pull(feature_id) - taxa_ids = taxa |> dplyr::pull(feature_id) - - if (length(setdiff(x_ids, taxa_ids)) > 0) { - stop("some ids found in the abundance object are not found in the taxa table") - } else if (length(setdiff(taxa_ids, x_ids)) > 0) { - setdiff(taxa_ids, x_ids) - stop("some ids found in the taxa table are not found in the abundance object") - } else { - - # rename taxa id column to the object level ID - x@taxonomy = taxa |> - dplyr::rename("feature_id" := feature_id) - x - } -} diff --git a/man/add_taxonomy.Rd b/man/add_taxonomy.Rd index 34d7e387..f726a3dc 100644 --- a/man/add_taxonomy.Rd +++ b/man/add_taxonomy.Rd @@ -1,13 +1,13 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/qsip_feature_data.R +% Please edit documentation in R/add_taxonomy.R \name{add_taxonomy} \alias{add_taxonomy} \title{Add a taxonomy table to qSIP abundance data} \usage{ -add_taxonomy(x, ...) +add_taxonomy(feature_object, taxa, feature_id) } \arguments{ -\item{x}{An object of \code{qsip_feature_data} class} +\item{feature_object}{An object of \code{qsip_feature_data} class} \item{taxa}{A taxa table}