Skip to content

Commit

Permalink
moved add_taxonomy from method to function
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkimbrel committed Sep 29, 2023
1 parent f17eae6 commit daad048
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: qSIP2
Title: qSIP Analysis
Version: 0.4.5.9004
Version: 0.4.5.9005
Authors@R:
person("Jeff", "Kimbrel", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Expand Down
31 changes: 31 additions & 0 deletions R/add_taxonomy.R
Original file line number Diff line number Diff line change
@@ -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
}
}
35 changes: 3 additions & 32 deletions R/qsip_feature_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 |>
Expand All @@ -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
}
}
6 changes: 3 additions & 3 deletions man/add_taxonomy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit daad048

Please sign in to comment.