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 each point group.

list_point_features

Return a DataFrame of available point features.

register_point_feature

Register a new point feature function.

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 descriptive stats for cells.

analyze_shapes

Analyze features of shapes.

list_shape_features

Return a dictionary of available shape features and their descriptions.

register_shape_feature

Register a shape feature function. The function should take an SpatialData object and a shape name as input.

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 the aspect ratio of the minimum rotated rectangle that contains each shape.

bounds

Compute the minimum and maximum coordinate values that bound each shape.

density

Compute the RNA density of each shape.

opening

Compute the opening (morphological) of distance d for each cell.

perimeter

Compute the perimeter of each shape.

radius

Compute the radius of each cell.

raster

Generate a grid of points contained within each shape.

second_moment

Compute the second moment of each shape.

span

Compute the length of the longest diagonal 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.

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; list available ones with bento.tl.gene_sets.

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

Computes frequencies of localization patterns across cells and genes.

lp_diff_discrete

Gene-wise test for differential localization across phenotype of interest.

lp_diff_continuous

Gene-wise test for differential localization across phenotype of interest.

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 DataFrame synced to AnnData object.

get_points_metadata

Get points metadata.

set_points_metadata

Write metadata in SpatialData points element as column(s).

get_shape

Get a GeoSeries of Polygon objects from an SpatialData object.

get_shape_metadata

Get shape metadata.

set_shape_metadata

Write metadata in SpatialData shapes element as column(s).

Geometric operations

overlay

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