{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "603fd2c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install TCT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a8e7dbf",
   "metadata": {},
   "source": [
    "Link to the TCT document: https://ncatstranslator.github.io/Translator_component_toolkit/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "eefc5f7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from TCT import translator_metakg\n",
    "from TCT import translator_kpinfo\n",
    "import matplotlib.pyplot as plt\n",
    "import networkx as nx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "daa95b6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_translator_resources():\n",
    "    \"\"\"\n",
    "    Load the necessary resources for the Translator.\n",
    "    \"\"\"\n",
    "    Translator_KP_info,APInames= translator_kpinfo.get_translator_kp_info()\n",
    "    metaKG = translator_metakg.get_KP_metadata(APInames) \n",
    "    APInames,metaKG = translator_metakg.add_plover_API(APInames, metaKG)\n",
    "    return  APInames, metaKG, Translator_KP_info\n",
    "\n",
    "APInames, metaKG, Translator_KP_info= load_translator_resources()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "de2db3bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "53\n",
      "(10580, 5)\n",
      "(22129, 5)\n",
      "42\n",
      "184\n",
      "108\n",
      "ARA list: {'Workflow-runner(Trapi v1.5.0)', 'Sri-answer-appraiser(Trapi v1.5.0)', 'mediKanren', 'Cqs(Trapi v1.5.0)', 'Autonomous Relay System (ARS) TRAPI', 'OpenPredict API', 'Aragorn(Trapi v1.5.0)', 'Sri-node-normalizer(Trapi v1.5.0)', 'Answer-coalesce(Trapi v1.5.0)', 'ARAX Translator Reasoner - TRAPI 1.5.0'}\n"
     ]
    }
   ],
   "source": [
    "# Preparation \n",
    "# Step1: List all the APIs in the translator system\n",
    "Translator_KP_info,APInames= translator_kpinfo.get_translator_kp_info()\n",
    "print(len(Translator_KP_info))\n",
    "# Step 2: Get metaKG and all predicates from Translator APIs through the SmartAPI system\n",
    "metaKG = translator_metakg.get_KP_metadata(APInames) \n",
    "print(metaKG.shape)\n",
    "# Add metaKG from Plover API based KG resources\n",
    "APInames,metaKG = translator_metakg.add_plover_API(APInames, metaKG)\n",
    "print(metaKG.shape)\n",
    "# Step 3: list metaKG information\n",
    "All_predicates = list(set(metaKG['Predicate']))\n",
    "All_categories = list((set(list(set(metaKG['Subject']))+list(set(metaKG['Object'])))))\n",
    "API_withMetaKG = list(set(metaKG['API']))\n",
    "print(len(API_withMetaKG))\n",
    "print(len(All_predicates))\n",
    "print(len(All_categories))\n",
    "\n",
    "# ARA list\n",
    "API_withMetaKG = set(metaKG['API'])\n",
    "print(\"ARA list:\", set(APInames.keys()) - API_withMetaKG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "18f64e80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'CATRAX BigGIM DrugResponse Performance Phase KP - TRAPI 1.5.0': 'https://multiomics.rtx.ai:9990/BigGIM_DrugResponse_PerformancePhase/query',\n",
       " 'Clinical Trials KP - TRAPI 1.5.0': 'https://multiomics.rtx.ai:9990/ctkp/query',\n",
       " 'imProving Agent for TRAPI 1.5': 'https://ia.transltr.io/api/v1.5/query/',\n",
       " 'mediKanren': 'https://medikanren-trapi.transltr.io/query/',\n",
       " 'Drug Approvals KP - TRAPI 1.5.0': 'https://multiomics.rtx.ai:9990/dakp/query',\n",
       " 'CATRAX Pharmacogenomics KP - TRAPI 1.5.0': 'https://multiomics.rtx.ai:9990/PharmacogenomicsKG/query',\n",
       " 'ARAX Translator Reasoner - TRAPI 1.5.0': 'https://arax.transltr.io/api/arax/v1.4/query/',\n",
       " 'Text Mined Cooccurrence API': 'https://cooccurrence.ci.transltr.io/query/',\n",
       " 'Service Provider TRAPI': 'https://bte.transltr.io/v1/team/Service%20Provider/query/',\n",
       " 'Connections Hypothesis Provider API': 'https://chp-api.transltr.io/query/',\n",
       " 'Gene-List Network Enrichment Analysis': 'https://translator.broadinstitute.org/gelinea-trapi/v1.5/query/',\n",
       " 'COHD TRAPI': 'https://cohd-api.transltr.io/api/query/',\n",
       " 'RTX KG2 - TRAPI 1.5.0': 'https://kg2cploverdb.ci.transltr.io/kg2c/query',\n",
       " 'Automat-cam-kp(Trapi v1.5.0)': 'https://automat.transltr.io/cam-kp/query/',\n",
       " 'Genetics Data Provider for NCATS Biomedical Translator Reasoners': 'https://genetics-kp.transltr.io/genetics_provider/trapi/v1.5/query/',\n",
       " 'MolePro': 'https://molepro-trapi.transltr.io/molepro/trapi/v1.5/query/',\n",
       " 'Microbiome KP - TRAPI 1.5.0': 'https://multiomics.rtx.ai:9990/mbkp/query',\n",
       " 'Multiomics KP - TRAPI 1.5.0': 'https://multiomics.rtx.ai:9990/multiomics/query',\n",
       " 'Automat-intact(Trapi v1.5.0)': 'https://automat.transltr.io/intact/query/',\n",
       " 'Automat-reactome(Trapi v1.5.0)': 'https://automat.transltr.io/reactome/query/',\n",
       " 'BioThings Explorer (BTE) TRAPI': 'https://bte.transltr.io/v1/query/',\n",
       " 'Automat-human-goa(Trapi v1.5.0)': 'https://automat.transltr.io/human-goa/query/',\n",
       " 'SPOKE KP for TRAPI 1.5': 'https://spokekp.transltr.io/api/v1.5/query/',\n",
       " 'Automat-binding-db(Trapi v1.5.0)': 'https://automat.transltr.io/binding-db/query/',\n",
       " 'Automat-ehr-may-treat-kp(Trapi v1.5.0)': 'https://automat.renci.org/ehr-may-treat-kp/query/',\n",
       " 'Knowledge Collaboratory API': 'https://collaboratory-api.transltr.io/query/',\n",
       " 'Aragorn(Trapi v1.5.0)': 'https://aragorn.transltr.io/aragorn/query/',\n",
       " 'Automat-pharos(Trapi v1.5.0)': 'https://automat.transltr.io/pharos/query/',\n",
       " 'Answer-coalesce(Trapi v1.5.0)': 'https://answer-coalesce.transltr.io/query/',\n",
       " 'Sri-answer-appraiser(Trapi v1.5.0)': 'https://answerappraiser.transltr.io/query/',\n",
       " 'Sri-node-normalizer(Trapi v1.5.0)': 'https://nodenorm.transltr.io/1.5/query/',\n",
       " 'Automat-hmdb(Trapi v1.5.0)': 'https://automat.transltr.io/hmdb/query/',\n",
       " 'Cqs(Trapi v1.5.0)': 'https://cqs.ci.transltr.io/query/',\n",
       " 'Automat-hgnc(Trapi v1.5.0)': 'https://automat.transltr.io/hgnc/query/',\n",
       " 'Automat-string-db(Trapi v1.5.0)': 'https://automat.transltr.io/string-db/query/',\n",
       " 'Autonomous Relay System (ARS) TRAPI': 'https://ars-prod.transltr.io/ars/api/submit/',\n",
       " 'OpenPredict API': 'https://openpredict.transltr.io/query/',\n",
       " 'Automat-gwas-catalog(Trapi v1.5.0)': 'https://automat.transltr.io/gwas-catalog/query/',\n",
       " 'Workflow-runner(Trapi v1.5.0)': 'https://translator-workflow-runner.transltr.io/query/',\n",
       " 'Automat-ehr-clinical-connections-kp(Trapi v1.5.0)': 'https://automat.renci.org/ehr-clinical-connections-kp/query/',\n",
       " 'Automat-ctd(Trapi v1.5.0)': 'https://automat.transltr.io/ctd/query/',\n",
       " 'Automat-genome-alliance(Trapi v1.5.0)': 'https://automat.transltr.io/genome-alliance/query/',\n",
       " 'Automat-hetionet(Trapi v1.5.0)': 'https://automat.transltr.io/hetio/query/',\n",
       " 'Automat-robokop(Trapi v1.5.0)': 'https://automat.transltr.io/robokopkg/query/',\n",
       " 'Automat-gtex(Trapi v1.5.0)': 'https://automat.transltr.io/gtex/query/',\n",
       " 'Automat-viral-proteome(Trapi v1.5.0)': 'https://automat.transltr.io/viral-proteome/query/',\n",
       " 'Automat-gtopdb(Trapi v1.5.0)': 'https://automat.transltr.io/gtopdb/query/',\n",
       " 'Automat-drug-central(Trapi v1.5.0)': 'https://automat.transltr.io/drugcentral/query/',\n",
       " 'Automat-panther(Trapi v1.5.0)': 'https://automat.transltr.io/panther/query/',\n",
       " 'Automat-icees-kg(Trapi v1.5.0)': 'https://automat.transltr.io/icees-kg/query/',\n",
       " 'Automat-monarchinitiative(Trapi v1.5.0)': 'https://automat.transltr.io/monarch-kg/query/',\n",
       " 'Automat-ubergraph(Trapi v1.5.0)': 'https://automat.transltr.io/ubergraph/query/'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "APInames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9f441c6d",
   "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>API</th>\n",
       "      <th>Predicate</th>\n",
       "      <th>Subject</th>\n",
       "      <th>Object</th>\n",
       "      <th>URL</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>imProving Agent for TRAPI 1.5</td>\n",
       "      <td>biolink:has_real_world_evidence_of_association...</td>\n",
       "      <td>biolink:ChemicalEntity</td>\n",
       "      <td>biolink:Disease</td>\n",
       "      <td>https://ia.transltr.io/api/v1.5/query/</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>imProving Agent for TRAPI 1.5</td>\n",
       "      <td>biolink:entity_regulates_entity</td>\n",
       "      <td>biolink:ChemicalEntity</td>\n",
       "      <td>biolink:Gene</td>\n",
       "      <td>https://ia.transltr.io/api/v1.5/query/</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>imProving Agent for TRAPI 1.5</td>\n",
       "      <td>biolink:has_side_effect</td>\n",
       "      <td>biolink:ChemicalEntity</td>\n",
       "      <td>biolink:PhenotypicFeature</td>\n",
       "      <td>https://ia.transltr.io/api/v1.5/query/</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>imProving Agent for TRAPI 1.5</td>\n",
       "      <td>biolink:has_real_world_evidence_of_association...</td>\n",
       "      <td>biolink:Disease</td>\n",
       "      <td>biolink:Disease</td>\n",
       "      <td>https://ia.transltr.io/api/v1.5/query/</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>imProving Agent for TRAPI 1.5</td>\n",
       "      <td>biolink:has_food_component</td>\n",
       "      <td>biolink:Food</td>\n",
       "      <td>biolink:ChemicalEntity</td>\n",
       "      <td>https://ia.transltr.io/api/v1.5/query/</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21946</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:NamedThing</td>\n",
       "      <td>biolink:Disease</td>\n",
       "      <td>https://kg2cploverdb.ci.transltr.io/kg2c/query</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21947</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:NamedThing</td>\n",
       "      <td>biolink:PhenotypicFeature</td>\n",
       "      <td>https://kg2cploverdb.ci.transltr.io/kg2c/query</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21948</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:NamedThing</td>\n",
       "      <td>biolink:BehavioralFeature</td>\n",
       "      <td>https://kg2cploverdb.ci.transltr.io/kg2c/query</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21949</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:ChemicalEntity</td>\n",
       "      <td>biolink:BehavioralFeature</td>\n",
       "      <td>https://kg2cploverdb.ci.transltr.io/kg2c/query</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21950</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:MolecularMixture</td>\n",
       "      <td>biolink:BehavioralFeature</td>\n",
       "      <td>https://kg2cploverdb.ci.transltr.io/kg2c/query</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>21951 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 API  \\\n",
       "0      imProving Agent for TRAPI 1.5   \n",
       "1      imProving Agent for TRAPI 1.5   \n",
       "2      imProving Agent for TRAPI 1.5   \n",
       "3      imProving Agent for TRAPI 1.5   \n",
       "4      imProving Agent for TRAPI 1.5   \n",
       "...                              ...   \n",
       "21946          RTX KG2 - TRAPI 1.5.0   \n",
       "21947          RTX KG2 - TRAPI 1.5.0   \n",
       "21948          RTX KG2 - TRAPI 1.5.0   \n",
       "21949          RTX KG2 - TRAPI 1.5.0   \n",
       "21950          RTX KG2 - TRAPI 1.5.0   \n",
       "\n",
       "                                               Predicate  \\\n",
       "0      biolink:has_real_world_evidence_of_association...   \n",
       "1                        biolink:entity_regulates_entity   \n",
       "2                                biolink:has_side_effect   \n",
       "3      biolink:has_real_world_evidence_of_association...   \n",
       "4                             biolink:has_food_component   \n",
       "...                                                  ...   \n",
       "21946                           biolink:applied_to_treat   \n",
       "21947                           biolink:applied_to_treat   \n",
       "21948                           biolink:applied_to_treat   \n",
       "21949                           biolink:applied_to_treat   \n",
       "21950                           biolink:applied_to_treat   \n",
       "\n",
       "                        Subject                     Object  \\\n",
       "0        biolink:ChemicalEntity            biolink:Disease   \n",
       "1        biolink:ChemicalEntity               biolink:Gene   \n",
       "2        biolink:ChemicalEntity  biolink:PhenotypicFeature   \n",
       "3               biolink:Disease            biolink:Disease   \n",
       "4                  biolink:Food     biolink:ChemicalEntity   \n",
       "...                         ...                        ...   \n",
       "21946        biolink:NamedThing            biolink:Disease   \n",
       "21947        biolink:NamedThing  biolink:PhenotypicFeature   \n",
       "21948        biolink:NamedThing  biolink:BehavioralFeature   \n",
       "21949    biolink:ChemicalEntity  biolink:BehavioralFeature   \n",
       "21950  biolink:MolecularMixture  biolink:BehavioralFeature   \n",
       "\n",
       "                                                  URL  \n",
       "0              https://ia.transltr.io/api/v1.5/query/  \n",
       "1              https://ia.transltr.io/api/v1.5/query/  \n",
       "2              https://ia.transltr.io/api/v1.5/query/  \n",
       "3              https://ia.transltr.io/api/v1.5/query/  \n",
       "4              https://ia.transltr.io/api/v1.5/query/  \n",
       "...                                               ...  \n",
       "21946  https://kg2cploverdb.ci.transltr.io/kg2c/query  \n",
       "21947  https://kg2cploverdb.ci.transltr.io/kg2c/query  \n",
       "21948  https://kg2cploverdb.ci.transltr.io/kg2c/query  \n",
       "21949  https://kg2cploverdb.ci.transltr.io/kg2c/query  \n",
       "21950  https://kg2cploverdb.ci.transltr.io/kg2c/query  \n",
       "\n",
       "[21951 rows x 5 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metaKG"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f172549",
   "metadata": {},
   "source": [
    "Understand the metaKG in biolink: https://biolink.github.io/biolink-model/categories.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "75292f59",
   "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>API</th>\n",
       "      <th>Predicate</th>\n",
       "      <th>Subject</th>\n",
       "      <th>Object</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>10675</th>\n",
       "      <td>Drug Approvals KP - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "      <td>biolink:Disease</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10676</th>\n",
       "      <td>Drug Approvals KP - TRAPI 1.5.0</td>\n",
       "      <td>biolink:treats</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "      <td>biolink:Disease</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10677</th>\n",
       "      <td>Drug Approvals KP - TRAPI 1.5.0</td>\n",
       "      <td>biolink:treats</td>\n",
       "      <td>biolink:ChemicalEntity</td>\n",
       "      <td>biolink:Disease</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10678</th>\n",
       "      <td>Drug Approvals KP - TRAPI 1.5.0</td>\n",
       "      <td>biolink:treats</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "      <td>biolink:PhenotypicFeature</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10679</th>\n",
       "      <td>Drug Approvals KP - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:ChemicalEntity</td>\n",
       "      <td>biolink:PhenotypicFeature</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10680</th>\n",
       "      <td>Drug Approvals KP - TRAPI 1.5.0</td>\n",
       "      <td>biolink:treats</td>\n",
       "      <td>biolink:ChemicalEntity</td>\n",
       "      <td>biolink:PhenotypicFeature</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10681</th>\n",
       "      <td>Drug Approvals KP - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:ChemicalEntity</td>\n",
       "      <td>biolink:Disease</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10682</th>\n",
       "      <td>Drug Approvals KP - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "      <td>biolink:PhenotypicFeature</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10683</th>\n",
       "      <td>Drug Approvals KP - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:MolecularMixture</td>\n",
       "      <td>biolink:Disease</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10684</th>\n",
       "      <td>Drug Approvals KP - TRAPI 1.5.0</td>\n",
       "      <td>biolink:treats</td>\n",
       "      <td>biolink:MolecularMixture</td>\n",
       "      <td>biolink:Disease</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10685</th>\n",
       "      <td>Drug Approvals KP - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:MolecularMixture</td>\n",
       "      <td>biolink:PhenotypicFeature</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10686</th>\n",
       "      <td>Drug Approvals KP - TRAPI 1.5.0</td>\n",
       "      <td>biolink:treats</td>\n",
       "      <td>biolink:MolecularMixture</td>\n",
       "      <td>biolink:PhenotypicFeature</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   API                 Predicate  \\\n",
       "10675  Drug Approvals KP - TRAPI 1.5.0  biolink:applied_to_treat   \n",
       "10676  Drug Approvals KP - TRAPI 1.5.0            biolink:treats   \n",
       "10677  Drug Approvals KP - TRAPI 1.5.0            biolink:treats   \n",
       "10678  Drug Approvals KP - TRAPI 1.5.0            biolink:treats   \n",
       "10679  Drug Approvals KP - TRAPI 1.5.0  biolink:applied_to_treat   \n",
       "10680  Drug Approvals KP - TRAPI 1.5.0            biolink:treats   \n",
       "10681  Drug Approvals KP - TRAPI 1.5.0  biolink:applied_to_treat   \n",
       "10682  Drug Approvals KP - TRAPI 1.5.0  biolink:applied_to_treat   \n",
       "10683  Drug Approvals KP - TRAPI 1.5.0  biolink:applied_to_treat   \n",
       "10684  Drug Approvals KP - TRAPI 1.5.0            biolink:treats   \n",
       "10685  Drug Approvals KP - TRAPI 1.5.0  biolink:applied_to_treat   \n",
       "10686  Drug Approvals KP - TRAPI 1.5.0            biolink:treats   \n",
       "\n",
       "                        Subject                     Object  \n",
       "10675     biolink:SmallMolecule            biolink:Disease  \n",
       "10676     biolink:SmallMolecule            biolink:Disease  \n",
       "10677    biolink:ChemicalEntity            biolink:Disease  \n",
       "10678     biolink:SmallMolecule  biolink:PhenotypicFeature  \n",
       "10679    biolink:ChemicalEntity  biolink:PhenotypicFeature  \n",
       "10680    biolink:ChemicalEntity  biolink:PhenotypicFeature  \n",
       "10681    biolink:ChemicalEntity            biolink:Disease  \n",
       "10682     biolink:SmallMolecule  biolink:PhenotypicFeature  \n",
       "10683  biolink:MolecularMixture            biolink:Disease  \n",
       "10684  biolink:MolecularMixture            biolink:Disease  \n",
       "10685  biolink:MolecularMixture  biolink:PhenotypicFeature  \n",
       "10686  biolink:MolecularMixture  biolink:PhenotypicFeature  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# find the KG in one individual API (optional)\n",
    "metaKG.loc[metaKG['API'] == 'Drug Approvals KP - TRAPI 1.5.0',['API','Predicate','Subject','Object']].drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "dc735638",
   "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>API</th>\n",
       "      <th>Predicate</th>\n",
       "      <th>Subject</th>\n",
       "      <th>Object</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2970</th>\n",
       "      <td>MolePro</td>\n",
       "      <td>biolink:location_of</td>\n",
       "      <td>biolink:GrossAnatomicalStructure</td>\n",
       "      <td>biolink:MolecularMixture</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2971</th>\n",
       "      <td>MolePro</td>\n",
       "      <td>biolink:has_side_effect</td>\n",
       "      <td>biolink:MolecularMixture</td>\n",
       "      <td>biolink:Behavior</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2972</th>\n",
       "      <td>MolePro</td>\n",
       "      <td>biolink:is_active_ingredient_of</td>\n",
       "      <td>biolink:Protein</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2973</th>\n",
       "      <td>MolePro</td>\n",
       "      <td>biolink:affected_by</td>\n",
       "      <td>biolink:MacromolecularComplexMixin</td>\n",
       "      <td>biolink:ChemicalEntity</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2974</th>\n",
       "      <td>MolePro</td>\n",
       "      <td>biolink:is_assessed_by</td>\n",
       "      <td>biolink:ProteinComplex</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3793</th>\n",
       "      <td>MolePro</td>\n",
       "      <td>biolink:affects</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "      <td>biolink:Gene</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3794</th>\n",
       "      <td>MolePro</td>\n",
       "      <td>biolink:affects</td>\n",
       "      <td>biolink:Gene</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3795</th>\n",
       "      <td>MolePro</td>\n",
       "      <td>biolink:interacts_with</td>\n",
       "      <td>biolink:Gene</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3796</th>\n",
       "      <td>MolePro</td>\n",
       "      <td>biolink:affected_by</td>\n",
       "      <td>biolink:Gene</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3797</th>\n",
       "      <td>MolePro</td>\n",
       "      <td>biolink:correlated_with</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>828 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          API                        Predicate  \\\n",
       "2970  MolePro              biolink:location_of   \n",
       "2971  MolePro          biolink:has_side_effect   \n",
       "2972  MolePro  biolink:is_active_ingredient_of   \n",
       "2973  MolePro              biolink:affected_by   \n",
       "2974  MolePro           biolink:is_assessed_by   \n",
       "...       ...                              ...   \n",
       "3793  MolePro                  biolink:affects   \n",
       "3794  MolePro                  biolink:affects   \n",
       "3795  MolePro           biolink:interacts_with   \n",
       "3796  MolePro              biolink:affected_by   \n",
       "3797  MolePro          biolink:correlated_with   \n",
       "\n",
       "                                 Subject                    Object  \n",
       "2970    biolink:GrossAnatomicalStructure  biolink:MolecularMixture  \n",
       "2971            biolink:MolecularMixture          biolink:Behavior  \n",
       "2972                     biolink:Protein     biolink:SmallMolecule  \n",
       "2973  biolink:MacromolecularComplexMixin    biolink:ChemicalEntity  \n",
       "2974              biolink:ProteinComplex     biolink:SmallMolecule  \n",
       "...                                  ...                       ...  \n",
       "3793               biolink:SmallMolecule              biolink:Gene  \n",
       "3794                        biolink:Gene     biolink:SmallMolecule  \n",
       "3795                        biolink:Gene     biolink:SmallMolecule  \n",
       "3796                        biolink:Gene     biolink:SmallMolecule  \n",
       "3797               biolink:SmallMolecule     biolink:SmallMolecule  \n",
       "\n",
       "[828 rows x 4 columns]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# find the KG in one individual API (optional)\n",
    "metaKG.loc[metaKG['API'] == 'MolePro',['API','Predicate','Subject','Object']].drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fcd343ec",
   "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>API</th>\n",
       "      <th>Predicate</th>\n",
       "      <th>Subject</th>\n",
       "      <th>Object</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>11130</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:close_match</td>\n",
       "      <td>biolink:Drug</td>\n",
       "      <td>biolink:MolecularMixture</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11131</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:close_match</td>\n",
       "      <td>biolink:Drug</td>\n",
       "      <td>biolink:ChemicalEntity</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11132</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:close_match</td>\n",
       "      <td>biolink:Drug</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11133</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:close_match</td>\n",
       "      <td>biolink:Drug</td>\n",
       "      <td>biolink:Drug</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11134</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:close_match</td>\n",
       "      <td>biolink:SmallMolecule</td>\n",
       "      <td>biolink:Drug</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21946</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:NamedThing</td>\n",
       "      <td>biolink:Disease</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21947</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:NamedThing</td>\n",
       "      <td>biolink:PhenotypicFeature</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21948</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:NamedThing</td>\n",
       "      <td>biolink:BehavioralFeature</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21949</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:ChemicalEntity</td>\n",
       "      <td>biolink:BehavioralFeature</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21950</th>\n",
       "      <td>RTX KG2 - TRAPI 1.5.0</td>\n",
       "      <td>biolink:applied_to_treat</td>\n",
       "      <td>biolink:MolecularMixture</td>\n",
       "      <td>biolink:BehavioralFeature</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10821 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                         API                 Predicate  \\\n",
       "11130  RTX KG2 - TRAPI 1.5.0       biolink:close_match   \n",
       "11131  RTX KG2 - TRAPI 1.5.0       biolink:close_match   \n",
       "11132  RTX KG2 - TRAPI 1.5.0       biolink:close_match   \n",
       "11133  RTX KG2 - TRAPI 1.5.0       biolink:close_match   \n",
       "11134  RTX KG2 - TRAPI 1.5.0       biolink:close_match   \n",
       "...                      ...                       ...   \n",
       "21946  RTX KG2 - TRAPI 1.5.0  biolink:applied_to_treat   \n",
       "21947  RTX KG2 - TRAPI 1.5.0  biolink:applied_to_treat   \n",
       "21948  RTX KG2 - TRAPI 1.5.0  biolink:applied_to_treat   \n",
       "21949  RTX KG2 - TRAPI 1.5.0  biolink:applied_to_treat   \n",
       "21950  RTX KG2 - TRAPI 1.5.0  biolink:applied_to_treat   \n",
       "\n",
       "                        Subject                     Object  \n",
       "11130              biolink:Drug   biolink:MolecularMixture  \n",
       "11131              biolink:Drug     biolink:ChemicalEntity  \n",
       "11132              biolink:Drug      biolink:SmallMolecule  \n",
       "11133              biolink:Drug               biolink:Drug  \n",
       "11134     biolink:SmallMolecule               biolink:Drug  \n",
       "...                         ...                        ...  \n",
       "21946        biolink:NamedThing            biolink:Disease  \n",
       "21947        biolink:NamedThing  biolink:PhenotypicFeature  \n",
       "21948        biolink:NamedThing  biolink:BehavioralFeature  \n",
       "21949    biolink:ChemicalEntity  biolink:BehavioralFeature  \n",
       "21950  biolink:MolecularMixture  biolink:BehavioralFeature  \n",
       "\n",
       "[10821 rows x 4 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# find the KG in one individual API (optional)\n",
    "metaKG.loc[metaKG['API'] == 'RTX KG2 - TRAPI 1.5.0',['API','Predicate','Subject','Object']].drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "aa556389",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 107056 characters]",
      "text/plain": [
       "<Figure size 1200x1200 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# draw the interaction graph between subject and object in the metaKG using networkx\n",
    "# first, we need to drop the interactions between subjects and objects that both subjects and objects are the same\n",
    "#metaKG = metaKG[metaKG['Subject'] != metaKG['Object']]\n",
    "# second, we need to filter the metaKG to only include the selected categories\n",
    "#metaKG = metaKG[metaKG['Subject'].isin(selected_categories) | metaKG['Object'].isin(selected_categories)]\n",
    "selected_KGs = ['Drug Approvals KP - TRAPI 1.5.0'\n",
    "                ]\n",
    "\n",
    "metaKG_sele = metaKG[metaKG['API'].isin(selected_KGs)]\n",
    "\n",
    "# build a multigraph to capture all edges (including duplicates) and their predicates\n",
    "G = nx.MultiGraph()\n",
    "for _, row in metaKG_sele.iterrows():\n",
    "        G.add_edge(row['Subject'], row['Object'], predicate=row['Predicate'])\n",
    "\n",
    "# layout and draw nodes + edges\n",
    "plt.figure(figsize=(12, 12))\n",
    "pos = nx.spring_layout(G, k=0.5, iterations=20)\n",
    "nx.draw(G, pos,\n",
    "                with_labels=True,\n",
    "                node_size=50,\n",
    "                font_size=12,\n",
    "                font_color='black',\n",
    "                node_color='blue',\n",
    "                edge_color='gray')\n",
    "\n",
    "# draw edge labels\n",
    "edge_labels = nx.get_edge_attributes(G, 'predicate')\n",
    "nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)\n",
    "plt.title('Interaction Graph of Subjects and Objects in metaKG')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "3.12.1",
   "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.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
