Relations and analysis

SpatialRelation is the generic structure for weighted associations between spatial elements. The RelationKind dispatch tokens select the algorithm used by analyze.

Relation kinds

SpatialOmics.MembershipType
Membership(; strict=false)
Membership{strict}

Assignment of source observations to containing destination shapes.

  • strict=false (default): containment is tested by point-in-polygon for transcripts, or centroid-in-polygon for cell shapes.
  • strict=true: full geometric containment is required.

See also

analyze, SpatialRelation

SpatialOmics.ProximityType
Proximity()

Relation kind for a pairwise distance matrix between elements.

The weights field of the resulting SpatialRelation is a symmetric N×N Float32 matrix of centroid distances. Boolean within-radius queries can be derived as weights .< r.

See also

distances, KNN

SpatialOmics.KNNType
KNN(; k=30)

Relation kind for a k-nearest-neighbour graph.

The resulting SpatialRelation has a N×k weight matrix and a flat N*k dst_ids edge list. Requires a NearestNeighbors.jl backend: using NearestNeighbors.

See also

Proximity, analyze

SpatialOmics.ExpressionType
Expression()

Relation kind for a cell × gene transcript count matrix.

Produced by analyze(Expression(), pts, cells), which assigns each transcript to the cell containing it and accumulates counts. The weights field is a Float32 matrix of shape (ncells × ngenes); var holds a named tuple with a :name column of gene names.

See also

analyze, nobs, nvar, var_names

Relations

SpatialOmics.SpatialRelationType
SpatialRelation{K<:RelationKind, W}

Weighted relation between two named spatial elements.

The relation kind K determines the semantics: Expression is a bipartite cell × gene count matrix; Membership is a source-to-destination assignment; Proximity and KNN are graph structures.

  • src, dst: element names in the parent dataset
  • src_ids, dst_ids: instance_id vectors identifying the rows/nodes
  • weights: the relation data (Matrix{Float32} or nothing)
  • obs: per-row metadata (Tables.jl-compatible)
  • var: per-column metadata (for Expression: gene names via :name)
  • kind: the RelationKind singleton

Constructors

SpatialRelation(kind, src, dst, src_ids, dst_ids, weights=nothing; obs)
SpatialRelation(Expression(), src, src_ids, weights; obs, var)

See also

analyze, annotate, nobs, nvar, var_names

SpatialOmics.nobsFunction
nobs(rel) → Int

Return the number of source observations (rows) in a SpatialRelation.

SpatialOmics.nvarFunction
nvar(rel) → Int

Return the number of variables (columns) in an Expression relation. Returns 0 for other relation kinds.

See also

var_names, nobs

SpatialOmics.var_namesFunction
var_names(rel) → Vector{String}

Return gene or variable names for an Expression relation.

Uses the :name column from rel.var if present; otherwise returns string-formatted column indices.

See also

nvar, annotate

SpatialOmics.annotateFunction
annotate(rel, labels; key::Symbol) → SpatialRelation

Return a new SpatialRelation with labels added as column key in rel.obs.

Pure — does not modify rel. labels must have length equal to nobs(rel).

types = assign_cell_types(rel)
rel2  = annotate(rel, types; key=:cell_type)

See also

SpatialRelation, nobs

Analysis

SpatialOmics.analyzeFunction
analyze(kind, args...) → SpatialRelation
analyze(pts::SpatialPoints, cells::SpatialShapes)      → Expression relation
analyze(src::SpatialShapes, dst::SpatialShapes)        → Membership relation
analyze(rel::SpatialRelation{Expression}; k=30)        → KNN relation

Compute a spatial relation between elements.

Dispatch on the RelationKind token selects the algorithm:

  • analyze(Expression(), pts, cells) — count transcripts per gene per cell. Each transcript is assigned to the first containing cell (bounding-box pre-filter, then exact point-in-polygon). Returns an ncells × ngenes count matrix.
  • analyze(Membership(), src, dst) — assign each point or shape in src to the containing shape in dst. strict=true requires full containment; default uses centroid or point containment.
  • analyze(KNN(k), rel) — k-nearest-neighbour graph on an Expression relation. Requires using NearestNeighbors.

The two-argument forms (pts, cells and src, dst) infer the kind from argument types and call the explicit form.

See also

SpatialRelation, Expression, Membership, KNN, distances

SpatialOmics.distancesFunction
distances(shapes_a, shapes_b) → Vector{Float32}
distances(shapes, roi) → Vector{Float32}

Compute the minimum distance from each shape in shapes_a to the nearest shape in shapes_b (or to a SpatialROI boundary), using centroid-to-shape signed distance.

Returns a Float32 vector of length length(shapes_a).

See also

analyze, Proximity

SpatialOmics.PointDensityType
PointDensity

Lazy density estimate descriptor for a SpatialPoints collection.

Produced by density(pts; resolution, feature). Passed to Makie plot verbs to render a rasterised kernel density map at display time.

See also

density

SpatialOmics.densityFunction
density(pts; resolution=512, feature=nothing) → PointDensity

Create a lazy density estimate descriptor for pts.

resolution sets the output grid size (pixels along the longer axis). feature restricts density computation to a single feature label; nothing uses all points.

See also

PointDensity

SpatialOmics.ShapeColorViewType
ShapeColorView

Display descriptor that pairs a SpatialShapes collection with per-shape color values from a SpatialRelation.

Passed to Makie's poly! to render shapes coloured by an expression or other quantitative measure.

See also

SpatialShapes, SpatialRelation