{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "//anaconda3/envs/scanpy/lib/python3.6/site-packages/matplotlib/__init__.py:886: MatplotlibDeprecationWarning: \n",
      "examples.directory is deprecated; in the future, examples will be found relative to the 'datapath' directory.\n",
      "  \"found relative to the 'datapath' directory.\".format(key))\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "import pandas as pd\n",
    "import scipy as sp\n",
    "import scanpy as sc\n",
    "sc.set_figure_params(dpi=120)\n",
    "\n",
    "DATA_PATH = os.path.expanduser(\"./\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count_matrix.mtx             graph_distances.mtx      raw_count_matrix.mtx\r\n",
      "embedding.csv                metadata.csv             scanpy_integration.R\r\n",
      "genes.csv                    pca.csv                  scanpy_integration.ipynb\r\n",
      "graph_connectivities.mtx pseudopca.csv\r\n"
     ]
    }
   ],
   "source": [
    "!ls $DATA_PATH"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "gene_df = pd.read_csv(DATA_PATH + \"genes.csv\")\n",
    "\n",
    "metadata = pd.read_csv(DATA_PATH + \"metadata.csv\")\n",
    "metadata.index = metadata.CellId\n",
    "del metadata[\"CellId\"]\n",
    "\n",
    "embedding_df = pd.read_csv(DATA_PATH + \"embedding.csv\")\n",
    "# Decide between using PCA or pseudo-PCA\n",
    "pseudopca_df = pd.read_csv(DATA_PATH + \"pseudopca.csv\")\n",
    "#pca_df = pd.read_csv(DATA_PATH + \"pca.csv\")\n",
    "\n",
    "graph_conn_mtx = sp.io.mmread(DATA_PATH + \"graph_connectivities.mtx\")\n",
    "graph_dist_mtx = sp.io.mmread(DATA_PATH + \"graph_distances.mtx\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create ScanPy object "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 12000 × 33694 "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Begin by reading in raw counts\n",
    "adata = sc.read_mtx(DATA_PATH + \"raw_count_matrix.mtx\")\n",
    "adata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "adata.var_names = gene_df[\"gene\"].values\n",
    "adata.obs_names = metadata.index.values\n",
    "\n",
    "adata.obs = metadata.copy()\n",
    "\n",
    "# Depends on which PCA you loaded\n",
    "adata.X_pca = pseudopca_df.values\n",
    "adata.obsm['X_pca'] = pseudopca_df.values\n",
    "\n",
    "# Name according to embedding you saved\n",
    "adata.X_umap = embedding_df.values\n",
    "adata.obsm['X_umap'] = embedding_df.values\n",
    "\n",
    "adata.uns['neighbors'] = dict(connectivities=graph_conn_mtx.tocsr(), distances=graph_dist_mtx.tocsr())\n",
    "adata.uns['neighbors']['params'] = dict(n_pcs=pca_df.shape[1], use_rep='X_pca', metric='cosine', method='umap', n_neighbors=30)",
    "\n",
    "# Assign raw counts to .raw slot, load in normalised counts\n",
    "adata.raw = adata\n",
    "adata_temp = sc.read_mtx(DATA_PATH + \"count_matrix.mtx\")\n",
    "adata.X = adata_temp.X\n",
    "\n",
    "# Change into categorical variable (helps with plotting)\n",
    "adata.obs['Cluster'] = adata.obs['Cluster'].astype('category')\n",
    "adata.obs['Dataset'] = adata.obs['Dataset'].astype('category')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 21644 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sc.pl.pca(adata, color='Dataset', frameon=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 103444 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sc.pl.umap(adata, color='Dataset', frameon=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 91660 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sc.pl.umap(adata, color='Cluster', frameon=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "//anaconda3/envs/scanpy/lib/python3.6/site-packages/anndata/core/anndata.py:846: FutureWarning: In anndata v0.7+, arrays contained within an AnnData object will maintain their dimensionality. For example, prior to v0.7 `adata[0, 0].X` returned a scalar and `adata[0, :]` returned a 1d array, post v0.7 they will return two dimensional arrays. If you would like to get a one dimensional array from your AnnData object, consider using the `adata.obs_vector`, `adata.var_vector` methods or accessing the array directly.\n",
      "  warn_flatten()\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 71176 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sc.pl.umap(adata, color='CD74', frameon=True, cmap='Reds', use_raw=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30804 characters]\n",
      "text/plain": [
       "<Figure size 426.312x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sc.tl.paga(adata, groups='Cluster')\n",
    "sc.pl.paga(adata, color='Cluster', threshold=0.2, fontsize=20, node_size_scale=1, edge_width_scale=0.2, node_size_power=0.3, layout='fr')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 104064 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sc.tl.draw_graph(adata, init_pos='paga')\n",
    "sc.pl.draw_graph(adata, color='Cluster', size=2, legend_fontsize=6, frameon=False, edges=True, title=\"\")"
   ]
  }
 ],
 "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.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
