Skip to content

Commit ec7805c

Browse files
committed
Fix bug & Added cluster_col to show_group_enrichment().
1 parent e5cd187 commit ec7805c

7 files changed

+74
-25
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,5 @@ Encoding: UTF-8
9393
LazyData: true
9494
Roxygen: list(markdown = TRUE, roclets = c("collate", "namespace", "rd",
9595
"roxytest::testthat_roclet"))
96-
RoxygenNote: 7.2.3
96+
RoxygenNote: 7.3.1
9797
Config/testthat/edition: 3

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(print,bytes)
34
S3method(sig_tally,CopyNumber)
45
S3method(sig_tally,MAF)
56
S3method(sig_tally,RS)

NEWS.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# sigminer 2.3.1
22

3-
- - Fixed the error in generating DBS and INDEL matrix when only one sample input (#453).
3+
- Added `cluster_col` to `show_group_enrichment()`.
4+
- Fixed the bug that error returned when `cluster_row = TRUE` & `return_list = TRUE` in function `show_group_enrichment()`.
5+
- Fixed the error in generating DBS and INDEL matrix when only one sample input (#453).
46

57
# sigminer 2.3.0
68

R/show_group_enrichment.R

+64-22
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#' @param cut_labels when `cut_p_value` is `TRUE`, this option set the labels.
2121
#' @param fill_scale a `Scale` object generated by `ggplot2` package to
2222
#' set color for continuous values.
23-
#' @param cluster_row if `TRUE`, cluster rows with Hierarchical Clustering ('complete' method).
23+
#' @param cluster_row,cluster_col if `TRUE`, cluster rows (or columns) with Hierarchical Clustering ('complete' method).
2424
#' @param ... other parameters passing to [ggplot2::facet_wrap], only used
2525
#' when `return_list` is `FALSE`.
2626
#'
@@ -40,12 +40,13 @@ show_group_enrichment <- function(df_enrich,
4040
midpoint = ifelse(fill_by_p_value, 0, 1)
4141
),
4242
cluster_row = FALSE,
43+
cluster_col = FALSE,
4344
...) {
4445
if (fill_by_p_value) {
4546
df_enrich$p_value_up <- if (use_fdr) {
46-
ifelse(df_enrich$fdr == 0, abs(log10(df_enrich$fdr + .Machine$double.xmin)), abs(log10(df_enrich$fdr)))
47+
ifelse(df_enrich$fdr == 0, abs(log10(df_enrich$fdr + .Machine$double.xmin)), abs(log10(df_enrich$fdr)))
4748
} else {
48-
ifelse(df_enrich$p_value == 0, abs(log10(df_enrich$p_value + .Machine$double.xmin)), abs(log10(df_enrich$p_value)))
49+
ifelse(df_enrich$p_value == 0, abs(log10(df_enrich$p_value + .Machine$double.xmin)), abs(log10(df_enrich$p_value)))
4950
}
5051
df_enrich$p_value_up <- data.table::fifelse(
5152
df_enrich$measure_observed >= 1,
@@ -68,7 +69,8 @@ show_group_enrichment <- function(df_enrich,
6869
cut_labels = cut_labels,
6970
add_text_annotation = add_text_annotation,
7071
use_fdr = use_fdr,
71-
cluster_row = cluster_row
72+
cluster_row = cluster_row,
73+
cluster_col = cluster_col
7274
)
7375
) -> xx
7476
p <- xx$gg
@@ -83,7 +85,8 @@ show_group_enrichment <- function(df_enrich,
8385
cut_labels = cut_labels,
8486
add_text_annotation = add_text_annotation,
8587
use_fdr = use_fdr,
86-
cluster_row = cluster_row
88+
cluster_row = cluster_row,
89+
cluster_col = cluster_col
8790
) +
8891
facet_wrap(~grp_var, scales = scales, ...)
8992
}
@@ -98,7 +101,8 @@ plot_enrichment_simple <- function(data, x, y, fill_scale,
98101
cut_labels = c("< -10", "< -1.3", "nosig", "> 1.3", "> 10"),
99102
add_text_annotation = TRUE,
100103
use_fdr = TRUE,
101-
cluster_row = FALSE) {
104+
cluster_row = FALSE,
105+
cluster_col = FALSE) {
102106
if (fill_by_p_value) {
103107
data$measure_observed <- round(data$measure_observed, 2)
104108
} else {
@@ -116,30 +120,68 @@ plot_enrichment_simple <- function(data, x, y, fill_scale,
116120
)
117121
}
118122

123+
get_cluster_order <- function(x, bycol = FALSE) {
124+
x <- x %>%
125+
tibble::column_to_rownames("grp1")
126+
if (min(dim(x)) < 2) {
127+
#warning("clustering is auto-disabled when any dim <2.", immediate. = TRUE)
128+
message("clustering is auto-disabled when any dim <2.")
129+
return(rownames(x))
130+
}
131+
132+
if (bycol) x = t(x)
133+
obj <- x %>%
134+
scale() %>%
135+
stats::dist() %>%
136+
stats::hclust() %>%
137+
stats::as.dendrogram()
138+
rownames(x)[stats::order.dendrogram(obj)]
139+
}
140+
119141
# 支持行聚类(subgroup)
120142
if (isTRUE(cluster_row)) {
121-
data2 <- data[, c(x, y, "grp_var", "measure_observed"), with = F]
143+
has_grp_var = "grp_var" %in% colnames(data)
144+
data2 <- data[, c(x, y, if (has_grp_var) "grp_var", "measure_observed"), with = F]
122145
data2 <- tidyr::pivot_wider(data2, names_from = x, values_from = "measure_observed")
123146

124-
get_cluster_order <- function(x) {
125-
x <- x %>%
126-
tibble::column_to_rownames("grp1")
127-
obj <- x %>%
128-
scale() %>%
129-
stats::dist() %>%
130-
stats::hclust() %>%
131-
stats::as.dendrogram()
132-
rownames(x)[stats::order.dendrogram(obj)]
147+
if (has_grp_var) {
148+
orders <- data2 %>%
149+
dplyr::group_split(.data$grp_var, .keep = FALSE) %>%
150+
purrr::map(get_cluster_order) %>%
151+
purrr::reduce(c) %>%
152+
unique()
153+
} else {
154+
orders <- get_cluster_order(data2) |>
155+
unique()
133156
}
134-
orders <- data2 %>%
135-
dplyr::group_split(.data$grp_var, .keep = FALSE) %>%
136-
purrr::map(get_cluster_order) %>%
137-
purrr::reduce(c) %>%
138-
unique()
139-
message("All subgroup orders: ", paste(orders, collapse = ", "))
157+
158+
message("subgroup orders: ", paste(orders, collapse = ", "))
140159
data$grp1 <- factor(data$grp1, levels = orders)
141160
}
142161

162+
# 支持列聚类(variable)
163+
if (isTRUE(cluster_col)) {
164+
has_grp_var = "grp_var" %in% colnames(data)
165+
data2 <- data[, c(x, y, if (has_grp_var) "grp_var", "measure_observed"), with = F]
166+
data2 <- tidyr::pivot_wider(data2, names_from = x, values_from = "measure_observed")
167+
168+
if (has_grp_var) {
169+
orders <- data2 %>%
170+
dplyr::group_split(.data$grp_var, .keep = FALSE) %>%
171+
purrr::map(get_cluster_order, bycol = TRUE) %>%
172+
purrr::reduce(c) %>%
173+
unique()
174+
} else {
175+
orders <- get_cluster_order(data2, bycol = TRUE) |>
176+
unique()
177+
}
178+
179+
message("variable orders: ", paste(orders, collapse = ", "))
180+
# 如果有多个 grp_var,enrich_var的顺序会在不同的grp_var中不同,仅使用第一个
181+
message(" - clustering column is suitable for case with one grp_var or return_list is TRUE.")
182+
data$enrich_var <- factor(data$enrich_var, levels = orders)
183+
}
184+
143185
p <- ggplot(
144186
data,
145187
aes_string(

R/utils_mem.R

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ show_bytes <- function(x) {
2626
structure(x, class = "bytes")
2727
}
2828

29+
#' @export
2930
print.bytes <- function(x, digits = 3, ...) {
3031
power <- min(floor(log(abs(x), 1000)), 4)
3132
if (power < 1) {

man/show_group_enrichment.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sigminer-package.Rd

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)