From 774a04b7c241619e2cd796f34ef17013044a78c0 Mon Sep 17 00:00:00 2001 From: Christophe Avenel Date: Fri, 11 Oct 2024 14:41:27 +0200 Subject: [PATCH] Use spatialdata_io.experimental.to_legacy_anndata --- bin/clustering.qmd | 33 +++++--------------------------- bin/quality_controls.qmd | 32 +++++-------------------------- bin/spatially_variable_genes.qmd | 26 ++----------------------- env/environment.yml | 14 ++++++++------ 4 files changed, 20 insertions(+), 85 deletions(-) diff --git a/bin/clustering.qmd b/bin/clustering.qmd index 8c62585..c6db937 100644 --- a/bin/clustering.qmd +++ b/bin/clustering.qmd @@ -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 @@ -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) @@ -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 @@ -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} diff --git a/bin/quality_controls.qmd b/bin/quality_controls.qmd index 784ec78..0bd6a7a 100644 --- a/bin/quality_controls.qmd +++ b/bin/quality_controls.qmd @@ -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) @@ -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 @@ -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 diff --git a/bin/spatially_variable_genes.qmd b/bin/spatially_variable_genes.qmd index 5adc45e..60e4686 100644 --- a/bin/spatially_variable_genes.qmd +++ b/bin/spatially_variable_genes.qmd @@ -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) diff --git a/env/environment.yml b/env/environment.yml index c376fb2..82c7e9f 100644 --- a/env/environment.yml +++ b/env/environment.yml @@ -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