Estimates the local cell density around each cell using a circular neighbourhood of a given radius. The query cell is excluded by identity; distinct cells sharing its coordinates are counted, including when a target phenotype is selected.
Arguments
- dt
A
data.tablewith columnssample_id,x, andy. Sample identifiers must be complete and nonblank; coordinates must be finite real numeric vectors. Samples are processed independently. Empty tables retain their columns and receive an empty result column.- radius
Finite nonnegative numeric scalar. Radius of the neighbourhood (in coordinate units).
- target_phenotype
Character string or
NULL. If provided, only cells of this phenotype are counted in the neighbourhood.
Value
The input data.table with an added density column representing
the number of neighbours within the specified radius.
Examples
dt <- data.table::data.table(
sample_id = "s1", cell_id = 1:50,
x = runif(50, 0, 100), y = runif(50, 0, 100)
)
result <- cell_density(dt, radius = 20)
head(result[, .(cell_id, density)])
#> cell_id density
#> <int> <int>
#> 1: 1 9
#> 2: 2 6
#> 3: 3 3
#> 4: 4 5
#> 5: 5 7
#> 6: 6 9