{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple showcase of the PyGeneBe library\n",
    "\n",
    "Install genebe using \n",
    "```\n",
    "pip install -U genebe\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Version:0.0.14\n"
     ]
    }
   ],
   "source": [
    "import genebe as gnb\n",
    "\n",
    "print('Version:' + gnb.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "GeneBe makes it easy to parse HGVS variants. It works with `c`, `m`, `g` and `n` representation. It does not support `p`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00,  3.43it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['1-230710021-G-A', '22-28695868-AG-A', '3-39394574-A-T', 'M-1243-T-C']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parsed = gnb.parse_hgvs(['ENST00000679957.1:c.803C>T',\n",
    "                         'ENST00000404276.6:c.1100del',\n",
    "                         'NC_000003.12:g.39394574A>T',\n",
    "                         'NC_012920.1:m.1243T>C'] )\n",
    "parsed"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is an example of annotating a list of genetic variants represented as `chr-pos-ref-alt` to the `dict` data structure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:you will log in ps209497@gmail.com\n",
      "100%|██████████| 1/1 [00:00<00:00,  2.42it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'chr': '1',\n",
       "  'pos': 230710021,\n",
       "  'ref': 'G',\n",
       "  'alt': 'A',\n",
       "  'transcript': 'NM_001384479.1',\n",
       "  'gene_symbol': 'AGT',\n",
       "  'dbsnp': '1228544607',\n",
       "  'gnomad_exomes_af': 6.840659807494376e-06,\n",
       "  'gnomad_exomes_ac': 10.0,\n",
       "  'gnomad_exomes_homalt': 0.0,\n",
       "  'revel_score': 0.18700000643730164,\n",
       "  'alphamissense_score': 0.06610000133514404,\n",
       "  'bayesdelnoaf_score': -0.3700000047683716,\n",
       "  'phylop100way_score': 4.302999973297119,\n",
       "  'acmg_score': -1,\n",
       "  'acmg_classification': 'Likely_benign',\n",
       "  'acmg_criteria': 'PM2_Supporting,BP4_Moderate',\n",
       "  'gene_hgnc_id': 333,\n",
       "  'hgvs_c': 'c.803C>T',\n",
       "  'consequences': 'missense_variant'},\n",
       " {'chr': '22',\n",
       "  'pos': 28695868,\n",
       "  'ref': 'AG',\n",
       "  'alt': 'A',\n",
       "  'transcript': 'NM_007194.4',\n",
       "  'gene_symbol': 'CHEK2',\n",
       "  'dbsnp': '555607708',\n",
       "  'gnomad_genomes_af': 0.0017200199654325843,\n",
       "  'gnomad_genomes_ac': 262.0,\n",
       "  'gnomad_genomes_homalt': 0.0,\n",
       "  'phylop100way_score': 8.668000221252441,\n",
       "  'acmg_score': 9,\n",
       "  'acmg_classification': 'Likely_pathogenic',\n",
       "  'acmg_criteria': 'PVS1,PP5',\n",
       "  'clinvar_disease': 'Hereditary cancer-predisposing syndrome,Li-Fraumeni syndrome 2,Familial cancer of breast,Breast and colorectal cancer, susceptibility to,not provided,Breast cancer,Li-Fraumeni syndrome,Breast neoplasm,Astrocytoma,CHEK2-Related Cancer Susceptibility,Leiomyosarcoma,Bone osteosarcoma,Malignant tumor of prostate,Colitis,Inflammation of the large intestine,Thrombocytopenia,Hematochezia,Breast and/or ovarian cancer,Carcinoma of pancreas,Breast carcinoma,Malignant tumor of breast,Colorectal cancer,Hereditary breast ovarian cancer syndrome,Predisposition to cancer,Li-Fraumeni syndrome 1,TUMOR PREDISPOSITION SYNDROME 4, BREAST/PROSTATE/COLORECTAL,CHEK2-related condition,Neoplasm of ovary',\n",
       "  'clinvar_classification': 'Conflicting interpretations of pathogenicity',\n",
       "  'gene_hgnc_id': 16627,\n",
       "  'hgvs_c': 'c.1100delC',\n",
       "  'consequences': 'frameshift_variant'},\n",
       " {'chr': '3',\n",
       "  'pos': 39394574,\n",
       "  'ref': 'A',\n",
       "  'alt': 'T',\n",
       "  'transcript': 'NM_017875.4',\n",
       "  'gene_symbol': 'SLC25A38',\n",
       "  'dbsnp': '121918332',\n",
       "  'gnomad_exomes_af': 1.368150037706073e-06,\n",
       "  'gnomad_exomes_ac': 2.0,\n",
       "  'gnomad_exomes_homalt': 0.0,\n",
       "  'bayesdelnoaf_score': 0.5899999737739563,\n",
       "  'phylop100way_score': 4.756999969482422,\n",
       "  'dbscsnv_ada_score': 0.9972096085548401,\n",
       "  'acmg_score': 8,\n",
       "  'acmg_classification': 'Likely_pathogenic',\n",
       "  'acmg_criteria': 'PVS1_Strong,PM2_Supporting,PP3,PP5_Moderate',\n",
       "  'clinvar_disease': 'Sideroblastic anemia 2',\n",
       "  'clinvar_classification': 'Pathogenic',\n",
       "  'gene_hgnc_id': 26054,\n",
       "  'hgvs_c': 'c.790A>T',\n",
       "  'consequences': 'stop_gained,splice_region_variant'},\n",
       " {'chr': 'M',\n",
       "  'pos': 1243,\n",
       "  'ref': 'T',\n",
       "  'alt': 'C',\n",
       "  'gene_symbol': 'RNR1',\n",
       "  'dbsnp': '28358572',\n",
       "  'gnomad_mito_homoplasmic': 839.0,\n",
       "  'gnomad_mito_heteroplasmic': 1.0,\n",
       "  'phylop100way_score': -1.902999997138977,\n",
       "  'acmg_score': -16,\n",
       "  'acmg_classification': 'Benign',\n",
       "  'acmg_criteria': 'BP6_Very_Strong,BA1',\n",
       "  'clinvar_disease': 'not specified,not provided',\n",
       "  'clinvar_classification': 'Benign'}]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flat = gnb.annotate(parsed, flatten_consequences=True, use_refseq=False, output_format=\"list\")\n",
    "flat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is natural to work with a lists of data in represented as Pandas dataframe. Here is an example of annotating a list of variants to the Pandas dataframe. In the `clingen-erepo.ipynb` example file you can find more examples of annotating and joining variants using Pandas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:you will log in ps209497@gmail.com\n",
      "100%|██████████| 1/1 [00:00<00:00,  3.33it/s]\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>chr</th>\n",
       "      <th>pos</th>\n",
       "      <th>ref</th>\n",
       "      <th>alt</th>\n",
       "      <th>transcript</th>\n",
       "      <th>gene_symbol</th>\n",
       "      <th>dbsnp</th>\n",
       "      <th>gnomad_exomes_af</th>\n",
       "      <th>gnomad_exomes_ac</th>\n",
       "      <th>gnomad_exomes_homalt</th>\n",
       "      <th>...</th>\n",
       "      <th>hgvs_c</th>\n",
       "      <th>consequences</th>\n",
       "      <th>gnomad_genomes_af</th>\n",
       "      <th>gnomad_genomes_ac</th>\n",
       "      <th>gnomad_genomes_homalt</th>\n",
       "      <th>clinvar_disease</th>\n",
       "      <th>clinvar_classification</th>\n",
       "      <th>dbscsnv_ada_score</th>\n",
       "      <th>gnomad_mito_homoplasmic</th>\n",
       "      <th>gnomad_mito_heteroplasmic</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>230710021</td>\n",
       "      <td>G</td>\n",
       "      <td>A</td>\n",
       "      <td>NM_001384479.1</td>\n",
       "      <td>AGT</td>\n",
       "      <td>1228544607</td>\n",
       "      <td>0.000007</td>\n",
       "      <td>10.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>c.803C&gt;T</td>\n",
       "      <td>missense_variant</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22</td>\n",
       "      <td>28695868</td>\n",
       "      <td>AG</td>\n",
       "      <td>A</td>\n",
       "      <td>NM_007194.4</td>\n",
       "      <td>CHEK2</td>\n",
       "      <td>555607708</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>c.1100delC</td>\n",
       "      <td>frameshift_variant</td>\n",
       "      <td>0.00172</td>\n",
       "      <td>262.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Hereditary cancer-predisposing syndrome,Li-Fra...</td>\n",
       "      <td>Conflicting interpretations of pathogenicity</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>39394574</td>\n",
       "      <td>A</td>\n",
       "      <td>T</td>\n",
       "      <td>NM_017875.4</td>\n",
       "      <td>SLC25A38</td>\n",
       "      <td>121918332</td>\n",
       "      <td>0.000001</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>c.790A&gt;T</td>\n",
       "      <td>stop_gained,splice_region_variant</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Sideroblastic anemia 2</td>\n",
       "      <td>Pathogenic</td>\n",
       "      <td>0.99721</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>M</td>\n",
       "      <td>1243</td>\n",
       "      <td>T</td>\n",
       "      <td>C</td>\n",
       "      <td>NaN</td>\n",
       "      <td>RNR1</td>\n",
       "      <td>28358572</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>not specified,not provided</td>\n",
       "      <td>Benign</td>\n",
       "      <td>NaN</td>\n",
       "      <td>839.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  chr        pos ref alt      transcript gene_symbol       dbsnp  \\\n",
       "0   1  230710021   G   A  NM_001384479.1         AGT  1228544607   \n",
       "1  22   28695868  AG   A     NM_007194.4       CHEK2   555607708   \n",
       "2   3   39394574   A   T     NM_017875.4    SLC25A38   121918332   \n",
       "3   M       1243   T   C             NaN        RNR1    28358572   \n",
       "\n",
       "   gnomad_exomes_af  gnomad_exomes_ac  gnomad_exomes_homalt  ...      hgvs_c  \\\n",
       "0          0.000007              10.0                   0.0  ...    c.803C>T   \n",
       "1               NaN               NaN                   NaN  ...  c.1100delC   \n",
       "2          0.000001               2.0                   0.0  ...    c.790A>T   \n",
       "3               NaN               NaN                   NaN  ...         NaN   \n",
       "\n",
       "                        consequences  gnomad_genomes_af  gnomad_genomes_ac  \\\n",
       "0                   missense_variant                NaN                NaN   \n",
       "1                 frameshift_variant            0.00172              262.0   \n",
       "2  stop_gained,splice_region_variant                NaN                NaN   \n",
       "3                                NaN                NaN                NaN   \n",
       "\n",
       "   gnomad_genomes_homalt                                    clinvar_disease  \\\n",
       "0                    NaN                                                NaN   \n",
       "1                    0.0  Hereditary cancer-predisposing syndrome,Li-Fra...   \n",
       "2                    NaN                             Sideroblastic anemia 2   \n",
       "3                    NaN                         not specified,not provided   \n",
       "\n",
       "                         clinvar_classification  dbscsnv_ada_score  \\\n",
       "0                                           NaN                NaN   \n",
       "1  Conflicting interpretations of pathogenicity                NaN   \n",
       "2                                    Pathogenic            0.99721   \n",
       "3                                        Benign                NaN   \n",
       "\n",
       "  gnomad_mito_homoplasmic gnomad_mito_heteroplasmic  \n",
       "0                     NaN                       NaN  \n",
       "1                     NaN                       NaN  \n",
       "2                     NaN                       NaN  \n",
       "3                   839.0                       1.0  \n",
       "\n",
       "[4 rows x 28 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = gnb.annotate(parsed, flatten_consequences=True, use_ensembl=False, output_format=\"dataframe\")\n",
    "df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "bio2",
   "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.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
