Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add biomarker colname meta-data and associated methods #198

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ S3method(autoplot,seroincidence.by)
S3method(autoplot,summary.seroincidence.by)
S3method(get_age,pop_data)
S3method(get_age_var,pop_data)
S3method(get_biomarker_levels,pop_data)
S3method(get_biomarker_names,pop_data)
S3method(get_biomarker_names_var,pop_data)
S3method(get_id,pop_data)
S3method(get_id_var,pop_data)
S3method(get_value,pop_data)
Expand All @@ -17,6 +20,7 @@ S3method(print,seroincidence.by)
S3method(print,summary.pop_data)
S3method(print,summary.seroincidence.by)
S3method(set_age,pop_data)
S3method(set_biomarker_var,pop_data)
S3method(set_id,pop_data)
S3method(set_value,pop_data)
S3method(strata,seroincidence.by)
Expand Down
20 changes: 12 additions & 8 deletions R/as_pop_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
#' @returns a `pop_data` object (a [tibble::tbl_df] with extra attribute `antigen_isos`)
#' @export
#' @examples
#' xs_data <- load_pop_data("https://osf.io/download//n6cp3/")
#' library(magrittr)
#' xs_data <-
#' "https://osf.io/download//n6cp3/" %>%
#' readr::read_rds() %>%
#' as_pop_data()
#'
#' print(xs_data)
as_pop_data <- function(data,
antigen_isos = NULL,
age = "Age",
value = "result",
id = "index_id",
standardize = TRUE) {
antigen_isos = NULL,
age = "Age",
value = "result",
id = "index_id",
standardize = TRUE) {


pop_data <-
Expand All @@ -38,8 +42,8 @@ as_pop_data <- function(data,
pop_data <- pop_data %>%
set_age(age = age, standardize = standardize) %>%
set_value(value = value, standardize = standardize) %>%
set_id(id = id, standardize = standardize)
set_id(id = id, standardize = standardize) %>%
set_biomarker_var(biomarker = "antigen_iso", standardize = standardize)

return(pop_data)
}

103 changes: 71 additions & 32 deletions R/load_pop_data.R
Original file line number Diff line number Diff line change
@@ -1,47 +1,20 @@
#' Load a cross-sectional antibody survey data set
#'
#' @param file_path path to an RDS file containing a cross-sectional antibody survey data set, stored as a [data.frame()] or [tibble::tbl_df]
#' @param antigen_isos [character()] vector of antigen isotypes to be used in analyses
#' @param age a [character()] identifying the age column
#' @param id a [character()] identifying the id column
#' @param value a [character()] identifying the value column
#' @param standardize a [logical()] to determine standardization of columns
#' @returns a `pop_data` object (a [tibble::tbl_df] with extra attribute `antigen_isos`)
#' @inheritDotParams as_pop_data
#' @returns a `pop_data` object (a [tibble::tbl_df] with extra attributes)
#' @export
#' @examples
#' xs_data <- load_pop_data("https://osf.io/download//n6cp3/")
#'
#' print(xs_data)
load_pop_data <- function(file_path,
antigen_isos = NULL,
age = "Age",
value = "result",
id = "index_id",
standardize = TRUE) {
if (file_path %>% substr(1, 4) == "http") {
file_path <- url(file_path)
}
...) {

pop_data <-
file_path %>%
readRDS() %>%
tibble::as_tibble()

class(pop_data) <-
c("pop_data", class(pop_data))

if (is.null(antigen_isos)) {
antigen_isos <- unique(pop_data$antigen_iso)
} else {
stopifnot(all(is.element(antigen_isos, pop_data$antigen_iso)))
}

attr(pop_data, "antigen_isos") <- antigen_isos

pop_data <- pop_data %>%
set_age(age = age, standardize = standardize) %>%
set_value(value = value, standardize = standardize) %>%
set_id(id = id, standardize = standardize)
readr::read_rds() %>%
as_pop_data(...)

return(pop_data)
}
Expand Down Expand Up @@ -118,6 +91,72 @@
return(id_var)
}

set_biomarker_var <- function(object, ...) {
UseMethod("set_biomarker_var", object)
}

#' @export
set_biomarker_var.pop_data = function(object,
biomarker = "antigen_iso",
standardize = TRUE,
...)
{
if (biomarker %in% colnames(object))
{
attr(object, "biomarker_var") <- biomarker
} else
{
cli::cli_abort('data does not include column "{biomarker}"')

Check warning on line 109 in R/load_pop_data.R

View check run for this annotation

Codecov / codecov/patch

R/load_pop_data.R#L109

Added line #L109 was not covered by tests
}

if (standardize)
{
object <- object %>%
rename(c("antigen_iso" = attr(object, "biomarker_var")))

# update attribute
attr(object, "biomarker_var") <- "antigen_iso"
}

return(object)

}

get_biomarker_levels <- function(object, ...)
{
UseMethod("get_biomarker_levels", object)

Check warning on line 127 in R/load_pop_data.R

View check run for this annotation

Codecov / codecov/patch

R/load_pop_data.R#L127

Added line #L127 was not covered by tests
}

#' @export
get_biomarker_levels.pop_data <- function(object, ...)
{
attr(object, "antigen_isos")

Check warning on line 133 in R/load_pop_data.R

View check run for this annotation

Codecov / codecov/patch

R/load_pop_data.R#L133

Added line #L133 was not covered by tests
}

get_biomarker_names <- function(object, ...) {
UseMethod("get_biomarker_names", object)

Check warning on line 137 in R/load_pop_data.R

View check run for this annotation

Codecov / codecov/patch

R/load_pop_data.R#L137

Added line #L137 was not covered by tests
}

#' @export
get_biomarker_names.pop_data <- function(object, ...) {
# get biomarker name data
biomarker_data <- object %>% pull(get_biomarker_names_var(object))

Check warning on line 143 in R/load_pop_data.R

View check run for this annotation

Codecov / codecov/patch

R/load_pop_data.R#L143

Added line #L143 was not covered by tests

return(biomarker_data)

Check warning on line 145 in R/load_pop_data.R

View check run for this annotation

Codecov / codecov/patch

R/load_pop_data.R#L145

Added line #L145 was not covered by tests
}

get_biomarker_names_var <- function(object, ...) {
UseMethod("get_biomarker_names_var", object)

Check warning on line 149 in R/load_pop_data.R

View check run for this annotation

Codecov / codecov/patch

R/load_pop_data.R#L149

Added line #L149 was not covered by tests
}

#' @export
get_biomarker_names_var.pop_data <- function(object, ...) {
# get value attribute
biomarker_var <- attributes(object)[["biomarker_var"]]

Check warning on line 155 in R/load_pop_data.R

View check run for this annotation

Codecov / codecov/patch

R/load_pop_data.R#L155

Added line #L155 was not covered by tests

return(biomarker_var)

Check warning on line 157 in R/load_pop_data.R

View check run for this annotation

Codecov / codecov/patch

R/load_pop_data.R#L157

Added line #L157 was not covered by tests
}


set_age <- function(object, ...) {
UseMethod("set_age", object)
Expand Down
6 changes: 5 additions & 1 deletion man/as_pop_data.Rd

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

30 changes: 12 additions & 18 deletions man/load_pop_data.Rd

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

Loading