{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Visualizing the tree structure\n",
    "\n",
    "In case you want to additionally visualize the tree structure on the data, you will have to install the pygraphviz library, which also requires the software Graphviz as explained [here](https://pygraphviz.github.io/documentation/stable/install.html)\n",
    "\n",
    "As it can be cumbersome to install this, we recommend the following visualizations only for expert users.\n",
    "\n",
    "We again use the tissue data and assume differential analysis of this data has been carried out (i.e. you have executed the [differential_expression.ipynb](differential_expression.ipynb))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "RESULTS_DIRECTORY = \"./data/mouse_tissues/results_pairwise_analysis\"\n",
    "SAMPLEMAP_FILE = \"./data/mouse_tissues/samplemap_200.tsv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "proteoform_df = pd.read_csv(RESULTS_DIRECTORY + \"/spleen_VS_testis.proteoforms.tsv\", sep='\\t')\n",
    "display(proteoform_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import alphaquant.utils.diffquant_utils as aq_diffquant_utils\n",
    "\n",
    "proteoform_df_filtered = aq_diffquant_utils.filter_proteoform_df(proteoform_df=proteoform_df, min_num_peptides=1, fcdiff_cutoff=0.5)\n",
    "display(proteoform_df_filtered)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import alphaquant.plotting.fcviz as aq_fcviz\n",
    "import alphaquant.plotting.tree_and_fc_viz as aq_tree_and_fc_viz\n",
    "import alphaquant.plotting.base_functions as aq_base_functions\n",
    "import alphaquant.utils.utils as aq_utils\n",
    "\n",
    "\n",
    "plotconfig = aq_fcviz.PlotConfig()\n",
    "\n",
    "quantification_info = aq_fcviz.CondpairQuantificationInfo((\"spleen\", \"testis\"), RESULTS_DIRECTORY, SAMPLEMAP_FILE)\n",
    "\n",
    "#load the trees containing the tree-based quantification info\n",
    "condpair_tree = aq_utils.read_condpair_tree(\"spleen\", \"testis\", results_folder=RESULTS_DIRECTORY)\n",
    "\n",
    "protein2node = {x.name : x for x in condpair_tree.children}\n",
    "for protein_of_interest in proteoform_df_filtered['protein'].unique():\n",
    "    print(protein_of_interest)\n",
    "    protein_node = protein2node[protein_of_interest]\n",
    "    combined_plotter = aq_tree_and_fc_viz.CombinedTreeAndFCPlotter(protein_node, quantification_info, plotconfig)\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Looking at quantification details of a specific protein"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's assume we now want to futher investigate one of the proteins. For this we take Q9WUM5, because it's a relatively small protein"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import alphaquant.plotting.fcviz as aq_fcviz\n",
    "import alphaquant.plotting.tree_and_fc_viz as aq_tree_and_fc_viz\n",
    "import alphaquant.utils.utils as aq_utils\n",
    "\n",
    "plotconfig = aq_fcviz.PlotConfig()\n",
    "\n",
    "condpair_tree = aq_utils.read_condpair_tree(\"spleen\", \"testis\", results_folder=RESULTS_DIRECTORY)\n",
    "quantification_info = aq_fcviz.CondpairQuantificationInfo((\"spleen\", \"testis\"), RESULTS_DIRECTORY, SAMPLEMAP_FILE)\n",
    "protein2node = {x.name : x for x in condpair_tree.children}\n",
    "protein_node = protein2node['Q9DCV4']\n",
    "combined_plotter = aq_tree_and_fc_viz.CombinedTreeAndFCPlotter(protein_node, quantification_info, plotconfig)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import alphaquant.plotting.fcviz as aq_fcviz\n",
    "import alphaquant.plotting.tree_and_fc_viz as aq_tree_and_fc_viz\n",
    "import alphaquant.plotting.base_functions as aq_base_functions\n",
    "import alphaquant.utils.utils as aq_utils\n",
    "\n",
    "plotconfig = aq_fcviz.PlotConfig()\n",
    "plotconfig.parent_level = \"ion_type\"\n",
    "\n",
    "condpair_tree = aq_utils.read_condpair_tree(\"spleen\", \"testis\", results_folder=RESULTS_DIRECTORY)\n",
    "quantification_info = aq_fcviz.CondpairQuantificationInfo((\"spleen\", \"testis\"), RESULTS_DIRECTORY, SAMPLEMAP_FILE)\n",
    "protein2node = {x.name : x for x in condpair_tree.children}\n",
    "protein_node = protein2node['Q9DCV4']#Q99M87;Q99M87-3\n",
    "print(protein_node.fc)\n",
    "combined_plotter = aq_tree_and_fc_viz.CombinedTreeAndFCPlotter(protein_node, quantification_info, plotconfig)\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This allows us to inspect everything in detail. As we can see for example, peptide HLGPVFNTVK in charge state 3 has inconsistent MS1 and MS2 quantification and therefore the charge state 2 peptide is chosen. Overall quantification looks very consistent, but we see differences on the peptide level, potentially due to proteoforms."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "alphaquant",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}