{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/anna.danese/anaconda3/envs/new_snapatac/lib/python3.6/site-packages/anndata/core/anndata.py:17: FutureWarning: pandas.core.index is deprecated and will be removed in a future version.  The public classes are available in the top-level namespace.\n",
      "  from pandas.core.index import RangeIndex\n",
      "/Users/anna.danese/anaconda3/envs/new_snapatac/lib/python3.6/site-packages/scanpy/api/__init__.py:6: FutureWarning: \n",
      "\n",
      "In a future version of Scanpy, `scanpy.api` will be removed.\n",
      "Simply use `import scanpy as sc` and `import scanpy.external as sce` instead.\n",
      "\n",
      "  FutureWarning\n"
     ]
    }
   ],
   "source": [
    "import episcanpy as epi\n",
    "import scanpy as sc\n",
    "import anndata as ad\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# figure settings\n",
    "sc.set_figure_params(scanpy=True, dpi=80, dpi_save=250,\n",
    "                     frameon=True, vector_friendly=True,\n",
    "                     color_map=None, format='pdf', transparent=False,\n",
    "                     ipython_format='png2x')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load enhancers mCG methylation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "play data available at:\n",
    "https://www.dropbox.com/sh/zwmtsj6c85woyqp/AADSCz0QVgVtammgMjasK3R4a?dl=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# path of input an output directories\n",
    "DATADIR =''\n",
    "OUT_DATADIR = './paper_data_methylation_episcanpy/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# if you want to load the raw count matrix and impute it at the same time\n",
    "#adata = epi.pp.readandimputematrix(DATADIR+'enhancer_c1_CG_paper.txt', min_coverage=500)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#to load the data without imputation \n",
    "adata = epi.pp.load_met_noimput(DATADIR+'enhancer_c1_CG_paper.txt')\n",
    "# save the data as a sparse AnnData rather than a dense tab separated file\n",
    "adata.write(OUT_DATADIR+'enhancer_c1_CG_paper.h5ad')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load the raw AnnData"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 3379 × 55017 \n",
       "    obs: 'cell_type', 'Library pool', 'Index i5', 'Index i7', 'i5 sequence', 'i7 sequence', 'random primer index', 'random primer index sequence', 'Total reads', 'Mapped reads', 'Filtered reads', 'mCCC/CCC', 'mCG/CG', 'mCH/CH', 'Estimated mCG/CG', 'Estimated mCH/CH', 'Neuron type', 'tSNE x coordinate', 'tSNE y coordinate\\n', 'binary'\n",
       "    uns: 'Estimated mCG', 'Estimated mCH', 'mCCC', 'mCG', 'mCH', 'omic'"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adata = ad.read(OUT_DATADIR+'enhancer_c1_CG_paper.h5ad')\n",
    "adata"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Quality controls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "# adding QC values\n",
    "length1 = len(adata.X[0,:])\n",
    "length2 = len(adata.X[:,0])\n",
    "adata.obs['coverage_cells'] = [length1 - np.isnan(line).sum() for line in adata.X]\n",
    "adata.obs['mean_cell_methylation'] = [np.nansum(line)/length1 for line in adata.X]\n",
    "adata.var['coverage_feature'] = [length2 - np.isnan(line).sum() for line in adata.X.T]\n",
    "adata.var['mean_feature_methylation'] = [np.nansum(line)/length2 for line in adata.X.T]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cell_type</th>\n",
       "      <th>Library pool</th>\n",
       "      <th>Index i5</th>\n",
       "      <th>Index i7</th>\n",
       "      <th>i5 sequence</th>\n",
       "      <th>i7 sequence</th>\n",
       "      <th>random primer index</th>\n",
       "      <th>random primer index sequence</th>\n",
       "      <th>Total reads</th>\n",
       "      <th>Mapped reads</th>\n",
       "      <th>...</th>\n",
       "      <th>mCCC/CCC</th>\n",
       "      <th>mCG/CG</th>\n",
       "      <th>mCH/CH</th>\n",
       "      <th>Estimated mCG/CG</th>\n",
       "      <th>Estimated mCH/CH</th>\n",
       "      <th>Neuron type</th>\n",
       "      <th>tSNE x coordinate</th>\n",
       "      <th>tSNE y coordinate\\n</th>\n",
       "      <th>binary</th>\n",
       "      <th>coverage_cells</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>index</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>allc_Pool_1000_AD002_indexed_qual30.tsv</th>\n",
       "      <td>mL5-1</td>\n",
       "      <td>K1K2</td>\n",
       "      <td>D508</td>\n",
       "      <td>D711</td>\n",
       "      <td>GTACTGAC</td>\n",
       "      <td>TCTCGCGC</td>\n",
       "      <td>AD002</td>\n",
       "      <td>CGATGT</td>\n",
       "      <td>3749296</td>\n",
       "      <td>1970400</td>\n",
       "      <td>...</td>\n",
       "      <td>278</td>\n",
       "      <td>1388</td>\n",
       "      <td>1138</td>\n",
       "      <td>148</td>\n",
       "      <td>1022</td>\n",
       "      <td>mL5-1</td>\n",
       "      <td>-2,79539</td>\n",
       "      <td>-0,105851\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>21715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_nuclei_600_S147_L006_qual30.tsv</th>\n",
       "      <td>mL5-1</td>\n",
       "      <td>N/A</td>\n",
       "      <td>D508</td>\n",
       "      <td>D712</td>\n",
       "      <td>GTACTGAC</td>\n",
       "      <td>AGCGATAG</td>\n",
       "      <td>AD012</td>\n",
       "      <td>CTTGTA</td>\n",
       "      <td>7069454</td>\n",
       "      <td>4358703</td>\n",
       "      <td>...</td>\n",
       "      <td>208</td>\n",
       "      <td>238</td>\n",
       "      <td>1174</td>\n",
       "      <td>23</td>\n",
       "      <td>1114</td>\n",
       "      <td>mL5-1</td>\n",
       "      <td>-0,352829</td>\n",
       "      <td>0,932857\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>9976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_nuclei_599_S146_L006_qual30.tsv</th>\n",
       "      <td>mL6-2</td>\n",
       "      <td>N/A</td>\n",
       "      <td>D507</td>\n",
       "      <td>D712</td>\n",
       "      <td>CAGGACGT</td>\n",
       "      <td>AGCGATAG</td>\n",
       "      <td>AD011</td>\n",
       "      <td>GGCTAC</td>\n",
       "      <td>18242158</td>\n",
       "      <td>11386231</td>\n",
       "      <td>...</td>\n",
       "      <td>88</td>\n",
       "      <td>1644</td>\n",
       "      <td>273</td>\n",
       "      <td>179</td>\n",
       "      <td>296</td>\n",
       "      <td>mL6-2</td>\n",
       "      <td>10,0546</td>\n",
       "      <td>-9,78996\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>20162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_nuclei_598_S145_L006_qual30.tsv</th>\n",
       "      <td>mL6-1</td>\n",
       "      <td>N/A</td>\n",
       "      <td>D506</td>\n",
       "      <td>D712</td>\n",
       "      <td>TAATCTTA</td>\n",
       "      <td>AGCGATAG</td>\n",
       "      <td>AD010</td>\n",
       "      <td>TAGCTT</td>\n",
       "      <td>7045252</td>\n",
       "      <td>4197889</td>\n",
       "      <td>...</td>\n",
       "      <td>18</td>\n",
       "      <td>2750</td>\n",
       "      <td>887</td>\n",
       "      <td>2765</td>\n",
       "      <td>0</td>\n",
       "      <td>mL6-1</td>\n",
       "      <td>9,37005</td>\n",
       "      <td>2,71283\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>10374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_nuclei_597_S144_L006_qual30.tsv</th>\n",
       "      <td>mL4</td>\n",
       "      <td>N/A</td>\n",
       "      <td>D505</td>\n",
       "      <td>D712</td>\n",
       "      <td>AGGCGAAG</td>\n",
       "      <td>AGCGATAG</td>\n",
       "      <td>AD008</td>\n",
       "      <td>ACTTGA</td>\n",
       "      <td>15116650</td>\n",
       "      <td>9483039</td>\n",
       "      <td>...</td>\n",
       "      <td>149</td>\n",
       "      <td>2213</td>\n",
       "      <td>702</td>\n",
       "      <td>238</td>\n",
       "      <td>70</td>\n",
       "      <td>mL4</td>\n",
       "      <td>-4,17269</td>\n",
       "      <td>-2,94523\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>17226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_Pool_839_AD006_indexed_qual30.tsv</th>\n",
       "      <td>mPv</td>\n",
       "      <td>I1I2</td>\n",
       "      <td>D507</td>\n",
       "      <td>D712</td>\n",
       "      <td>CAGGACGT</td>\n",
       "      <td>AGCGATAG</td>\n",
       "      <td>AD006</td>\n",
       "      <td>GCCAAT</td>\n",
       "      <td>6760981</td>\n",
       "      <td>2875103</td>\n",
       "      <td>...</td>\n",
       "      <td>205</td>\n",
       "      <td>244</td>\n",
       "      <td>891</td>\n",
       "      <td>2264</td>\n",
       "      <td>821</td>\n",
       "      <td>mPv</td>\n",
       "      <td>-0,198246</td>\n",
       "      <td>19,7879\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>28767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_Pool_83_AD002_indexed_qual30.tsv</th>\n",
       "      <td>mL2/3</td>\n",
       "      <td>A1A2</td>\n",
       "      <td>D503</td>\n",
       "      <td>D711</td>\n",
       "      <td>CCTATCCT</td>\n",
       "      <td>TCTCGCGC</td>\n",
       "      <td>AD002</td>\n",
       "      <td>CGATGT</td>\n",
       "      <td>5254487</td>\n",
       "      <td>2706493</td>\n",
       "      <td>...</td>\n",
       "      <td>141</td>\n",
       "      <td>528</td>\n",
       "      <td>494</td>\n",
       "      <td>541</td>\n",
       "      <td>484</td>\n",
       "      <td>mL2/3</td>\n",
       "      <td>-13,1049</td>\n",
       "      <td>0,642965\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>27409</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_Pool_83_AD006_indexed_qual30.tsv</th>\n",
       "      <td>mL4</td>\n",
       "      <td>A1A2</td>\n",
       "      <td>D503</td>\n",
       "      <td>D711</td>\n",
       "      <td>CCTATCCT</td>\n",
       "      <td>TCTCGCGC</td>\n",
       "      <td>AD006</td>\n",
       "      <td>GCCAAT</td>\n",
       "      <td>5332867</td>\n",
       "      <td>1913346</td>\n",
       "      <td>...</td>\n",
       "      <td>93</td>\n",
       "      <td>1060</td>\n",
       "      <td>228</td>\n",
       "      <td>1071</td>\n",
       "      <td>230</td>\n",
       "      <td>mL4</td>\n",
       "      <td>-6,94662</td>\n",
       "      <td>-9,61211\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>26023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_Pool_995_AD002_indexed_qual30.tsv</th>\n",
       "      <td>mL6-2</td>\n",
       "      <td>K1K2</td>\n",
       "      <td>D503</td>\n",
       "      <td>D711</td>\n",
       "      <td>CCTATCCT</td>\n",
       "      <td>TCTCGCGC</td>\n",
       "      <td>AD002</td>\n",
       "      <td>CGATGT</td>\n",
       "      <td>3508294</td>\n",
       "      <td>1813927</td>\n",
       "      <td>...</td>\n",
       "      <td>273</td>\n",
       "      <td>2144</td>\n",
       "      <td>1038</td>\n",
       "      <td>2156</td>\n",
       "      <td>93</td>\n",
       "      <td>mL6-2</td>\n",
       "      <td>14,0262</td>\n",
       "      <td>-4,28728\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>20658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_nuclei-319_S59_L002_qual30_1.tsv</th>\n",
       "      <td>mL4</td>\n",
       "      <td>20160217 pool B</td>\n",
       "      <td>D503</td>\n",
       "      <td>D708</td>\n",
       "      <td>CCTATCCT</td>\n",
       "      <td>TAATGCGC</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>3480402</td>\n",
       "      <td>1625695</td>\n",
       "      <td>...</td>\n",
       "      <td>15</td>\n",
       "      <td>2158</td>\n",
       "      <td>597</td>\n",
       "      <td>226</td>\n",
       "      <td>582</td>\n",
       "      <td>mL4</td>\n",
       "      <td>-4,58041</td>\n",
       "      <td>-5,07954\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>1486</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3379 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        cell_type     Library pool Index i5  \\\n",
       "index                                                                         \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv     mL5-1             K1K2     D508   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv        mL5-1              N/A     D508   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv        mL6-2              N/A     D507   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv        mL6-1              N/A     D506   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv          mL4              N/A     D505   \n",
       "...                                           ...              ...      ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv        mPv             I1I2     D507   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv       mL2/3             A1A2     D503   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv         mL4             A1A2     D503   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv      mL6-2             K1K2     D503   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv         mL4  20160217 pool B     D503   \n",
       "\n",
       "                                        Index i7 i5 sequence i7 sequence  \\\n",
       "index                                                                      \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv     D711    GTACTGAC    TCTCGCGC   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv        D712    GTACTGAC    AGCGATAG   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv        D712    CAGGACGT    AGCGATAG   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv        D712    TAATCTTA    AGCGATAG   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv        D712    AGGCGAAG    AGCGATAG   \n",
       "...                                          ...         ...         ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv      D712    CAGGACGT    AGCGATAG   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv       D711    CCTATCCT    TCTCGCGC   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv       D711    CCTATCCT    TCTCGCGC   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv      D711    CCTATCCT    TCTCGCGC   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv       D708    CCTATCCT    TAATGCGC   \n",
       "\n",
       "                                        random primer index  \\\n",
       "index                                                         \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv               AD002   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv                  AD012   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv                  AD011   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv                  AD010   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv                  AD008   \n",
       "...                                                     ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv                AD006   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv                 AD002   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv                 AD006   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv                AD002   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv                         \n",
       "\n",
       "                                        random primer index sequence  \\\n",
       "index                                                                  \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv                       CGATGT   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv                          CTTGTA   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv                          GGCTAC   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv                          TAGCTT   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv                          ACTTGA   \n",
       "...                                                              ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv                        GCCAAT   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv                         CGATGT   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv                         GCCAAT   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv                        CGATGT   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv                                  \n",
       "\n",
       "                                        Total reads Mapped reads  ...  \\\n",
       "index                                                             ...   \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv     3749296      1970400  ...   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv        7069454      4358703  ...   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv       18242158     11386231  ...   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv        7045252      4197889  ...   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv       15116650      9483039  ...   \n",
       "...                                             ...          ...  ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv      6760981      2875103  ...   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv       5254487      2706493  ...   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv       5332867      1913346  ...   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv      3508294      1813927  ...   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv       3480402      1625695  ...   \n",
       "\n",
       "                                        mCCC/CCC  mCG/CG  mCH/CH  \\\n",
       "index                                                              \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv      278    1388    1138   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv         208     238    1174   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv          88    1644     273   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv          18    2750     887   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv         149    2213     702   \n",
       "...                                          ...     ...     ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv       205     244     891   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv        141     528     494   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv         93    1060     228   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv       273    2144    1038   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv         15    2158     597   \n",
       "\n",
       "                                         Estimated mCG/CG  Estimated mCH/CH  \\\n",
       "index                                                                         \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv               148              1022   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv                   23              1114   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv                  179               296   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv                 2765                 0   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv                  238                70   \n",
       "...                                                   ...               ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv               2264               821   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv                 541               484   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv                1071               230   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv               2156                93   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv                 226               582   \n",
       "\n",
       "                                         Neuron type tSNE x coordinate  \\\n",
       "index                                                                    \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv        mL5-1          -2,79539   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv           mL5-1         -0,352829   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv           mL6-2           10,0546   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv           mL6-1           9,37005   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv             mL4          -4,17269   \n",
       "...                                              ...               ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv           mPv         -0,198246   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv          mL2/3          -13,1049   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv            mL4          -6,94662   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv         mL6-2           14,0262   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv            mL4          -4,58041   \n",
       "\n",
       "                                        tSNE y coordinate\\n      binary  \\\n",
       "index                                                                     \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv         -0,105851\\n  inhibitory   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv             0,932857\\n  inhibitory   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv             -9,78996\\n  inhibitory   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv              2,71283\\n  inhibitory   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv             -2,94523\\n  inhibitory   \n",
       "...                                                     ...         ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv            19,7879\\n  inhibitory   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv            0,642965\\n  inhibitory   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv            -9,61211\\n  inhibitory   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv           -4,28728\\n  inhibitory   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv            -5,07954\\n  inhibitory   \n",
       "\n",
       "                                        coverage_cells  \n",
       "index                                                   \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv          21715  \n",
       "allc_nuclei_600_S147_L006_qual30.tsv              9976  \n",
       "allc_nuclei_599_S146_L006_qual30.tsv             20162  \n",
       "allc_nuclei_598_S145_L006_qual30.tsv             10374  \n",
       "allc_nuclei_597_S144_L006_qual30.tsv             17226  \n",
       "...                                                ...  \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv           28767  \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv            27409  \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv            26023  \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv           20658  \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv             1486  \n",
       "\n",
       "[3379 rows x 21 columns]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adata.obs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24132 characters]\n",
      "text/plain": [
       "<Figure size 320x320 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 281,
       "width": 299
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# number of peaks in a cell\n",
    "plt.axvline(x=10000, color='r') # minimum number of feature covered per cells\n",
    "np.histogram(adata.obs['coverage_cells'])\n",
    "plt.hist(adata.obs['coverage_cells'], bins=50)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x1354389e8>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 51740 characters]\n",
      "text/plain": [
       "<Figure size 320x320 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 281,
       "width": 320
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.axvline(x=500, color='r') # minimum number of cells covered to keep a feature\n",
    "sns.set_style('whitegrid')\n",
    "sns.kdeplot(np.array(adata.var['coverage_feature']), bw=0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cell_type</th>\n",
       "      <th>Library pool</th>\n",
       "      <th>Index i5</th>\n",
       "      <th>Index i7</th>\n",
       "      <th>i5 sequence</th>\n",
       "      <th>i7 sequence</th>\n",
       "      <th>random primer index</th>\n",
       "      <th>random primer index sequence</th>\n",
       "      <th>Total reads</th>\n",
       "      <th>Mapped reads</th>\n",
       "      <th>...</th>\n",
       "      <th>mCG/CG</th>\n",
       "      <th>mCH/CH</th>\n",
       "      <th>Estimated mCG/CG</th>\n",
       "      <th>Estimated mCH/CH</th>\n",
       "      <th>Neuron type</th>\n",
       "      <th>tSNE x coordinate</th>\n",
       "      <th>tSNE y coordinate\\n</th>\n",
       "      <th>binary</th>\n",
       "      <th>coverage_cells</th>\n",
       "      <th>mean_cell_methylation</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>index</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>allc_Pool_1000_AD002_indexed_qual30.tsv</th>\n",
       "      <td>mL5-1</td>\n",
       "      <td>K1K2</td>\n",
       "      <td>D508</td>\n",
       "      <td>D711</td>\n",
       "      <td>GTACTGAC</td>\n",
       "      <td>TCTCGCGC</td>\n",
       "      <td>AD002</td>\n",
       "      <td>CGATGT</td>\n",
       "      <td>3749296</td>\n",
       "      <td>1970400</td>\n",
       "      <td>...</td>\n",
       "      <td>1388</td>\n",
       "      <td>1138</td>\n",
       "      <td>148</td>\n",
       "      <td>1022</td>\n",
       "      <td>mL5-1</td>\n",
       "      <td>-2,79539</td>\n",
       "      <td>-0,105851\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>21715</td>\n",
       "      <td>0.243341</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_nuclei_600_S147_L006_qual30.tsv</th>\n",
       "      <td>mL5-1</td>\n",
       "      <td>N/A</td>\n",
       "      <td>D508</td>\n",
       "      <td>D712</td>\n",
       "      <td>GTACTGAC</td>\n",
       "      <td>AGCGATAG</td>\n",
       "      <td>AD012</td>\n",
       "      <td>CTTGTA</td>\n",
       "      <td>7069454</td>\n",
       "      <td>4358703</td>\n",
       "      <td>...</td>\n",
       "      <td>238</td>\n",
       "      <td>1174</td>\n",
       "      <td>23</td>\n",
       "      <td>1114</td>\n",
       "      <td>mL5-1</td>\n",
       "      <td>-0,352829</td>\n",
       "      <td>0,932857\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>9976</td>\n",
       "      <td>0.110672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_nuclei_599_S146_L006_qual30.tsv</th>\n",
       "      <td>mL6-2</td>\n",
       "      <td>N/A</td>\n",
       "      <td>D507</td>\n",
       "      <td>D712</td>\n",
       "      <td>CAGGACGT</td>\n",
       "      <td>AGCGATAG</td>\n",
       "      <td>AD011</td>\n",
       "      <td>GGCTAC</td>\n",
       "      <td>18242158</td>\n",
       "      <td>11386231</td>\n",
       "      <td>...</td>\n",
       "      <td>1644</td>\n",
       "      <td>273</td>\n",
       "      <td>179</td>\n",
       "      <td>296</td>\n",
       "      <td>mL6-2</td>\n",
       "      <td>10,0546</td>\n",
       "      <td>-9,78996\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>20162</td>\n",
       "      <td>0.216064</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_nuclei_598_S145_L006_qual30.tsv</th>\n",
       "      <td>mL6-1</td>\n",
       "      <td>N/A</td>\n",
       "      <td>D506</td>\n",
       "      <td>D712</td>\n",
       "      <td>TAATCTTA</td>\n",
       "      <td>AGCGATAG</td>\n",
       "      <td>AD010</td>\n",
       "      <td>TAGCTT</td>\n",
       "      <td>7045252</td>\n",
       "      <td>4197889</td>\n",
       "      <td>...</td>\n",
       "      <td>2750</td>\n",
       "      <td>887</td>\n",
       "      <td>2765</td>\n",
       "      <td>0</td>\n",
       "      <td>mL6-1</td>\n",
       "      <td>9,37005</td>\n",
       "      <td>2,71283\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>10374</td>\n",
       "      <td>0.148192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_nuclei_597_S144_L006_qual30.tsv</th>\n",
       "      <td>mL4</td>\n",
       "      <td>N/A</td>\n",
       "      <td>D505</td>\n",
       "      <td>D712</td>\n",
       "      <td>AGGCGAAG</td>\n",
       "      <td>AGCGATAG</td>\n",
       "      <td>AD008</td>\n",
       "      <td>ACTTGA</td>\n",
       "      <td>15116650</td>\n",
       "      <td>9483039</td>\n",
       "      <td>...</td>\n",
       "      <td>2213</td>\n",
       "      <td>702</td>\n",
       "      <td>238</td>\n",
       "      <td>70</td>\n",
       "      <td>mL4</td>\n",
       "      <td>-4,17269</td>\n",
       "      <td>-2,94523\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>17226</td>\n",
       "      <td>0.199148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_Pool_839_AD006_indexed_qual30.tsv</th>\n",
       "      <td>mPv</td>\n",
       "      <td>I1I2</td>\n",
       "      <td>D507</td>\n",
       "      <td>D712</td>\n",
       "      <td>CAGGACGT</td>\n",
       "      <td>AGCGATAG</td>\n",
       "      <td>AD006</td>\n",
       "      <td>GCCAAT</td>\n",
       "      <td>6760981</td>\n",
       "      <td>2875103</td>\n",
       "      <td>...</td>\n",
       "      <td>244</td>\n",
       "      <td>891</td>\n",
       "      <td>2264</td>\n",
       "      <td>821</td>\n",
       "      <td>mPv</td>\n",
       "      <td>-0,198246</td>\n",
       "      <td>19,7879\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>28767</td>\n",
       "      <td>0.393699</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_Pool_83_AD002_indexed_qual30.tsv</th>\n",
       "      <td>mL2/3</td>\n",
       "      <td>A1A2</td>\n",
       "      <td>D503</td>\n",
       "      <td>D711</td>\n",
       "      <td>CCTATCCT</td>\n",
       "      <td>TCTCGCGC</td>\n",
       "      <td>AD002</td>\n",
       "      <td>CGATGT</td>\n",
       "      <td>5254487</td>\n",
       "      <td>2706493</td>\n",
       "      <td>...</td>\n",
       "      <td>528</td>\n",
       "      <td>494</td>\n",
       "      <td>541</td>\n",
       "      <td>484</td>\n",
       "      <td>mL2/3</td>\n",
       "      <td>-13,1049</td>\n",
       "      <td>0,642965\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>27409</td>\n",
       "      <td>0.275200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_Pool_83_AD006_indexed_qual30.tsv</th>\n",
       "      <td>mL4</td>\n",
       "      <td>A1A2</td>\n",
       "      <td>D503</td>\n",
       "      <td>D711</td>\n",
       "      <td>CCTATCCT</td>\n",
       "      <td>TCTCGCGC</td>\n",
       "      <td>AD006</td>\n",
       "      <td>GCCAAT</td>\n",
       "      <td>5332867</td>\n",
       "      <td>1913346</td>\n",
       "      <td>...</td>\n",
       "      <td>1060</td>\n",
       "      <td>228</td>\n",
       "      <td>1071</td>\n",
       "      <td>230</td>\n",
       "      <td>mL4</td>\n",
       "      <td>-6,94662</td>\n",
       "      <td>-9,61211\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>26023</td>\n",
       "      <td>0.279163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_Pool_995_AD002_indexed_qual30.tsv</th>\n",
       "      <td>mL6-2</td>\n",
       "      <td>K1K2</td>\n",
       "      <td>D503</td>\n",
       "      <td>D711</td>\n",
       "      <td>CCTATCCT</td>\n",
       "      <td>TCTCGCGC</td>\n",
       "      <td>AD002</td>\n",
       "      <td>CGATGT</td>\n",
       "      <td>3508294</td>\n",
       "      <td>1813927</td>\n",
       "      <td>...</td>\n",
       "      <td>2144</td>\n",
       "      <td>1038</td>\n",
       "      <td>2156</td>\n",
       "      <td>93</td>\n",
       "      <td>mL6-2</td>\n",
       "      <td>14,0262</td>\n",
       "      <td>-4,28728\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>20658</td>\n",
       "      <td>0.248668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allc_nuclei-319_S59_L002_qual30_1.tsv</th>\n",
       "      <td>mL4</td>\n",
       "      <td>20160217 pool B</td>\n",
       "      <td>D503</td>\n",
       "      <td>D708</td>\n",
       "      <td>CCTATCCT</td>\n",
       "      <td>TAATGCGC</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>3480402</td>\n",
       "      <td>1625695</td>\n",
       "      <td>...</td>\n",
       "      <td>2158</td>\n",
       "      <td>597</td>\n",
       "      <td>226</td>\n",
       "      <td>582</td>\n",
       "      <td>mL4</td>\n",
       "      <td>-4,58041</td>\n",
       "      <td>-5,07954\\n</td>\n",
       "      <td>inhibitory</td>\n",
       "      <td>1486</td>\n",
       "      <td>0.017435</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3379 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        cell_type     Library pool Index i5  \\\n",
       "index                                                                         \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv     mL5-1             K1K2     D508   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv        mL5-1              N/A     D508   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv        mL6-2              N/A     D507   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv        mL6-1              N/A     D506   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv          mL4              N/A     D505   \n",
       "...                                           ...              ...      ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv        mPv             I1I2     D507   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv       mL2/3             A1A2     D503   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv         mL4             A1A2     D503   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv      mL6-2             K1K2     D503   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv         mL4  20160217 pool B     D503   \n",
       "\n",
       "                                        Index i7 i5 sequence i7 sequence  \\\n",
       "index                                                                      \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv     D711    GTACTGAC    TCTCGCGC   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv        D712    GTACTGAC    AGCGATAG   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv        D712    CAGGACGT    AGCGATAG   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv        D712    TAATCTTA    AGCGATAG   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv        D712    AGGCGAAG    AGCGATAG   \n",
       "...                                          ...         ...         ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv      D712    CAGGACGT    AGCGATAG   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv       D711    CCTATCCT    TCTCGCGC   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv       D711    CCTATCCT    TCTCGCGC   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv      D711    CCTATCCT    TCTCGCGC   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv       D708    CCTATCCT    TAATGCGC   \n",
       "\n",
       "                                        random primer index  \\\n",
       "index                                                         \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv               AD002   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv                  AD012   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv                  AD011   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv                  AD010   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv                  AD008   \n",
       "...                                                     ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv                AD006   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv                 AD002   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv                 AD006   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv                AD002   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv                         \n",
       "\n",
       "                                        random primer index sequence  \\\n",
       "index                                                                  \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv                       CGATGT   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv                          CTTGTA   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv                          GGCTAC   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv                          TAGCTT   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv                          ACTTGA   \n",
       "...                                                              ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv                        GCCAAT   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv                         CGATGT   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv                         GCCAAT   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv                        CGATGT   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv                                  \n",
       "\n",
       "                                        Total reads Mapped reads  ... mCG/CG  \\\n",
       "index                                                             ...          \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv     3749296      1970400  ...   1388   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv        7069454      4358703  ...    238   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv       18242158     11386231  ...   1644   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv        7045252      4197889  ...   2750   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv       15116650      9483039  ...   2213   \n",
       "...                                             ...          ...  ...    ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv      6760981      2875103  ...    244   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv       5254487      2706493  ...    528   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv       5332867      1913346  ...   1060   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv      3508294      1813927  ...   2144   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv       3480402      1625695  ...   2158   \n",
       "\n",
       "                                         mCH/CH  Estimated mCG/CG  \\\n",
       "index                                                               \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv    1138               148   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv       1174                23   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv        273               179   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv        887              2765   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv        702               238   \n",
       "...                                         ...               ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv      891              2264   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv       494               541   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv       228              1071   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv     1038              2156   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv       597               226   \n",
       "\n",
       "                                         Estimated mCH/CH  Neuron type  \\\n",
       "index                                                                    \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv              1022        mL5-1   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv                 1114        mL5-1   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv                  296        mL6-2   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv                    0        mL6-1   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv                   70          mL4   \n",
       "...                                                   ...          ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv                821          mPv   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv                 484        mL2/3   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv                 230          mL4   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv                 93        mL6-2   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv                 582          mL4   \n",
       "\n",
       "                                         tSNE x coordinate  \\\n",
       "index                                                        \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv           -2,79539   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv             -0,352829   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv               10,0546   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv               9,37005   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv              -4,17269   \n",
       "...                                                    ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv           -0,198246   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv             -13,1049   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv             -6,94662   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv             14,0262   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv             -4,58041   \n",
       "\n",
       "                                        tSNE y coordinate\\n      binary  \\\n",
       "index                                                                     \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv         -0,105851\\n  inhibitory   \n",
       "allc_nuclei_600_S147_L006_qual30.tsv             0,932857\\n  inhibitory   \n",
       "allc_nuclei_599_S146_L006_qual30.tsv             -9,78996\\n  inhibitory   \n",
       "allc_nuclei_598_S145_L006_qual30.tsv              2,71283\\n  inhibitory   \n",
       "allc_nuclei_597_S144_L006_qual30.tsv             -2,94523\\n  inhibitory   \n",
       "...                                                     ...         ...   \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv            19,7879\\n  inhibitory   \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv            0,642965\\n  inhibitory   \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv            -9,61211\\n  inhibitory   \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv           -4,28728\\n  inhibitory   \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv            -5,07954\\n  inhibitory   \n",
       "\n",
       "                                        coverage_cells mean_cell_methylation  \n",
       "index                                                                         \n",
       "allc_Pool_1000_AD002_indexed_qual30.tsv          21715              0.243341  \n",
       "allc_nuclei_600_S147_L006_qual30.tsv              9976              0.110672  \n",
       "allc_nuclei_599_S146_L006_qual30.tsv             20162              0.216064  \n",
       "allc_nuclei_598_S145_L006_qual30.tsv             10374              0.148192  \n",
       "allc_nuclei_597_S144_L006_qual30.tsv             17226              0.199148  \n",
       "...                                                ...                   ...  \n",
       "allc_Pool_839_AD006_indexed_qual30.tsv           28767              0.393699  \n",
       "allc_Pool_83_AD002_indexed_qual30.tsv            27409              0.275200  \n",
       "allc_Pool_83_AD006_indexed_qual30.tsv            26023              0.279163  \n",
       "allc_Pool_995_AD002_indexed_qual30.tsv           20658              0.248668  \n",
       "allc_nuclei-319_S59_L002_qual30_1.tsv             1486              0.017435  \n",
       "\n",
       "[3379 rows x 22 columns]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# quick look at the different cell metadata available\n",
    "adata.obs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 3379 × 55017 \n",
       "    obs: 'cell_type', 'Library pool', 'Index i5', 'Index i7', 'i5 sequence', 'i7 sequence', 'random primer index', 'random primer index sequence', 'Total reads', 'Mapped reads', 'Filtered reads', 'mCCC/CCC', 'mCG/CG', 'mCH/CH', 'Estimated mCG/CG', 'Estimated mCH/CH', 'Neuron type', 'tSNE x coordinate', 'tSNE y coordinate\\n', 'binary', 'coverage_cells', 'mean_cell_methylation'\n",
       "    var: 'coverage_feature', 'mean_feature_methylation'\n",
       "    uns: 'Estimated mCG', 'Estimated mCH', 'mCCC', 'mCG', 'mCH', 'omic'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 149972 characters]\n",
      "text/plain": [
       "<Figure size 417.2x320 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 318,
       "width": 460
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot coverage VS mean methylation level in cells\n",
    "sc.pl.scatter(adata, 'coverage_cells', 'mean_cell_methylation', color='Neuron type')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 264752 characters]\n",
      "text/plain": [
       "<Figure size 417.2x320 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 318,
       "width": 507
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sc.pl.scatter(adata, 'coverage_cells', 'Mapped reads', color='Neuron type')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "# convert string annotation of Mapped reads into int values\n",
    "adata.obs['Mapped reads'] = [int(x) for x in adata.obs['Mapped reads']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 151564 characters]\n",
      "text/plain": [
       "<Figure size 417.2x320 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 318,
       "width": 468
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sc.pl.scatter(adata, 'coverage_cells', 'Mapped reads', color='Neuron type')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 134260 characters]\n",
      "text/plain": [
       "<Figure size 327.28x320 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 300,
       "width": 289
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sc.pl.scatter(adata, 'coverage_feature', 'mean_feature_methylation')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Filtering low quality cells"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 3346 × 55017 \n",
       "    obs: 'cell_type', 'Library pool', 'Index i5', 'Index i7', 'i5 sequence', 'i7 sequence', 'random primer index', 'random primer index sequence', 'Total reads', 'Mapped reads', 'Filtered reads', 'mCCC/CCC', 'mCG/CG', 'mCH/CH', 'Estimated mCG/CG', 'Estimated mCH/CH', 'Neuron type', 'tSNE x coordinate', 'tSNE y coordinate\\n', 'binary', 'coverage_cells', 'mean_cell_methylation'\n",
       "    var: 'coverage_feature', 'mean_feature_methylation'\n",
       "    uns: 'Estimated mCG', 'Estimated mCH', 'mCCC', 'mCG', 'mCH', 'omic', 'Neuron type_colors'"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#filter out cells with less than 10000 enhancers covered\n",
    "adata=adata[adata.obs['coverage_cells']>10000,:].copy()\n",
    "adata"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Imputation missing data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "adata = epi.pp.imputation_met(adata, number_cell_covered=500, imputation_value='mean', save=None, copy=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 3346 × 54710 \n",
       "    obs: 'cell_type', 'Library pool', 'Index i5', 'Index i7', 'i5 sequence', 'i7 sequence', 'random primer index', 'random primer index sequence', 'Total reads', 'Mapped reads', 'Filtered reads', 'mCCC/CCC', 'mCG/CG', 'mCH/CH', 'Estimated mCG/CG', 'Estimated mCH/CH', 'Neuron type', 'tSNE x coordinate', 'tSNE y coordinate\\n', 'binary', 'coverage_cells', 'mean_cell_methylation'\n",
       "    var: 'coverage_feature', 'mean_feature_methylation'\n",
       "    uns: 'Estimated mCG', 'Estimated mCH', 'mCCC', 'mCG', 'mCH', 'omic', 'Neuron type_colors'"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "# recalculing qc values\n",
    "length1 = len(adata.X[0,:])\n",
    "length2 = len(adata.X[:,0])\n",
    "adata.obs['coverage_cells'] = [length1 - np.isnan(line).sum() for line in adata.X]\n",
    "adata.obs['mean_cell_methylation'] = [np.nansum(line)/length1 for line in adata.X]\n",
    "adata.var['coverage_feature'] = [length2 - np.isnan(line).sum() for line in adata.X.T]\n",
    "adata.var['mean_feature_methylation'] = [np.nansum(line)/length2 for line in adata.X.T]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Low dimensional representation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "# perform PCA, neighbor graph, tSNE and UMAP\n",
    "epi.pp.lazy(adata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save intermediary file\n",
    "#adata.write(OUT_DATADIR+'imputed_min500cells_min10000enhancers.h5ad')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 3346 × 54710 \n",
       "    obs: 'cell_type', 'Library pool', 'Index i5', 'Index i7', 'i5 sequence', 'i7 sequence', 'random primer index', 'random primer index sequence', 'Total reads', 'Mapped reads', 'Filtered reads', 'mCCC/CCC', 'mCG/CG', 'mCH/CH', 'Estimated mCG/CG', 'Estimated mCH/CH', 'Neuron type', 'tSNE x coordinate', 'tSNE y coordinate\\n', 'binary', 'coverage_cells', 'mean_cell_methylation'\n",
       "    var: 'coverage_feature', 'mean_feature_methylation'\n",
       "    uns: 'Estimated mCG', 'Estimated mCH', 'mCCC', 'mCG', 'mCH', 'omic', 'Neuron type_colors', 'pca', 'neighbors'\n",
       "    obsm: 'X_pca', 'X_tsne', 'X_umap'\n",
       "    varm: 'PCs'"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 2141148 characters]\n",
      "text/plain": [
       "<Figure size 2304x960 with 13 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 856,
       "width": 1912
      }
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 2075228 characters]\n",
      "text/plain": [
       "<Figure size 2304x960 with 13 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 856,
       "width": 1912
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "## Plot low dimensional representation (before normalisation of the numerb of reads per cell)\n",
    "sc.pl.pca(adata, color=['Library pool', 'Mapped reads',\n",
    "                         'Index i5', 'Index i7',\n",
    "                         'i5 sequence', 'i7 sequence',\n",
    "                         'mCCC/CCC', 'mCG/CG', 'mCH/CH',], wspace=0.8)\n",
    "sc.pl.umap(adata, color=['Library pool', 'Mapped reads',\n",
    "                         'Index i5', 'Index i7',\n",
    "                         'i5 sequence', 'i7 sequence',\n",
    "                         'mCCC/CCC', 'mCG/CG', 'mCH/CH'], wspace=0.8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 343272 characters]\n",
      "text/plain": [
       "<Figure size 1152x320 with 2 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 299,
       "width": 1092
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# visualisation of known cell types\n",
    "sc.pl.pca(adata, color=['cell_type', \"Neuron type\"], wspace=0.8)\n",
    "sc.pl.umap(adata, color=['cell_type', \"Neuron type\"], wspace=0.8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Normalisation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Linear regression of the number of Mapped reads per cell. \n",
    "sc.pp.regress_out(adata, 'Mapped reads')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "# redoing PCA, neighborhood graph, tSNE and UMAP\n",
    "epi.pp.lazy(adata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1914160 characters]\n",
      "text/plain": [
       "<Figure size 2304x960 with 13 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 856,
       "width": 1912
      }
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 1687356 characters]\n",
      "text/plain": [
       "<Figure size 2304x960 with 13 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 856,
       "width": 1912
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# visualisation\n",
    "sc.pl.pca(adata, color=['Library pool', 'Mapped reads',\n",
    "                         'Index i5', 'Index i7',\n",
    "                         'i5 sequence', 'i7 sequence',\n",
    "                         'mCCC/CCC', 'mCG/CG', 'mCH/CH',], wspace=0.8)\n",
    "sc.pl.umap(adata, color=['Library pool', 'Mapped reads',\n",
    "                         'Index i5', 'Index i7',\n",
    "                         'i5 sequence', 'i7 sequence',\n",
    "                         'mCCC/CCC', 'mCG/CG', 'mCH/CH'], wspace=0.8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 386220 characters]\n",
      "text/plain": [
       "<Figure size 1152x320 with 2 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 299,
       "width": 1092
      }
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 252856 characters]\n",
      "text/plain": [
       "<Figure size 1152x320 with 2 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 299,
       "width": 1092
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# visulisation of known cell type annotation.\n",
    "sc.pl.pca(adata, color=['cell_type', \"Neuron type\"], wspace=0.8)\n",
    "sc.pl.umap(adata, color=['cell_type', \"Neuron type\"], wspace=0.8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "# recomputing the UMAP\n",
    "epi.tl.umap(adata, min_dist=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 551568 characters]\n",
      "text/plain": [
       "<Figure size 1152x320 with 2 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 299,
       "width": 1092
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sc.pl.umap(adata, color=['cell_type', \"Neuron type\"], wspace=0.8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save the processed file. \n",
    "adata.write(OUT_DATADIR+'imputed_min500cells_min10000enhancers_normalised.h5ad')"
   ]
  },
  {
   "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.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
