From 1ffc2b3b889d17b4087c512de9b6a0e36e4cdfab Mon Sep 17 00:00:00 2001 From: Stefano Mangiola Date: Mon, 13 Jan 2025 20:27:34 +1030 Subject: [PATCH] hotfix row and column ordering when griuping converting them to factors --- R/methods.R | 8 + R/utilities.R | 10 +- tests/testthat/Rplots.pdf | Bin 3611 -> 3611 bytes .../tests/grouped-annotated-heatmap-2.svg | 10 +- .../tests/grouped-custom-color-both.svg | 28 +- tests/testthat/_snaps/tests/legend.svg | 106 +++---- tests/testthat/_snaps/tests/multi-type.svg | 10 +- tests/testthat/_snaps/tests/sample-swap.svg | 167 +++++++++++ .../_snaps/tests/show-heatmap-legend.svg | 28 +- .../testthat/_snaps/tests/size-annotation.svg | 266 ++++++------------ .../tests/tile-factor-custom-palette.svg | 12 +- tests/testthat/tests.R | 19 ++ 12 files changed, 383 insertions(+), 281 deletions(-) create mode 100644 tests/testthat/_snaps/tests/sample-swap.svg diff --git a/R/methods.R b/R/methods.R index 592f508..1f8e7b0 100644 --- a/R/methods.R +++ b/R/methods.R @@ -323,6 +323,14 @@ heatmap_ <- .data |> + # Hotfix for wrong grouping of elements + # https://github.com/stemangiola/tidyHeatmap/issues/116 + # We have to find the root cause ASAP + mutate( + !!.column := as.factor(!!.column), + !!.row := as.factor(!!.row) + ) |> + # # Check if data is rectangular # ifelse_pipe( # !check_if_data_rectangular((.), !!.column, !!.row, !!.value), diff --git a/R/utilities.R b/R/utilities.R index 38e1483..caee135 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -715,6 +715,7 @@ get_top_left_annotation = function(.data_, .column, .row, .abundance, annotation ungroup() %>% mutate(color = map2(annot, idx, ~ { + if(.x %>% class %in% c("factor", "character", "logical")){ # If is colorRamp @@ -723,13 +724,8 @@ get_top_left_annotation = function(.data_, .column, .row, .abundance, annotation # If it is a list of colors else - if (is(.x, "factor")) { - color_vector <- palette_annotation$discrete[[.y]] %>% setNames(levels(.x)) - color_vector[!is.na(names(color_vector))] - - } else { - colorRampPalette(palette_annotation$discrete[[.y]])(length(unique(.x))) %>% setNames(unique(.x)) - } + colorRampPalette(palette_annotation$discrete[[.y]])(length(unique(.x))) %>% setNames(unique(.x)) + } else if (.x %>% class %in% c("integer", "numerical", "numeric", "double")){ diff --git a/tests/testthat/Rplots.pdf b/tests/testthat/Rplots.pdf index 15bd2b835916eabbf6d13443faf592d1eb393f11..893b7bfac53eb1e6b2968251ed098bc96a8fe6c1 100644 GIT binary patch delta 57 zcmbO&Gh1eYiMpwQp`o#nfsvV^ktUbEZ+?nPVo9okhKm(Y3`uTcv<|b0p5ex>iM#-z CsSb$% delta 57 zcmbO&Gh1eYiMp|YnUR^Hg@KWYxh9vsZ+?nPVo9okhKrSvff172#AqF6BRzwST@!f$ DrBDv0 diff --git a/tests/testthat/_snaps/tests/grouped-annotated-heatmap-2.svg b/tests/testthat/_snaps/tests/grouped-annotated-heatmap-2.svg index 40c04e4..3b94b1c 100644 --- a/tests/testthat/_snaps/tests/grouped-annotated-heatmap-2.svg +++ b/tests/testthat/_snaps/tests/grouped-annotated-heatmap-2.svg @@ -906,15 +906,15 @@ untreated1 paired-end - - + + single-read - - + + condition Intracellular @@ -1227,6 +1227,6 @@ treated untreated - + diff --git a/tests/testthat/_snaps/tests/grouped-custom-color-both.svg b/tests/testthat/_snaps/tests/grouped-custom-color-both.svg index c9a681b..a081570 100644 --- a/tests/testthat/_snaps/tests/grouped-custom-color-both.svg +++ b/tests/testthat/_snaps/tests/grouped-custom-color-both.svg @@ -911,18 +911,18 @@ - - - - + + + + condition - - - - - - - + + + + + + + type Intracellular @@ -1235,11 +1235,11 @@ treated untreated - + type paired-end single-read - - + + diff --git a/tests/testthat/_snaps/tests/legend.svg b/tests/testthat/_snaps/tests/legend.svg index d638904..4f71733 100644 --- a/tests/testthat/_snaps/tests/legend.svg +++ b/tests/testthat/_snaps/tests/legend.svg @@ -798,60 +798,60 @@ 11458 11450 11467 - - - - - - - - - - - - - + + + + + + + + + + + + + UBR - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Cell type diff --git a/tests/testthat/_snaps/tests/multi-type.svg b/tests/testthat/_snaps/tests/multi-type.svg index acf29f5..73bf03d 100644 --- a/tests/testthat/_snaps/tests/multi-type.svg +++ b/tests/testthat/_snaps/tests/multi-type.svg @@ -912,10 +912,10 @@ treated3 treated2 treated1 - - - - + + + + @@ -1333,6 +1333,6 @@ treated untreated - + diff --git a/tests/testthat/_snaps/tests/sample-swap.svg b/tests/testthat/_snaps/tests/sample-swap.svg new file mode 100644 index 0000000..1ef8a84 --- /dev/null +++ b/tests/testthat/_snaps/tests/sample-swap.svg @@ -0,0 +1,167 @@ + + + + + + + + + + + + + + + + + + +col +Compound_Name + + + + + + + + + + + + +L-homoserineAA +D-maltoseCH +cellobioseCH +raffinoseCH +pectinCH +1 + +AA + +CH +log2fc +-4 +-2 +0 +2 +4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/tests/show-heatmap-legend.svg b/tests/testthat/_snaps/tests/show-heatmap-legend.svg index 0eba09d..7085813 100644 --- a/tests/testthat/_snaps/tests/show-heatmap-legend.svg +++ b/tests/testthat/_snaps/tests/show-heatmap-legend.svg @@ -911,18 +911,18 @@ - - - - + + + + condition - - - - - - - + + + + + + + type Intracellular @@ -1120,11 +1120,11 @@ treated untreated - + type paired-end single-read - - + + diff --git a/tests/testthat/_snaps/tests/size-annotation.svg b/tests/testthat/_snaps/tests/size-annotation.svg index 60871f2..2b640f9 100644 --- a/tests/testthat/_snaps/tests/size-annotation.svg +++ b/tests/testthat/_snaps/tests/size-annotation.svg @@ -798,184 +798,96 @@ 11458 11450 11467 - - - - - - - - - - - - - + + + + + + + + + + + + + UBR - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Cell type -Cell type -E -F -M -T - - - - -UBR -11400 -11420 -11440 -11460 -11480 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Cell type +E +F +M +T + + + + +UBR +11405 +11420 +11425 +11427 +11433 +11437 +11438 +11444 +11450 +11452 +11453 +11458 +11467 + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/tests/tile-factor-custom-palette.svg b/tests/testthat/_snaps/tests/tile-factor-custom-palette.svg index edaddf7..ef284eb 100644 --- a/tests/testthat/_snaps/tests/tile-factor-custom-palette.svg +++ b/tests/testthat/_snaps/tests/tile-factor-custom-palette.svg @@ -913,10 +913,10 @@ untreated2 untreated1 - - - - + + + + group @@ -1041,8 +1041,8 @@ third_level fourth_level - - + + diff --git a/tests/testthat/tests.R b/tests/testthat/tests.R index f3a59b9..2bde6e4 100644 --- a/tests/testthat/tests.R +++ b/tests/testthat/tests.R @@ -764,3 +764,22 @@ test_that("text",{ layer_text( `count normalised adjusted log` < 6 & sample == "untreated3" , .value=my_text, .size = my_size) ) }) + + +test_that("sample_swap",{ + +df <- tribble(~Compound_Name, ~Compound_Class, ~col, ~log2fc, + "L-homoserineAA", "AA", 1, 2.93, + "cellobioseCH", "CH", 1, 2.09, + "D-maltoseCH", "CH", 1, 3.08, + "pectinCH", "CH", 1, -3.04, + "raffinoseCH", "CH", 1, -2.10) + +vdiffr::expect_doppelganger( + "sample_swap", + df %>% + group_by(Compound_Class) %>% + heatmap(.row = Compound_Name, .col = col, .value = log2fc) + ) + +}) \ No newline at end of file