Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

New aggregateNet of no-significant-pathway for netVisual_diffInteraction #688

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions R/analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,7 @@ mergeInteractions <- function(object, group.merged) {
#' ligand.pct.2: threshold for the percent of expressed genes in the cells except for the defined 'positive' cell group
#'
#' @param receptor.pvalues,receptor.logFC,receptor.pct.1,receptor.pct.2 set threshold for receptor genes
#' @param non.significant.pathway Inherited feature from \code{\link{aggregateNet}}. Default set to NULL
#' @importFrom dplyr select group_by summarize groups
#' @importFrom stringr str_split
#' @importFrom BiocGenerics as.data.frame
Expand Down Expand Up @@ -1962,9 +1963,9 @@ subsetCommunication <- function(object = NULL, net = NULL, slot.name = "net",
sources.use = NULL, targets.use = NULL,
signaling = NULL,
pairLR.use = NULL,
thresh = 0.05,
thresh = NULL,
datasets = NULL, ligand.pvalues = NULL, ligand.logFC = NULL, ligand.pct.1 = NULL, ligand.pct.2 = NULL,
receptor.pvalues = NULL, receptor.logFC = NULL, receptor.pct.1 = NULL, receptor.pct.2 = NULL) {
receptor.pvalues = NULL, receptor.logFC = NULL, receptor.pct.1 = NULL, receptor.pct.2 = NULL, non.significant.pathway = NULL) {
if (!is.null(pairLR.use)) {
if (!is.data.frame(pairLR.use)) {
stop("pairLR.use should be a data frame with a signle column named either 'interaction_name' or 'pathway_name' ")
Expand All @@ -1990,7 +1991,7 @@ subsetCommunication <- function(object = NULL, net = NULL, slot.name = "net",
pairLR.use = pairLR.use,
thresh = thresh,
datasets = datasets, ligand.pvalues = ligand.pvalues, ligand.logFC = ligand.logFC, ligand.pct.1 = ligand.pct.1, ligand.pct.2 = ligand.pct.2,
receptor.pvalues = receptor.pvalues, receptor.logFC = receptor.logFC, receptor.pct.1 = receptor.pct.1, receptor.pct.2 =receptor.pct.2)
receptor.pvalues = receptor.pvalues, receptor.logFC = receptor.logFC, receptor.pct.1 = receptor.pct.1, receptor.pct.2 =receptor.pct.2, non.significant.pathway = non.significant.pathway)
} else if (object@options$mode == "merged") {
if (is.null(net)) {
net0 <- slot(object, "net")
Expand All @@ -2007,7 +2008,7 @@ subsetCommunication <- function(object = NULL, net = NULL, slot.name = "net",
pairLR.use = pairLR.use,
thresh = thresh,
datasets = datasets, ligand.pvalues = ligand.pvalues, ligand.logFC = ligand.logFC, ligand.pct.1 = ligand.pct.1, ligand.pct.2 = ligand.pct.2,
receptor.pvalues = receptor.pvalues, receptor.logFC = receptor.logFC, receptor.pct.1 = receptor.pct.1, receptor.pct.2 =receptor.pct.2)
receptor.pvalues = receptor.pvalues, receptor.logFC = receptor.logFC, receptor.pct.1 = receptor.pct.1, receptor.pct.2 =receptor.pct.2, non.significant.pathway = non.significant.pathway)
}
} else {
LR <- data.frame()
Expand All @@ -2022,7 +2023,7 @@ subsetCommunication <- function(object = NULL, net = NULL, slot.name = "net",
pairLR.use = pairLR.use,
thresh = thresh,
datasets = datasets, ligand.pvalues = ligand.pvalues, ligand.logFC = ligand.logFC, ligand.pct.1 = ligand.pct.1, ligand.pct.2 = ligand.pct.2,
receptor.pvalues = receptor.pvalues, receptor.logFC = receptor.logFC, receptor.pct.1 = receptor.pct.1, receptor.pct.2 =receptor.pct.2)
receptor.pvalues = receptor.pvalues, receptor.logFC = receptor.logFC, receptor.pct.1 = receptor.pct.1, receptor.pct.2 =receptor.pct.2, non.significant.pathway = non.significant.pathway)
}

}
Expand All @@ -2044,6 +2045,7 @@ subsetCommunication <- function(object = NULL, net = NULL, slot.name = "net",
#' @param thresh threshold of the p-value for determining significant interaction
#' @param datasets select the inferred cell-cell communications from a particular `datasets` when inputing a data frame `net`
#' @param ligand.pvalues,ligand.logFC,ligand.pct.1,ligand.pct.2 set threshold for ligand genes
#' @param non.significant.pathway Inherited feature from \code{\link{subsetCommunication}}. Default set to NULL
#'
#' ligand.pvalues: threshold for pvalues in the differential expression gene analysis (DEG)
#'
Expand All @@ -2066,19 +2068,27 @@ subsetCommunication_internal <- function(net, LR, cells.level, slot.name = "net"
sources.use = NULL, targets.use = NULL,
signaling = NULL,
pairLR.use = NULL,
thresh = 0.05,
thresh = NULL,
datasets = NULL, ligand.pvalues = NULL, ligand.logFC = NULL, ligand.pct.1 = NULL, ligand.pct.2 = NULL,
receptor.pvalues = NULL, receptor.logFC = NULL, receptor.pct.1 = NULL, receptor.pct.2 = NULL) {
receptor.pvalues = NULL, receptor.logFC = NULL, receptor.pct.1 = NULL, receptor.pct.2 = NULL, non.significant.pathway = NULL) {
if (!is.data.frame(net)) {
if(is.null(thresh)){
message("Threshold is set to 0.05")
thresh = 0.05
}
prob <- net$prob
pval <- net$pval
prob[pval >= thresh] <- 0
net <- reshape2::melt(prob, value.name = "prob")
colnames(net)[1:3] <- c("source","target","interaction_name")
net.pval <- reshape2::melt(pval, value.name = "pval")
net$pval <- net.pval$pval
# remove the interactions with zero values
net <- subset(net, prob > 0)
if(!is.null(non.significant.pathway)){
message("CellChat warning: non.sisgnificant pair included, none of the interactions for the pathway will be removed")
} else {
# remove the interactions with zero values
net <- subset(net, prob > 0)
}
}
if (!("ligand" %in% colnames(net))) {
pairLR <- dplyr::select(LR, c("interaction_name_2", "pathway_name", "ligand", "receptor" ,"annotation","evidence"))
Expand Down Expand Up @@ -2178,7 +2188,9 @@ subsetCommunication_internal <- function(net, LR, cells.level, slot.name = "net"
net$source_target <- paste(net$source, net$target, sep = "sourceTotarget")
# net$source_target_pathway <- paste(paste(net$source, net$target, sep = "_"), net$pathway_name, sep = "_")
net.pval <- net %>% group_by(source_target, pathway_name) %>% summarize(pval = mean(pval), .groups = 'drop')
net <- net %>% group_by(source_target, pathway_name) %>% summarize(prob = sum(prob), .groups = 'drop')
if(is.null(non.significant.pathway)){
net <- net %>% group_by(source_target, pathway_name) %>% summarize(prob = sum(prob), .groups = 'drop')
}
a <- stringr::str_split(net$source_target, "sourceTotarget", simplify = T)
net$source <- as.character(a[, 1])
net$target <- as.character(a[, 2])
Expand Down
10 changes: 6 additions & 4 deletions R/modeling.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ computeCommunProbPathway <- function(object = NULL, net = NULL, pairLR.use = NUL
#' @param remove.isolate whether removing the isolate cell groups without any interactions when applying \code{\link{subsetCommunication}}
#' @param thresh threshold of the p-value for determining significant interaction
#' @param return.object whether return an updated CellChat object
#' @param non.significant.pathway wheter the pathway to be aggregated was detected as not overrepresented (with no significant interaction) in group. Default is NULL, set to TRUE when intending to use \code{\link{netVisual_diffInteraction}} for comparing a not overrepresented pathway in group.
#' @importFrom dplyr group_by summarize groups
#' @importFrom stringr str_split
#'
Expand All @@ -340,7 +341,7 @@ computeCommunProbPathway <- function(object = NULL, net = NULL, pairLR.use = NUL
#'
#' @export
#'
aggregateNet <- function(object, sources.use = NULL, targets.use = NULL, signaling = NULL, pairLR.use = NULL, remove.isolate = TRUE, thresh = 0.05, return.object = TRUE) {
aggregateNet <- function(object, sources.use = NULL, targets.use = NULL, signaling = NULL, pairLR.use = NULL, remove.isolate = TRUE, thresh = 0.05, return.object = TRUE, non.significant.pathway = NULL) {
net <- object@net
if (is.null(sources.use) & is.null(targets.use) & is.null(signaling) & is.null(pairLR.use)) {
prob <- net$prob
Expand All @@ -356,10 +357,11 @@ aggregateNet <- function(object, sources.use = NULL, targets.use = NULL, signali
sources.use = sources.use, targets.use = targets.use,
signaling = signaling,
pairLR.use = pairLR.use,
thresh = thresh)
thresh = thresh,
non.significant.pathway = non.significant.pathway)
df.net$source_target <- paste(df.net$source, df.net$target, sep = "_")
df.net2 <- df.net %>% group_by(source_target) %>% summarize(count = n(), .groups = 'drop')
df.net3 <- df.net %>% group_by(source_target) %>% summarize(prob = sum(prob), .groups = 'drop')
df.net2 <- df.net %>% group_by(source_target) %>% dplyr::summarize(count = n(), .groups = 'drop')
df.net3 <- df.net %>% group_by(source_target) %>% dplyr::summarize(prob = sum(prob), .groups = 'drop')
df.net2$prob <- df.net3$prob
a <- stringr::str_split(df.net2$source_target, "_", simplify = T)
df.net2$source <- as.character(a[, 1])
Expand Down