Skip to content

Commit

Permalink
Use spatialdata_io.experimental.to_legacy_anndata
Browse files Browse the repository at this point in the history
  • Loading branch information
cavenel committed Oct 11, 2024
1 parent 7c25f5f commit 774a04b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 85 deletions.
33 changes: 5 additions & 28 deletions bin/clustering.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import os
import scanpy as sc
import numpy as np
import pandas as pd
from spatialdata_io.experimental import to_legacy_anndata
from anndata import AnnData
from umap import UMAP
from matplotlib import pyplot as plt
Expand All @@ -34,33 +35,9 @@ import leidenalg
from IPython.display import display, Markdown
```

```{python}
# Make sure we can use scanpy plots with the AnnData object exported from
# `sdata.tables`. This code is taken from the early version of https://github.com/scverse/spatialdata-io/pull/102/
# Once that PR is merged into spatialdata-io, we should instead use
# `spatialdata_io.to_legacy_anndata(sdata)`.
def to_legacy_anndata(sdata: spatialdata.SpatialData) -> AnnData:
adata = sdata.tables["table"]
for dataset_id in adata.uns["spatial"]:
adata.uns["spatial"][dataset_id]["images"] = {
"hires": np.array(sdata.images[f"{dataset_id}_hires_image"]).transpose([1, 2, 0]),
"lowres": np.array(sdata.images[f"{dataset_id}_lowres_image"]).transpose([1, 2, 0]),
}
adata.uns["spatial"][dataset_id]["scalefactors"] = {
"tissue_hires_scalef": spatialdata.transformations.get_transformation(
sdata.shapes[dataset_id], to_coordinate_system="downscaled_hires"
).scale[0],
"tissue_lowres_scalef": spatialdata.transformations.get_transformation(
sdata.shapes[dataset_id], to_coordinate_system="downscaled_lowres"
).scale[0],
"spot_diameter_fullres": sdata.shapes[dataset_id]["radius"][0] * 2,
}
return adata
```

```{python}
sdata = spatialdata.read_zarr(input_sdata, ["images", "tables", "shapes"])
adata = to_legacy_anndata(sdata)
adata = to_legacy_anndata(sdata, coordinate_system="downscaled_hires", table_name="table", include_images=True)
print("Content of the SpatialData table object:")
print(adata)
Expand Down Expand Up @@ -151,8 +128,8 @@ spatial coordinates by overlaying the spots on the tissue image itself.
```{python}
#| layout-nrow: 2
plt.rcParams["figure.figsize"] = (8, 8)
sc.pl.spatial(adata, img_key="hires", color="total_counts", size=1.25)
sc.pl.spatial(adata, img_key="hires", color="n_genes_by_counts", size=1.25)
sc.pl.spatial(adata, img_key="hires", library_id="visium_hires_image", color="total_counts", size=1.25)
sc.pl.spatial(adata, img_key="hires", library_id="visium_hires_image", color="n_genes_by_counts", size=1.25)
```

To gain insights into tissue organization and potential inter-cellular
Expand All @@ -164,7 +141,7 @@ organization of cells.
```{python}
# TODO: Can the colour bar on this figure be fit to the figure?
plt.rcParams["figure.figsize"] = (7, 7)
sc.pl.spatial(adata, img_key="hires", color="clusters", size=1.25)
sc.pl.spatial(adata, img_key="hires", library_id="visium_hires_image", color="clusters", size=1.25)
```

```{python}
Expand Down
32 changes: 5 additions & 27 deletions bin/quality_controls.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,17 @@ import scanpy as sc
import scipy
import seaborn as sns
import spatialdata
from spatialdata_io.experimental import to_legacy_anndata
from anndata import AnnData
from IPython.display import display, Markdown
from textwrap import dedent
plt.rcParams["figure.figsize"] = (6, 6)
```

```{python}
# Make sure we can use scanpy plots with the AnnData object exported from sdata.tables
# This code is taken from the early version of https://github.com/scverse/spatialdata-io/pull/102/
# Once the PR will be merged in spatialdata-io, we should use spatialdata_io.to_legacy_anndata(sdata).
def to_legacy_anndata(sdata: spatialdata.SpatialData) -> AnnData:
adata = sdata.tables["table"]
for dataset_id in adata.uns["spatial"]:
adata.uns["spatial"][dataset_id]["images"] = {
"hires": np.array(sdata.images[f"{dataset_id}_hires_image"]).transpose([1, 2, 0]),
"lowres": np.array(sdata.images[f"{dataset_id}_lowres_image"]).transpose([1, 2, 0]),
}
adata.uns["spatial"][dataset_id]["scalefactors"] = {
"tissue_hires_scalef": spatialdata.transformations.get_transformation(
sdata.shapes[dataset_id], to_coordinate_system="downscaled_hires"
).scale[0],
"tissue_lowres_scalef": spatialdata.transformations.get_transformation(
sdata.shapes[dataset_id], to_coordinate_system="downscaled_lowres"
).scale[0],
"spot_diameter_fullres": sdata.shapes[dataset_id]["radius"][0] * 2,
}
return adata
```

