{
    "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/pfc5k_counts_only.h5ad\")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 4,
            "metadata": {},
            "outputs": [],
            "source": [
                "adata2 = anet.pp.normalize(adata, copy = True)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 5,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "[[0.         0.         0.         0.         0.        ]\n",
                        " [0.         0.         0.         1.43148847 0.        ]\n",
                        " [0.         0.         0.         0.         0.        ]\n",
                        " [0.         0.         0.         0.         0.        ]\n",
                        " [0.         0.         0.         0.         0.        ]\n",
                        " [1.57973691 0.         1.72499038 0.         0.        ]\n",
                        " [0.         0.         0.         0.         0.        ]\n",
                        " [0.         0.         0.         0.         0.        ]\n",
                        " [0.         0.         0.         0.         0.        ]\n",
                        " [0.         0.         0.         0.         0.        ]]\n"
                    ]
                }
            ],
            "source": [
                "print(adata2.X[0:5, 0:10].toarray().T)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 6,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Computing reduced ACTION kernel (sparse):\n",
                        "\tPerforming SVD on original matrix: IRLB (sparse) -- A: 17658 x 4505\n"
                    ]
                }
            ],
            "source": [
                "anet.pp.reduce_adata(adata2)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 7,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "[[ 11.01893973   9.06837029 -15.69997786  27.50794143  -1.99668638]\n",
                        " [ 11.31674892   9.06837029 -16.17988985  29.10352969  -2.51962805]\n",
                        " [ 10.27441678   9.06837029 -16.31700756  27.69941202  -2.18684699]\n",
                        " [ 11.91236728  11.79768561 -18.51089093  30.44382382  -3.32781063]\n",
                        " [ 12.35908105  10.03683701 -16.38556641  29.10352969  -3.99337276]]\n"
                    ]
                }
            ],
            "source": [
                "print(adata2.obsm[\"ACTION\"][0:5, 0:5])"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 8,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Running ACTION (8 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: 5\n",
                        "\tUnreliable archetypes: 17\n",
                        "\tTrivial archetypes: 0\n",
                        "Unifying 442 archetypes (8 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=8)\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(adata2, thread_no=8) "
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 9,
            "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": 9,
                    "metadata": {},
                    "output_type": "execute_result"
                },
                {
                    "data": {
                        "image/png": "[encoded data, 129140 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "anet.pl.plot_ACTIONet(adata2, \"Celltype\")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 11,
            "metadata": {},
            "outputs": [],
            "source": [
                "adata2.write_h5ad(\"data/pfc5k_ACTIONet_v2_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
}
