Build a Spatial Neighborhood Edge List from 2-D Cell Coordinates
Source:R/create_Edgelist_Spatial.R
create_Edgelist_Spatial.RdConstructs a directed edge list representing spatial cell neighborhoods. Two computation modes are available for inter-cell edges:
kNN mode – finds
knearest neighbors per cell via a kd-tree (RANN::nn2). Only mutual edges are retained (A->B kept only if B->A also exists). Non-mutual edges are discarded.Radius mode – finds all cells within Euclidean distance
radvia a kd-tree (dbscan::frNN). Edges are inherently symmetric.
Autocrine self-loop edges (A->A, distance = 0) are always appended for
every cell in both modes.
Arguments
- coord
data.frame. First column = x, second column = y. Row names must be unique cell identifiers.- k
integerorNULL. Number of nearest neighbors (kNN mode). Positive integer.- rad
numericorNULL. Neighborhood radius (radius mode). Positive finite number in the same units ascoord.- n.cores
integer. CPU cores. Default 1. Uses socket-based clusters (parallel::makeCluster) which work on all platforms (Windows, macOS, Linux).
Value
A data.table keyed on (from, to) with columns:
fromSending cell barcode (character).
toReceiving cell barcode (character).
distanceEuclidean distance; 0 for self-loops (numeric).
x.from,y.fromSending cell coordinates (numeric).
x.to,y.toReceiving cell coordinates (numeric).
Carries metadata attributes: mode, param, n.cells,
cell.names.
Details
This function is a low-level primitive: it builds the spatial edge
list that create_NICHESObject consumes internally. Most
users should call create_NICHESObject(mode = "spatial", ...),
which invokes this function and assembles the result into a full
NICHESObject in one step. create_Edgelist_Spatial() is
exposed for power users who want to inspect or post-process the edge
list, benchmark alternative k or rad values without
re-running the scoring step, or feed the edges into a non-NICHESv2
scoring pipeline.
Parameter resolution (priority):
Both NULL / invalid -> default
k = 4.Both valid -> radius mode; user notified.
Only
radvalid -> radius mode.Only
kvalid -> kNN mode (mutual edges only).
Examples
set.seed(1)
co <- data.frame(x = runif(50), y = runif(50),
row.names = paste0("cell_", 1:50))
el <- create_Edgelist_Spatial(co, k = 5)
#> [create_Edgelist_Spatial] Mode: kNN | Param: 5 | Cells: 50 | Inter-cell: 198 | Autocrine: 50
el.rad <- create_Edgelist_Spatial(co, rad = 0.2)
#> [create_Edgelist_Spatial] Mode: radius | Param: 0.2 | Cells: 50 | Inter-cell: 286 | Autocrine: 50