spatialcells.spatial

Module for computing cell communities and region boundaries.

Access via either spatialcells.spatial or spatialcells.spa.

assignPointsToRegions

spatialcells.spatial.assignPointsToRegions(anndata, boundaries_list, region_names, assigncolumn='region', default='BG')[source]

Assign points to regions based on the boundaries. The region assignment is based on the order of the boundaries, so the innermost region should be the first element of boundaries_list.

Parameters:
  • anndata – Anndata object

  • boundaries_list – List of boundaries

  • region_names – List of region names. The order and length should match boundaries_list

  • assigncolumn – Column name for the region assignment

  • default – Default region name for points that are not assigned to any region

bufferBoundary

spatialcells.spatial.bufferBoundary(boundary, offset)[source]

Buffer a boundary by a given offset. Negative offset will shrink the boundary.

Parameters:
  • boundary – the boundary to be buffered

  • offset – the offset

Returns:

the buffered boundary

estimateInitialDistance

spatialcells.spatial.estimateInitialDistance(adata, markers_of_interest, sampling_ratio=0.1)[source]

Use hierarchical clustering to get the checkpoints to estimate the distance parameter for density-based clustering algorithms, e.g., DBSCAN.

Parameters:
  • adata – the anndata object

  • markers_of_interest – the list of marker names to subset the data

  • sampling_ratio – the sampling ratio to subsample the data

Returns:

the list of distances

getBoundary

spatialcells.spatial.getBoundary(anndata, communitycolumn, communityIndexList, alpha=100, debug=False)[source]

Get a boundary for the communities of interest as a Shapely MultiPolygon.

The boundary is defined based on the alpha shape of points in the communities. The alpha shape is the generalization of the convex hull, and is generated via Delaunay triangulation of all the points of interest. The alpha parameter controls the longest edge that can appear in the alpha shape. Smaller alpha gives more detailed boundary, but may appear more jagged and may leave out some points that are too far away from the rest of the points.

Parameters:
  • anndata – the anndata object

  • communitycolumn – the column name of the community

  • communityIndexList – the list of community indexes

  • alpha – the alpha parameter for alpha shape. Smaller alpha gives more detailed boundary, but may appear more jagged.

  • debug – whether to return the polygons and edge components

Returns:

the boundaries of components as a MultiPolygon object

getCommunities

spatialcells.spatial.getCommunities(adata, markers_of_interest, eps, min_samples=20, newcolumn='COI_community', core_only=False)[source]

Get the communities of interest (COI) using DBSCAN

Parameters:
  • adata – the anndata object

  • markers_of_interest – the list of marker names to subset the data

  • eps – the eps parameter for DBSCAN

  • min_samples – Minimum number of samples in each community

  • newcolumn – the column name of the community

Returns:

the communities of interest (COI)

getExtendedBoundary

spatialcells.spatial.getExtendedBoundary(boundaries, offset=200)[source]

Get the expand the boundary of the communities of interest

Parameters:
  • boundaries – the boundaries of components

  • offset – the offset to extend the boundary

Returns:

the extended boundaries of components.

getShrunkenBoundary

spatialcells.spatial.getShrunkenBoundary(boundaries, offset=200)[source]

Shrink the boundary of the communities of interest. Wrapper of bufferBoundary with negative offset.

Parameters:
  • boundaries – the boundaries of components

  • offset – the offset to shrink the boundary

Returns:

the shrunken boundaries of components.

Utility functions

spatialcells.spatial._utils.getAlphaShapes(points, alpha, debug=False)[source]

Compute the alpha shape of a set of points.

Parameters:
  • points – np.array of shape (n,2) points.

  • alpha – alpha value.

Returns:

set of (i,j) point pairs representing edges of the alpha-shape.

spatialcells.spatial._utils.getComponents(boundary, keep_holes=True)[source]

Get the components of a boundary defined by a MultiPolygon.

Parameters:
  • boundary – the boundary to get components from

  • keep_holes – whether to keep holes

Returns:

a list of components, where each component is a MultiPolygon

spatialcells.spatial._utils.getHoles(boundary)[source]

Get the holes within boundary components.

Parameters:

boundary – the boundary to get holes from

Returns:

a list of holes, where each hole is a MultiPolygon

spatialcells.spatial._utils.pruneSmallComponents(boundary, min_area=0, min_edges=0, holes_min_area=0, holes_min_edges=0)[source]

Prune small components from a boundary defined by a MultiPolygon.

Parameters:
  • boundary – the boundary to prune

  • min_area – the minimum area of a polygon component to keep

  • min_edges – the minimum number of edges of a polygon component to keep

  • holes_min_area – the minimum area of a hole to keep the hole

  • holes_min_edges – the minimum number of edges of a hole to keep the hole

Returns:

the pruned boundary