Spatial operations
sopa.spatial.sjoin(sdata, left_element, right_element, how='left', target_coordinate_system=None, **kwargs)
Spatial join of two shapes
GeoDataFrames, as in geopandas.sjoin.
Shapes are automatically aligned on the same coordinate system (which can be chosen using the target_coordinate_system
argument).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
left_element
|
str | GeoDataFrame
|
The name of a GeoDataFrame in |
required |
right_element
|
str | GeoDataFrame
|
The name of a GeoDataFrame in |
required |
how
|
str
|
The GeoPandas type of join. By default, left geometries are retained. |
'left'
|
target_coordinate_system
|
str | None
|
The name of the coordinate system on which the shapes will be transformed. By default, uses the intrinsic coordinate system of the |
None
|
**kwargs
|
int
|
Kwargs provided to the geopandas.sjoin function |
{}
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
The joined |
Source code in sopa/spatial/join.py
sopa.spatial.assign_transcript_to_cell(sdata, points_key=None, shapes_key=None, key_added='cell_index', unassigned_value=None)
Assign each transcript to a cell based on the cell boundaries. It updates the transcript dataframe by adding a new column.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
points_key
|
str | None
|
Key of the spatialdata object containing the transcript dataframe. |
None
|
shapes_key
|
str | None
|
Key of the spatialdata object containing the cell boundaries. |
None
|
key_added
|
str
|
Key that will be added to the transcript dataframe containing the cell ID |
'cell_index'
|
unassigned_value
|
int | None
|
If |
None
|
Source code in sopa/spatial/join.py
sopa.spatial.mean_distance(adata, group_key, target_group_key=None, ignore_zeros=False, correction=False, relative_change=False)
Mean distance between two groups (typically, between cell-types, or between cell-types and domains).
Warning
When computing distances for multiple slides, differences in cell-type proportions can introduce bias. For example, if group G is more abundant in one slide, the average distance from other groups to G will naturally appear smaller in that slide, simply due to higher local density. If you want to perform fair comparisons across samples, consider using the correction
argument to adjust for this bias.
Note
The distance is a number of hops, i.e. a distance of 10 between a pDC and a T cell means that there are 10 cells on the closest path from one to the other cell.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adata
|
AnnData
|
An |
required |
group_key
|
str
|
Key of |
required |
target_group_key
|
str | None
|
Key of |
None
|
ignore_zeros
|
bool
|
If |
False
|
correction
|
bool
|
If |
False
|
relative_change
|
bool
|
Only used if |
False
|
Returns:
Type | Description |
---|---|
DataFrame
|
|
Source code in sopa/spatial/distance.py
sopa.spatial.vectorize_niches(adata, niche_key, buffer='auto', perc_area_th=0.05)
Converts the niches to shapely polygons, and put into a GeoDataFrame
. Note that each niche can appear multiple times, as they can be separated by other niches ; in this case, we call them different "components" of the same niche ID.
Plot components
You can show niches components with GeoPandas
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adata
|
AnnData | SpatialData
|
An |
required |
niche_key
|
str
|
Key of |
required |
buffer
|
int | str
|
Expansion radius applied on components. By default, |
'auto'
|
perc_area_th
|
float
|
For each niche, components whose area is less than |
0.05
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
A |
Source code in sopa/spatial/morpho.py
sopa.spatial.niches_geometry_stats(adata, niche_key, aggregation='min', key_added_suffix='_distance_to_niche_', **vectorize_niches_kwargs)
Computes statistics over niches geometries
Details
n_components
: Number of connected component of a niche (a component is a group of neighbor cells with the same niche attribute)length
: Mean distance of the exterior/boundary of the components of a nichearea
: Mean area of the components of a nicheroundness
: Float value between 0 and 1. The higher the value, the closer to a circle. Computed via4 * pi * area / length**2
mean_distance_to_niche_X
: mean distance to the niche (between the two closest points of the niches)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adata
|
AnnData | SpatialData
|
An |
required |
niche_key
|
str
|
Key of |
required |
aggregation
|
str | list[str]
|
Aggregation mode. Either one string such as |
'min'
|
key_added_suffix
|
str
|
Suffix added in the DataFrame columns. Defaults to "distance_to_niche". |
'_distance_to_niche_'
|
vectorize_niches_kwargs
|
str
|
Kwargs to the |
{}
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
A |
Source code in sopa/spatial/morpho.py
sopa.spatial.cells_to_groups(adata, group_key, key_added_prefix=None, ignore_zeros=False)
Compute the hop-distance between each cell and a cell category/group.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adata
|
AnnData
|
An |
required |
group_key
|
str
|
Key of |
required |
key_added_prefix
|
str | None
|
Prefix to the key added in |
None
|
ignore_zeros
|
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
DataFrame | None
|
A |
Source code in sopa/spatial/distance.py
sopa.spatial.spatial_neighbors(adata, radius, library_key=None, percentile=None, set_diag=False)
Create a Delaunay graph from spatial coordinates. This function comes from squidpy.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adata
|
AnnData | SpatialData
|
AnnData object |
required |
radius
|
tuple[float, float] | None
|
tuple that prunes the final graph to only contain edges in interval |
required |
library_key
|
str | None
|
Optional batch key in adata.obs |
None
|
percentile
|
float | None
|
Percentile of the distances to use as threshold. |
None
|
set_diag
|
bool
|
Whether to set the diagonal of the spatial connectivities to |
False
|
Source code in sopa/spatial/_build.py
sopa.spatial.prepare_network(adata, cell_type_key, niche_key, clip_weight=3, node_colors=('#5c7dc4', '#f05541'), node_sizes=(1.3, 5))
Create a dataframe representing weights between cell-types and/or niches. This can be later use to plot a cell-type/niche represention of a whole slide using the netgraph library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adata
|
AnnData
|
An |
required |
cell_type_key
|
str
|
Key of |
required |
niche_key
|
str
|
Key of |
required |
clip_weight
|
float
|
Maximum weight |
3
|
node_colors
|
tuple[str]
|
Tuple of (cell-type color, niche color) |
('#5c7dc4', '#f05541')
|
node_sizes
|
tuple[float | int]
|
Tuple of (cell-type size, niche size) |
(1.3, 5)
|
Returns:
Type | Description |
---|---|
tuple[DataFrame, dict, dict, dict]
|
A DataFrame of weights between cell-types and/or niches, and three dict for netgraph display |