```{python}
# Read the data
sdata = spatialdata.read_zarr(input_sdata, ["images", "tables", "shapes"])
adata = to_legacy_anndata(sdata)
adata = to_legacy_anndata(sdata, coordinate_system="downscaled_hires", table_name="table", include_images=True)
# Convert X matrix from CSR to CSC dense matrix for output compatibility
adata.X = scipy.sparse.csc_matrix(adata.X)
Expand Down Expand Up @@ -132,8 +110,8 @@ spatial patterns may be discerned:

```{python}
#| layout-nrow: 2
sc.pl.spatial(adata, color = ["total_counts", "n_genes_by_counts"], size=1.25)
sc.pl.spatial(adata, color = ["pct_counts_mt", "pct_counts_ribo", "pct_counts_hb"], size=1.25)
sc.pl.spatial(adata, img_key="hires", library_id="visium_hires_image", color = ["total_counts", "n_genes_by_counts"], size=1.25)
sc.pl.spatial(adata, img_key="hires", library_id="visium_hires_image", color = ["pct_counts_mt", "pct_counts_ribo", "pct_counts_hb"], size=1.25)
```

## Scatter plots
Expand Down Expand Up @@ -169,7 +147,7 @@ are uninformative and are thus removed.
adata.obs["in_tissue_str"] = ["In tissue" if x == 1 else "Outside tissue" for x in adata.obs["in_tissue"]]
# Plot spots inside tissue
sc.pl.spatial(adata, color=["in_tissue_str"], title="Spots in tissue", size=1.25)
sc.pl.spatial(adata, img_key="hires", library_id="visium_hires_image", color=["in_tissue_str"], title="Spots in tissue", size=1.25)
del adata.obs["in_tissue_str"]
# Remove spots outside tissue and print results
Expand Down
26 changes: 2 additions & 24 deletions bin/spatially_variable_genes.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,16 @@ import pandas as pd
import scanpy as sc
import squidpy as sq
import spatialdata
from spatialdata_io.experimental import to_legacy_anndata
from anndata import AnnData
from matplotlib import pyplot as plt
```

```{python}
# Make sure we can use scanpy plots with the AnnData object exported from sdata.tables
# This code is taken from the early version of https://github.com/scverse/spatialdata-io/pull/102/
# Once the PR will be merged in spatialdata-io, we should use spatialdata_io.to_legacy_anndata(sdata).
def to_legacy_anndata(sdata: spatialdata.SpatialData) -> AnnData:
adata = sdata.tables["table"]
for dataset_id in adata.uns["spatial"]:
adata.uns["spatial"][dataset_id]["images"] = {
"hires": np.array(sdata.images[f"{dataset_id}_hires_image"]).transpose([1, 2, 0]),
"lowres": np.array(sdata.images[f"{dataset_id}_lowres_image"]).transpose([1, 2, 0]),
}
adata.uns["spatial"][dataset_id]["scalefactors"] = {
"tissue_hires_scalef": spatialdata.transformations.get_transformation(
sdata.shapes[dataset_id], to_coordinate_system="downscaled_hires"
).scale[0],
"tissue_lowres_scalef": spatialdata.transformations.get_transformation(
sdata.shapes[dataset_id], to_coordinate_system="downscaled_lowres"
).scale[0],
"spot_diameter_fullres": sdata.shapes[dataset_id]["radius"][0] * 2,
}
return adata
```

```{python}
# Read data
sdata = spatialdata.read_zarr(input_sdata, ["images", "tables", "shapes"])
adata = to_legacy_anndata(sdata, coordinate_system="downscaled_hires", table_name="table", include_images=True)
adata = to_legacy_anndata(sdata)
print("Content of the AnnData object:")
print(adata)
Expand Down
14 changes: 8 additions & 6 deletions env/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ dependencies:
- libgdal=3.8.3
- gxx=13.2.0
- imagecodecs=2024.1.1
- pip:
- scanpy==1.10.0
- squidpy==1.4.1
- spatialdata==0.1.2
- spatialdata-io==0.1.2
- spatialdata-plot==0.2.1
- scipy=1.12.0
- harmonypy=0.0.10
- scanorama=1.7.4
- spatialdata=0.2.3
- spatialdata-io=0.1.5
- spatialdata-plot=0.2.6
- scanpy=1.10.3
- squidpy==1.5.0

0 comments on commit 774a04b

Please sign in to comment.