Skip to content

Bug in netVisual_heatmap may compute incorrect differences when row/column orders differ between compared networks #462

Description

@SDelb

I believe there is a bug in netVisual_heatmap in the visualization.R script.
Around lines 1844-1846, the difference between 2 groups is computed as:

obj1 <- object@net[[comparison[1]]][[measure]]
obj2 <- object@net[[comparison[2]]][[measure]]
net.diff <- obj2-obj1

This assumes that both objects have identical row and column ordering. I found that this is not necessarily the case when comparing CellChat output across conditions, resulting in a heatmap with incorrect values. See below an example of my CellChat results:

Image

Expected behavior:
Before subtraction, the two matrices should be aligned by row and column names.

Actual behavior:
The function directly subtracts matrices without checking ordering of rows and columns.

Suggested fix:

if (! all(rownames(obj1) %in% rownames(obj2))) {
stop("obj1 and obj2 do not contain the same row names (senders).")
}

if (! all(colnames(obj1) %in% colnames(obj2))) {
stop("obj1 and obj2 do not contain the same column names (receivers).")
}

obj2 <- obj2[rownames(obj1), colnames(obj1)]

net.diff <- obj2 - obj1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions