clusterModularity {scran} | R Documentation

## Compute the cluster-wise modularity

### Description

Calculate the modularity of each cluster from a graph, based on a null model of random connections between nodes.

### Usage

    clusterModularity(graph, clusters, get.weights = FALSE,
      get.values = NULL, as.ratio = FALSE)

### Arguments

`graph` | A graph object from igraph, usually where each node represents a cell.
`clusters` | Factor specifying the cluster identity for each node.
`get.weights` | Logical scalar indicating whether the observed and expected edge weights should be returned, rather than the modularity.
`get.values` | Deprecated, same as `get.weights`.
`as.ratio` | Logical scalar indicating whether the log-ratio of observed to expected weights should be returned.

### Details

This function computes a modularity score in the same manner as that from `modularity`. The modularity is defined as the (scaled) difference between the observed and expected number of edges between nodes in the same cluster. The expected number of edges is defined by a null model where edges are randomly distributed among nodes. The same logic applies for weighted graphs, replacing the number of edges with the summed weight of edges.

Whereas `modularity` returns a modularity score for the entire graph, `clusterModularity` provides scores for the individual clusters. The sum of the diagonal elements of the output matrix should be equal to the output of `modularity` (after supplying weights to the latter, if necessary). A well-separated cluster should have mostly intra-cluster edges and a high modularity score on the corresponding diagonal entry, while two closely related clusters that are weakly separated will have many inter-cluster edges and a high off-diagonal score.

In practice, the modularity may not the most effective metric for evaluating cluster separatedness. This is because the modularity is proportional to the number of cells, so larger clusters will naturally have a large score regardless of separation. An alternative approach is to set `as.ratio=TRUE`, which returns the ratio of the observed to expected weights for each entry of the matrix. This adjusts for differences in cluster size and improves resolution of differences between clusters.

Directed graphs are treated as undirected inputs with `mode="each"` in `as.undirected`. In the rare case that self-loops are present, these will also be handled correctly.

### Value

By default, an upper triangular numeric matrix of order equal to the number of clusters is returned. Each entry corresponds to a pair of clusters and is proportional to the difference between the observed and expected edge weights between those clusters.

If `as.ratio=TRUE`, an upper triangular numeric matrix is again returned. Here, each entry is equal to the ratio between the observed and expected edge weights.

If `get.weights=TRUE`, a list is returned containing two upper triangular numeric matrices. The `observed` matrix contains the observed sum of edge weights between and within clusters, while the `expected` matrix contains the expected sum of edge weights under the random model.

### Author(s)

Aaron Lun

### See Also

`buildSNNGraph`, for one method to construct `graph`.

`modularity`, for the calculation of the entire graph modularity.

### Examples

    library(scater)
    sce <- mockSCE()
    sce <- logNormCounts(sce)
    g <- buildSNNGraph(sce)
    clusters <- igraph::cluster_walktrap(g)$membership

    # Examining the modularity values directly.
    out <- clusterModularity(g, clusters)
    out

    # Compute the ratio instead, for visualization
    # (log-transform to improve range of colors).
    out <- clusterModularity(g, clusters, as.ratio=TRUE)
    image(log2(out+1))

    # Alternatively, get the edge weights directly:
    out <- clusterModularity(g, clusters, get.weights=TRUE)
    out

[Package scran version 1.14.6 Index]