API

Import Bento with:

import bento as bt

Bento’s API structure takes inspiration from other libraries in the Scverse ecosystem. It is organized under a set of modules including:

  • bt.io: provides out of the box compatibility with SpatialData objects

  • bt.tl: subcellular analysis tools

  • bt.pl: conveniently plot spatial data and embeddings

  • bt.geo: manipulating data structures

  • bt.datasets: included spatial transcriptomics datasets WIP

Read and Write

Bento is designed to work with SpatialData objects out of the box! Check out SpatialData documentation to learn how to bring your own data, whether it is from commercial platforms or a custom data format.

prep

Computes spatial indices for elements in SpatialData to enable usage of bento-tools.

Tools

Bento provides a set of tools to analyze the spatial composition of cells and molecules. These tools are designed to work with SpatialData objects and can be used to compute spatial summary statistics, shape features, and RNA localization patterns.

Composition

comp

Calculate the average gene composition for shapes across all cells.

comp_diff

Calculate the average difference in gene composition for shapes across batches of cells.

Point Features

Compute spatial summary statistics describing groups of molecules e.g. distance to the cell membrane, relative symmetry, dispersion, etc. The set of available point features is described in the Point Feature Catalog. Use the function bt.tl.analyze_points() to compute features and add your own custom calculation. See the tutorial for more information.

analyze_points

Calculate features for point groups within cells.

list_point_features

List available point feature calculations.

register_point_feature

Register a new point feature calculation class.

Point Feature Catalog

The set of implemented point features is described below. Each feature is computed using the bt.tl.analyze_points() function.

TODO

Shape Features

Compute spatial properties of shape features e.g. area, aspect ratio, etc. of the cell, nucleus, or other region of interest. The set of available shape features is described in the Shape Feature Catalog. Use the function bt.analyze_points() to compute features and add your own custom calculation. See the tutorial for more information.

shape_stats

Compute common shape statistics.

analyze_shapes

Compute multiple shape features.

list_shape_features

List available shape feature calculations.

register_shape_feature

Register a new shape feature calculation function.

Shape Feature Catalog

The set of implemented shape features is described below. Each feature is computed using the bt.analyze_shapes() function.

area

Compute the area of each shape.

aspect_ratio

Compute aspect ratio of minimum rotated rectangle containing each shape.

bounds

Compute bounding box coordinates for each shape.

density

Compute RNA density (molecules per area) for each shape.

opening

Compute morphological opening of each shape.

perimeter

Compute perimeter length of each shape.

radius

Compute average radius of each shape.

raster

Generate grid of points within each shape.

second_moment

Compute second moment of each shape relative to its centroid.

span

Compute maximum diameter of each shape.

RNAflux: Subcellular RNA embeddings and domains

Methods for computing RNAflux embeddings and semantic segmentation of subcellular domains.

flux

Compute RNAflux embeddings of each pixel as local composition normalized by cell composition.

fluxmap

Cluster flux embeddings using self-organizing maps (SOMs) and vectorize clusters as Polygon shapes.

fe

Perform functional enrichment of RNAflux embeddings using decoupler's wsum function.

fe_fazal2019

Compute enrichment scores from subcellular compartment gene sets from Fazal et al. 2019 (APEX-seq).

fe_xia2019

Compute enrichment scores from subcellular compartment gene sets from Xia et al. 2019 (MERFISH 10k U2-OS).

load_gene_sets

Load a gene set from the predefined collection.

RNAforest: Predict RNA localization patterns

Perform multilabel classification of RNA localization patterns using spatial summary statistics as features.

lp

Predict transcript subcellular localization patterns.

lp_stats

Compute frequencies of localization patterns across cells and genes.

lp_diff_discrete

Test for differential localization patterns between discrete phenotype groups.

lp_diff_continuous

Test correlation between localization patterns and continuous phenotype values.

RNAcoloc: Colocalization analysis

Methods for compartments-ecific gene-gene colocalization analyses.

colocation

Decompose a tensor of pairwise colocalization quotients into signatures.

coloc_quotient

Calculate pairwise gene colocalization quotient in each cell.

Plotting

These are convenient functions for static 2D plots of cells, molecules, and embeddings. We generate matplotlib style figures for accessible publication quality plots. There are a couple additional functions summarizing results from bt.tl analysis.

Spatial plots

points

Plot points scatter.

density

Plot points as 2D density.

shapes

Plot shape layers.

Composition

comp

Plot gene composition across set of shapes.

Shape features

shape_stats

Plot shape statistic distributions for each cell.

RNAflux

flux

Plot colorized representation of RNAflux embedding.

fluxmap

Plot fluxmap shapes in different colors.

fe

Plot spatial heatmap of flux enrichment scores.

RNAforest

lp_dist

Plot pattern combination frequencies as an UpSet plot.

lp_genes

Plot the pattern distribution of each group in a RadViz plot.

lp_diff_discrete

Visualize gene pattern frequencies between groups of cells.

Colocalization analysis

factor

Plot a heatmap representation of a loadings matrix, optionally z-scored and subsetted to the n_top rows of each factor.

colocation

Plot colocation of signatures for specified rank across each dimension.

Utilities for data manipulation

Convenient methods for setting, getting, and reformatting data. These functions are used internally by other functions in Bento.

get_points

Get points data synchronized with cell boundaries.

get_points_metadata

Get metadata columns from points data.

set_points_metadata

Add metadata columns to points data.

get_shape

Get shape geometries synchronized with cell boundaries.

get_shape_metadata

Get metadata columns from shapes data.

set_shape_metadata

Add metadata columns to shapes data.

Geometric operations

overlay

Overlay two shape elements in a SpatialData object and store the result as a new shape element.