{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run IGoR using pygor3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With our personalized models or not we can run IGoR to make inference, evaluation and generation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running demo code\n",
      "Working directory set to: \"/tmp/\"\n",
      "Reading genomic templates\n",
      "Reading sequences and aligning\n",
      "mkdir: cannot create directory '/tmp/igor_demo/': File exists\n",
      "V_gene alignments [||||||||||||||||||||||||||||||||||||||||||||||||||]  Done. \n",
      "D_gene alignments [||||||||||||||||||||||||||||||||||||||||||||||||||]  Done. \n",
      "J_gene alignments [||||||||||||||||||||||||||||||||||||||||||||||||||]  Done. \n",
      "Alignments procedure lasted: 5.36886 seconds\n",
      "for 300 TCRb sequences of 60bp(from murugan and al), against 89 Vs,3 Ds, and 15 Js full sequences\n",
      "Construct the model\n",
      "Write and read back the model\n",
      "GeneChoice read\n",
      "GeneChoice read\n",
      "GeneChoice read\n",
      "Deletion read\n",
      "Deletion read\n",
      "Deletion read\n",
      "Deletion read\n",
      "Insertion read\n",
      "Insertion read\n",
      "DinucMarkov read\n",
      "DinucMarkov read\n",
      "Infer model\n",
      "mkdir: cannot create directory '/tmp/igor_demo/run_demo': File exists\n",
      "Performing Evaluate/Inference iteration 1\n",
      "Initializing probability bounds...\n",
      "Initialization of probability bounds over.\n",
      "Iteration 1 [||||||||||||||||||||||||||||||||||||||||||||||||||]  Done. \n",
      "Performing Evaluate/Inference iteration 2\n",
      "Initializing probability bounds...\n",
      "Initialization of probability bounds over.\n",
      "Iteration 2 [||||||||||||||||||||||||||||||||||||||||||||||||||]  Done. \n",
      "Performing Evaluate/Inference iteration 3\n",
      "Initializing probability bounds...\n",
      "Initialization of probability bounds over.\n",
      "Iteration 3 [||||||||||||||||||||||||||||||||||||||||||||||||||]  Done. \n",
      "Performing Evaluate/Inference iteration 4\n",
      "Initializing probability bounds...\n",
      "Initialization of probability bounds over.\n",
      "Iteration 4 [||||||||||||||||||||||||||||||||||||||||||||||||||]  Done. \n",
      "Model inference procedure lasted: 37.9841 seconds\n",
      "Generate sequences\n",
      "Sequence generation [||||||||||||||||||||||||||||||||||||||||||||||||||]  Done. \n",
      "\n"
     ]
    }
   ],
   "source": [
    "import pygor3 as p3\n",
    "task0 = p3.IgorTask()\n",
    "aaa = task0.run_demo()\n",
    "print(aaa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pygor3 as p3\n",
    "task01 = p3.IgorTask()\n",
    "task01."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generating random sequences from model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pygor3 as p3\n",
    "task1 = p3.IgorTask.default_model(\"human\", \"beta\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 16636 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# task = p3.IgorTask()\n",
    "task1.mdl.parms.plot_Graph()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If batchname is not supply a random name is generated"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'dataIGoRM61172r9hi'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "task1.igor_batchname"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'igor_species': 'human',\n",
       " 'igor_chain': 'beta',\n",
       " 'igor_model_dir_path': '/home/alfaceor/.local/share/igor/models//human/tcr_beta',\n",
       " 'igor_path_ref_genome': '',\n",
       " 'fln_genomicVs': '',\n",
       " 'fln_genomicDs': '',\n",
       " 'fln_genomicJs': '',\n",
       " 'fln_V_gene_CDR3_anchors': '',\n",
       " 'igor_wd': '/home/alfaceor/Dropbox/PosDoc/IGoR/dev/MyGithub/pygor3/notebooks',\n",
       " 'igor_batchname': 'dataIGoR4jwrKCeYQA',\n",
       " 'igor_model_parms_file': '/home/alfaceor/.local/share/igor/models//human/tcr_beta/models/model_parms.txt',\n",
       " 'igor_model_marginals_file': '/home/alfaceor/.local/share/igor/models//human/tcr_beta/models/model_marginals.txt',\n",
       " 'igor_read_seqs': '',\n",
       " 'igor_threads': '',\n",
       " 'igor_fln_indexed_sequences': '',\n",
       " 'igor_fln_indexed_CDR3': '',\n",
       " 'igor_fln_align_V_alignments': '',\n",
       " 'igor_fln_align_J_alignments': '',\n",
       " 'igor_fln_align_D_alignments': '',\n",
       " 'igor_fln_infer_final_marginals': '',\n",
       " 'igor_fln_infer_final_parms': '',\n",
       " 'igor_fln_evaluate_final_marginals': '',\n",
       " 'igor_fln_evaluate_final_parms': '',\n",
       " 'igor_fln_output_pgen': '',\n",
       " 'igor_fln_output_scenarios': '',\n",
       " 'igor_fln_output_coverage': '',\n",
       " 'igor_fln_generated_realizations_werr': '',\n",
       " 'igor_fln_generated_seqs_werr': '',\n",
       " 'igor_fln_generation_info': '',\n",
       " 'igor_fln_db': '',\n",
       " 'b_read_seqs': False,\n",
       " 'b_align': False,\n",
       " 'b_infer': False,\n",
       " 'b_evaluate': False,\n",
       " 'b_generate': False}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "task.to_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/alfaceor/.local/bin/igor -set_wd /home/alfaceor/Dropbox/PosDoc/IGoR/dev/MyGithub/pygor3/notebooks -batch dataIGoRThZmIaWTTI -set_custom_model /home/alfaceor/.local/share/igor/models//human/tcr_beta/models/model_parms.txt /home/alfaceor/.local/share/igor/models//human/tcr_beta/models/model_marginals.txt -generate 10\n"
     ]
    },
    {
     "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>nt_sequence</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seq_index</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>GATGCTGGAGTTATCCAGTCACCCCGGCACGAGGTGACAGAGATGG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>GGTGCTGTCGTCTCTCAACATCCGAGCTGGGTTATCTGTAAGAGTG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>GGTGCTGGAGTCTCCCAGTCTCCCAGGTACAAAGTCACAAAGAGGG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>GGTGCTGGAGTCTCCCAGTCCCCAAGGTACAAAGTCGCAAAGAGGG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>GATGCTGGAGTTATCCAGTCACCCCGGCACGAGGTGACAGAGATGG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>GGAGCTGGAGTCTCCCAGTCCCCCAGTAACAAGGTCACAGAGAAGG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>GATGCTGGAGTTATCCAGTCACCCCGCCATGAGGTGACAGAGATGG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>AAGGCTGGAGTCACTCAAACTCCAAGATATCTGATCAAAACGAGAG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>ATTGCTGGGATCACCCAGGCACCAACATCTCAGATCCTGGCAGCAG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>AATGCTGGTGTCACTCAGACCCCAAAATTCCGCATCCTGAAGATAG...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 nt_sequence\n",
       "seq_index                                                   \n",
       "0          GATGCTGGAGTTATCCAGTCACCCCGGCACGAGGTGACAGAGATGG...\n",
       "1          GGTGCTGTCGTCTCTCAACATCCGAGCTGGGTTATCTGTAAGAGTG...\n",
       "2          GGTGCTGGAGTCTCCCAGTCTCCCAGGTACAAAGTCACAAAGAGGG...\n",
       "3          GGTGCTGGAGTCTCCCAGTCCCCAAGGTACAAAGTCGCAAAGAGGG...\n",
       "4          GATGCTGGAGTTATCCAGTCACCCCGGCACGAGGTGACAGAGATGG...\n",
       "5          GGAGCTGGAGTCTCCCAGTCCCCCAGTAACAAGGTCACAGAGAAGG...\n",
       "6          GATGCTGGAGTTATCCAGTCACCCCGCCATGAGGTGACAGAGATGG...\n",
       "7          AAGGCTGGAGTCACTCAAACTCCAAGATATCTGATCAAAACGAGAG...\n",
       "8          ATTGCTGGGATCACCCAGGCACCAACATCTCAGATCCTGGCAGCAG...\n",
       "9          AATGCTGGTGTCACTCAGACCCCAAAATTCCGCATCCTGAAGATAG..."
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_seqs = task1.run_generate(10)\n",
    "random_seqs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/alfaceor/.local/bin/igor -set_wd /home/alfaceor/Dropbox/PosDoc/IGoR/dev/MyGithub/pygor3/notebooks -batch dataIGoRThZmIaWTTI -set_custom_model /home/alfaceor/.local/share/igor/models//human/tcr_beta/models/model_parms.txt /home/alfaceor/.local/share/igor/models//human/tcr_beta/models/model_marginals.txt -generate 10\n"
     ]
    },
    {
     "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>nt_sequence</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seq_index</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>GCTGGAATCACCCAGAGCCCAAGACACAAGGTCACAGAGACAGGAA...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AAGGCTGGAGTCACTCAAACTCCAAGATATCTGATCAAAACGAGAG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>GATGCTAGAGTCACCCAGACACCAAGGCACAAGGTGACAGAGATGG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>GGTGCTGTCGTCTCTCAACATCCGAGCTGGGTTATCTGTAAGAGTG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>GATGCTGAAATCACCCAGAGCCCAAGACACAAGATCACAGAGACAG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>ACGGGAGTTACGCAGACACCAAGACACCTGGTCATGGGAATGACAA...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>GATTCTGGAGTCACACAAACCCCAAAGCACCTGATCACAGCAACTG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>GATGTGAAAGTAACCCAGAGCTCGAGATATCTAGTCAAAAGGACGG...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>ACGGGAGTTACGCAGACACCAAGACACCTGGTCATGGGAATGACAA...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>GATGCTGGAGTTATCCAGTCACCCCGGCACGAGGTGACAGAGATGG...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 nt_sequence\n",
       "seq_index                                                   \n",
       "0          GCTGGAATCACCCAGAGCCCAAGACACAAGGTCACAGAGACAGGAA...\n",
       "1          AAGGCTGGAGTCACTCAAACTCCAAGATATCTGATCAAAACGAGAG...\n",
       "2          GATGCTAGAGTCACCCAGACACCAAGGCACAAGGTGACAGAGATGG...\n",
       "3          GGTGCTGTCGTCTCTCAACATCCGAGCTGGGTTATCTGTAAGAGTG...\n",
       "4          GATGCTGAAATCACCCAGAGCCCAAGACACAAGATCACAGAGACAG...\n",
       "5          ACGGGAGTTACGCAGACACCAAGACACCTGGTCATGGGAATGACAA...\n",
       "6          GATTCTGGAGTCACACAAACCCCAAAGCACCTGATCACAGCAACTG...\n",
       "7          GATGTGAAAGTAACCCAGAGCTCGAGATATCTAGTCAAAAGGACGG...\n",
       "8          ACGGGAGTTACGCAGACACCAAGACACCTGGTCATGGGAATGACAA...\n",
       "9          GATGCTGGAGTTATCCAGTCACCCCGGCACGAGGTGACAGAGATGG..."
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "random_seqs2 = task1.run_generate(10)\n",
    "random_seqs2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "task1.run_clean_batch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "task = p3.IgorTask(fln_geno='')\n",
    "task.fln_genomicVs = \"fasd\"\n",
    "task.fln_genomicDs = \"dfa\"\n",
    "task.fln_genomicJs = \"\"\n",
    "task.run_align()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Evaluate Sequences"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can evaluate sequence, given a model, to know the pgen of a sequence and the possible scenarios "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"igor_species\" : self.igor_species, \n",
      "\"igor_chain\" : self.igor_chain, \n",
      "\"igor_model_dir_path\" : self.igor_model_dir_path, \n",
      "\"igor_path_ref_genome\" : self.igor_path_ref_genome, \n",
      "\"fln_genomicVs\" : self.fln_genomicVs, \n",
      "\"fln_genomicDs\" : self.fln_genomicDs, \n",
      "\"fln_genomicJs\" : self.fln_genomicJs, \n",
      "\"fln_V_gene_CDR3_anchors\" : self.fln_V_gene_CDR3_anchors, \n",
      "\"igor_wd\" : self.igor_wd, \n",
      "\"igor_batchname\" : self.igor_batchname, \n",
      "\"igor_model_parms_file\" : self.igor_model_parms_file, \n",
      "\"igor_model_marginals_file\" : self.igor_model_marginals_file, \n",
      "\"igor_read_seqs\" : self.igor_read_seqs, \n",
      "\"igor_threads\" : self.igor_threads, \n",
      "\"igor_fln_indexed_sequences\" : self.igor_fln_indexed_sequences, \n",
      "\"igor_fln_indexed_CDR3\" : self.igor_fln_indexed_CDR3, \n",
      "\"igor_fln_align_V_alignments\" : self.igor_fln_align_V_alignments, \n",
      "\"igor_fln_align_J_alignments\" : self.igor_fln_align_J_alignments, \n",
      "\"igor_fln_align_D_alignments\" : self.igor_fln_align_D_alignments, \n",
      "\"igor_fln_infer_final_marginals\" : self.igor_fln_infer_final_marginals, \n",
      "\"igor_fln_infer_final_parms\" : self.igor_fln_infer_final_parms, \n",
      "\"igor_fln_evaluate_final_marginals\" : self.igor_fln_evaluate_final_marginals, \n",
      "\"igor_fln_evaluate_final_parms\" : self.igor_fln_evaluate_final_parms, \n",
      "\"igor_fln_output_pgen\" : self.igor_fln_output_pgen, \n",
      "\"igor_fln_output_scenarios\" : self.igor_fln_output_scenarios, \n",
      "\"igor_fln_output_coverage\" : self.igor_fln_output_coverage, \n",
      "\"igor_fln_db\" : self.igor_fln_db, \n"
     ]
    }
   ],
   "source": [
    "for mmm in mmm_list:\n",
    "    print(\"\\\"\"+mmm+\"\\\"\" +\" : \"+ \"self.\"+mmm+ \", \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
