metaspace_converter package
Submodules
Module contents
- metaspace_converter.anndata_to_image_array(adata, layer=None)[source]
Extracts an array of ion images from an AnnData object (that has been generated through the
metaspace_to_anndata
function).- Parameters:
- Return type:
- Returns:
A three-dimensional Numpy array in the following shape
Dimension 0: Number of ion images in the order of
adata.var_names
Dimension 1: Image height
adata.uns["metaspace"]["image_size"]["y"]
Dimension 2: Image width
adata.uns["metaspace"]["image_size"]["x"]
- Raises:
ValueError – If the AnnData object has been modified. E.g. Pixel have been removed/added and the number of pixels and their coordinates do not match the original image dimensions.
- metaspace_converter.metaspace_to_anndata(dataset=None, dataset_id=None, database=None, fdr=0.1, use_tic=False, metadata_as_obs=False, add_optical_image=False, sm=None, **annotation_filter)[source]
Downloads a METASPACE dataset to an
AnnData
object.See: https://metaspace2020.eu/about
- Parameters:
dataset (
Optional
[SMDataset
]) – A METASPACE dataset instance. If not provided, the dataset_id must be given.dataset_id (
Optional
[str
]) – The unique ID of a dataset on METASPACE, e.g. “2021-09-03_11h43m13s”database (
Optional
[tuple
[str
,str
]]) – A single METASPACE database given as a tuple of name and version. Usually it is displayed on METASPACE as “HMDB – v4” which corresponds to (“HMDB”, “v4”).fdr (
Optional
[float
]) – Returns only annotations for which the false discovery rate is less or equal to this limit.use_tic (
bool
) – When True, the output values will be scaled by the total ion count per pixel and will be in 0.0 to 1.0 range.metadata_as_obs (
bool
) – Whether to store metadata in the obs dataframe instead of uns. For a single METASPACE dataset, metadata is the same for all pixels, so it would be duplicated for all obs. When combining multiple datasets, it would be preserved in obs but not in uns.add_optical_image (
bool
) – Whether to embed the optical image for SquidPysm (
Optional
[SMInstance
]) – Optionally a cached SMInstanceannotation_filter – Additional keyword arguments passed to the METASPACE API.
- Return type:
- Returns:
- An
AnnData
object with ion intensities:
.X
ion image pixel coordinates:
.obs[["ion_image_pixel_x", "ion_image_pixel_y"]]
spatial coordinates:
.obsm["spatial"]
ion properties:
.var
, for example “formula”, “adduct”, “mz”, “fdr”, “moleculeNames”, “moleculeIds”, “intensity”…METASPACE metadata:
.uns["metaspace"]
if notmetadata_as_obs
SquidPy metadata:
.uns["spatial"]
ifadd_optical_image
- An
- Raises:
ValueError – If something is wrong with the input data or parameters
metaspace.sm_annotation_utils.DatasetNotFound – If the dataset ID is not found
ConnectionError – For any other network connectivity problems
- metaspace_converter.metaspace_to_spatialdata(dataset=None, dataset_id=None, database=None, fdr=0.1, use_tic=False, metadata_as_obs=False, add_optical_image=True, optical_name_added='optical_image', add_points=True, points_name_added='maldi_points', sm=None, **annotation_filter)[source]
Download a METASPACE dataset as a
SpatialData
object.See: https://metaspace2020.eu/about
- Parameters:
dataset (
Optional
[SMDataset
]) – A METASPACE dataset instance. If not provided, thedataset_id
must be given.dataset_id (
Optional
[str
]) – The unique ID of a dataset on METASPACE, e.g. “2021-09-03_11h43m13s”database (
Optional
[tuple
[str
,str
]]) – A single METASPACE database given as a tuple of name and version. Usually it is displayed on METASPACE as “HMDB – v4” which corresponds to("HMDB", "v4")
.fdr (
float
) – Returns only annotations for which the false discovery rate is less or equal to this limit.use_tic (
bool
) – When True, the output values will be scaled by the total ion count per pixel and will be in 0.0 to 1.0 range.metadata_as_obs (
bool
) – Whether to store metadata in theobs
dataframe instead ofuns
. For a single METASPACE dataset, metadata is the same for all pixels, so it would be duplicated for allobs
. When combining multiple datasets, it would be preserved inobs
but not inuns
.add_optical_image (
bool
) – Whether to also add the optical image (if one exists) to SpatialData images. If none exists, it is not added, and no error raised.optical_name_added (
str
) – Name of the element where to store the image in the SpatialData objectadd_points (
bool
) – Whether to also add ion image pixel coordinates as SpatialData points. This allows to spatially visualize the ion image values. If False, only ion intensities are added to the table and coordinates are added toobs
.points_name_added (
str
) – Name of the element where to store the points in the SpatialData objectsm (
Optional
[SMInstance
]) – Optionally a cached SMInstanceannotation_filter – Additional keyword arguments passed to the METASPACE API.
- Return type:
- Returns:
- A
SpatialData
object with ion intensities and metadata:
.table
optical image:
.images["optical_image"]
(oroptical_name_added
)sampling coordinates:
.points["maldi_points"]
(orpoints_name_added
) in a coordinate system relative to the top-left image corner and physical scale (micrometers)
- A
- Raises:
ValueError – If something is wrong with the input data or parameters
metaspace.sm_annotation_utils.DatasetNotFound – If the dataset ID is not found
ConnectionError – For any other network connectivity problems