{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import scanpy as sc\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from ccHBGF import ccHBGF\n",
    "\n",
    "from igraph import Graph\n",
    "from scanpy import _utils\n",
    "from typing import Any"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's load an example dataset from ScanPy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 2700 × 32738\n",
       "    var: 'gene_ids'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adata = sc.datasets.pbmc3k()\n",
    "adata"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's preprocess for clustering"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 2700 × 3000\n",
       "    var: 'gene_ids', 'highly_variable', 'highly_variable_rank', 'means', 'variances', 'variances_norm'\n",
       "    uns: 'hvg', 'log1p'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Denoise and normalize the the data\n",
    "sc.pp.highly_variable_genes(adata, flavor='seurat_v3_paper', n_top_genes=3_000, inplace=True, subset=True)\n",
    "sc.pp.normalize_total(adata, target_sum=1e6)\n",
    "sc.pp.log1p(adata)\n",
    "\n",
    "adata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 2700 × 3000\n",
       "    var: 'gene_ids', 'highly_variable', 'highly_variable_rank', 'means', 'variances', 'variances_norm'\n",
       "    uns: 'hvg', 'log1p', 'pca', 'neighbors', 'umap'\n",
       "    obsm: 'X_pca', 'X_umap'\n",
       "    varm: 'PCs'\n",
       "    obsp: 'distances', 'connectivities'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Calculate Neighbourhood Graph\n",
    "sc.pp.pca(adata)\n",
    "sc.pp.neighbors(adata, n_neighbors=15, use_rep='X_pca')\n",
    "\n",
    "# Calculate UMAP for later visualization\n",
    "sc.tl.umap(adata, random_state=0)\n",
    "\n",
    "adata"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's run leiden clustering for 100 different seeds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def igraph_leiden(\n",
    "    g: Graph,\n",
    "    resolution: float = 1.0,\n",
    "    n_iterations: int = 2,\n",
    "    random_state: _utils.AnyRandom = 0,\n",
    "    **clustering_args: dict[str, Any]\n",
    ") -> list[int]:\n",
    "    \"\"\"\n",
    "    connectivities_key = adata.uns['neighbors']['connectivities_key']\n",
    "    adata_igraph = sc._utils.get_igraph_from_adjacency(adata.obsp[connectivities_key])\n",
    "    \"\"\"\n",
    "    # Copy clustering arguments and set number of iterations\n",
    "    clustering_args = dict(clustering_args)\n",
    "    clustering_args[\"n_iterations\"] = n_iterations\n",
    "    \n",
    "    # Set resolution if provided\n",
    "    if resolution is not None:\n",
    "        clustering_args[\"resolution\"] = resolution\n",
    "\n",
    "    # Set default objective function if not specified\n",
    "    clustering_args.setdefault(\"objective_function\", \"modularity\")\n",
    "    \n",
    "    # Run Leiden clustering with specified random state\n",
    "    with _utils.set_igraph_random_state(random_state):\n",
    "        part = g.community_leiden(**clustering_args)\n",
    "\n",
    "    return part.membership\n",
    "\n",
    "connectivities_key = adata.uns['neighbors']['connectivities_key']\n",
    "adata_igraph = sc._utils.get_igraph_from_adjacency(adata.obsp[connectivities_key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "N_SEEDS = 100\n",
    "RESOLUTION = 5\n",
    "\n",
    "solutions_matrix = np.zeros((adata.shape[0], N_SEEDS))\n",
    "\n",
    "for seed in range(N_SEEDS):\n",
    "    solutions_matrix[:, seed] = igraph_leiden(adata_igraph, resolution=RESOLUTION, random_state=seed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's find the consensus labels between these solutions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "ccHBGF - 02:04:43: Detected 51 clusters.\n",
      "ccHBGF - 02:04:43: Graph adjacency matrix (A) constructed with shape (2700, 4682)\n",
      "ccHBGF - 02:04:44: Transformed A to Laplacian Matrix (L) of shape (2700, 4682)\n",
      "ccHBGF - 02:04:44: Decomposed L into Singular Values (SVs)\n",
      "ccHBGF - 02:04:44: Normalized SVs\n",
      "ccHBGF - 02:04:44: Initialized Centers\n",
      "ccHBGF - 02:04:44: Orthogonalized Centers\n",
      "ccHBGF - 02:04:44: KMeans model fitted to UVt\n",
      "ccHBGF - 02:04:44: Consensus Labels Found\n"
     ]
    }
   ],
   "source": [
    "consensus_labels = ccHBGF(solutions_matrix, init='orthogonal', tol=0.1, verbose=True, random_state=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's visualize the resulting clusters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 56328 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "adata.obs['consensus_clusters'] = pd.Categorical(consensus_labels)\n",
    "\n",
    "sc.pl.umap(adata, color='consensus_clusters', legend_loc=None, palette='tab20', frameon=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
