From f17eae6ff5b8cf85e639e5017d7f33a4d4e4bbf4 Mon Sep 17 00:00:00 2001 From: Jeff Kimbrel Date: Thu, 28 Sep 2023 22:06:58 -0700 Subject: [PATCH] feature table --- DESCRIPTION | 2 +- NAMESPACE | 2 +- R/qsip_abundance_object.R | 63 ------------------------------ R/qsip_feature_data.R | 75 ++++++++++++++++++++++++++++++++++++ man/add_taxonomy.Rd | 4 +- man/qsip_abundance_object.Rd | 24 ------------ man/qsip_feature_data.Rd | 20 ++++++++++ 7 files changed, 99 insertions(+), 91 deletions(-) delete mode 100644 R/qsip_abundance_object.R create mode 100644 R/qsip_feature_data.R delete mode 100644 man/qsip_abundance_object.Rd create mode 100644 man/qsip_feature_data.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 03a012d7..2200910b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: qSIP2 Title: qSIP Analysis -Version: 0.4.5.9003 +Version: 0.4.5.9004 Authors@R: person("Jeff", "Kimbrel", , "kimbrel1@llnl.gov", role = c("aut", "cre"), comment = c(ORCID = "YOUR-ORCID-ID")) diff --git a/NAMESPACE b/NAMESPACE index 18da5779..f5e75ae4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,7 +9,7 @@ export(gradient_pos_density_validation) export(gradient_position_validation) export(isotope_validation) export(plot_sample_curves) -export(qsip_abundance_object) +export(qsip_feature_data) export(qsip_sample_data) export(qsip_source_data) export(remove_isotopolog_label) diff --git a/R/qsip_abundance_object.R b/R/qsip_abundance_object.R deleted file mode 100644 index c077a45b..00000000 --- a/R/qsip_abundance_object.R +++ /dev/null @@ -1,63 +0,0 @@ -#' qSIP abundance table class -#' -#' A class to hold and validate an abundance table -#' -#' @slot data A dataframe or tibble -#' @slot id Column name with unique taxa IDs -#' -#' @export -#' -#' @keywords object - -qsip_abundance_object <- S7::new_class( - "qsip_abundance_object", - properties = list( - data = S7::class_data.frame, - id = S7::class_character, - taxonomy = S7::class_data.frame - ), - validator = function(self) { - if (any(duplicated(self@data[self@id]))) { - message(glue::glue("There appear to be duplicate ids in the {self@id} column")) - } - - qSIP2::abundance_validation(self@data, self@id) - - } -) - - - -#' Add a taxonomy table to qSIP abundance data -#' -#' @param x An object of `qsip_abundance_object` 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_abundance_object) <- function(x, taxa, id) { - - x_ids = x@data |> dplyr::pull(x@id) - taxa_ids = taxa |> dplyr::pull(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(!!x@id := id) - - x - } -} diff --git a/R/qsip_feature_data.R b/R/qsip_feature_data.R new file mode 100644 index 00000000..6ed58936 --- /dev/null +++ b/R/qsip_feature_data.R @@ -0,0 +1,75 @@ +#' qSIP feature table class +#' +#' A class to hold and validate a feature abundance table +#' +#' @slot data A dataframe or tibble +#' @slot feature_id Column name with unique taxa IDs +#' +#' @export +#' +#' @keywords object + +qsip_feature_data <- S7::new_class( + "qsip_feature_data", + properties = list( + data = S7::class_data.frame, + feature_id = S7::class_character, + taxonomy = S7::class_data.frame + ), + constructor = function(data, feature_id, taxonomy = data.frame()) { + + # rename columns to standardized names + data = data |> + dplyr::select(feature_id = feature_id, + dplyr::everything()) |> + dplyr::ungroup() + + S7::new_object(S7::S7_object(), + data = data, + feature_id = feature_id, + taxonomy = taxonomy) + }, + validator = function(self) { + if (any(duplicated(self@data['feature_id']))) { + message(glue::glue("There appear to be duplicate ids in the {self@id} column")) + } + + #qSIP2::abundance_validation(self@data, 'feature_id') + + } +) + + + +#' 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 3eabf77a..34d7e387 100644 --- a/man/add_taxonomy.Rd +++ b/man/add_taxonomy.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/qsip_abundance_object.R +% Please edit documentation in R/qsip_feature_data.R \name{add_taxonomy} \alias{add_taxonomy} \title{Add a taxonomy table to qSIP abundance data} @@ -7,7 +7,7 @@ add_taxonomy(x, ...) } \arguments{ -\item{x}{An object of \code{qsip_abundance_object} class} +\item{x}{An object of \code{qsip_feature_data} class} \item{taxa}{A taxa table} diff --git a/man/qsip_abundance_object.Rd b/man/qsip_abundance_object.Rd deleted file mode 100644 index f44887a6..00000000 --- a/man/qsip_abundance_object.Rd +++ /dev/null @@ -1,24 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/qsip_abundance_object.R -\name{qsip_abundance_object} -\alias{qsip_abundance_object} -\title{qSIP abundance table class} -\usage{ -qsip_abundance_object( - data = class_missing, - id = class_missing, - taxonomy = class_missing -) -} -\description{ -A class to hold and validate an abundance table -} -\section{Slots}{ - -\describe{ -\item{\code{data}}{A dataframe or tibble} - -\item{\code{id}}{Column name with unique taxa IDs} -}} - -\keyword{object} diff --git a/man/qsip_feature_data.Rd b/man/qsip_feature_data.Rd new file mode 100644 index 00000000..b6c17369 --- /dev/null +++ b/man/qsip_feature_data.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/qsip_feature_data.R +\name{qsip_feature_data} +\alias{qsip_feature_data} +\title{qSIP feature table class} +\usage{ +qsip_feature_data(data, feature_id, taxonomy = data.frame()) +} +\description{ +A class to hold and validate a feature abundance table +} +\section{Slots}{ + +\describe{ +\item{\code{data}}{A dataframe or tibble} + +\item{\code{feature_id}}{Column name with unique taxa IDs} +}} + +\keyword{object}