diff --git a/merfish/to_zarr.py b/merfish/to_zarr.py index f694764..91a7ec4 100644 --- a/merfish/to_zarr.py +++ b/merfish/to_zarr.py @@ -44,7 +44,7 @@ expression = cells.copy() del expression.obsm["region_radius"] del expression.obsm["spatial"] -expression.obs["cell_id"] = np.array(map(str, np.arange(len(cells)))) +expression.obs["cell_id"] = np.arange(len(cells)) expression = sd.TableModel.parse( adata=expression, region="/shapes/cells", @@ -52,22 +52,22 @@ ) xy = cells.obsm["spatial"] regions = sd.ShapesModel.parse( - coords=xy, - shape_type="Circle", - shape_size=cells.obsm["region_radius"], + xy, + geometry=0, + radius=cells.obsm["region_radius"], + index=expression.obs['cell_id'].copy() ) -adata_polygons = sd.PolygonsModel.parse( - path_read / "anatomical.geojson", instance_key="region_id" +polygons = sd.ShapesModel.parse( + path_read / "anatomical.geojson" ) ## sdata = sd.SpatialData( table=expression, - shapes={"cells": regions}, + shapes={"cells": regions, "anatomical": polygons}, points={"single_molecule": single_molecule}, images={"rasterized": img}, - polygons={"anatomical": adata_polygons}, ) print(sdata) ## diff --git a/spatialdata-sandbox-prepare-data.ipynb b/spatialdata-sandbox-prepare-data.ipynb deleted file mode 100644 index 72743d2..0000000 --- a/spatialdata-sandbox-prepare-data.ipynb +++ /dev/null @@ -1,195 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "51d6cc61-a687-4d0f-aaeb-e2d00a2b83af", - "metadata": {}, - "source": [ - "First you need to clone this repos, install them in dev mode `pip install -e .` and switch to relevant branch\n", - "\n", - "- clone https://github.com/giovp/spatialdata-sandbox and switch to branch `io/updates`\n", - " - run `download_visium.py` in `xenium` folder to download visium.\n", - " - run `download.py` in `xenium` folder to download xenium\n", - " - run `download.py` in nanostring to download nanostring.\n", - "- clone https://github.com/scverse/spatialdata and switch to branch `shapesmodel/id`\n", - "- clone https://github.com/scverse/spatialdata-io and switch to branch `io/nanostring`\n", - "\n", - "You can then read the data" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "538d3b44-0110-4ce6-bb8c-d758ca5437d3", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/giovanni.palla/miniconda3/envs/sdata/lib/python3.10/site-packages/geopandas/_compat.py:123: UserWarning: The Shapely GEOS version (3.11.1-CAPI-1.17.1) is incompatible with the GEOS version PyGEOS was compiled with (3.10.4-CAPI-1.16.2). Conversions between both will be slow.\n", - " warnings.warn(\n", - "/Users/giovanni.palla/Projects/spatialdata/spatialdata/_compat.py:18: UserWarning: Geopandas was set to use PyGEOS, changing to shapely 2.0 with:\n", - "\n", - "\tgeopandas.options.use_pygeos = True\n", - "\n", - "If you intended to use PyGEOS, set the option to False.\n", - " warnings.warn(\n", - "/Users/giovanni.palla/miniconda3/envs/sdata/lib/python3.10/site-packages/anndata/experimental/pytorch/_annloader.py:18: UserWarning: Сould not load pytorch.\n", - " warnings.warn(\"Сould not load pytorch.\")\n" - ] - } - ], - "source": [ - "from spatialdata_io import visium, cosmx\n", - "from pathlib import Path\n", - "\n", - "%load_ext autoreload\n", - "%autoreload 1\n", - "%load_ext lab_black" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "bbc4a345-f9e9-48ed-9ab5-ac4a6c598ff2", - "metadata": {}, - "outputs": [], - "source": [ - "nanostring_path = Path(\n", - " \"/Users/giovanni.palla/Projects/spatialdata-sandbox/nanostring_cosmx/data/data_lung5_rep2\"\n", - ")\n", - "nanostring_dataset_id = \"Lung5_Rep2\"\n", - "visium_path = Path(\n", - " \"/Users/giovanni.palla/Projects/spatialdata-sandbox/xenium/data/visium\"\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "f1cdbbc4-3214-43b3-909e-a9da97137904", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[34mINFO \u001b[0m reading \n", - " \u001b[35m/Users/giovanni.palla/Projects/spatialdata-sandbox/xenium/data/visium/\u001b[0m\u001b[95mCytAssist_FFPE_Human_Breast_Cancer_f\u001b[0m\n", - " \u001b[95miltered_feature_bc_matrix.h5\u001b[0m \n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/giovanni.palla/miniconda3/envs/sdata/lib/python3.10/site-packages/anndata/_core/anndata.py:1830: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.\n", - " utils.warn_names_duplicates(\"var\")\n" - ] - } - ], - "source": [ - "sdata_visium = visium(visium_path)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "5823f685-04de-407d-8c1f-7ed5d6dab4e6", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'c'\u001b[0m, \u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n", - "\u001b[34mINFO \u001b[0m `dims` is set to: \u001b[1m(\u001b[0m\u001b[32m'y'\u001b[0m, \u001b[32m'x'\u001b[0m\u001b[1m)\u001b[0m \n" - ] - } - ], - "source": [ - "sdata_cosmx = cosmx(nanostring_path, nanostring_dataset_id)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.8" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/toy/to_zarr.py b/toy/to_zarr.py index 31ced33..ed9d33d 100644 --- a/toy/to_zarr.py +++ b/toy/to_zarr.py @@ -135,9 +135,9 @@ def get_points(begin_end): plt.show() ## a_circles = sd.ShapesModel.parse( - coords=points, - shape_type="Circle", - shape_size=np.sqrt(np.array(sizes) / np.pi), + points, + geometry=0, + radius=np.sqrt(np.array(sizes) / np.pi), ) a_points = sd.PointsModel.parse(xy) diff --git a/visium/to_zarr.py b/visium/to_zarr.py index 329ff1f..dd0bbf8 100644 --- a/visium/to_zarr.py +++ b/visium/to_zarr.py @@ -30,7 +30,7 @@ table.var_names_make_unique() table.obs["annotating"] = f"/shapes/{lib}" table.obs["library"] = lib - table.obs["visium_spot_id"] = np.array(map(str, np.arange(len(table)))) + table.obs["spot_id"] = np.arange(len(table)) table_list.append(table) # setup @@ -54,9 +54,9 @@ # prepare circles diameter = table.uns["spatial"][lib_key]["scalefactors"]["spot_diameter_fullres"] shape_regions = sd.ShapesModel.parse( - coords=table.obsm["spatial"], - shape_type="Circle", - shape_size=diameter, + table.obsm["spatial"], + geometry=0, + radius=diameter / 2, transformations={lib: Identity()}, ) shapes[lib] = shape_regions @@ -73,7 +73,7 @@ table, region=[f"/shapes/{lib}" for lib in libraries], region_key="annotating", - instance_key="visium_spot_id", + instance_key="spot_id", ) sdata = sd.SpatialData( diff --git a/visium_io/README.md b/visium_io/README.md new file mode 100644 index 0000000..4c2fed8 --- /dev/null +++ b/visium_io/README.md @@ -0,0 +1 @@ +# TESTED ON 27-02 AND NOT WORKING \ No newline at end of file diff --git a/xenium_io/to_zarr.py b/xenium_io/to_zarr.py index 0ea3d0b..682860b 100644 --- a/xenium_io/to_zarr.py +++ b/xenium_io/to_zarr.py @@ -19,6 +19,7 @@ sdata = xenium( path=str(path_read), n_jobs=8, + cells_as_shapes=True, # morphology_mip=False, # morphology_focus=False, # nucleus_boundaries=False,