Skip to contents

This function implements an algorithm to compute a layout on a coarsened version of the graph. By coarsening the graph, it is possible to process large graphs faster and using less memory.

Usage

layout_with_coarsened_pmds(
  g,
  dim = 3,
  resolution = 1,
  pivots = 200,
  n_iter = 10,
  jitter_sd = 0.01,
  weight_edges_by = c("tp", "crossing_edges"),
  seed = 123,
  verbose = FALSE
)

Arguments

g

A tbl_graph or igraph object.

dim

Number of dimensions for the layout (default is 3).

resolution

Resolution parameter for the community detection step. Higher values lead to more communities.

pivots

Number of pivots to use for the PMDS layout step.

n_iter

Number of iterations for the smoothing step.

jitter_sd

Standard deviation of the jitter added during smoothing.

weight_edges_by

How to weight edges in the coarsened graph for the PMDS layout. "tp" uses bidirectional transition probabilities for 5-step random walks using the layout_with_weighted_pmds function. "crossing_edges" uses 1 divided by the number of edges crossing between communities (from the coarsening step) as weights.

seed

Random seed for reproducibility.

verbose

Whether to print messages about the coarsening process.

Value

A matrix of layout coordinates for the original graph.

Algorithm

  1. Cluster the graph using the Leiden method and create a coarsened version of the graph where each community is represented as a single node. Edge weights represent the number of connections between communities. The resolution parameter controls the granularity of the clustering, with higher values leading to more communities. Fewer communities will lead to faster computation but potentially less accurate layouts.

  2. Compute a layout for the coarsened graph using PMDS. The weight_edges_by parameter controls how edges are weighted in the coarsened graph. "tp" uses bidirectional transition probabilities as described in layout_with_weighted_pmds, while "crossing_edges" uses 1 divided by the number of edges crossing between communities.

  3. Each node in the original graph is assigned the coordinates of its corresponding community in the coarsened graph. Then, the nodes are iteratively "wiggled" by replacing their coordinates with a weighted average of their neighbors' coordinates + some random jitter (jitter_sd).

Examples

library(dplyr)
se <- ReadPNA_Seurat(minimal_pna_pxl_file()) %>%
  LoadCellGraphs(cells = colnames(.)[4], verbose = FALSE)
#>  Created a <Seurat> object with 5 cells and 158 targeted surface proteins

cg <- CellGraphs(se)[[4]]
g <- cg@cellgraph

xyz <- layout_with_coarsened_pmds(g) %>%
  as_tibble(.name_repair = ~ c("x", "y", "z"))

plotly::plot_ly(
  xyz,
  x = ~x, y = ~y, z = ~z,
  mode = "markers",
  type = "scatter3d",
  marker = list(size = 1)
)