{
    "cells": [
        {
            "cell_type": "code",
            "execution_count": 1,
            "metadata": {},
            "outputs": [],
            "source": [
                "import anndata as ad\n",
                "import ACTIONet as anet"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 2,
            "metadata": {},
            "outputs": [],
            "source": [
                "%load_ext autoreload\n",
                "%autoreload 2\n",
                "%aimport ACTIONet"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 3,
            "metadata": {},
            "outputs": [],
            "source": [
                "adata = ad.read_h5ad(\"data/MouseBrain_sce.h5ad\")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 4,
            "metadata": {},
            "outputs": [],
            "source": [
                "anet.pp.normalize(adata)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 5,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "  (3, 0)\t0.19426360329792525\n",
                        "  (6, 0)\t0.09472177501057862\n",
                        "  (8, 0)\t0.003300493166379891\n",
                        "  (5, 1)\t0.0028526684625663932\n",
                        "  (9, 1)\t0.00811486255005374\n",
                        "  (3, 5)\t0.08475168045168134\n",
                        "  (4, 5)\t0.8498425296261223\n",
                        "  (3, 7)\t0.029056006129995482\n",
                        "  (4, 7)\t1.7248040540782026\n",
                        "  (0, 8)\t0.048739462328497005\n",
                        "  (2, 8)\t0.05819561193116353\n",
                        "  (3, 8)\t0.45674371974454114\n",
                        "  (4, 8)\t0.4636560928195409\n",
                        "  (6, 8)\t0.5425038398520935\n",
                        "  (1, 9)\t1.70360174787663\n",
                        "  (8, 9)\t0.28562589429689433\n"
                    ]
                }
            ],
            "source": [
                "print(adata.X[0:10, 0:10])"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 6,
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "index\n",
                            "CORTEX1_SMART_SEQ2.M2_EXPT2_1      Cortex1.Smart-seq2\n",
                            "CORTEX1_SMART_SEQ2.M2_EXPT2_100    Cortex1.Smart-seq2\n",
                            "CORTEX1_SMART_SEQ2.M2_EXPT2_101    Cortex1.Smart-seq2\n",
                            "CORTEX1_SMART_SEQ2.M2_EXPT2_103    Cortex1.Smart-seq2\n",
                            "CORTEX1_SMART_SEQ2.M2_EXPT2_104    Cortex1.Smart-seq2\n",
                            "CORTEX1_SMART_SEQ2.M2_EXPT2_106    Cortex1.Smart-seq2\n",
                            "CORTEX1_SMART_SEQ2.M2_EXPT2_11     Cortex1.Smart-seq2\n",
                            "CORTEX1_SMART_SEQ2.M2_EXPT2_110    Cortex1.Smart-seq2\n",
                            "CORTEX1_SMART_SEQ2.M2_EXPT2_111    Cortex1.Smart-seq2\n",
                            "Name: batch, dtype: category\n",
                            "Categories (8, object): ['Cortex1.10x Chromium', 'Cortex1.DroNc-seq', 'Cortex1.sci-RNA-seq', 'Cortex1.Smart-seq2', 'Cortex2.10x Chromium', 'Cortex2.DroNc-seq', 'Cortex2.sci-RNA-seq', 'Cortex2.Smart-seq2']"
                        ]
                    },
                    "execution_count": 6,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "batch = adata.obs[\"batch\"]\n",
                "batch[0:9]"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 7,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Computing reduced ACTION kernel (sparse):\n",
                        "\tPerforming SVD on original matrix: IRLB (sparse) -- A: 22229 x 11518\n",
                        "Harmony 0/10\n",
                        "Harmony 1/10\n",
                        "Harmony converged after 1 iterations\n"
                    ]
                }
            ],
            "source": [
                "anet.pp.reduce_and_batch_correct_adata_Harmony(adata, \"batch\")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 8,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "[[ 3.22184116  1.48700361  3.15988268  2.72617329  2.66421481]\n",
                        " [ 3.81315213  0.56912718 -7.22791524 -8.99220951  4.21154116]\n",
                        " [-3.18417682  1.59208841  0.2041139  -0.36740502 -3.26582238]\n",
                        " [-4.3833672  -4.6158185  -8.73352708 -8.43466114 -4.15091591]\n",
                        " [ 5.04551092  1.69173013  5.22358778  4.12544717  3.85833188]]\n"
                    ]
                }
            ],
            "source": [
                "print(adata.obsm[\"ACTION\"][0:5, 0:5].T)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 9,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "[[ 0.51764229 -0.87485257  0.68923611  0.46881149 -0.09071189]\n",
                        " [ 3.98214797  1.9217082  -3.45492723 -5.3818075   4.3431438 ]\n",
                        " [-1.12356627  2.87146292  1.59554893  0.86385045 -1.09090288]\n",
                        " [-0.94841657 -0.23533191 -3.69585145 -3.57886167 -0.73142582]\n",
                        " [ 2.42311872 -0.30924331  2.66045457  1.79130454  1.23699806]]\n"
                    ]
                }
            ],
            "source": [
                "print(adata.obsm[\"Harmony\"][0:5, 0:5].T)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 10,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Running ACTION (10 threads):\n",
                        "\tIterating from k = 2 ... 30: 29/29 finished\n",
                        "Joining trace of C & H matrices (depth = 30) ... done (464 archetypes)\n",
                        "Pruning archetypes:\n",
                        "\tNon-specific archetypes: 4\n",
                        "\tUnreliable archetypes: 34\n",
                        "\tTrivial archetypes: 2\n",
                        "Unifying 425 archetypes (10 threads):\n",
                        "# unified archetypes: 23\n",
                        "Building adaptive network (density = 1.00)\n",
                        "\tBuilding index ... done\n",
                        "\tIdentifying nearest neighbors ... done\n",
                        "\tConstructing adaptive-nearest neighbor graph ... done\n",
                        "\tFinalizing network ... \n",
                        "\t\tKeeping mutual nearest-neighbors only ... done\n",
                        "Laying-out input network: method = umap, a = 0.115, b = 1.929 (epochs = 100, threads=10)\n",
                        "Computing 2D layout ... done\n",
                        "Computing 3D layout ... done\n",
                        "Computing de novo node colors ... done\n",
                        "Computing feature specificity ... done\n",
                        "Building adaptive network (density = 0.50)\n",
                        "\tBuilding index ... done\n",
                        "\tIdentifying nearest neighbors ... done\n",
                        "\tConstructing adaptive-nearest neighbor graph ... done\n",
                        "\tFinalizing network ... \n",
                        "\t\tKeeping mutual nearest-neighbors only ... done\n"
                    ]
                }
            ],
            "source": [
                "anet.run_ACTIONet(adata)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 11,
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "AnnData object with n_obs × n_vars = 11518 × 22229\n",
                            "    obs: 'NAME', 'nGene', 'nUMI', 'percent.mito', 'Cluster', 'CellType', 'Experiment', 'Method', 'batch', 'assigned_archetype', 'node_centrality'\n",
                            "    var: 'Symbol', 'Ensemble_Gene_ID'\n",
                            "    uns: 'metadata', 'obsm_annot', 'varm_annot', 'ACTION'\n",
                            "    obsm: 'ACTION', 'ACTION_B', 'Harmony', 'Harmony_normalized', 'C_stacked', 'H_stacked', 'C_unified', 'H_unified', 'archetype_footprint', 'ACTIONred', 'ACTIONet2D', 'ACTIONet3D', 'denovo_color'\n",
                            "    varm: 'ACTION_V', 'ACTION_A', 'unified_feature_profile', 'unified_feature_specificity'\n",
                            "    obsp: 'ACTIONet'"
                        ]
                    },
                    "execution_count": 11,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "adata"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 12,
            "metadata": {},
            "outputs": [
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "/Users/mohammadi/miniconda3/envs/actionet/lib/python3.10/site-packages/scanpy/plotting/_tools/scatterplots.py:392: UserWarning:\n",
                        "\n",
                        "No data for colormapping provided via 'c'. Parameters 'cmap' will be ignored\n",
                        "\n"
                    ]
                },
                {
                    "data": {
                        "text/plain": [
                            "<AxesSubplot: title={'center': '__annotations__'}, xlabel='actionet2d1', ylabel='actionet2d2'>"
                        ]
                    },
                    "execution_count": 12,
                    "metadata": {},
                    "output_type": "execute_result"
                },
                {
                    "data": {
                        "image/png": "[encoded data, 237196 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "anet.pl.plot_ACTIONet(adata, \"batch\")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 13,
            "metadata": {},
            "outputs": [
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "/Users/mohammadi/miniconda3/envs/actionet/lib/python3.10/site-packages/scanpy/plotting/_tools/scatterplots.py:392: UserWarning:\n",
                        "\n",
                        "No data for colormapping provided via 'c'. Parameters 'cmap' will be ignored\n",
                        "\n"
                    ]
                },
                {
                    "data": {
                        "text/plain": [
                            "<AxesSubplot: title={'center': '__annotations__'}, xlabel='actionet2d1', ylabel='actionet2d2'>"
                        ]
                    },
                    "execution_count": 13,
                    "metadata": {},
                    "output_type": "execute_result"
                },
                {
                    "data": {
                        "image/png": "[encoded data, 241872 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "anet.pl.plot_ACTIONet(adata, \"CellType\")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 15,
            "metadata": {},
            "outputs": [],
            "source": [
                "adata.write_h5ad(\"data/MouseBrain_ACTIONet_HarmonySPA_python.h5ad\")"
            ]
        }
    ],
    "metadata": {
        "interpreter": {
            "hash": "e6b19d727065d69898d5719ef57bdd3f52a377d995ad1aaa3251e7a9df51309b"
        },
        "kernelspec": {
            "display_name": "Python 3.8.8 64-bit",
            "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.6"
        },
        "orig_nbformat": 4
    },
    "nbformat": 4,
    "nbformat_minor": 2
}
