{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import scanpy as sc\n",
    "import sciduck as sd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load in raw data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "adata = ad.read_h5ad(\"YOUR_RAW_DATA.h5ad\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Standard scanpy preprocessing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Normalize the count matrix after storing the raw counts in the raw slot\n",
    "adata.raw = adata\n",
    "sc.pp.normalize_total(adata, target_sum=1e6)\n",
    "sc.pp.log1p(adata)\n",
    "\n",
    "## Calculate some basic QC metrics around UMI and gene counts\n",
    "sc.pp.calculate_qc_metrics(adata, inplace=True)\n",
    "\n",
    "## Compute clusters for use later\n",
    "sc.tl.pca(adata, svd_solver='arpack', use_highly_variable=True)\n",
    "sc.pp.neighbors(adata, n_neighbors=10, n_pcs=40)\n",
    "sc.tl.leiden(adata, flavor=\"igraph\", n_iterations=2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic quality control (`sciduck`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Filter cells/nuclei on UMI and gene count thresholds, showing the default values.\n",
    "sd.basic_qc.filter_on_counts_genes(adata, \n",
    "                                    min_counts = 2000, max_counts = 100000, \n",
    "                                    min_genes = 1000, max_genes = 13000,\n",
    "                                    inplace = True)\n",
    "adata.obs.keeper_cells.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Describe whats happening!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Filter cells/nuclei on mitochondrial gene expression, showing the default values.\n",
    "adata = sd.basic_qc.filter_on_precomputed_metrics(adata, \n",
    "                                                    doublet_score = 0.3, \n",
    "                                                    pct_counts_mt = 3.0,\n",
    "                                                    GEX_Reads_mapped_confidently_to_genome = 0.0, \n",
    "                                                    GEX_Reads_mapped_to_genome = 0.0, \n",
    "                                                    GEX_Reads_with_TSO = 1.0, \n",
    "                                                    inplace = False)\n",
    "adata.obs.keeper_cells.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quality control involving coarse labels (Neuron / Non-neuron)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "adata = sd.basic_qc.filter_utilizing_coarse_labels(adata, \n",
    "                                            coarse_label_column = \"Class\", \n",
    "                                            coarse_label_map = {'Neurons': ['Excitatory', 'Inhibitory'], \n",
    "                                                                'Non-Neurons': ['Astrocytes', 'Oligodendrocytes', 'Microglia', 'Endothelial', 'Pericytes']}, \n",
    "                                            coarse_label_gene_threshold = {'Neurons': 2000, 'Non-Neurons': 1000})\n",
    "adata.obs.keeper_cells.value_counts()"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
