{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Analysis of in silico samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import os\n",
    "from datetime import datetime\n",
    "from scipy.stats import mannwhitneyu\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.options.display.float_format = '{:.3f}'.format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# VARS\n",
    "project_name = 'EM_EVPools'\n",
    "file_16S = f\"../data/{project_name}/16_S_datasheet/16S_analysis.xlsx\"\n",
    "\n",
    "date = \"2023-12-05\"\n",
    "file_EVs = f\"../results_diversity/{date}/df_mean_cutoff_nonNA.tsv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_16S = pd.read_excel(file_16S, index_col=0)\n",
    "df_16S = df_16S.iloc[1:]\n",
    "\n",
    "# Set columns to be the name of the sample and the type of MS / HC\n",
    "columns=[(i, j) for i, j in zip(df_16S.iloc[0, :].values, df_16S.columns.values)]\n",
    "df_16S.columns = pd.MultiIndex.from_tuples(columns)\n",
    "\n",
    "df_16S = df_16S.iloc[1:].astype(float)\n",
    "df_16S"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_EVs = pd.read_csv(file_EVs, sep='\\t', index_col=0)\n",
    "\n",
    "df_EVs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "names_genres = [i.split(' - ')[1] for i in df_EVs.index[1:]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for name in names_genres:\n",
    "    display(name)\n",
    "    df_name = df_16S.loc[[name in i for i in df_16S.index]]\n",
    "    display(df_name)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_16S.loc[:, 'RR'].astype(float).mean(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 0\n",
    "for name in names_genres:\n",
    "    df_name = df_16S.loc[[name in i for i in df_16S.index]]\n",
    "    if len(df_name) > 0:\n",
    "        N += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axs = plt.subplots(2, N // 2 + 1, figsize=(3 * (N // 2 + 1), 3 * 2))\n",
    "i = 0\n",
    "\n",
    "for name in names_genres:\n",
    "    df_name = df_16S.loc[[name in i for i in df_16S.index]]\n",
    "\n",
    "\n",
    "    if len(df_name) > 0:\n",
    "        display(name)\n",
    "        print('RR', df_name.loc[:, 'RR'].iloc[0, :].mean(), df_name.loc[:, 'RR'].iloc[0, :].std())\n",
    "        print('HC', df_name.loc[:, 'HC'].iloc[0, :].mean(), df_name.loc[:, 'HC'].iloc[0, :].std())\n",
    "\n",
    "        val, pval = mannwhitneyu(x = df_name.loc[:, 'HC'].mean().values, y = df_name.loc[:, 'RR'].mean().values)\n",
    "\n",
    "        print('pval', pval)\n",
    "\n",
    "        sns.boxplot([df_name.loc[:, 'HC'].mean().values, df_name.loc[:, 'RR'].mean().values], ax=axs.ravel()[i], )\n",
    "        axs.ravel()[i].set_xticklabels(['HC', 'RR'])\n",
    "        axs.ravel()[i].set_title(f\"{name} (p={pval:.2f})\")\n",
    "        i += 1\n",
    "\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axs = plt.subplots(2, N // 2 + 1, figsize=(3 * (N // 2 + 1), 3 * 2))\n",
    "i = 0\n",
    "\n",
    "for name in names_genres:\n",
    "    df_name = df_16S.loc[[name in i for i in df_16S.index]]\n",
    "\n",
    "\n",
    "    if len(df_name) > 0:\n",
    "        display(name)\n",
    "        print('RR', df_name.loc[:, 'RR'].mean().mean(), df_name.loc[:, 'RR'].mean().std())\n",
    "        print('HC', df_name.loc[:, 'HC'].mean().mean(), df_name.loc[:, 'HC'].mean().std())\n",
    "\n",
    "        val, pval = mannwhitneyu(x = df_name.loc[:, 'HC'].mean().values, y = df_name.loc[:, 'RR'].mean().values)\n",
    "\n",
    "        print('pval', pval)\n",
    "\n",
    "        sns.boxplot([df_name.loc[:, 'HC'].mean().values, df_name.loc[:, 'RR'].mean().values], ax=axs.ravel()[i], )\n",
    "        axs.ravel()[i].set_xticklabels(['HC', 'RR'])\n",
    "        axs.ravel()[i].set_title(f\"{name} (p={pval:.2f})\")\n",
    "        i += 1\n",
    "\n",
    "plt.tight_layout()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "EVs",
   "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.10.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
