Skip to content

Commit a2e24b5

Browse files
committed
Fix group wise normalization annotation shuffling bug
Annotations were erroneously shuffled after group-wise normalization. This is now fixed by preserving the original column order.
1 parent a9ac5ab commit a2e24b5

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

R/sidebar_setup_helpers_normalization.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ normalize.data <- function(data, # a data matrix
5252
## group-specific normalization
5353
} else {
5454

55+
## store original column order to preserve it after normalization
56+
original.col.order <- colnames(data)
57+
5558
## extract groups
5659
groups <- unique(grp.vec)
5760

@@ -72,6 +75,9 @@ normalize.data <- function(data, # a data matrix
7275
}
7376
} ## end for
7477

78+
## reorder columns to match original order
79+
data.norm <- data.norm[, original.col.order, drop = FALSE]
80+
7581
} ## end else
7682

7783
return(data.norm)

tests/testthat/test-normalization.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ test_that("normalize.data handles group normalization", {
3636
expect_equal(dim(result_group), dim(test_data))
3737
})
3838

39+
test_that("normalize.data preserves column order after group normalization", {
40+
# Create test data with columns in a specific order
41+
# This tests the bug where columns were reordered by group
42+
test_data <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), nrow = 3, ncol = 4)
43+
rownames(test_data) <- paste0("gene_", 1:3)
44+
colnames(test_data) <- paste0("sample_", 1:4)
45+
46+
# Create group vector where groups appear in non-alphabetical order
47+
# Original order: sample_1 (B), sample_2 (A), sample_3 (B), sample_4 (A)
48+
# If unique() sorts alphabetically, it would process A then B, reordering columns
49+
test_grp_vec <- c("B", "A", "B", "A")
50+
names(test_grp_vec) <- colnames(test_data)
51+
52+
# Store original column order
53+
original_col_order <- colnames(test_data)
54+
55+
# Test group normalization
56+
result_group <- normalize.data(test_data, method = "Median", grp.vec = test_grp_vec)
57+
58+
# Verify column order is preserved
59+
expect_equal(colnames(result_group), original_col_order)
60+
expect_equal(dim(result_group), dim(test_data))
61+
})
62+
3963
test_that("normalize.data handles single group", {
4064
# Create test data with proper dimnames
4165
test_data <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)

0 commit comments

Comments
 (0)