{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is an jupyter notebook from iKISS package 🐍.\n",
    "\n",
    "By Julie Orjuela (UMR DIADE - IRD)\n",
    "\n",
    "iKISS 💋 is a software under GNU v3 Licence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Manhattan plot of the distribution of kmers (LFMM outliers) mapped on a genome reference "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from qmplot import manhattanplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df= pd.read_csv(\"11.OUTLIERS_LFMM_POSITION/outliers_with_position.csv\", sep='\\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "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>sequence</th>\n",
       "      <th>bedname</th>\n",
       "      <th>segment_nb</th>\n",
       "      <th>pvalue</th>\n",
       "      <th>kmer_number</th>\n",
       "      <th>REF</th>\n",
       "      <th>position</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AAAAAAAAAAAAAAAAAAAAAAGACTAGGCT</td>\n",
       "      <td>output_file.0</td>\n",
       "      <td>10</td>\n",
       "      <td>2.842436e-07</td>\n",
       "      <td>22834</td>\n",
       "      <td>NC_052403.1</td>\n",
       "      <td>12496329</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AAAAAAAAAAAAAAAAAGCAGTACCAGAAGA</td>\n",
       "      <td>output_file.0</td>\n",
       "      <td>10</td>\n",
       "      <td>9.925288e-05</td>\n",
       "      <td>114441</td>\n",
       "      <td>NW_024068428.1</td>\n",
       "      <td>100424</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AAAAAAAAAAAAAAAACTAGTAATCCACCTC</td>\n",
       "      <td>output_file.0</td>\n",
       "      <td>10</td>\n",
       "      <td>1.340398e-11</td>\n",
       "      <td>138122</td>\n",
       "      <td>NW_024068444.1</td>\n",
       "      <td>113925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>AAAAAAAAAAAAAAACCATGGGACATGGATT</td>\n",
       "      <td>output_file.0</td>\n",
       "      <td>10</td>\n",
       "      <td>4.368884e-05</td>\n",
       "      <td>175590</td>\n",
       "      <td>NW_024067773.1</td>\n",
       "      <td>276691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>AAAAAAAAAAAAAAAGACTAGGCTTGGATTT</td>\n",
       "      <td>output_file.0</td>\n",
       "      <td>10</td>\n",
       "      <td>2.287188e-26</td>\n",
       "      <td>189201</td>\n",
       "      <td>NC_052403.1</td>\n",
       "      <td>12496336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>836444</th>\n",
       "      <td>AAAAGCAGAGAATAATAGGTAATTTACACAA</td>\n",
       "      <td>output_file.9</td>\n",
       "      <td>9</td>\n",
       "      <td>1.410008e-04</td>\n",
       "      <td>959291</td>\n",
       "      <td>NW_024068119.1</td>\n",
       "      <td>35814</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>836445</th>\n",
       "      <td>AAAAGCAGGCAGCTCTTTACTCGTTTCTTTG</td>\n",
       "      <td>output_file.9</td>\n",
       "      <td>9</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>971015</td>\n",
       "      <td>NW_024068444.1</td>\n",
       "      <td>58016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>836446</th>\n",
       "      <td>AAAAGCAGGTACGAAGGATCTAAGTCCAACA</td>\n",
       "      <td>output_file.9</td>\n",
       "      <td>9</td>\n",
       "      <td>7.547603e-05</td>\n",
       "      <td>972781</td>\n",
       "      <td>NW_024067794.1</td>\n",
       "      <td>325984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>836447</th>\n",
       "      <td>AAAAGCATAATTCAGTAGCACTGTCGATAGC</td>\n",
       "      <td>output_file.9</td>\n",
       "      <td>9</td>\n",
       "      <td>2.113210e-15</td>\n",
       "      <td>983524</td>\n",
       "      <td>NW_024067923.1</td>\n",
       "      <td>342200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>836448</th>\n",
       "      <td>AAAAGCATCGAGAAAGGCCCATTTTCCTGTA</td>\n",
       "      <td>output_file.9</td>\n",
       "      <td>9</td>\n",
       "      <td>6.656175e-36</td>\n",
       "      <td>996904</td>\n",
       "      <td>NW_024068475.1</td>\n",
       "      <td>115454</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>836449 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                               sequence        bedname  segment_nb  \\\n",
       "0       AAAAAAAAAAAAAAAAAAAAAAGACTAGGCT  output_file.0          10   \n",
       "1       AAAAAAAAAAAAAAAAAGCAGTACCAGAAGA  output_file.0          10   \n",
       "2       AAAAAAAAAAAAAAAACTAGTAATCCACCTC  output_file.0          10   \n",
       "3       AAAAAAAAAAAAAAACCATGGGACATGGATT  output_file.0          10   \n",
       "4       AAAAAAAAAAAAAAAGACTAGGCTTGGATTT  output_file.0          10   \n",
       "...                                 ...            ...         ...   \n",
       "836444  AAAAGCAGAGAATAATAGGTAATTTACACAA  output_file.9           9   \n",
       "836445  AAAAGCAGGCAGCTCTTTACTCGTTTCTTTG  output_file.9           9   \n",
       "836446  AAAAGCAGGTACGAAGGATCTAAGTCCAACA  output_file.9           9   \n",
       "836447  AAAAGCATAATTCAGTAGCACTGTCGATAGC  output_file.9           9   \n",
       "836448  AAAAGCATCGAGAAAGGCCCATTTTCCTGTA  output_file.9           9   \n",
       "\n",
       "              pvalue  kmer_number             REF  position  \n",
       "0       2.842436e-07        22834     NC_052403.1  12496329  \n",
       "1       9.925288e-05       114441  NW_024068428.1    100424  \n",
       "2       1.340398e-11       138122  NW_024068444.1    113925  \n",
       "3       4.368884e-05       175590  NW_024067773.1    276691  \n",
       "4       2.287188e-26       189201     NC_052403.1  12496336  \n",
       "...              ...          ...             ...       ...  \n",
       "836444  1.410008e-04       959291  NW_024068119.1     35814  \n",
       "836445  0.000000e+00       971015  NW_024068444.1     58016  \n",
       "836446  7.547603e-05       972781  NW_024067794.1    325984  \n",
       "836447  2.113210e-15       983524  NW_024067923.1    342200  \n",
       "836448  6.656175e-36       996904  NW_024068475.1    115454  \n",
       "\n",
       "[836449 rows x 7 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting outliers pvalues in chromosomes\n",
    "\n",
    "Here you can find a fonction manhattan allow you create a manhattan plot using outliers kmers with position generated by iKISS. \n",
    "\n",
    "Please modify this line in fonction in fonction of chromosomes in the reference you have used.\n",
    "\n",
    "` liste.append(\"GEN\") if \"NC\" or \"NW\" or \"CM\" in row else liste.append(\"PAN\")` \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "def manhattan(df, title, plot):  \n",
    "    # on fait une colonne classe\n",
    "    liste=[]\n",
    "    xtick=[]\n",
    "    for row in df['REF']:\n",
    "        # modify this line in fonction of chromosomes in the reference you have used\n",
    "        liste.append(\"GEN\") if \"NC\" or \"NW\" or \"CM\" in row else liste.append(\"PAN\")\n",
    "    df['classe'] = liste\n",
    "    ### sort par col ref puis par position\n",
    "    df = df.sort_values(by = ['REF', 'position'], ascending = [True, True], na_position = 'first')\n",
    "    df['-logp']= - np.log10(df['pvalue'])\n",
    "    run_pos = 0\n",
    "    cum_pos = []\n",
    "    # calcule cumulative positions to give a x axe size in manhattan\n",
    "    for chrom, group_df in df.groupby('REF'):\n",
    "        cum_pos.append(group_df['position'] + run_pos)\n",
    "        run_pos += group_df['position'].max()\n",
    "    # new columns\n",
    "    df['cum_pos'] = pd.concat(cum_pos)\n",
    "    df['kmernb'] = df.index\n",
    "   \n",
    "    if plot:\n",
    "    # fait le manhattan\n",
    "        g = sns.relplot(data=df, x='cum_pos', y='-logp', aspect=4, hue=\"REF\", palette='Set1', linewidth=0, s=8, legend= None )\n",
    "        g.ax.set_xlabel('Chromosome')\n",
    "        g.ax.set_xticks(df.groupby('REF')['cum_pos'].median())\n",
    "        g.ax.set_xticklabels(df['REF'].unique())\n",
    "        g.ax.axhline(20, linestyle=\"--\", linewidth=1)\n",
    "        g.fig.suptitle(title)        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/.local/lib/python3.10/site-packages/pandas/core/arraylike.py:402: RuntimeWarning: divide by zero encountered in log10\n",
      "  result = getattr(ufunc, method)(*inputs, **kwargs)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 654672 characters]\n",
      "text/plain": [
       "<Figure size 2000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "title='manhattan plot using kmer position in genome male phoenix reference'\n",
    "manhattan(df,title,True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### plotting only chromosomes 1 to 18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_187975/1270294576.py:12: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df['classe'] = liste\n",
      "/home/orjuela/.local/lib/python3.10/site-packages/pandas/core/arraylike.py:402: RuntimeWarning: divide by zero encountered in log10\n",
      "  result = getattr(ufunc, method)(*inputs, **kwargs)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 293192 characters]\n",
      "text/plain": [
       "<Figure size 2000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "options = [\"NC_052392.1\", \"NC_052393.1\", \"NC_052394.1\", \"NC_052395.1\", \"NC_052396.1\", \"NC_052397.1\", \"NC_052398.1\", \"NC_052399.1\", \"NC_052400.1\", \"NC_052401.1\", \"NC_052402.1\", \"NC_052403.1\", \"NC_052404.1\", \n",
    "\"NC_052405.1\", \"NC_052406.1\", \"NC_052407.1\", \"NC_052408.1\", \"NC_052409.1\"]\n",
    "# selecting rows based on condition\n",
    "dff=df[df['REF'].isin(options)]\n",
    "title='manhattan plot using kmer position in  NC_052403.1 genome male BarheeBC4 phoenix'\n",
    "# nrows=10000\n",
    "manhattan(dff,title,True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### plotting only chr11 to chr15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_187975/1270294576.py:12: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df['classe'] = liste\n",
      "/home/orjuela/.local/lib/python3.10/site-packages/pandas/core/arraylike.py:402: RuntimeWarning: divide by zero encountered in log10\n",
      "  result = getattr(ufunc, method)(*inputs, **kwargs)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 215104 characters]\n",
      "text/plain": [
       "<Figure size 2000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "options = [\"NC_052402.1\", \"NC_052403.1\", \"NC_052404.1\", \"NC_052405.1\", \"NC_052406.1\"]\n",
    "# selecting rows based on condition\n",
    "dff=df[df['REF'].isin(options)]\n",
    "title='manhattan plot using kmer position in chr11 to 15 genome male BarheeBC4 phoenix'\n",
    "# nrows=10000\n",
    "manhattan(dff,title,True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
