diff --git a/DESCRIPTION b/DESCRIPTION index 8999698..2c7b1d8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: romic Type: Package Title: R for High-Dimensional Omic Data -Version: 1.1.4.9000 +Version: 1.2.0 Authors@R: c( person( given = "Sean", diff --git a/NAMESPACE b/NAMESPACE index a3e96c3..a94cc12 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -36,6 +36,7 @@ export(plot_heatmap) export(plot_univariate) export(plotsaverInput) export(plotsaverServer) +export(reform_tidy_omic) export(remove_missing_values) export(shiny_filter_test) export(shiny_ggbiv_test) diff --git a/R/app_heatmap.R b/R/app_heatmap.R index 26bfedc..97c9d37 100644 --- a/R/app_heatmap.R +++ b/R/app_heatmap.R @@ -338,12 +338,12 @@ plot_heatmap <- function( checkmate::assertNumber(max_display_features) if ("NULL" %in% class(x_label)) { - x_label <- feature_var + x_label <- sample_var } checkmate::assertMultiClass(x_label, c("character", "expression")) if ("NULL" %in% class(y_label)) { - y_label <- sample_var + y_label <- feature_var } checkmate::assertMultiClass(y_label, c("character", "expression")) @@ -470,7 +470,7 @@ plot_heatmap <- function( return(heatmap_plot) } else if (plot_type == "plotly") { suppressWarnings( - plotly::ggplotly(heatmap_plot) %>% + plotly::ggplotly(p = heatmap_plot) %>% plotly::layout(margin = 0) ) } else { diff --git a/R/data_classes.R b/R/data_classes.R index e4a9869..f17ce37 100644 --- a/R/data_classes.R +++ b/R/data_classes.R @@ -638,6 +638,11 @@ triple_to_tidy <- function(triple_omic) { output$data <- tidy_output output$design <- triple_omic$design + if ("unstructured" %in% names(triple_omic)) { + # copy unstructured data as-is + output$unstructured <- triple_omic$unstructured + } + class(output) <- c("tidy_omic", "tomic", class(triple_omic)[3]) output @@ -691,6 +696,11 @@ tidy_to_triple <- function(tidy_omic) { design = tidy_omic$design ) + if ("unstructured" %in% names(tidy_omic)) { + # copy unstructured data as-is + output$unstructured <- tidy_omic$unstructured + } + class(output) <- c("triple_omic", "tomic", class(tidy_omic)[3]) output @@ -1009,3 +1019,40 @@ infer_tomic_table_type <- function(tomic, tomic_table) { return(table_type) } +#' Reform Tidy Omic +#' +#' This function recreates a `tidy_omic` object from the "data" and "design" +#' attributes of this object. +#' +#' @param tidy_data A tibble containing measurements along with sample metadata. This +#' table can be obtained as the "data" attribute from a romic "tidy_omic" object. +#' @inheritParams check_design +#' +#' @details This is handy for passing data and metadata through approaches like parsnip +#' which expect data to be formatted as a data.frame +#' +#' @examples +#' tidy_data <- romic::brauer_2008_tidy$data +#' reform_tidy_omic(tidy_data, romic::brauer_2008_tidy$design) +#' +#' @export +reform_tidy_omic <- function(tidy_data, tomic_design) { + + checkmate::assertTibble(tidy_data) + romic::check_design(tomic_design) + + feature_attributes <- tomic_design$features$variable[tomic_design$features$type != "feature_primary_key"] + sample_attributes <- tomic_design$samples$variable[tomic_design$samples$type != "sample_primary_key"] + + tidy_omic <- romic::create_tidy_omic( + df = tidy_data, + feature_pk = tomic_design$feature_pk, + feature_vars = feature_attributes, + sample_pk = tomic_design$sample_pk, + sample_vars = sample_attributes, + verbose = FALSE + ) + + return(tidy_omic) +} + diff --git a/R/dim_reduction.R b/R/dim_reduction.R index 36a1a6b..cda4041 100644 --- a/R/dim_reduction.R +++ b/R/dim_reduction.R @@ -251,7 +251,9 @@ impute_missing_values <- function( tomic, impute_var_name = "imputed", value_var = NULL, - ...) { + ... + ){ + if (!("impute" %in% rownames(utils::installed.packages()))) { stop("Install \"impute\" using remotes::install_bioc(\"impute\") to use this function") } @@ -309,6 +311,20 @@ impute_missing_values <- function( ) %>% dplyr::as_tibble() + # coerce feature and/or sample variables to their original classes + # (they will be converted to characters by impute.knn) + + imputed_measurements[[feature_pk]] <- coerce_to_classes( + imputed_measurements[[feature_pk]], + triple_omic$features[[feature_pk]] + ) + + imputed_measurements[[sample_pk]] <- coerce_to_classes( + imputed_measurements[[sample_pk]], + triple_omic$samples[[sample_pk]] + ) + + # if in-place imputation is desired then remove the old variable updated_measurements <- triple_omic$measurements if (value_var == impute_var_name) { updated_measurements <- updated_measurements %>% diff --git a/man/reform_tidy_omic.Rd b/man/reform_tidy_omic.Rd new file mode 100644 index 0000000..f6cc003 --- /dev/null +++ b/man/reform_tidy_omic.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data_classes.R +\name{reform_tidy_omic} +\alias{reform_tidy_omic} +\title{Reform Tidy Omic} +\usage{ +reform_tidy_omic(tidy_data, tomic_design) +} +\arguments{ +\item{tidy_data}{A tibble containing measurements along with sample metadata. This +table can be obtained as the "data" attribute from a romic "tidy_omic" object.} + +\item{tomic_design}{a list with named attributes describing feature, +sample, and measurement variables.} +} +\description{ +This function recreates a `tidy_omic` object from the "data" and "design" +attributes of this object. +} +\details{ +This is handy for passing data and metadata through approaches like parsnip +which expect data to be formatted as a data.frame +} +\examples{ +tidy_data <- romic::brauer_2008_tidy$data +reform_tidy_omic(tidy_data, romic::brauer_2008_tidy$design) + +} diff --git a/tests/testthat/test-data_classes.R b/tests/testthat/test-data_classes.R index 9f43aea..1d38d92 100644 --- a/tests/testthat/test-data_classes.R +++ b/tests/testthat/test-data_classes.R @@ -111,6 +111,19 @@ test_that("Create triple omic", { sample_pk = "sample_id" ) + testthat::expect_s3_class(simple_triple, "triple_omic") +}) + +test_that("Unstructured data preserved using tomic_to", { + + triple_omic <- tomic_to(simple_triple, "triple_omic") + triple_omic$unstructured$dat <- "test" + + tidy_copy <- triple_to_tidy(triple_omic) + expect_equal(tidy_copy$unstructured$dat, "test") + + triple_restore <- tidy_to_triple(tidy_copy) + expect_equal(triple_restore$unstructured$dat, "test") }) @@ -159,3 +172,10 @@ test_that("Test that get_tomic_table() can retrieve various tables", { error = TRUE ) }) + +test_that("reform_tidy_omic() can create a tidy_omic object from its attributes", { + tidy_data <- romic::brauer_2008_tidy$data + tomic <- reform_tidy_omic(tidy_data, romic::brauer_2008_tidy$design) + + expect_s3_class(tomic, "tidy_omic") +}) diff --git a/tests/testthat/test-dim_reduction.R b/tests/testthat/test-dim_reduction.R index f7c0a50..f77db3b 100644 --- a/tests/testthat/test-dim_reduction.R +++ b/tests/testthat/test-dim_reduction.R @@ -26,3 +26,18 @@ test_that("Removing missing values works", { expect_equal(nrow(filtered_brauer$samples), 34) expect_equal(nrow(filtered_brauer$measurements), 15674) }) + +test_that("Matrices keys are reconstructed with appropriate classes", { + + tomic_with_missing_values <- simple_triple + tomic_with_missing_values$measurements <- tomic_with_missing_values$measurements %>% + dplyr::slice(-c(1:5)) + + if (!("impute" %in% rownames(utils::installed.packages()))) { + cli::cli_alert("{.pkg impute} is not available for testing; imputation tests will be skipped") + } else{ + imputed_tomic <- impute_missing_values(tomic_with_missing_values) + expect_s3_class(imputed_tomic, "triple_omic") + } + +})