
Pairwise Adjacency
pairwiseAdjacency.Rd
Counts cell-cell interactions based on a previously-generated
NN graph and computes interaction scores (enrichments and p-values)
for each cell pair. For symmetrical NN definitions, will only return a single
value for each pair, but for directed graphs (e.g. KNN
) will return values for
each "direction" (e.g. "Macrophages -> Tumor" and "Tumor -> Macrophages").
Usage
pairwiseAdjacency(
object,
feature,
nn,
method = c("hypergeometric", "permutation"),
drop.self = TRUE,
n_permutations = 100,
p.correction = "bonf",
analyze = "regions",
...
)
Arguments
- object
A SpatialMap or Region object.
- feature
The name of a cellMetadata column (character).
- nn
The name of a
nearestNeighborGraph
fromobject
(character).- method
What test method to calculate adjacency with - permutation or hypergeometric. Hypergeometric is much faster. See the note below for important differences between these methods if you have multiple
Region
s in your dataset that belong to the same biological sample.- drop.self
If TRUE, ensures that the index cell is not counted as its own interaction, even if such self-interactions are present in the network-graph (
NN
object). This is recommended to be TRUE in almost all cases.- n_permutations
If
method = "permutation"
, this dictates how many permutations will be performed.- p.correction
p.value correction to apply to enrichment tests within each Region (passed to
stats::p.adjust
)- analyze
Argument passed to
.smapply
. See note below on how to use this argument for sample-level analysis.- ...
Arguments passed to
.smapply
Value
A SpatialMap or Region object with the results of the pairwise adjacency calculations added as a data frame
to the misc
slot of the specified nn
with the name {feature}.adjacency
Note
Credit to Camara lab for the inspiration (https://doi.org/10.1126/sciadv.abc5464).
While the default options for pairwiseAdjacency
run this analysis on a Region
basis (typically spatially
continuous areas of tissue), this can present statistical problems for the calculations performed by
compareAdjacency
down the line in the case when multiple images (i.e. Region
s) were taken from the same
patient or tissue sample. Since compareAdjacency
makes an assumption of statistical independence for each Region
,
you may obtain artificially inflated p-values when running this analysis on a Region
level if you have this type of
experimental design.
Therefore, when you have multiple Region
s associated with the same sample, we recommend running this function with
analyze = "sample_label"
, where "sample_label"
can be replaced with any value in your object
's
projectMetadata()
. With this option selected, pairwiseAdjacency
will sum together cell interaction counts across
all Region
s in each sample, with the following approach taken for the two methods:
method = "hypergeometric"
: The background distribution is derived after summing together the counts of each cell type across allRegion
s in each sample. This is similar to an assumption of spatial contiguity across all regions (i.e. any given cell is equally likely to have appeared in anyRegion
associated with that sample).method = "permutation"
: The background distribution is derived by permuting cell labels within eachRegion
(the same approach that is used without aggregation) and then summing permuted interaction counts across allRegion
s in each sample.
Examples
sm_skin <- load_sm_data("skin")
sm_skin <- spatialNearestNeighbors(sm_skin)
sm_skin <- pairwiseAdjacency(sm_skin, "cell_type", "spatial_knn_5")
df <- .smapply(sm_skin, function(x) {
nn.misc(NNs(x, "spatial_knn_5"))[["cell_type.adjacency"]]
}, cat.output = "rbind")
head(df)
#> Region feature1 feature2 num.interactions
#> 1 Skin1 B cells (CD20+) B cells (CD20+) 1
#> 2 Skin1 Keratinocytes (PanCK+) B cells (CD20+) 1
#> 3 Skin1 MDSCs (CD14+ HLA-DR-) B cells (CD20+) 1
#> 4 Skin1 Macs (CD68+) B cells (CD20+) 5
#> 5 Skin1 Melanocytes (PanCK+ CD117+) B cells (CD20+) 1
#> 6 Skin1 Monocytes (CD14+ HLA-DR+) B cells (CD20+) 1
#> interaction.proportion theoretical.proportion log2.enrichment
#> 1 3.988831e-05 8.101760e-08 8.9435150
#> 2 3.988831e-05 3.102974e-05 0.3623144
#> 3 3.988831e-05 1.539334e-06 4.6955875
#> 4 1.994416e-04 1.725675e-05 3.5307334
#> 5 3.988831e-05 2.576360e-05 0.6306320
#> 6 3.988831e-05 2.471037e-06 4.0127776
#> p.value.enrichment p.value.depletion p.value possible.hits total.pairs
#> 1 1.031312e-06 0.9999990 2.062624e-06 2 24685993
#> 2 1.832506e-01 0.8167494 3.665013e-01 766 24685993
#> 3 7.075762e-04 0.9992924 1.415152e-03 38 24685993
#> 4 6.102909e-06 0.9999939 1.220582e-05 426 24685993
#> 5 1.371700e-01 0.8628300 2.743400e-01 636 24685993
#> 6 1.813558e-03 0.9981864 3.627116e-03 61 24685993
#> possible.non.hits draws p.adj padj.enrichment padj.depletion
#> 1 24685992 25070 0.0004640905 0.0002320452 1
#> 2 24685228 25070 1.0000000000 1.0000000000 1
#> 3 24685956 25070 0.3184092904 0.1592046452 1
#> 4 24685568 25070 0.0027463091 0.0013731545 1
#> 5 24685358 25070 1.0000000000 1.0000000000 1
#> 6 24685933 25070 0.8161011356 0.4080505678 1
#> log10.padj log10.padj.enrichment log10.padj.depletion
#> 1 3.33339736 3.6344274 0
#> 2 0.00000000 0.0000000 0
#> 3 0.49701427 0.7980443 0
#> 4 2.56125059 2.8622806 0
#> 5 0.00000000 0.0000000 0
#> 6 0.08825602 0.3892860 0