{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ACTIONet import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 2638 × 1838 \n",
       "    obs: 'n_genes', 'percent_mito', 'n_counts', 'louvain'\n",
       "    var: 'n_cells'\n",
       "    uns: 'draw_graph', 'louvain', 'louvain_colors', 'neighbors', 'pca', 'rank_genes_groups'\n",
       "    obsm: 'X_pca', 'X_tsne', 'X_umap', 'X_draw_graph_fr'\n",
       "    varm: 'PCs'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Imports PBMC 3k dataset from scanpy\n",
    "import scanpy as sc\n",
    "pbmc = sc.datasets.pbmc3k_processed()\n",
    "ACTIONet_out = pbmc\n",
    "ACTIONet_out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running reduction in dense mode ...\n",
      "Done.\n"
     ]
    }
   ],
   "source": [
    "# Performs kernel reduction\n",
    "reduce_kernel(ACTIONet_out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running ACTION ... \n",
      "Done.\n"
     ]
    }
   ],
   "source": [
    "# Runs ACTION for a range of archetypes (2..30 by default)\n",
    "C, H = run_ACTION(ACTIONet_out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Preprocesses archetypes to prune likely noisy archetypes.\n",
    "prune_archetypes(ACTIONet_out, C, H)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Building ACTIONet graph ...\n",
      "Done.\n"
     ]
    }
   ],
   "source": [
    "# Uses k*-NN together with JS metric on archetypes to construct the cell-cell network (ACTIONet graph)\n",
    "build_ACTIONet(ACTIONet_out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Computes 2D/3D embedding of cells, as well as their de novo coloring\n",
    "layout_ACTIONet(ACTIONet_out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Identifies and unifies redundant archetypes into equivalent classes and assigns cells to each class\n",
    "unify_archetypes(ACTIONet_out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "compute_archetype_core_centrality(ACTIONet_out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 25084 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 45828 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 42608 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sc.plotting.embedding(ACTIONet_out, \"ACTIONet2D\", color=\"louvain\")\n",
    "sc.plotting.embedding(ACTIONet_out, \"ACTIONet2D\", color=\"assigned_archetype\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 61980 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 73452 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 56816 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 45828 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sc.plotting.embedding(ACTIONet_out, \"pca\", color=\"louvain\")\n",
    "sc.plotting.embedding(ACTIONet_out, \"tsne\", color=\"louvain\")\n",
    "sc.plotting.embedding(ACTIONet_out, \"umap\", color=\"louvain\")\n",
    "sc.plotting.embedding(ACTIONet_out, \"ACTIONet2D\", color=\"louvain\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def unify_archetypes(\n",
    "    ACTIONet_out: AnnData,\n",
    "    minPoints: Optional[int] = 10,\n",
    "    minClusterSize: Optional[int] = 10,\n",
    "    outlier_threshold: Optional[float] = 0, \n",
    "    copy: Optional[bool] = False       \n",
    ") -> Optional[AnnData]:\n",
    "    \"\"\"\\\n",
    "    Archetype unification\n",
    "\n",
    "    Aggregates redundant archetypes.\n",
    "\n",
    "    Parameters\n",
    "    ----------\n",
    "    ACTIONet_out:\n",
    "        Current AnnData object storing the ACTIONet results    \n",
    "    int minPoints, int minClusterSize, double outlier_threshold:\n",
    "        HDBSCAN parameters\n",
    "    copy\n",
    "        Determines whether a copy of ACTIONet_out is returned. \n",
    "    Returns\n",
    "    -------\n",
    "        None, if copy is False, and ACTIONet_out: AnnData, otherwise. \n",
    "        In either case, \"C_pruned\" and \"H_pruned\" are added to the ACTIONet_out.obsm.\n",
    "    \"\"\"\n",
    "    G = None #ACTIONet_out.obsp[\"ACTIONet_connectivities\"]\n",
    "    S_r = ACTIONet_out.obsm['ACTION_S_r'].T\n",
    "    C_stacked = ACTIONet_out.obsm['C_stacked']\n",
    "    H_stacked = ACTIONet_out.obsm['H_stacked'].T\n",
    "    \n",
    "    unification_out = an.unify_archetypes(G, S_r, C_stacked, H_stacked, minPoints, minClusterSize, outlier_threshold)\n",
    "    \n",
    "    \n",
    "    ACTIONet_out.obsm['C_unified'] = unification_out[\"C_unified\"]\n",
    "    ACTIONet_out.obsm['H_unified'] = unification_out[\"H_unified\"].T\n",
    "    \n",
    "    return ACTIONet_out if copy else None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "unify_archetypes(ACTIONet_out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ACTIONet_out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
