From 309238d0e476e2dd44636914a94eb183c2d472e5 Mon Sep 17 00:00:00 2001 From: Sergei Rybakov Date: Mon, 5 Aug 2024 15:34:22 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Mutate=20tiledbsoma=20store=20(#?= =?UTF-8?q?137)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/scrna6.ipynb | 87 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 4 deletions(-) diff --git a/docs/scrna6.ipynb b/docs/scrna6.ipynb index 4d3ac11..641b720 100644 --- a/docs/scrna6.ipynb +++ b/docs/scrna6.ipynb @@ -37,6 +37,7 @@ "source": [ "import lamindb as ln\n", "import pandas as pd\n", + "import scanpy as sc\n", "import tiledbsoma\n", "import tiledbsoma.io\n", "from functools import reduce" @@ -288,14 +289,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Query the array" + "## Query the array store" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Open and query the experiment. We can use the registered `Artifact`." + "Open and query the experiment. We can use the registered `Artifact`. We query `X` and `obs` from the array store." ] }, { @@ -308,8 +309,86 @@ }, "outputs": [], "source": [ - "with soma_artifact.open() as soma_array:\n", - " print(soma_array[\"obs\"].read().concat().to_pandas())" + "with soma_artifact.open() as soma_store:\n", + " obs = soma_store[\"obs\"]\n", + " ms_rna = soma_store[\"ms\"][\"RNA\"]\n", + " \n", + " n_obs = len(obs)\n", + " n_var = len(ms_rna[\"var\"])\n", + " X = ms_rna[\"X\"][\"data\"].read().coos((n_obs, n_var)).concat().to_scipy()\n", + " \n", + " print(obs.read().concat().to_pandas())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Update the array store" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Calculate PCA from the queried `X`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pca_array = sc.pp.pca(X, n_comps=2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "soma_artifact" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Open the array store in write mode and add PCA. When the store is updated, the corresponding artifact also gets updated with a new version. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "with soma_artifact.open(mode=\"w\") as soma_store:\n", + " tiledbsoma.io.add_matrix_to_collection(\n", + " exp=soma_store,\n", + " measurement_name=\"RNA\",\n", + " collection_name=\"obsm\",\n", + " matrix_name=\"pca\",\n", + " matrix_data=pca_array\n", + " )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note that the artifact has been changed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "soma_artifact" ] } ],