From d54a6c4977b0a0f29668dc3d7a8382073d7b0e7f Mon Sep 17 00:00:00 2001 From: Stu Field Date: Thu, 7 Mar 2024 11:17:30 -0700 Subject: [PATCH] Create S3 method for `getAdatVersion()` - new S3 method to allow passing of different objects to the function: - namely `soma_adat` or `list` - closes #92 --- NAMESPACE | 3 +++ R/adat-helpers.R | 36 +++++++++++++++++++++------- man/adat-helpers.Rd | 17 ++++++------- man/soma_adat.Rd | 18 +++++++------- tests/testthat/test-getAdatVersion.R | 13 ++++++++++ 5 files changed, 62 insertions(+), 25 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 911e4a3..987fcd2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -17,6 +17,9 @@ S3method(arrange,soma_adat) S3method(count,soma_adat) S3method(filter,soma_adat) S3method(full_join,soma_adat) +S3method(getAdatVersion,default) +S3method(getAdatVersion,list) +S3method(getAdatVersion,soma_adat) S3method(getAnalytes,character) S3method(getAnalytes,data.frame) S3method(getAnalytes,default) diff --git a/R/adat-helpers.R b/R/adat-helpers.R index 0c58326..803f7fb 100644 --- a/R/adat-helpers.R +++ b/R/adat-helpers.R @@ -1,6 +1,6 @@ #' Helpers to Extract Information from an ADAT #' -#' Retrieves elements of the `HEADER` attributes of the object:\cr\cr +#' Retrieve elements of the `HEADER` attribute of a `soma_adat` object:\cr\cr #' [getAdatVersion()] determines the the ADAT version #' number from a parsed ADAT header.\cr\cr #' [getSomaScanVersion()] determines the original SomaScan assay version @@ -18,23 +18,34 @@ #' \cr #' [getSignalSpace()] determines the current signal space of #' the RFU values, which may differ from the original SomaScan -#' signal space if the data have been lifted. See [lift_adat()]. +#' signal space if the data have been lifted. See [lift_adat()] and +#' `vignette("lifting-and-bridging", package = "SomaDataIO")`. #' #' @name adat-helpers -#' @param atts The *attributes* of a `soma_adat` object. +#' @param x Either a `soma_adat` object with intact attributes or +#' the attributes themselves of a `soma_adat` object. #' @return #' \item{[getAdatVersion()]}{The key-value of the `Version` as a string.} #' @author Stu Field #' @examples -#' atts <- attributes(example_data) -#' getAdatVersion(atts) +#' getAdatVersion(example_data) #' -#' atts$Header.Meta$HEADER$Version <- "99.0" -#' getAdatVersion(atts) +#' attr(example_data, "Header.Meta")$HEADER$Version <- "99.9" +#' getAdatVersion(example_data) #' @export -getAdatVersion <- function(atts) { +getAdatVersion <- function(x) UseMethod("getAdatVersion") - x <- atts$Header.Meta$HEADER + +#' @export +getAdatVersion.default <- function(x) { + stop("Unable to find a method for class: ", .value(class(x)), call. = FALSE) +} + + +#' @export +getAdatVersion.list <- function(x) { + + x <- x$Header.Meta$HEADER vidx <- grep("^Version$|^AdatVersion$", names(x)) if ( length(vidx) == 0L ) { @@ -64,6 +75,13 @@ getAdatVersion <- function(atts) { } +#' @export +getAdatVersion.soma_adat <- function(x) { + stopifnot("Attributes for `x` must be instact." = is_intact_attr(x)) + getAdatVersion(attributes(x)) +} + + #' Gets the SomaScan version #' #' @rdname adat-helpers diff --git a/man/adat-helpers.Rd b/man/adat-helpers.Rd index 1430ba7..29408a4 100644 --- a/man/adat-helpers.Rd +++ b/man/adat-helpers.Rd @@ -9,7 +9,7 @@ \alias{getSomaScanLiftCCC} \title{Helpers to Extract Information from an ADAT} \usage{ -getAdatVersion(atts) +getAdatVersion(x) getSomaScanVersion(adat) @@ -20,7 +20,8 @@ checkSomaScanVersion(ver) getSomaScanLiftCCC(matrix = c("plasma", "serum")) } \arguments{ -\item{atts}{The \emph{attributes} of a \code{soma_adat} object.} +\item{x}{Either a \code{soma_adat} object with intact attributes or +the attributes themselves of a \code{soma_adat} object.} \item{adat}{A \code{soma_adat} object (with intact attributes), typically created using \code{\link[=read_adat]{read_adat()}}.} @@ -44,7 +45,7 @@ typically created using \code{\link[=read_adat]{read_adat()}}.} \code{serum} or \code{plasma} CCC between various versions of the SomaScan assay.} } \description{ -Retrieves elements of the \code{HEADER} attributes of the object:\cr\cr +Retrieve elements of the \code{HEADER} attribute of a \code{soma_adat} object:\cr\cr \code{\link[=getAdatVersion]{getAdatVersion()}} determines the the ADAT version number from a parsed ADAT header.\cr\cr \code{\link[=getSomaScanVersion]{getSomaScanVersion()}} determines the original SomaScan assay version @@ -62,18 +63,18 @@ Table of SomaScan Assay versions: \cr \code{\link[=getSignalSpace]{getSignalSpace()}} determines the current signal space of the RFU values, which may differ from the original SomaScan -signal space if the data have been lifted. See \code{\link[=lift_adat]{lift_adat()}}. +signal space if the data have been lifted. See \code{\link[=lift_adat]{lift_adat()}} and +\code{vignette("lifting-and-bridging", package = "SomaDataIO")}. \code{\link[=getSomaScanLiftCCC]{getSomaScanLiftCCC()}} accesses the lifting Concordance Correlation Coefficients between various SomaScan versions. For more about CCC metrics see \code{\link[=lift_adat]{lift_adat()}}. } \examples{ -atts <- attributes(example_data) -getAdatVersion(atts) +getAdatVersion(example_data) -atts$Header.Meta$HEADER$Version <- "99.0" -getAdatVersion(atts) +attr(example_data, "Header.Meta")$HEADER$Version <- "99.9" +getAdatVersion(example_data) ver <- getSomaScanVersion(example_data) ver diff --git a/man/soma_adat.Rd b/man/soma_adat.Rd index db9f463..e1262fb 100644 --- a/man/soma_adat.Rd +++ b/man/soma_adat.Rd @@ -91,14 +91,16 @@ dispatched on class \code{soma_adat}. Below is a list of \emph{all} currently available S3 methods that dispatch on the \code{soma_adat} class: -\if{html}{\out{
}}\preformatted{#> [1] [ [[ [[<- [<- == -#> [6] $ $<- anti_join arrange count -#> [11] filter full_join getAnalytes getMeta group_by -#> [16] inner_join is_seqFormat left_join Math median -#> [21] merge mutate Ops print rename -#> [26] right_join row.names<- sample_frac sample_n select -#> [31] semi_join separate slice_sample slice summary -#> [36] Summary transform ungroup unite +\if{html}{\out{
}}\preformatted{#> [1] [ [[ [[<- [<- +#> [5] == $ $<- anti_join +#> [9] arrange count filter full_join +#> [13] getAdatVersion getAnalytes getMeta group_by +#> [17] inner_join is_seqFormat left_join Math +#> [21] median merge mutate Ops +#> [25] print rename right_join row.names<- +#> [29] sample_frac sample_n select semi_join +#> [33] separate slice_sample slice summary +#> [37] Summary transform ungroup unite #> see '?methods' for accessing help and source code }\if{html}{\out{
}} diff --git a/tests/testthat/test-getAdatVersion.R b/tests/testthat/test-getAdatVersion.R index 857ab52..8c7b6a3 100644 --- a/tests/testthat/test-getAdatVersion.R +++ b/tests/testthat/test-getAdatVersion.R @@ -33,3 +33,16 @@ test_that("`getAdatVersion()` catches JAVA version number format", { fixed = TRUE ) }) + +test_that("`getAdatVersion()` S3 method returns the same character", { + expect_equal( + getAdatVersion(example_data), # soma_adat + getAdatVersion(attributes(example_data)) # list + ) +}) + +test_that("`getAdatVersion()` S3 default method trips errror", { + expect_error( + getAdatVersion(""), "Unable to find a method for class: 'character'" + ) +})