{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.simplefilter(action='ignore', category=FutureWarning)\n",
    "\n",
    "import multigrate as mtg\n",
    "import scanpy as sc\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MOFA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 7362 × 10\n",
       "    obs: 'source', 'replicate', 'experiment', 'tsne_1', 'tsne_2', 'cell_type', 'n_genes', 'n_genes_by_counts', 'log1p_n_genes_by_counts', 'total_counts', 'log1p_total_counts', 'total_counts_mt', 'log1p_total_counts_mt', 'pct_counts_mt'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latent = sc.read('../data/integrated/mofa/cao-mofa.h5ad')\n",
    "latent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "latent.obsm['latent'] = latent.X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Clustering...\n",
      "Graph connectivity...\n",
      "ASW label...\n",
      "NMI cluster/label...\n",
      "ARI cluster/label...\n"
     ]
    },
    {
     "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>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>graph_conn</th>\n",
       "      <td>0.762252</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ASW_label</th>\n",
       "      <td>0.575249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NMI_cluster/label</th>\n",
       "      <td>0.663998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ARI_cluster/label</th>\n",
       "      <td>0.498524</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      score\n",
       "graph_conn         0.762252\n",
       "ASW_label          0.575249\n",
       "NMI_cluster/label  0.663998\n",
       "ARI_cluster/label  0.498524"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics = mtg.metrics.metrics(None, latent,\n",
    "                      batch_key=None,\n",
    "                      label_key='cell_type',\n",
    "                      isolated_label_f1=False,\n",
    "                      isolated_label_asw=False,\n",
    "                      pcr_batch=False,\n",
    "                      asw_batch=False,\n",
    "                      embed='latent', \n",
    "                      save='cao-mofa.csv', \n",
    "                      method='mofa')\n",
    "metrics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Seurat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 7362 × 20\n",
       "    obs: 'cell_type', 'nCount_ATAC', 'nFeature_ATAC', 'nCount_SCT', 'nFeature_SCT', 'SCT.weight', 'ATAC.weight'\n",
       "    obsp: 'connectivities'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latent = sc.read('../data/integrated/seurat/cao-seurat.h5ad')\n",
    "latent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 7362 × 4000\n",
       "    obs: 'source', 'replicate', 'experiment', 'tsne_1', 'tsne_2', 'cell_type', 'n_genes', 'n_genes_by_counts', 'log1p_n_genes_by_counts', 'total_counts', 'log1p_total_counts', 'total_counts_mt', 'log1p_total_counts_mt', 'pct_counts_mt'\n",
       "    var: 'gene_type', 'gene_short_name', 'n_cells', 'mt', 'n_cells_by_counts', 'mean_counts', 'log1p_mean_counts', 'pct_dropout_by_counts', 'total_counts', 'log1p_total_counts', 'highly_variable', 'means', 'dispersions', 'dispersions_norm', 'highly_variable_nbatches', 'highly_variable_intersection'\n",
       "    uns: 'cell_type_colors', 'neighbors', 'replicate_colors', 'umap'\n",
       "    obsm: 'X_pca', 'X_tsne', 'X_umap'\n",
       "    layers: 'count'\n",
       "    obsp: 'connectivities', 'distances'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rna = sc.read('../data/cao-2018/expressions.h5ad')\n",
    "rna"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(rna.obs_names != latent.obs_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 7362 × 20\n",
       "    obs: 'cell_type', 'nCount_ATAC', 'nFeature_ATAC', 'nCount_SCT', 'nFeature_SCT', 'SCT.weight', 'ATAC.weight'\n",
       "    obsm: 'latent'\n",
       "    obsp: 'connectivities'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latent.obsm['latent'] = latent.X\n",
    "latent.obs['cell_type'] = rna.obs['cell_type'].astype('category')\n",
    "latent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Clustering...\n",
      "Graph connectivity...\n",
      "ASW label...\n",
      "NMI cluster/label...\n",
      "ARI cluster/label...\n"
     ]
    },
    {
     "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>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>graph_conn</th>\n",
       "      <td>0.692209</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ASW_label</th>\n",
       "      <td>0.561667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NMI_cluster/label</th>\n",
       "      <td>0.623704</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ARI_cluster/label</th>\n",
       "      <td>0.542221</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      score\n",
       "graph_conn         0.692209\n",
       "ASW_label          0.561667\n",
       "NMI_cluster/label  0.623704\n",
       "ARI_cluster/label  0.542221"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics = mtg.metrics.metrics(None, latent,\n",
    "                      batch_key=None,\n",
    "                      label_key='cell_type',\n",
    "                      isolated_label_f1=False,\n",
    "                      isolated_label_asw=False,\n",
    "                      pcr_batch=False,\n",
    "                      asw_batch=False,\n",
    "                      embed='latent', \n",
    "                      save='cao-seurat.csv', \n",
    "                      method='seurat')\n",
    "metrics"
   ]
  },
  {
   "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
