Skip to content

Commit cfcdcb9

Browse files
committed
enh(CCCPlot): allow specifying aggregation method for magnitude score
1 parent 47f43e9 commit cfcdcb9

3 files changed

Lines changed: 54 additions & 52 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Suggests:
4343
scattermore,
4444
gridGraphics,
4545
iNEXT,
46-
metap
46+
metap (>= 1.11)
4747
LazyData: true
4848
LazyDataCompression: xz
4949
Remotes:

R/cccplot.R

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@
4141
#' @param specificity The column name in the data to use as the specificity of the communication.
4242
#' By default, the last column will be used.
4343
#' If the method doesn't have a specificity, set it to NULL.
44-
#' @param weighted Whether to use the magnitude as the weight of the aggregation interaction strength.
45-
#' for source-target pairs. Default is TRUE. Otherwise, the number of interactions will be used.
46-
#' Only used when `method` is "aggregation".
44+
#' @param magnitude_agg A function to aggregate the magnitude of the communication.
45+
#' Default is `sum`.
46+
#' @param magnitude_name The name of the magnitude in the plot.
47+
#' Default is "Total interaction strength".
4748
#' @param meta_specificity The method to calculate the specificity when there are multiple
4849
#' ligand-receptor pairs interactions. Default is "sumlog".
4950
#' It should be one of the methods in the `metap` package.
@@ -78,10 +79,11 @@
7879
#' set.seed(8525)
7980
#' data(cellphonedb_res)
8081
#' CCCPlot(data = cellphonedb_res, plot_type = "network", legend.position = "none",
81-
#' theme = "theme_blank", theme_args = list(add_coord = FALSE))
82+
#' theme = "theme_blank", theme_args = list(add_coord = FALSE))
8283
#' CCCPlot(cellphonedb_res, plot_type = "chord")
8384
#' CCCPlot(cellphonedb_res, plot_type = "heatmap")
84-
#' CCCPlot(cellphonedb_res, plot_type = "dot", weighted = FALSE)
85+
#' CCCPlot(cellphonedb_res, plot_type = "dot",
86+
#' magnitude_agg = mean, magnitude_name = "Average Interaction Length")
8587
#' CCCPlot(cellphonedb_res, plot_type = "sankey")
8688
#'
8789
#' cellphonedb_res_sub <- cellphonedb_res[
@@ -96,7 +98,8 @@ CCCPlot <- function(
9698
method = c("aggregation", "interaction"),
9799
magnitude = waiver(),
98100
specificity = waiver(),
99-
weighted = TRUE,
101+
magnitude_agg = sum,
102+
magnitude_name = "Total interaction strength",
100103
meta_specificity = "sumlog",
101104
split_by = NULL,
102105
x_text_angle = 90,
@@ -107,7 +110,7 @@ CCCPlot <- function(
107110
show_column_names = TRUE,
108111
...
109112
) {
110-
stopifnot("'facet_by' is not supported in CCCPlot." = is.null(facet_by))
113+
stopifnot("[CCCPlot] 'facet_by' is not supported." = is.null(facet_by))
111114

112115
plot_type <- match.arg(plot_type)
113116
method <- match.arg(method)
@@ -116,57 +119,50 @@ CCCPlot <- function(
116119
ligand_col <- check_columns(data, "ligand", force_factor = TRUE)
117120
receptor_col <- check_columns(data, "receptor", force_factor = TRUE)
118121

119-
stopifnot("Columns 'source', 'target', 'ligand', and 'receptor' are required." =
122+
stopifnot("[CCCPlot] Columns 'source', 'target', 'ligand', and 'receptor' are required." =
120123
!is.null(source_col) && !is.null(target_col) && !is.null(ligand_col) && !is.null(receptor_col))
121124

122125
if (inherits(magnitude, "waiver")) {
123126
magnitude <- names(data)[ncol(data) - 1]
127+
if (!is.numeric(data[[magnitude]])) {
128+
stop("[CCCPlot] The column '", magnitude, "' is not numeric, specify it explicitly.")
129+
}
124130
}
125131
if (inherits(specificity, "waiver")) {
126132
specificity <- names(data)[ncol(data)]
133+
if (!is.numeric(data[[specificity]])) {
134+
stop("[CCCPlot] The column '", specificity, "' is not numeric, specify it explicitly.")
135+
}
127136
}
128-
stopifnot("At least one of 'magnitude' and 'specificity' is required." =
137+
stopifnot("[CCCPlot] At least one of 'magnitude' and 'specificity' is required." =
129138
!inherits(magnitude, "waiver") || !inherits(specificity, "waiver"))
130139

131140
if (method == "aggregation") {
132141
links <- data %>% group_by(!!!syms(c(source_col, target_col, split_by)))
133-
if (!weighted || is.null(magnitude)) {
134-
link_weight_name = "No. of interactions"
135-
if (!is.null(specificity)) {
136-
metap_fn <- getFromNamespace(meta_specificity, "metap")
137-
links <- suppressWarnings({ links %>%
138-
filter(!is.na(!!sym(specificity))) %>%
139-
summarise(
140-
interactionStrength = n(),
141-
.specificity = if (n() == 1) !!sym(specificity) else metap_fn(!!sym(specificity))$p,
142-
.groups = "drop")%>%
143-
replace_na(list(.specificity = 0))
144-
})
145-
} else {
146-
links <- links %>%
147-
summarise(interactionStrength = n(), .groups = "drop")
148-
}
149-
} else {
150-
link_weight_name = "Interaction strength"
151-
if (!is.null(specificity)) {
152-
metap_fn <- getFromNamespace(meta_specificity, "metap")
153-
links <- suppressWarnings({ links %>%
154-
filter(!is.na(!!sym(specificity))) %>%
155-
summarise(
156-
interactionStrength = sum(!!sym(magnitude)),
157-
.specificity = if (n() == 1) !!sym(specificity) else metap_fn(!!sym(specificity))$p,
158-
.groups = "drop") %>%
159-
replace_na(list(.specificity = 0))
160-
})
161-
} else {
162-
links <- links %>%
163-
summarise(interactionStrength = sum(!!sym(magnitude)), .groups = "drop")
164-
}
142+
if (is.null(magnitude)) {
143+
magnitiude <- "mag_score"
144+
links[[magnitiude]] <- NA
165145
}
166146

147+
metap_fn <- getFromNamespace(meta_specificity, "metap")
148+
links <- suppressWarnings({ links %>%
149+
filter(!is.na(!!sym(specificity))) %>%
150+
summarise(
151+
interactionStrength = sum(!!sym(magnitude)),
152+
.specificity = if (is.null(specificity)) {
153+
NA
154+
} else if (n() == 1) {
155+
!!sym(specificity)
156+
} else {
157+
metap_fn(!!sym(specificity))$p
158+
},
159+
.groups = "drop") %>%
160+
replace_na(list(.specificity = 0))
161+
})
162+
167163
if (plot_type == "network") {
168164
Network(links, from = source_col, to = target_col, node_fill_by = "name", split_by = split_by,
169-
link_curvature = link_curvature, link_weight_name = link_weight_name, link_alpha = link_alpha,
165+
link_curvature = link_curvature, link_weight_name = magnitude_name, link_alpha = link_alpha,
170166
node_fill_name = "Source/Target", link_weight_by = "interactionStrength", ...)
171167
} else if (plot_type %in% c("chord", "circos")) {
172168
ChordPlot(links, y = "interactionStrength", from = source_col, to = target_col,
@@ -180,7 +176,7 @@ CCCPlot <- function(
180176
links <- pivot_wider(links, names_from = source_col, values_from = "interactionStrength",
181177
values_fill = 0)
182178
Heatmap(links, rows = sources, columns_by = target_col, rows_name = "source", split_by = split_by,
183-
name = link_weight_name, show_row_names = show_row_names, show_column_names = show_column_names,
179+
name = magnitude_name, show_row_names = show_row_names, show_column_names = show_column_names,
184180
...)
185181
} else if (plot_type %in% c("sankey", "alluvial")) {
186182
SankeyPlot(links, y = "interactionStrength", x = c(source_col, target_col), split_by = split_by,
@@ -189,13 +185,16 @@ CCCPlot <- function(
189185
if (!is.null(specificity)) {
190186
DotPlot(links, x = source_col, y = target_col, size_by = "interactionStrength",
191187
fill_by = ".specificity", fill_name = paste0(meta_specificity, "(", specificity, ")"),
192-
size_name = link_weight_name, x_text_angle = x_text_angle, split_by = split_by, ...)
188+
size_name = magnitude_name, x_text_angle = x_text_angle, split_by = split_by, ...)
193189
} else {
194190
DotPlot(links, x = source_col, y = target_col, size_by = "interactionStrength",
195-
size_name = link_weight_name, x_text_angle = x_text_angle, split_by = split_by, ...)
191+
size_name = magnitude_name, x_text_angle = x_text_angle, split_by = split_by, ...)
196192
}
197193
}
198194
} else if (method == "interaction") {
195+
stopifnot("[CCCPlot] 'magnitude' is required when 'method' is 'interaction'." =
196+
!is.null(magnitude))
197+
199198
if (plot_type == "dot") {
200199
if (!is.null(specificity)) {
201200
data[[specificity]] <- -log10(data[[specificity]])
@@ -220,7 +219,7 @@ CCCPlot <- function(
220219
show_row_names = show_row_names, show_column_names = show_column_names,
221220
...)
222221
} else {
223-
stop("Plot type '", plot_type, "' is not supported for method 'interaction' in CCCPlot yet.")
222+
stop("[CCCPlot] Plot type '", plot_type, "' is not supported for method 'interaction' yet.")
224223
}
225224
}
226225
}

man/CCCPlot.Rd

Lines changed: 8 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)