Skip to content

Commit

Permalink
fix: handle rownames in heatmap plot
Browse files Browse the repository at this point in the history
  • Loading branch information
kelly-sovacool committed Feb 7, 2025
1 parent 6c01565 commit 0a4ffd7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
7 changes: 4 additions & 3 deletions R/batch-correction.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#' batch_correct_counts(
#' count_type = "norm",
#' sub_count_type = "voom",
#' covariates_colname = "Group",
#' covariates_colnames = "Group",
#' batch_colname = "Batch",
#' label_colname = "Label"
#' )
Expand Down Expand Up @@ -160,9 +160,10 @@ batch_correct_counts <- function(moo,
counts_dat = combat_edata,
sample_metadata = sample_metadata,
sample_id_colname = sample_id_colname,
feature_id_colname = feature_id_colname,
group_colname = group_colname,
label_colname = label_colname,
anno_column = group_colname,
anno_colors = colors_for_plots
color_values = colors_for_plots
) + ggplot2::labs(caption = "batch-corrected counts")

print(pca_plot)
Expand Down
9 changes: 7 additions & 2 deletions R/heatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#' @return heatmap ggproto object
#' @keywords internal
#'
plot_heatmap <- function(counts_dat, sample_metadata,
plot_heatmap <- function(counts_dat,
sample_metadata,
sample_id_colname = NULL,
feature_id_colname = NULL,
group_colname = "Group",
Expand Down Expand Up @@ -44,7 +45,11 @@ plot_heatmap <- function(counts_dat, sample_metadata,
old <- sample_metadata[[sample_id_colname]]
new <- sample_metadata[[label_colname]]
names(old) <- new
counts_dat <- dplyr::rename(counts_dat, tidyselect::any_of(old))
counts_dat %<>% dplyr::rename(tidyselect::any_of(old))
if (feature_id_colname %in% colnames(counts_dat)) {
counts_dat %<>%
tibble::column_to_rownames(var = feature_id_colname)
}

mat <- as.matrix(counts_dat)
tcounts <- t(mat)
Expand Down
18 changes: 10 additions & 8 deletions R/normalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,24 @@ normalize_counts <- function(moo,
) + ggplot2::labs(caption = "normalized counts")
print(pca_plot)
hist_plot <- plot_histogram(
df.voom,
sample_metadata,
feature_id_colname,
group_colname,
label_colname,
counts_dat = df.voom,
sample_metadata = sample_metadata,
sample_id_colname = sample_id_colname,
feature_id_colname = feature_id_colname,
group_colname = group_colname,
label_colname = label_colname,
color_values = colors_for_plots,
x_axis_label = "Normalized Counts"
) + ggplot2::labs(caption = "normalized counts")
print(hist_plot)
corHM_plot <- plot_heatmap(
counts_dat = df.filt,
sample_metadata = sample_metadata,
anno_colors = colors_for_plots,
anno_column = group_colname,
sample_id_colname = sample_id_colname,
feature_id_colname = feature_id_colname,
group_colname = group_colname,
label_colname = label_colname,
sample_id_colname = sample_id_colname
color_values = colors_for_plots
) + ggplot2::labs(caption = "normalized counts")
print(corHM_plot)
}
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-batch-correction.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ test_that("batch_correction works for NIDAP", {
batch_correct_counts(
count_type = "norm",
sub_count_type = "voom",
covariates_colname = "Group",
covariates_colnames = "Group",
batch_colname = "Batch",
label_colname = "Label"
label_colname = "Label",
print_plots = TRUE
)
# TODO: getting different results than nidap_batch_corrected_counts
expect_true(all.equal(
Expand Down

0 comments on commit 0a4ffd7

Please sign in to comment.