Loading AstroPath Sample Data#
We have written data loaders which allow querying data from a sample or a collection
of samples into the SpatialData framework. This framework
was designed to efficiently organize spatial omics data and integrate it with
common omics analysis data packages.
- In this tutorial we demonstrate how to:
Load basic sample data into the framework
Load cell level data
Customize the data loaded into elements
Access the different elements
Plot image, annotation, and cell level data
Add and plot new shape annotations to the sample instance
Loading Basic Sample Data#
First, import AstroDB and load_astrosample(). Next, connect to the
appropriate database with AstroDB to connect to the appropriate
database and pass this to load_astrosample() with a sampleid of interest.
[1]:
from astropathdb import AstroDB, load_astrosample
[2]:
astrodb = AstroDB(database = 'apmifwsi001_DR2')
astrosample = load_astrosample(sampleid = 19, astrodb=astrodb)
Note
load_astrosample() also accepts a database parameter which allows us to
avoid creating a AstroDB but the functionality and performance is
improved when the object is initialized beforehand. When an AstroDB
is passed the database parameter is ignored so it is important to make sure
that the AstroDB targets the appropriate database.
See also
AstroDBor Logging into SciServer Locally or Connecting to AstroPath:For details on how to input SciServer credentials
- Introduction and AstroPath Database Schema:
More information about
contexts
After we create a sample object (defined as astrosample above), we can look
at some of it’s properties. SpatialData provides a pretty print
function, so writing the object to the console shows us what is in the object.
[3]:
astrosample
[3]:
SpatialData object
├── Images
│ └── 'sampleid_19_image': DataTree[cyx] (8, 38656, 38656), (8, 19328, 19328), (8, 9664, 9664), (8, 4832, 4832), (8, 2416, 2416), (8, 1208, 1208), (8, 604, 604), (8, 302, 302), (8, 151, 151), (8, 75, 75)
└── Shapes
└── 'sampleid_19_annotations': GeoDataFrame shape: (2, 5) (2D shapes)
with coordinate systems:
▸ 'global', with elements:
sampleid_19_image (Images), sampleid_19_annotations (Shapes)
When adding new sample data, pass the object to the sdata argument along with the
AstroDB and a new sampleid. Here the astrosample passed will be updated
in place, so no need to set the function result to anything.
Note
Since we are passing an AstroDB instance we are using a pre-existing
database connection; this is where performance starts to improve by pre-initializing
the AstroDB object.
[4]:
load_astrosample(sampleid = 2, astrodb=astrodb, sdata=astrosample)
astrosample
[4]:
SpatialData object
├── Images
│ ├── 'sampleid_2_image': DataTree[cyx] (8, 58368, 58368), (8, 29184, 29184), (8, 14592, 14592), (8, 7296, 7296), (8, 3648, 3648), (8, 1824, 1824), (8, 912, 912), (8, 456, 456), (8, 228, 228), (8, 114, 114)
│ └── 'sampleid_19_image': DataTree[cyx] (8, 38656, 38656), (8, 19328, 19328), (8, 9664, 9664), (8, 4832, 4832), (8, 2416, 2416), (8, 1208, 1208), (8, 604, 604), (8, 302, 302), (8, 151, 151), (8, 75, 75)
└── Shapes
├── 'sampleid_2_annotations': GeoDataFrame shape: (2, 5) (2D shapes)
└── 'sampleid_19_annotations': GeoDataFrame shape: (2, 5) (2D shapes)
with coordinate systems:
▸ 'global', with elements:
sampleid_2_image (Images), sampleid_19_image (Images), sampleid_2_annotations (Shapes), sampleid_19_annotations (Shapes)
Note
We could have also loaded both samples in one command by passing a
list of sampleids to the sampleid argument.
Loading Cell Level Data#
Above only the image and annotation elements are imported from AstroPath. The
cell level data is not loaded by default because these elements take a bit longer
to load. There are three primary cell elements that can be loaded; the cells
boundaries, the cells positions, and the cells expression data (or cells table).
The boundaries and positions are loaded into the shapes
element of the object as a GeoDataFrame and the
cells tables are loaded as an AnnData object.
The following command will load each of these elements for sampleid 19.
[5]:
load_astrosample(
sampleid = 19, astrodb=astrodb, sdata=astrosample,
cells_boundaries=True, cells_positions=True, cells_table=True
)
astrosample
[5]:
SpatialData object
├── Images
│ ├── 'sampleid_2_image': DataTree[cyx] (8, 58368, 58368), (8, 29184, 29184), (8, 14592, 14592), (8, 7296, 7296), (8, 3648, 3648), (8, 1824, 1824), (8, 912, 912), (8, 456, 456), (8, 228, 228), (8, 114, 114)
│ └── 'sampleid_19_image': DataTree[cyx] (8, 38656, 38656), (8, 19328, 19328), (8, 9664, 9664), (8, 4832, 4832), (8, 2416, 2416), (8, 1208, 1208), (8, 604, 604), (8, 302, 302), (8, 151, 151), (8, 75, 75)
├── Points
│ └── 'sampleid_19_cells_points': DataFrame with shape: (<Delayed>, 3) (2D points)
├── Shapes
│ ├── 'sampleid_2_annotations': GeoDataFrame shape: (2, 5) (2D shapes)
│ ├── 'sampleid_19_annotations': GeoDataFrame shape: (2, 5) (2D shapes)
│ ├── 'sampleid_19_cells_boundaries': GeoDataFrame shape: (956088, 1) (2D shapes)
│ └── 'sampleid_19_cells_positions': GeoDataFrame shape: (962920, 3) (2D shapes)
└── Tables
└── 'sampleid_19_mIF': AnnData (962920, 6)
with coordinate systems:
▸ 'global', with elements:
sampleid_2_image (Images), sampleid_19_image (Images), sampleid_19_cells_points (Points), sampleid_2_annotations (Shapes), sampleid_19_annotations (Shapes), sampleid_19_cells_boundaries (Shapes), sampleid_19_cells_positions (Shapes)
Customizing Data Loaded into Elements#
The arguments for load_astrosample() also accept keyword mappings for
selecting certain types of data for each element (not including the
images). This allows us to customize what data is loaded for a particular
element past the default loadings.
Of note, each of the mappings accept a ‘columns’ argument, which will add additional columns from the table the element loads its data from. In addition, the mappings accept filters for some of the columns in the specific tables.
For example, the cells boundaries reads data from the Cellgeom Table
which has a ptype column and we can select only ‘CD8’ and ‘FoxP3’ cells
as well as including the ptype column in that element using the following
command.
[6]:
load_astrosample(
sampleid = 19, astrodb=astrodb, sdata=astrosample,
cells_boundaries={'columns': 'ptype', 'ptype': ['CD8', 'FoxP3']},
)
astrosample
[6]:
SpatialData object
├── Images
│ ├── 'sampleid_2_image': DataTree[cyx] (8, 58368, 58368), (8, 29184, 29184), (8, 14592, 14592), (8, 7296, 7296), (8, 3648, 3648), (8, 1824, 1824), (8, 912, 912), (8, 456, 456), (8, 228, 228), (8, 114, 114)
│ └── 'sampleid_19_image': DataTree[cyx] (8, 38656, 38656), (8, 19328, 19328), (8, 9664, 9664), (8, 4832, 4832), (8, 2416, 2416), (8, 1208, 1208), (8, 604, 604), (8, 302, 302), (8, 151, 151), (8, 75, 75)
├── Points
│ └── 'sampleid_19_cells_points': DataFrame with shape: (<Delayed>, 3) (2D points)
├── Shapes
│ ├── 'sampleid_2_annotations': GeoDataFrame shape: (2, 5) (2D shapes)
│ ├── 'sampleid_19_annotations': GeoDataFrame shape: (2, 5) (2D shapes)
│ ├── 'sampleid_19_cells_boundaries': GeoDataFrame shape: (121778, 2) (2D shapes)
│ └── 'sampleid_19_cells_positions': GeoDataFrame shape: (962920, 3) (2D shapes)
└── Tables
└── 'sampleid_19_mIF': AnnData (962920, 6)
with coordinate systems:
▸ 'global', with elements:
sampleid_2_image (Images), sampleid_19_image (Images), sampleid_19_cells_points (Points), sampleid_2_annotations (Shapes), sampleid_19_annotations (Shapes), sampleid_19_cells_boundaries (Shapes), sampleid_19_cells_positions (Shapes)
This functionality uses a filter on ptype, which accepts both a list and single input.
More details on filtering subsets of cells and which tables each of the elements are loaded
from can be found in the load_astrosample() documentation.
To reload all parts of an element we can pass 'reload' back into the appropriate argument,
as demonstrated below.
[7]:
load_astrosample(
sampleid = 19, astrodb=astrodb, sdata=astrosample,
cells_boundaries='reload'
)
astrosample
[7]:
SpatialData object
├── Images
│ ├── 'sampleid_2_image': DataTree[cyx] (8, 58368, 58368), (8, 29184, 29184), (8, 14592, 14592), (8, 7296, 7296), (8, 3648, 3648), (8, 1824, 1824), (8, 912, 912), (8, 456, 456), (8, 228, 228), (8, 114, 114)
│ └── 'sampleid_19_image': DataTree[cyx] (8, 38656, 38656), (8, 19328, 19328), (8, 9664, 9664), (8, 4832, 4832), (8, 2416, 2416), (8, 1208, 1208), (8, 604, 604), (8, 302, 302), (8, 151, 151), (8, 75, 75)
├── Points
│ └── 'sampleid_19_cells_points': DataFrame with shape: (<Delayed>, 3) (2D points)
├── Shapes
│ ├── 'sampleid_2_annotations': GeoDataFrame shape: (2, 5) (2D shapes)
│ ├── 'sampleid_19_annotations': GeoDataFrame shape: (2, 5) (2D shapes)
│ ├── 'sampleid_19_cells_boundaries': GeoDataFrame shape: (956088, 1) (2D shapes)
│ └── 'sampleid_19_cells_positions': GeoDataFrame shape: (962920, 3) (2D shapes)
└── Tables
└── 'sampleid_19_mIF': AnnData (962920, 6)
with coordinate systems:
▸ 'global', with elements:
sampleid_2_image (Images), sampleid_19_image (Images), sampleid_19_cells_points (Points), sampleid_2_annotations (Shapes), sampleid_19_annotations (Shapes), sampleid_19_cells_boundaries (Shapes), sampleid_19_cells_positions (Shapes)
Accessing data in the object#
This object acts like a Python dictionary, so we can access elements using their names. Below we index into the annotations and plot them as basic geopandas plots.
[8]:
astrosample['sampleid_2_annotations']
[8]:
| sampleid | annoid | lname | color | geometry | |
|---|---|---|---|---|---|
| 0 | 2 | 20001 | good tissue | 0xffff00 | MULTIPOLYGON (((53942 12849, 53944 12851, 5395... |
| 1 | 2 | 20002 | tumor | 0x00ff00 | POLYGON ((41521 1986, 41870 1994, 41918 1994, ... |
[9]:
astrosample['sampleid_2_annotations'].boundary.plot(color = ('red', 'blue'));
The image elements are stored in a DataTree and lazy loaded using dask
as a multiscale image. We can see the different scales by looking at the ‘groups’
attribute of the image and view the lowest resolution element (‘scale9’) as below.
[10]:
print('groups:', astrosample['sampleid_19_image'].groups)
astrosample['sampleid_19_image']['scale9'].image
groups: ('/', '/scale0', '/scale1', '/scale2', '/scale3', '/scale4', '/scale5', '/scale6', '/scale7', '/scale8', '/scale9')
[10]:
<xarray.DataArray 'image' (c: 8, y: 75, x: 75)> Size: 45kB
dask.array<from-value, shape=(8, 75, 75), dtype=uint8, chunksize=(8, 75, 75), chunktype=numpy.ndarray>
Coordinates:
* c (c) int64 64B 0 1 2 3 4 5 6 7
* y (y) float64 600B 257.7 773.1 1.289e+03 ... 3.788e+04 3.84e+04
* x (x) float64 600B 257.7 773.1 1.289e+03 ... 3.788e+04 3.84e+04
Attributes:
transform: {'global': Sequence \n Scale (x, y)\n [512. 512.]\n...See also
- Plotting Elements with sidus tutorial
A brief demonstration on how to index into regions of the image for further use
- xarray or dask
Further documentation on using these kinds of objects
Finally, the cells table is loaded as an AnnData object and can be similarly
viewed.
[11]:
astrosample['sampleid_19_mIF']
[11]:
AnnData object with n_obs × n_vars = 962920 × 6
obs: 'sampleid', 'cellid', 'ptype', 'exprphenotype', 'tdist', 'region', 'batchid'
var: 'targets'
uns: 'spatialdata_attrs', 'astropath_keys', 'sidus_attrs'
obsm: 'spatial'
layers: None (.X)
Plotting Elements#
What’s even more interesting than above is being able to plot the elements over each other. For this, we recommend sidus which has a static API for plotting elements. The tutorial Plotting Elements with sidus there is a great resource, here the plotting options are briefly demonstrated.
First, the plotting module is imported. Then the image from sampleid 19 in astrosample
is rendered. The plotting API is designed around plotting elements from a particular
sample.
[12]:
from sidus import plotting as pl
[13]:
pl.SpatialPlot(astrosample, sampleid=19, elements_to_plot='images').show();
A scale for rendering the image is automatically choosen based on the size of the figure.
Moreover, the geometries are automatically scaled to the appropriate coordinate system
and can be plotted using SpatialPlot as below.
[14]:
pl.SpatialPlot(astrosample, sampleid=2).show();
By default all of the shapes elements are plotted, but we can specify a shapes element and bounding box as below.
[15]:
pl.SpatialPlot(
data=astrosample,
sampleid=19,
shapes='annotations',
elements_to_plot=['images', 'shapes'],
bbox = (13000, 20000, 29000, 33500), # (ymin, ymax, xmin, xmax)
).show();
..note:
Here the shapes element we specified was not the full key and the
code looks up the appropriate element using the sampleid.
We can also plot the cells and color them based on a column in the
table obs element or var_name. Below we plot the data based
on the tumor expression.
[16]:
pl.SpatialPlot(
data=astrosample,
sampleid=19,
shapes='cells_boundaries',
elements_to_plot=['images', 'shapes'],
column="centirecell620",
shapes_kwargs = {'cmap': "magma", 'scale': 32},
bbox = (13000, 20000, 25000, 35500),
).show();
See also
SpatialPlot:For details on input arguments to plot the shapes
- Plotting Elements with sidus:
For a full tutorial on plotting elements
Adding Elements#
Adding new elements to the object can be done with the spatialdata.models.ShapesModel.parse().
We add the ‘global’ transfomation (an Identity, as for the
shapes loaded above) to the shape when passing it to spatialdata.models.ShapesModel.parse().
This lets the various plotting methods know how to align the new shapes to their images.
Below we import detected “hotspot” fields from the database and add them as new shapes. Then plot the new “hotspots” with the previous image.
[17]:
from spatialdata.models import ShapesModel
from spatialdata.transformations import Identity
[18]:
sql = f"""
select 'hotspots' lname, h.sampleid, gtprim.STAsText() wkt, p
from dbo.HotspotCD8 h, dbo.FieldGeometry f
where h.sampleid = 2
and h.hpfid = f.hpfid
and p between 0 and .3
"""
geom = astrodb.gquery(sql)
astrosample.shapes['sampleid_2_top_30_hotspots'] = ShapesModel.parse(
geom, transformations = {'global': Identity()}
)
[19]:
pl.SpatialPlot(
astrosample,
sampleid=2,
bbox = (1300, 20000, 31500, 56000),
shapes_kwargs = {'cmap': {"good tissue": (135, 206, 235), "tumor": (255, 127, 80), "hotspots": (255, 255, 0)}}
).show();