{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Heatmaps\n",
    "\n",
    "This example will show you how to generate various heatmaps and how to annotate \n",
    "them using the plotly library.\n",
    "\n",
    "### Datasets\n",
    "\n",
    "This example makes use of two of the built-in datasets listed below\n",
    "\n",
    "- `covid` - Protein expression data from a cohort of COVID-19 patients\n",
    "- `ros-mito` - High content imaging features from an experiment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import\n",
    "from hcitools import datasets, plot\n",
    "\n",
    "# Load datasets\n",
    "covid = datasets.load_dataset('covid')\n",
    "ros = datasets.load_dataset('ros-mito')\n",
    "\n",
    "# Plotly renderer\n",
    "plot.set_renderer('notebook')  # Use this when running notebook\n",
    "plot.set_renderer('iframe_connected')  # Use this when rendering docs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Protein Expression Heatmaps\n",
    "\n",
    "Here, we'll create a heatmap to look at the expression of proteins in the \n",
    "patients' blood. We'll include colorbars for patient sex and mortality. \n",
    "We'll also look at how you could add annotations to highlight certain regions \n",
    "of the heatmap."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"720px\"\n",
       "    height=\"420\"\n",
       "    src=\"iframe_figures/figure_2.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Prepare data frame\n",
    "data = (covid.copy()\n",
    "    .filter(regex='^B-', axis=1))  # Keep only blood markers\n",
    "metadata =  covid[['Mortality', 'Sex']]\n",
    "data.columns = [x[2:] for x in data.columns]\n",
    "\n",
    "# Define groups for heatmap\n",
    "row_groups = {\n",
    "    k: list(v.values()) for k, v in metadata.to_dict(orient='index').items()\n",
    "}\n",
    "row_group_names = ['Mortality', 'Sex']\n",
    "row_colors = {'Alive': '#38d652', 'Dead': '#d93e38',\n",
    "              'Male': 'blue', 'Female': 'pink'}\n",
    "\n",
    "# Create heatmap\n",
    "fig = plot.heatmap(\n",
    "    data=data,\n",
    "    clust_rows=True,\n",
    "    clust_cols=True,\n",
    "    row_colors=row_colors,\n",
    "    row_groups=row_groups,\n",
    "    row_group_names=row_group_names\n",
    ")\n",
    "\n",
    "# Add a title and tweak the size\n",
    "fig.update_layout(\n",
    "    title='Blood Biomarkers',\n",
    "    title_x=0.5,\n",
    "    height=400,\n",
    "    width=700\n",
    ")\n",
    "\n",
    "# Annotate highly expressed proteins\n",
    "fig.add_shape(\n",
    "    type='rect',\n",
    "    x0='MCP-1', x1='EGF',\n",
    "    y0=0, y1=88,\n",
    "    row=1, col=3,\n",
    "    line=dict(color='black')\n",
    ")\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Correlation Maps\n",
    "\n",
    "Here, we'll generate a heatmap to visualize the correlation of blood proteins \n",
    "with markers of clinical severity."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"720px\"\n",
       "    height=\"420\"\n",
       "    src=\"iframe_figures/figure_3.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Prepare data frame\n",
    "vars = ['APACHE1h', 'APACHE24h', 'CCI']\n",
    "data = (covid.copy()\n",
    "    .set_index(vars)\n",
    "    .filter(regex='^B-')\n",
    "    .reset_index()\n",
    "    .corr()\n",
    "    .loc[vars, :]\n",
    "    .drop(vars, axis=1))\n",
    "data.columns = [x[2:] for x in data.columns]\n",
    "\n",
    "# Create heatmap\n",
    "fig = plot.heatmap(\n",
    "    data=data,\n",
    "    clust_cols=True,\n",
    "    clust_rows=True\n",
    ")\n",
    "\n",
    "fig.update_layout(\n",
    "    title='Correlation with Clinical Severity',\n",
    "    title_x=0.5,\n",
    "    height=400,\n",
    "    width=700\n",
    ")\n",
    "\n",
    "# Show ticks on the y axis (these are hidden by default)\n",
    "fig.update_yaxes(\n",
    "    showticklabels=True, \n",
    "    tickfont_size=14\n",
    ")\n",
    "\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plate Map\n",
    "\n",
    "Next, we'll show how you can generate an interactive heatmap to view expression \n",
    "across a 96 (or 384) well plate using high-content imaging data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe\n",
       "    scrolling=\"no\"\n",
       "    width=\"920px\"\n",
       "    height=\"520\"\n",
       "    src=\"iframe_figures/figure_4.html\"\n",
       "    frameborder=\"0\"\n",
       "    allowfullscreen\n",
       "></iframe>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plot.plate_heatmap(\n",
    "    data=ros,\n",
    "    feature=\"Non-border cells - Number of Objects\"\n",
    ")\n",
    "fig.update_layout(width=900, height=500)\n",
    "\n",
    "fig.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.5 ('hcitools')",
   "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.8.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "da8dbd75bce17023e286ecaa5b28898715c4e19720edbfe0df36bac9075c0ff0"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
