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

Issue 76 - misc bugfixes #77

Merged
merged 4 commits into from
Apr 22, 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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions R/app_heatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
observe({
# need double parenthesis since its a reactive of a reactive
print(glue::glue(
"Filtering results: tidy_filtered_samples is {nrow(tidy_filtered_samples()()$data)} rows"

Check warning on line 108 in R/app_heatmap.R

View workflow job for this annotation

GitHub Actions / lint

file=R/app_heatmap.R,line=108,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 99 characters.
))
})

Expand Down Expand Up @@ -302,7 +302,7 @@
#' )
#' @export
plot_heatmap <- function(
tomic,

Check warning on line 305 in R/app_heatmap.R

View workflow job for this annotation

GitHub Actions / lint

file=R/app_heatmap.R,line=305,col=4,[indentation_linter] Indentation should be 2 spaces but is 4 spaces.
feature_var = NULL,
sample_var = NULL,
value_var = NULL,
Expand All @@ -315,7 +315,7 @@
x_label = NULL,
y_label = NULL,
colorbar_label = NULL
) {

Check warning on line 318 in R/app_heatmap.R

View workflow job for this annotation

GitHub Actions / lint

file=R/app_heatmap.R,line=318,col=4,[indentation_linter] Indentation should be 0 spaces but is 4 spaces.
checkmate::assertClass(tomic, "tomic")

if ("NULL" %in% class(feature_var)) {
Expand All @@ -338,12 +338,12 @@
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"))

Expand Down Expand Up @@ -410,7 +410,7 @@
nrow()

heatmap_theme <- theme_minimal() +
theme(

Check warning on line 413 in R/app_heatmap.R

View workflow job for this annotation

GitHub Actions / lint

file=R/app_heatmap.R,line=413,col=5,[indentation_linter] Indentation should be 4 spaces but is 5 spaces.
text = element_text(size = 16, color = "black"),
title = element_text(size = 20, color = "black"),
strip.text = element_text(size = 18),
Expand All @@ -423,7 +423,7 @@
theme(axis.text.y = element_blank())
} else {
heatmap_theme <- heatmap_theme +
theme(axis.text.y = element_text(size = pmin(20, 60 * sqrt(1 / n_features))))

Check warning on line 426 in R/app_heatmap.R

View workflow job for this annotation

GitHub Actions / lint

file=R/app_heatmap.R,line=426,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 83 characters.
}

if (n_samples > 200) {
Expand Down Expand Up @@ -470,7 +470,7 @@
return(heatmap_plot)
} else if (plot_type == "plotly") {
suppressWarnings(
plotly::ggplotly(heatmap_plot) %>%
plotly::ggplotly(p = heatmap_plot) %>%
plotly::layout(margin = 0)
)
} else {
Expand Down
47 changes: 47 additions & 0 deletions R/data_classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@
#' )
#' @export
create_tidy_omic <- function(
df,

Check warning on line 62 in R/data_classes.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data_classes.R,line=62,col=4,[indentation_linter] Indentation should be 2 spaces but is 4 spaces.
feature_pk,
feature_vars = NULL,
sample_pk,
sample_vars = NULL,
omic_type_tag = "general",
verbose = TRUE
) {

Check warning on line 69 in R/data_classes.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data_classes.R,line=69,col=4,[indentation_linter] Indentation should be 0 spaces but is 4 spaces.

checkmate::assertDataFrame(df)
checkmate::assertString(omic_type_tag)
Expand All @@ -86,7 +86,7 @@

n_var_uses <- table(c(feature_pk, feature_vars, sample_pk, sample_vars))
if (any(n_var_uses > 1)) {
invalid_vars <- names(n_var_uses)[n_var_uses > 1]

Check warning on line 89 in R/data_classes.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data_classes.R,line=89,col=5,[object_usage_linter] local variable 'invalid_vars' assigned but may not be used

cli::cli_abort(
"{paste(invalid_vars, collapse = ', ')} were assigned to multiple
Expand Down Expand Up @@ -134,7 +134,7 @@
measurement_vars <- setdiff(
output$design$measurements$variable,
c(feature_pk, sample_pk)
)

Check warning on line 137 in R/data_classes.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data_classes.R,line=137,col=6,[indentation_linter] Indentation should be 4 spaces but is 6 spaces.

message(glue::glue(
"{length(measurement_vars)} measurement variables were defined as the
Expand Down Expand Up @@ -638,6 +638,11 @@
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
Expand Down Expand Up @@ -691,6 +696,11 @@
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
Expand Down Expand Up @@ -1009,3 +1019,40 @@
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)
}

18 changes: 17 additions & 1 deletion R/dim_reduction.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 %>%
Expand Down
28 changes: 28 additions & 0 deletions man/reform_tidy_omic.Rd

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

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


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

})
Loading