{ "cells": [ { "cell_type": "markdown", "id": "ee3f7972", "metadata": { "papermill": { "duration": 0.005236, "end_time": "2025-07-07T04:27:38.478074", "exception": false, "start_time": "2025-07-07T04:27:38.472838", "status": "completed" }, "tags": [] }, "source": [ "# Working with scanpy Package" ] }, { "cell_type": "markdown", "id": "652dcfd3", "metadata": { "papermill": { "duration": 0.002323, "end_time": "2025-07-07T04:27:38.485139", "exception": false, "start_time": "2025-07-07T04:27:38.482816", "status": "completed" }, "tags": [] }, "source": [ "Scanpy is a one of the best toolkit for single-cell RNA sequencing data analysis in Python. stLearn is created based on a part of scanpy and also our tool is partially compatible with scanpy. \n", "\n", "We recommend to use both scanpy and stLearn for the analysis." ] }, { "cell_type": "markdown", "id": "ca52a131", "metadata": { "papermill": { "duration": 0.002714, "end_time": "2025-07-07T04:27:38.490655", "exception": false, "start_time": "2025-07-07T04:27:38.487941", "status": "completed" }, "tags": [] }, "source": [ "### Convert AnnData from scanpy to work with stLearn function" ] }, { "cell_type": "markdown", "id": "ab6d946e", "metadata": { "papermill": { "duration": 0.002151, "end_time": "2025-07-07T04:27:38.495106", "exception": false, "start_time": "2025-07-07T04:27:38.492955", "status": "completed" }, "tags": [] }, "source": [ "stLearn also uses the AnnData as the core object like scanpy. However, we store the spatial data in different way. \n", "\n", "Then if you read data by scanpy, you need to convert to stLearn AnnData format. But if you read the data by stLearn, you can use almost of all functions from scanpy." ] }, { "cell_type": "code", "execution_count": 1, "id": "c64779b1", "metadata": { "execution": { "iopub.execute_input": "2025-07-07T04:27:38.501464Z", "iopub.status.busy": "2025-07-07T04:27:38.501049Z", "iopub.status.idle": "2025-07-07T04:27:40.370154Z", "shell.execute_reply": "2025-07-07T04:27:40.369800Z" }, "papermill": { "duration": 1.873748, "end_time": "2025-07-07T04:27:40.371015", "exception": false, "start_time": "2025-07-07T04:27:38.497267", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/andrew/conda/stlearn/lib/python3.10/site-packages/louvain/__init__.py:54: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.\n", " from pkg_resources import get_distribution, DistributionNotFound\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Users/andrew/conda/stlearn/lib/python3.10/site-packages/stlearn/tl/cci/het.py:206: NumbaDeprecationWarning: The keyword argument 'nopython=False' was supplied. From Numba 0.59.0 the default is being changed to True and use of 'nopython=False' will raise a warning as the argument will have no effect. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\n", " @jit(parallel=True, nopython=False)\n" ] } ], "source": [ "import stlearn as st\n", "import scanpy as sc\n", "import pathlib as pathlib" ] }, { "cell_type": "code", "execution_count": 2, "id": "4160aedf", "metadata": { "execution": { "iopub.execute_input": "2025-07-07T04:27:40.373381Z", "iopub.status.busy": "2025-07-07T04:27:40.373128Z", "iopub.status.idle": "2025-07-07T04:27:41.087741Z", "shell.execute_reply": "2025-07-07T04:27:41.087323Z" }, "papermill": { "duration": 0.716513, "end_time": "2025-07-07T04:27:41.088523", "exception": false, "start_time": "2025-07-07T04:27:40.372010", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/andrew/conda/stlearn/lib/python3.10/site-packages/anndata/_core/anndata.py:1758: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.\n", " utils.warn_names_duplicates(\"var\")\n", "/Users/andrew/conda/stlearn/lib/python3.10/site-packages/anndata/_core/anndata.py:1758: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.\n", " utils.warn_names_duplicates(\"var\")\n" ] } ], "source": [ "# Read Visium data by scanpy\n", "st.settings.datasetdir = pathlib.Path.cwd().parent / \"data\"\n", "adata = sc.datasets.visium_sge()" ] }, { "cell_type": "code", "execution_count": 3, "id": "36df4498", "metadata": { "execution": { "iopub.execute_input": "2025-07-07T04:27:41.092930Z", "iopub.status.busy": "2025-07-07T04:27:41.092590Z", "iopub.status.idle": "2025-07-07T04:27:41.095953Z", "shell.execute_reply": "2025-07-07T04:27:41.095656Z" }, "papermill": { "duration": 0.006988, "end_time": "2025-07-07T04:27:41.096640", "exception": false, "start_time": "2025-07-07T04:27:41.089652", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "AnnData object with n_obs × n_vars = 3798 × 36601\n", " obs: 'in_tissue', 'array_row', 'array_col'\n", " var: 'gene_ids', 'feature_types', 'genome'\n", " uns: 'spatial'\n", " obsm: 'spatial'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "adata" ] }, { "cell_type": "code", "execution_count": 4, "id": "23597811", "metadata": { "execution": { "iopub.execute_input": "2025-07-07T04:27:41.099190Z", "iopub.status.busy": "2025-07-07T04:27:41.099006Z", "iopub.status.idle": "2025-07-07T04:27:41.103613Z", "shell.execute_reply": "2025-07-07T04:27:41.103353Z" }, "papermill": { "duration": 0.006605, "end_time": "2025-07-07T04:27:41.104274", "exception": false, "start_time": "2025-07-07T04:27:41.097669", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Convert AnnData object to work with stLearn\n", "adata = st.convert_scanpy(adata)" ] }, { "cell_type": "code", "execution_count": 5, "id": "1db88dc5", "metadata": { "execution": { "iopub.execute_input": "2025-07-07T04:27:41.106870Z", "iopub.status.busy": "2025-07-07T04:27:41.106695Z", "iopub.status.idle": "2025-07-07T04:27:41.108884Z", "shell.execute_reply": "2025-07-07T04:27:41.108654Z" }, "papermill": { "duration": 0.004048, "end_time": "2025-07-07T04:27:41.109545", "exception": false, "start_time": "2025-07-07T04:27:41.105497", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "AnnData object with n_obs × n_vars = 3798 × 36601\n", " obs: 'in_tissue', 'array_row', 'array_col', 'imagecol', 'imagerow'\n", " var: 'gene_ids', 'feature_types', 'genome'\n", " uns: 'spatial'\n", " obsm: 'spatial'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "adata" ] }, { "cell_type": "markdown", "id": "c2468157", "metadata": { "papermill": { "duration": 0.000949, "end_time": "2025-07-07T04:27:41.111535", "exception": false, "start_time": "2025-07-07T04:27:41.110586", "status": "completed" }, "tags": [] }, "source": [ "After that you can work with all functions in scanpy and please check their tutorials for the detail processes." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.18" }, "papermill": { "default_parameters": {}, "duration": 5.870182, "end_time": "2025-07-07T04:27:43.729925", "environment_variables": {}, "exception": null, "input_path": "./working_with_scanpy.ipynb", "output_path": "../output/./working_with_scanpy.ipynb", "parameters": {}, "start_time": "2025-07-07T04:27:37.859743", "version": "2.6.0" } }, "nbformat": 4, "nbformat_minor": 5 }