Skip to main content

Convert Metaspace datasets to AnnData

Project description

METASPACE converter

Python package to download and convert datasets from the METASPACE knowledge base to common formats for single-cell and spatial omics analysis. Datasets can be directly downloaded to AnnData and SpatialData objects.

AnnData provides is underlying data format of many packages of the scverse such as scanpy for single-cell data analysis and squidpy for spatial omics analysis.

Another supported format that is part of the scverse is SpatialData for storage, alignment, and processing of spatial omics data. This enables users to easily align and integrate METASPACE datasets to other spatial omics modalities.

If you encounter any bugs or have suggestions for new features, please open an issue in the github repository.

Installation

Our package requires python >= 3.9.

pip install -U git+https://github.com/metaspace2020/metaspace-converter@master

Import package

import metaspace_converter

Short tutorial

The METASPACE-converter package is using the python client download datasets from METASPACE. It serves as a wrapper that downloads converts datasets directly to AnnData and SpatialData objects.

If you also need to upload or modify datasets on METASPACE, please check the METASPACE python client documentation.

AnnData

Datasets can be downloaded to AnnData objects using the metaspace_to_anndata function. This allows downstream analysis e.g. with scanpy or squidpy.

Scanpy

from metaspace_converter import metaspace_to_anndata
import scanpy as sc

# Download data and convert to an AnnData object
adata = metaspace_to_anndata(
    dataset_id="2022-08-05_17h28m56s",
    fdr=0.1,
    database=("BraChemDB", "2018-01"),
)

# Visualization with ScanPy
sc.pl.spatial(
    adata,
    # Choose the first ion for visualization
    color=adata.var.index[0],
    img_key=None,
    spot_size=1,
)

Image

Squidpy

Optical images can also be downloaded and save in the object if available. Squidpy allows for an easy overlay of ion iamge and optical image.

from metaspace_converter import metaspace_to_anndata
import squidpy as sq

# Download dataset with optical background image
adata = metaspace_to_anndata(
    dataset_id="2022-08-05_17h28m56s",
    fdr=0.1,
    database=("BraChemDB", "2018-01"),
    add_optical_image=True,
)

sq.pl.spatial_scatter(
    adata, color=adata.var.index[0], shape="square", img=True, size=15, alpha=0.5
)

Image

Convert AnnData objects to ion image arrays

If you want to work with the ion images as numpy arrays, the function anndata_to_image_array can convert previoysly workloaded AnnData objects to numpy arrays.

from metaspace_converter import metaspace_to_anndata, anndata_to_image_array

# Download data
adata2 = metaspace_to_anndata(dataset_id="2023-11-14_21h58m39s", fdr=0.1)

ion_images = anndata_to_image_array(adata2)

# 6 ion images of shape 61x78
print(ion_images.shape)
# > (6, 61, 78)

SpatialData

Download to the SpatialData format equally easy and can be done with the metaspace_to_spatialdata function.

Here using a reversed colormap which better represents intense values on bright background.

from metaspace_converter import metaspace_to_spatialdata
import spatialdata_plot  # noqa: Not directly used but extends spatialdata

# Download dataset with optical background image
sdata = metaspace_to_spatialdata(
    dataset_id="2022-08-05_17h28m56s",
    fdr=0.1,
    database=("BraChemDB", "2018-01"),
)

# Workaround: spatialdata-plot currently does not use points transformation
sdata.points["maldi_points"] = sdata.transform_element_to_coordinate_system(
    sdata.points["maldi_points"], "global"
)

(
    sdata.pl.render_images("optical_image")
    .pl.render_points(
        "maldi_points",
        color=sdata.table.var.index[0],
        alpha=1,
        size=2,
        cmap="viridis_r",
    )
    .pl.show(title=sdata.table.var.index[0], coordinate_systems="global")
)

Image

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metaspace-converter-1.0.0.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

metaspace_converter-1.0.0-py3-none-any.whl (26.3 kB view details)

Uploaded Python 3

File details

Details for the file metaspace-converter-1.0.0.tar.gz.

File metadata

  • Download URL: metaspace-converter-1.0.0.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for metaspace-converter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 44e63cfe0f700cae53c25f1faf0056254e6f5fd613a3d84dbebc6b1f55a239cb
MD5 6d10f3be3e44624efc5475bfef59232a
BLAKE2b-256 bff53241282f5c781bed2ddc2427779dbe7640db6dd140970ca50195487e48bf

See more details on using hashes here.

File details

Details for the file metaspace_converter-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for metaspace_converter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a701f573a638084172131f458378fca443ab6a8f6ad04d07cb5a94aa7900c1c8
MD5 c071e5f6e153d18dad7d1220129cd2f2
BLAKE2b-256 72760c65959a2fd653426587a156be25c525baf62666ca3b8f193dd897c9181c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page