{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Genetic distance calculation\n",
    "\n",
    "## Fast pairwise distance estimation\n",
    "\n",
    "For a limited number of evolutionary models a fast implementation is available."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "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;\">Specify a pairwise genetic distance calculator using 'Abbreviation' (case insensitive).</span><span></span></caption>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>Abbreviation</th>\n",
       "<th>Suitable for moltype</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">paralinear</td>\n",
       "<td>dna, rna, protein</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">logdet</td>\n",
       "<td>dna, rna, protein</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">jc69</td>\n",
       "<td>dna, rna</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">tn93</td>\n",
       "<td>dna, rna</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">hamming</td>\n",
       "<td>dna, rna, protein, text</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">percent</td>\n",
       "<td>dna, rna, protein, text</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "<p>\n",
       "6 rows x 2 columns</p>"
      ],
      "text/plain": [
       "Specify a pairwise genetic distance calculator using 'Abbreviation' (case insensitive).\n",
       "=======================================\n",
       "Abbreviation       Suitable for moltype\n",
       "---------------------------------------\n",
       "  paralinear          dna, rna, protein\n",
       "      logdet          dna, rna, protein\n",
       "        jc69                   dna, rna\n",
       "        tn93                   dna, rna\n",
       "     hamming    dna, rna, protein, text\n",
       "     percent    dna, rna, protein, text\n",
       "---------------------------------------\n",
       "\n",
       "6 rows x 2 columns"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from cogent3 import available_distances\n",
    "\n",
    "available_distances()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Computing genetic distances using the `Alignment` object\n",
    "\n",
    "Abbreviations listed from `available_distances()` can be used as values for the `distance_matrix(calc=<abbreviation>)`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "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",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th></th>\n",
       "<th>Chimpanzee</th>\n",
       "<th>Galago</th>\n",
       "<th>Gorilla</th>\n",
       "<th>HowlerMon</th>\n",
       "<th>Human</th>\n",
       "<th>Orangutan</th>\n",
       "<th>Rhesus</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Chimpanzee</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.192</td>\n",
       "<td style=\"font-family: monospace !important;\">0.005</td>\n",
       "<td style=\"font-family: monospace !important;\">0.070</td>\n",
       "<td style=\"font-family: monospace !important;\">0.009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.040</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Galago</td>\n",
       "<td style=\"font-family: monospace !important;\">0.192</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.192</td>\n",
       "<td style=\"font-family: monospace !important;\">0.216</td>\n",
       "<td style=\"font-family: monospace !important;\">0.196</td>\n",
       "<td style=\"font-family: monospace !important;\">0.194</td>\n",
       "<td style=\"font-family: monospace !important;\">0.196</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Gorilla</td>\n",
       "<td style=\"font-family: monospace !important;\">0.005</td>\n",
       "<td style=\"font-family: monospace !important;\">0.192</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.070</td>\n",
       "<td style=\"font-family: monospace !important;\">0.009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.039</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">HowlerMon</td>\n",
       "<td style=\"font-family: monospace !important;\">0.070</td>\n",
       "<td style=\"font-family: monospace !important;\">0.216</td>\n",
       "<td style=\"font-family: monospace !important;\">0.070</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.074</td>\n",
       "<td style=\"font-family: monospace !important;\">0.072</td>\n",
       "<td style=\"font-family: monospace !important;\">0.074</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Human</td>\n",
       "<td style=\"font-family: monospace !important;\">0.009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.196</td>\n",
       "<td style=\"font-family: monospace !important;\">0.009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.074</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.017</td>\n",
       "<td style=\"font-family: monospace !important;\">0.042</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Orangutan</td>\n",
       "<td style=\"font-family: monospace !important;\">0.014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.194</td>\n",
       "<td style=\"font-family: monospace !important;\">0.014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.072</td>\n",
       "<td style=\"font-family: monospace !important;\">0.017</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.041</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Rhesus</td>\n",
       "<td style=\"font-family: monospace !important;\">0.040</td>\n",
       "<td style=\"font-family: monospace !important;\">0.196</td>\n",
       "<td style=\"font-family: monospace !important;\">0.039</td>\n",
       "<td style=\"font-family: monospace !important;\">0.074</td>\n",
       "<td style=\"font-family: monospace !important;\">0.042</td>\n",
       "<td style=\"font-family: monospace !important;\">0.041</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "===========================================================================================\n",
       "              Chimpanzee    Galago    Gorilla    HowlerMon     Human    Orangutan    Rhesus\n",
       "-------------------------------------------------------------------------------------------\n",
       "Chimpanzee        0.0000    0.1921     0.0054       0.0704    0.0089       0.0140    0.0396\n",
       "    Galago        0.1921    0.0000     0.1923       0.2157    0.1965       0.1944    0.1962\n",
       "   Gorilla        0.0054    0.1923     0.0000       0.0700    0.0086       0.0137    0.0393\n",
       " HowlerMon        0.0704    0.2157     0.0700       0.0000    0.0736       0.0719    0.0736\n",
       "     Human        0.0089    0.1965     0.0086       0.0736    0.0000       0.0173    0.0423\n",
       " Orangutan        0.0140    0.1944     0.0137       0.0719    0.0173       0.0000    0.0411\n",
       "    Rhesus        0.0396    0.1962     0.0393       0.0736    0.0423       0.0411    0.0000\n",
       "-------------------------------------------------------------------------------------------"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from cogent3 import load_aligned_seqs\n",
    "aln = load_aligned_seqs('../data/primate_brca1.fasta', moltype=\"dna\")\n",
    "dists = aln.distance_matrix(calc=\"tn93\", show_progress=False)\n",
    "dists"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Using the distance calculator directly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<cogent3.evolve.fast_distance.TN93Pair at 0x1163fc820>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from cogent3 import load_aligned_seqs, get_distance_calculator\n",
    "aln = load_aligned_seqs('../data/primate_brca1.fasta')\n",
    "dist_calc = get_distance_calculator(\"tn93\", alignment=aln)\n",
    "dist_calc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "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",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th></th>\n",
       "<th>Chimpanzee</th>\n",
       "<th>Galago</th>\n",
       "<th>Gorilla</th>\n",
       "<th>HowlerMon</th>\n",
       "<th>Human</th>\n",
       "<th>Orangutan</th>\n",
       "<th>Rhesus</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Chimpanzee</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.192</td>\n",
       "<td style=\"font-family: monospace !important;\">0.005</td>\n",
       "<td style=\"font-family: monospace !important;\">0.070</td>\n",
       "<td style=\"font-family: monospace !important;\">0.009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.040</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Galago</td>\n",
       "<td style=\"font-family: monospace !important;\">0.192</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.192</td>\n",
       "<td style=\"font-family: monospace !important;\">0.216</td>\n",
       "<td style=\"font-family: monospace !important;\">0.196</td>\n",
       "<td style=\"font-family: monospace !important;\">0.194</td>\n",
       "<td style=\"font-family: monospace !important;\">0.196</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Gorilla</td>\n",
       "<td style=\"font-family: monospace !important;\">0.005</td>\n",
       "<td style=\"font-family: monospace !important;\">0.192</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.070</td>\n",
       "<td style=\"font-family: monospace !important;\">0.009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.039</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">HowlerMon</td>\n",
       "<td style=\"font-family: monospace !important;\">0.070</td>\n",
       "<td style=\"font-family: monospace !important;\">0.216</td>\n",
       "<td style=\"font-family: monospace !important;\">0.070</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.074</td>\n",
       "<td style=\"font-family: monospace !important;\">0.072</td>\n",
       "<td style=\"font-family: monospace !important;\">0.074</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Human</td>\n",
       "<td style=\"font-family: monospace !important;\">0.009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.196</td>\n",
       "<td style=\"font-family: monospace !important;\">0.009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.074</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.017</td>\n",
       "<td style=\"font-family: monospace !important;\">0.042</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Orangutan</td>\n",
       "<td style=\"font-family: monospace !important;\">0.014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.194</td>\n",
       "<td style=\"font-family: monospace !important;\">0.014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.072</td>\n",
       "<td style=\"font-family: monospace !important;\">0.017</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.041</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Rhesus</td>\n",
       "<td style=\"font-family: monospace !important;\">0.040</td>\n",
       "<td style=\"font-family: monospace !important;\">0.196</td>\n",
       "<td style=\"font-family: monospace !important;\">0.039</td>\n",
       "<td style=\"font-family: monospace !important;\">0.074</td>\n",
       "<td style=\"font-family: monospace !important;\">0.042</td>\n",
       "<td style=\"font-family: monospace !important;\">0.041</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "===========================================================================================\n",
       "              Chimpanzee    Galago    Gorilla    HowlerMon     Human    Orangutan    Rhesus\n",
       "-------------------------------------------------------------------------------------------\n",
       "Chimpanzee        0.0000    0.1921     0.0054       0.0704    0.0089       0.0140    0.0396\n",
       "    Galago        0.1921    0.0000     0.1923       0.2157    0.1965       0.1944    0.1962\n",
       "   Gorilla        0.0054    0.1923     0.0000       0.0700    0.0086       0.0137    0.0393\n",
       " HowlerMon        0.0704    0.2157     0.0700       0.0000    0.0736       0.0719    0.0736\n",
       "     Human        0.0089    0.1965     0.0086       0.0736    0.0000       0.0173    0.0423\n",
       " Orangutan        0.0140    0.1944     0.0137       0.0719    0.0173       0.0000    0.0411\n",
       "    Rhesus        0.0396    0.1962     0.0393       0.0736    0.0423       0.0411    0.0000\n",
       "-------------------------------------------------------------------------------------------"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist_calc.run(show_progress=False)\n",
    "dists = dist_calc.get_pairwise_distances()\n",
    "dists"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "The distance calculation object can provide more information. For instance, the standard errors.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "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;\">Standard Error of Pairwise Distances</span><span></span></caption>\n",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th>Seq1 \\ Seq2</th>\n",
       "<th>Galago</th>\n",
       "<th>HowlerMon</th>\n",
       "<th>Rhesus</th>\n",
       "<th>Orangutan</th>\n",
       "<th>Gorilla</th>\n",
       "<th>Human</th>\n",
       "<th>Chimpanzee</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Galago</td>\n",
       "<td style=\"font-family: monospace !important;\">0</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0103</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0096</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0095</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0095</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0096</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0095</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">HowlerMon</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0103</td>\n",
       "<td style=\"font-family: monospace !important;\">0</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0054</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0053</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0053</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0054</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0053</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Rhesus</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0096</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0054</td>\n",
       "<td style=\"font-family: monospace !important;\">0</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0039</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0039</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0040</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0039</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Orangutan</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0095</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0053</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0039</td>\n",
       "<td style=\"font-family: monospace !important;\">0</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0022</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0025</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0023</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Gorilla</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0095</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0053</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0039</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0022</td>\n",
       "<td style=\"font-family: monospace !important;\">0</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0018</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0014</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Human</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0096</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0054</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0040</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0025</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0018</td>\n",
       "<td style=\"font-family: monospace !important;\">0</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0018</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Chimpanzee</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0095</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0053</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0039</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0023</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.0018</td>\n",
       "<td style=\"font-family: monospace !important;\">0</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "<p>\n",
       "7 rows x 8 columns</p>"
      ],
      "text/plain": [
       "Standard Error of Pairwise Distances\n",
       "============================================================================================\n",
       "Seq1 \\ Seq2    Galago    HowlerMon    Rhesus    Orangutan    Gorilla     Human    Chimpanzee\n",
       "--------------------------------------------------------------------------------------------\n",
       "     Galago         0       0.0103    0.0096       0.0095     0.0095    0.0096        0.0095\n",
       "  HowlerMon    0.0103            0    0.0054       0.0053     0.0053    0.0054        0.0053\n",
       "     Rhesus    0.0096       0.0054         0       0.0039     0.0039    0.0040        0.0039\n",
       "  Orangutan    0.0095       0.0053    0.0039            0     0.0022    0.0025        0.0023\n",
       "    Gorilla    0.0095       0.0053    0.0039       0.0022          0    0.0018        0.0014\n",
       "      Human    0.0096       0.0054    0.0040       0.0025     0.0018         0        0.0018\n",
       " Chimpanzee    0.0095       0.0053    0.0039       0.0023     0.0014    0.0018             0\n",
       "--------------------------------------------------------------------------------------------\n",
       "\n",
       "7 rows x 8 columns"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist_calc.stderr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Likelihood based pairwise distance estimation\n",
    "\n",
    "The standard ``cogent3`` likelihood function can also be used to estimate distances. Because these require numerical optimisation they can be significantly slower than the fast estimation approach above.\n",
    "\n",
    "The following will use the F81 nucleotide substitution model and perform numerical optimisation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "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",
       "<thead style=\"background: rgba(161, 195, 209, 0.75); font-weight: bold; text-align: center;\">\n",
       "<th></th>\n",
       "<th>Chimpanzee</th>\n",
       "<th>Galago</th>\n",
       "<th>Gorilla</th>\n",
       "<th>HowlerMon</th>\n",
       "<th>Human</th>\n",
       "<th>Orangutan</th>\n",
       "<th>Rhesus</th>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Chimpanzee</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.189</td>\n",
       "<td style=\"font-family: monospace !important;\">0.005</td>\n",
       "<td style=\"font-family: monospace !important;\">0.070</td>\n",
       "<td style=\"font-family: monospace !important;\">0.009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.039</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Galago</td>\n",
       "<td style=\"font-family: monospace !important;\">0.189</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.189</td>\n",
       "<td style=\"font-family: monospace !important;\">0.211</td>\n",
       "<td style=\"font-family: monospace !important;\">0.193</td>\n",
       "<td style=\"font-family: monospace !important;\">0.192</td>\n",
       "<td style=\"font-family: monospace !important;\">0.193</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Gorilla</td>\n",
       "<td style=\"font-family: monospace !important;\">0.005</td>\n",
       "<td style=\"font-family: monospace !important;\">0.189</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.069</td>\n",
       "<td style=\"font-family: monospace !important;\">0.009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.039</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">HowlerMon</td>\n",
       "<td style=\"font-family: monospace !important;\">0.070</td>\n",
       "<td style=\"font-family: monospace !important;\">0.211</td>\n",
       "<td style=\"font-family: monospace !important;\">0.069</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.073</td>\n",
       "<td style=\"font-family: monospace !important;\">0.071</td>\n",
       "<td style=\"font-family: monospace !important;\">0.073</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Human</td>\n",
       "<td style=\"font-family: monospace !important;\">0.009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.193</td>\n",
       "<td style=\"font-family: monospace !important;\">0.009</td>\n",
       "<td style=\"font-family: monospace !important;\">0.073</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.017</td>\n",
       "<td style=\"font-family: monospace !important;\">0.042</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Orangutan</td>\n",
       "<td style=\"font-family: monospace !important;\">0.014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.192</td>\n",
       "<td style=\"font-family: monospace !important;\">0.014</td>\n",
       "<td style=\"font-family: monospace !important;\">0.071</td>\n",
       "<td style=\"font-family: monospace !important;\">0.017</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "<td style=\"font-family: monospace !important;\">0.041</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td style=\"background: rgba(161, 195, 209, 0.25); font-weight: 600;\">Rhesus</td>\n",
       "<td style=\"font-family: monospace !important;\">0.039</td>\n",
       "<td style=\"font-family: monospace !important;\">0.193</td>\n",
       "<td style=\"font-family: monospace !important;\">0.039</td>\n",
       "<td style=\"font-family: monospace !important;\">0.073</td>\n",
       "<td style=\"font-family: monospace !important;\">0.042</td>\n",
       "<td style=\"font-family: monospace !important;\">0.041</td>\n",
       "<td style=\"font-family: monospace !important;\">0.000</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "===========================================================================================\n",
       "              Chimpanzee    Galago    Gorilla    HowlerMon     Human    Orangutan    Rhesus\n",
       "-------------------------------------------------------------------------------------------\n",
       "Chimpanzee        0.0000    0.1892     0.0054       0.0697    0.0089       0.0140    0.0395\n",
       "    Galago        0.1892    0.0000     0.1891       0.2112    0.1934       0.1915    0.1930\n",
       "   Gorilla        0.0054    0.1891     0.0000       0.0693    0.0086       0.0136    0.0391\n",
       " HowlerMon        0.0697    0.2112     0.0693       0.0000    0.0729       0.0713    0.0729\n",
       "     Human        0.0089    0.1934     0.0086       0.0729    0.0000       0.0173    0.0421\n",
       " Orangutan        0.0140    0.1915     0.0136       0.0713    0.0173       0.0000    0.0410\n",
       "    Rhesus        0.0395    0.1930     0.0391       0.0729    0.0421       0.0410    0.0000\n",
       "-------------------------------------------------------------------------------------------"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from cogent3 import load_aligned_seqs, get_model\n",
    "from cogent3.evolve import distance\n",
    "\n",
    "aln = load_aligned_seqs('../data/primate_brca1.fasta', moltype=\"dna\")\n",
    "d = distance.EstimateDistances(aln, submodel=get_model(\"F81\"))\n",
    "d.run(show_progress=False)\n",
    "dists = d.get_pairwise_distances()\n",
    "dists"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "All `cogent3` substitution models can be used for distance calculation via this approach, with the caveat that identifiability issues mean this is not possible for some non-stationary model classes."
   ]
  }
 ],
 "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.8.1"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
