% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/R-neighbourhood.R
\name{count_all_neighborhoods}
\alias{count_all_neighborhoods}
\title{Count neighborhood composition for all clusters}
\usage{
count_all_neighborhoods(
  df,
  clusters = NULL,
  k = 100,
  max_dist = NULL,
  inlaid_col = NULL,
  coords = c("x", "y"),
  cluster_col = "cluster"
)
}
\arguments{
\item{df}{A data.frame containing at least the columns `x`, `y`, cluster
identifier column, and `spot_id`.
  - `x`, `y`: numeric coordinates of spots
- cluster column (name specified by `cluster_col`): cluster assignment for
each spot
- `spot_id`: unique identifier for each spot (optional, required for detailed
analysis)}

\item{clusters}{Optional vector of cluster labels to process. If `NULL`,
all unique clusters in `df` are used.}

\item{k}{Integer. Number of nearest neighbors to consider. Default is 100.
Larger values capture more distant neighbors.}

\item{max_dist}{Numeric. Maximum distance from the target to consider.
If `NULL` (default), all neighbors up to k are included without distance
filtering.}

\item{inlaid_col}{Character. Name of the column containing inlaid/annotation
values to count in neighborhoods. If `NULL` (default), uses the
  `cluster_col` value. This allows counting neighborhood composition by any
  column (e.g., cell type, tissue type).}

\item{coords}{Character vector of length 2 giving the coordinate column
names.
  Default is `c("x", "y")`.}

\item{cluster_col}{Character. Name of the column containing cluster
assignments.
  Default is `"cluster"`.}
}
\value{
A data.frame with columns:
\describe{
  \item{cluster}{The source cluster being analyzed.}
  \item{neighborhood}{The neighbor cluster type or inlaid value.}
  \item{count}{Number of neighbor spots of this cluster type.}
\item{proportion}{Proportion of this cluster among all neighbors of the
cluster.}
}
Rows are grouped by source cluster and sorted by count within each group
(descending).
}
\description{
This function computes neighborhood statistics for all clusters in a dataset
in a single call. It returns a dataframe with counts of neighboring cluster
types
for each source cluster.
}
\details{
This is a batch processing function that applies [count_neighborhood()] to
all
clusters and combines the results into a single dataframe. Each row
represents
a neighbor cluster found around a source cluster, with counts and
proportions.
}
\examples{
data("visium_simulated_spe", package = "Battlefield")
spe <- visium_simulated_spe
df <- data.frame(
  spot_id = colnames(spe),
  x = SpatialExperiment::spatialCoords(spe)[, 1],
  y = SpatialExperiment::spatialCoords(spe)[, 2],
  cluster = SummarizedExperiment::colData(spe)$cluster
)
# Get neighborhood statistics for all clusters
all_neighbors <- count_all_neighborhoods(df, k = 50)
head(all_neighbors)

}
