Skip to content

Commit

Permalink
Create S3 method for getAdatVersion()
Browse files Browse the repository at this point in the history
- new S3 method to allow passing of different
  objects to the function:
  - namely `soma_adat` or `list`
- closes #92
  • Loading branch information
stufield committed Mar 7, 2024
1 parent 2bb91b2 commit f212bea
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 27 additions & 9 deletions R/adat-helpers.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 ) {
Expand Down Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions man/adat-helpers.Rd

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

18 changes: 10 additions & 8 deletions man/soma_adat.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test-getAdatVersion.R
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
)
})

0 comments on commit f212bea

Please sign in to comment.