{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `natsel_neutral` -- a test for selective neutrality\n",
    "\n",
    "We employ codon models to test hypotheses regarding the mode of natural selection that has operated on a gene.\n",
    "\n",
    "Noting that ω (omega) is the ratio of nonsynonymous substitutions to synonymous substitutions, ω=1 is indicative a gene is evolving neutrally.  (Note I'm setting `optimise_motif_probs=False` to speed up execution of the examples, not because it's a good idea!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "cogent3.app.result.hypothesis_result"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from cogent3.app import io, evo\n",
    "\n",
    "loader = io.load_aligned(format=\"fasta\", moltype=\"dna\")\n",
    "aln = loader(\"../data/primate_brca1.fasta\")\n",
    "\n",
    "omega_eq_1 = evo.natsel_neutral(\"GNC\",\n",
    "                                tree=\"../data/primate_brca1.tree\",\n",
    "                                optimise_motif_probs=False)\n",
    "result = omega_eq_1(aln)\n",
    "type(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<style>\n",
       "tr:last-child {border-bottom: 1px solid #000;} tr > th {text-align: center !important;} tr > td {text-align: left !important;}\n",
       "</style>\n",
       "<caption style=\"color: rgb(250, 250, 250); background: rgba(30, 140, 200, 1); align=top;\"><span style=\"font-weight: bold;\">Statistics</span><span></span></caption>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>LR</th>\n",
       "<th>df</th>\n",
       "<th>pvalue</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"font-family: monospace !important;\">4.5034</td>\n",
       "<td style=\"font-family: monospace !important;\">1</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0338</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "\n",
       "<table>\n",
       "<style>\n",
       "tr:last-child {border-bottom: 1px solid #000;} tr > th {text-align: center !important;} tr > td {text-align: left !important;}\n",
       "</style>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>hypothesis</th>\n",
       "<th>key</th>\n",
       "<th>lnL</th>\n",
       "<th>nfp</th>\n",
       "<th>DLC</th>\n",
       "<th>unique_Q</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td>null</td>\n",
       "<td>'GNC-null'</td>\n",
       "<td style=\"font-family: monospace !important;\">-6715.5250</td>\n",
       "<td style=\"font-family: monospace !important;\">22</td>\n",
       "<td>True</td>\n",
       "<td></td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td>alt</td>\n",
       "<td>'GNC-alt'</td>\n",
       "<td style=\"font-family: monospace !important;\">-6713.2733</td>\n",
       "<td style=\"font-family: monospace !important;\">23</td>\n",
       "<td>True</td>\n",
       "<td></td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "Statistics\n",
       "======================\n",
       "    LR    df    pvalue\n",
       "----------------------\n",
       "4.5034     1    0.0338\n",
       "----------------------\n",
       "=================================================================\n",
       "hypothesis           key           lnL    nfp     DLC    unique_Q\n",
       "-----------------------------------------------------------------\n",
       "      null    'GNC-null'    -6715.5250     22    True            \n",
       "       alt     'GNC-alt'    -6713.2733     23    True            \n",
       "-----------------------------------------------------------------"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<h4>GNC-alt</h4>\n",
       "<p>log-likelihood = -6713.2733</p>\n",
       "<p>number of free parameters = 23</p>\n",
       "<table>\n",
       "<style>\n",
       "tr:last-child {border-bottom: 1px solid #000;} tr > th {text-align: center !important;} tr > td {text-align: left !important;}\n",
       "</style>\n",
       "<caption style=\"color: rgb(250, 250, 250); background: rgba(30, 140, 200, 1); align=top;\"><span style=\"font-weight: bold;\">Global params</span><span></span></caption>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>A&gt;C</th>\n",
       "<th>A&gt;G</th>\n",
       "<th>A&gt;T</th>\n",
       "<th>C&gt;A</th>\n",
       "<th>C&gt;G</th>\n",
       "<th>C&gt;T</th>\n",
       "<th>G&gt;A</th>\n",
       "<th>G&gt;C</th>\n",
       "<th>G&gt;T</th>\n",
       "<th>T&gt;A</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"font-family: monospace !important;\">0.8614</td>\n",
       "<td style=\"font-family: monospace !important;\">3.5373</td>\n",
       "<td style=\"font-family: monospace !important;\">0.9792</td>\n",
       "<td style=\"font-family: monospace !important;\">1.6667</td>\n",
       "<td style=\"font-family: monospace !important;\">2.2042</td>\n",
       "<td style=\"font-family: monospace !important;\">6.2565</td>\n",
       "<td style=\"font-family: monospace !important;\">7.9195</td>\n",
       "<td style=\"font-family: monospace !important;\">1.2253</td>\n",
       "<td style=\"font-family: monospace !important;\">0.8014</td>\n",
       "<td style=\"font-family: monospace !important;\">1.2911</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "<table>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>T&gt;C</th>\n",
       "<th>omega</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"font-family: monospace !important;\">3.0723</td>\n",
       "<td style=\"font-family: monospace !important;\">0.8204</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "\n",
       "<table>\n",
       "<style>\n",
       "tr:last-child {border-bottom: 1px solid #000;} tr > th {text-align: center !important;} tr > td {text-align: left !important;}\n",
       "</style>\n",
       "<caption style=\"color: rgb(250, 250, 250); background: rgba(30, 140, 200, 1); align=top;\"><span style=\"font-weight: bold;\">Edge params</span><span></span></caption>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>edge</th>\n",
       "<th>parent</th>\n",
       "<th>length</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Galago</td>\n",
       "<td>root</td>\n",
       "<td style=\"font-family: monospace !important;\">0.5232</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">HowlerMon</td>\n",
       "<td>root</td>\n",
       "<td style=\"font-family: monospace !important;\">0.1338</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Rhesus</td>\n",
       "<td>edge.3</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0640</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Orangutan</td>\n",
       "<td>edge.2</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0233</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Gorilla</td>\n",
       "<td>edge.1</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0075</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Human</td>\n",
       "<td>edge.0</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0182</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Chimpanzee</td>\n",
       "<td>edge.0</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0085</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">edge.0</td>\n",
       "<td>edge.1</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0000</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">edge.1</td>\n",
       "<td>edge.2</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0100</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">edge.2</td>\n",
       "<td>edge.3</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0366</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">edge.3</td>\n",
       "<td>root</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0238</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "\n",
       "<table>\n",
       "<style>\n",
       "tr:last-child {border-bottom: 1px solid #000;} tr > th {text-align: center !important;} tr > td {text-align: left !important;}\n",
       "</style>\n",
       "<caption style=\"color: rgb(250, 250, 250); background: rgba(30, 140, 200, 1); align=top;\"><span style=\"font-weight: bold;\">Motif params</span><span></span></caption>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>AAA</th>\n",
       "<th>AAC</th>\n",
       "<th>AAG</th>\n",
       "<th>AAT</th>\n",
       "<th>ACA</th>\n",
       "<th>ACC</th>\n",
       "<th>ACG</th>\n",
       "<th>ACT</th>\n",
       "<th>AGA</th>\n",
       "<th>AGC</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"font-family: monospace !important;\">0.0556</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0235</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0344</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0556</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0228</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0046</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0008</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0289</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0231</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0286</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "<table>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>AGG</th>\n",
       "<th>AGT</th>\n",
       "<th>ATA</th>\n",
       "<th>ATC</th>\n",
       "<th>ATG</th>\n",
       "<th>ATT</th>\n",
       "<th>CAA</th>\n",
       "<th>CAC</th>\n",
       "<th>CAG</th>\n",
       "<th>CAT</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"font-family: monospace !important;\">0.0140</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0381</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0186</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0070</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0128</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0192</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0196</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0052</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0238</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0221</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "<table>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>CCA</th>\n",
       "<th>CCC</th>\n",
       "<th>CCG</th>\n",
       "<th>CCT</th>\n",
       "<th>CGA</th>\n",
       "<th>CGC</th>\n",
       "<th>CGG</th>\n",
       "<th>CGT</th>\n",
       "<th>CTA</th>\n",
       "<th>CTC</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"font-family: monospace !important;\">0.0195</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0062</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0006</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0263</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0011</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0023</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0032</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0137</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0078</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "<table>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>CTG</th>\n",
       "<th>CTT</th>\n",
       "<th>GAA</th>\n",
       "<th>GAC</th>\n",
       "<th>GAG</th>\n",
       "<th>GAT</th>\n",
       "<th>GCA</th>\n",
       "<th>GCC</th>\n",
       "<th>GCG</th>\n",
       "<th>GCT</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"font-family: monospace !important;\">0.0125</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0105</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0755</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0105</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0303</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0315</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0158</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0096</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0137</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "<table>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>GGA</th>\n",
       "<th>GGC</th>\n",
       "<th>GGG</th>\n",
       "<th>GGT</th>\n",
       "<th>GTA</th>\n",
       "<th>GTC</th>\n",
       "<th>GTG</th>\n",
       "<th>GTT</th>\n",
       "<th>TAC</th>\n",
       "<th>TAT</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"font-family: monospace !important;\">0.0161</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0090</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0067</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0133</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0148</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0070</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0069</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0213</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0023</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0101</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "<table>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>TCA</th>\n",
       "<th>TCC</th>\n",
       "<th>TCG</th>\n",
       "<th>TCT</th>\n",
       "<th>TGC</th>\n",
       "<th>TGG</th>\n",
       "<th>TGT</th>\n",
       "<th>TTA</th>\n",
       "<th>TTC</th>\n",
       "<th>TTG</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"font-family: monospace !important;\">0.0221</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0082</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0015</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0251</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0018</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0040</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0201</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0212</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0078</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0108</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "<table>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>TTT</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"font-family: monospace !important;\">0.0187</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<cogent3.evolve.parameter_controller.AlignmentLikelihoodFunction at 0x115f609e8>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.alt.lf"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:c3dev] *",
   "language": "python",
   "name": "conda-env-c3dev-py"
  },
  "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.7.3"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
