{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## This notebook can be used to rank a list of nodes from a category that connect to an entity such as a gene. \n",
    "#### Example 1: Which disease are associate with NPM1 gene?\n",
    "#### Example 2: Which proteins interacts with protein NPM1?\n",
    "#### Example 3: Which drugs or small molecules can interact with NPM1?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('../TCT')\n",
    "import TCT as TCT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Sri-name-resolver',\n",
       " 'Monarch API',\n",
       " 'Complex Portal Web Service',\n",
       " 'Sri-answer-appraiser(Trapi v1.5.0)',\n",
       " 'LitVar API',\n",
       " 'CTD API',\n",
       " 'EBI Proteins API',\n",
       " 'Workflow-runner(Trapi v1.5.0)',\n",
       " 'Ontology Lookup Service API',\n",
       " 'Cqs(Trapi v1.5.0)',\n",
       " 'Automat-monarchinitiative(Trapi v1.5.0)',\n",
       " 'QuickGO API',\n",
       " 'RaMP API v1.0.1',\n",
       " 'Automat-genome-alliance(Trapi v1.5.0)',\n",
       " 'Connections Hypothesis Provider API',\n",
       " 'Automat-gwas-catalog(Trapi v1.5.0)',\n",
       " 'mediKanren',\n",
       " 'Automat-hmdb(Trapi v1.5.0)',\n",
       " 'Automat-robokop(Trapi v1.5.0)',\n",
       " 'Automat-hgnc(Trapi v1.5.0)',\n",
       " 'Sri-node-normalizer(Trapi v1.5.0)',\n",
       " 'Automat-binding-db(Trapi v1.5.0)',\n",
       " 'Automat-gtex(Trapi v1.5.0)',\n",
       " 'Automat-reactome(Trapi v1.5.0)',\n",
       " 'Automat-ehr-may-treat-kp(Trapi v1.5.0)',\n",
       " 'Automat-gtopdb(Trapi v1.5.0)',\n",
       " 'Autonomous Relay System (ARS) TRAPI',\n",
       " 'Automat-human-goa(Trapi v1.5.0)',\n",
       " 'Automat-pharos(Trapi v1.5.0)',\n",
       " 'Automat-cam-kp(Trapi v1.5.0)',\n",
       " 'Automat-viral-proteome(Trapi v1.5.0)',\n",
       " 'Automat-string-db(Trapi v1.5.0)',\n",
       " 'Automat-drug-central(Trapi v1.5.0)',\n",
       " 'Aragorn(Trapi v1.5.0)',\n",
       " 'Automat-ubergraph(Trapi v1.5.0)',\n",
       " 'Automat-hetionet(Trapi v1.5.0)',\n",
       " 'Automat-ctd(Trapi v1.5.0)',\n",
       " 'Automat-icees-kg(Trapi v1.5.0)',\n",
       " 'Automat-panther(Trapi v1.5.0)',\n",
       " 'Automat-intact(Trapi v1.5.0)',\n",
       " 'Automat-ehr-clinical-connections-kp(Trapi v1.5.0)',\n",
       " 'Biolink Lookup',\n",
       " 'COHD TRAPI',\n",
       " 'Text Mined Cooccurrence API',\n",
       " 'SmartAPI API',\n",
       " 'BioThings Rhea API',\n",
       " 'Text Mined Cooccurrence API',\n",
       " 'MyVariant.info API',\n",
       " 'MyDisease.info API',\n",
       " 'BioThings UBERON API',\n",
       " 'MyGene.info API',\n",
       " 'Answer-coalesce(Trapi v1.5.0)',\n",
       " 'OpenPredict API',\n",
       " 'BioThings HPO API',\n",
       " 'Drug Approvals KP - TRAPI 1.5.0',\n",
       " 'Gene-List Network Enrichment Analysis',\n",
       " 'MolePro',\n",
       " 'Multiomics KP - TRAPI 1.5.0',\n",
       " 'Microbiome KP - TRAPI 1.5.0',\n",
       " 'BioThings RARe-SOURCE API',\n",
       " 'BioThings GO Biological Process API',\n",
       " 'imProving Agent for TRAPI 1.5',\n",
       " 'Clinical Trials KP - TRAPI 1.5.0',\n",
       " 'BioThings EBIgene2phenotype API',\n",
       " 'BioThings DDInter API',\n",
       " 'MyChem.info API',\n",
       " 'BioThings BioPlanet Pathway-Disease API',\n",
       " 'BioThings BioPlanet Pathway-Gene API',\n",
       " 'BioThings MGIgene2phenotype API',\n",
       " 'BioThings FooDB API',\n",
       " 'BioThings PFOCR API',\n",
       " 'BioThings BindingDB API',\n",
       " 'PharmGKB REST API',\n",
       " 'BioThings DISEASES API',\n",
       " 'BioThings GO Molecular Function API',\n",
       " 'BioThings GO Cellular Component API',\n",
       " 'Genetics Data Provider for NCATS Biomedical Translator Reasoners',\n",
       " 'SPOKE KP for TRAPI 1.5',\n",
       " 'BioThings IDISK API',\n",
       " 'Translator Annotation Service',\n",
       " 'BioThings repoDB API',\n",
       " 'BioThings InnateDB API',\n",
       " 'BioThings FoodData Central API',\n",
       " 'BioThings AGR API',\n",
       " 'BioThings GTRx API',\n",
       " 'BioThings Explorer (BTE) TRAPI',\n",
       " 'Knowledge Collaboratory API',\n",
       " 'Service Provider TRAPI',\n",
       " 'RTX KG2 - TRAPI 1.5.0',\n",
       " 'BioThings SuppKG API',\n",
       " 'Multiomics Wellness KP API',\n",
       " 'BioThings DGIdb API',\n",
       " 'Multiomics EHR Risk KP API',\n",
       " 'BioThings SEMMEDDB API',\n",
       " 'Multiomics BigGIM-DrugResponse KP API',\n",
       " 'Biothings Therapeutic Target Database API',\n",
       " 'Text Mining Targeted Association API',\n",
       " 'ARAX Translator Reasoner - TRAPI 1.5.0']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Step1: List all the APIs in the translator system\n",
    "APInames = TCT.list_Translator_APIs()\n",
    "APInames_current = TCT.get_Translator_APIs()\n",
    "#set(APInames_current)-set(APInames)\n",
    "APInames_current\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['API', 'KG_category', 'Subject', 'Object', 'URL'], dtype='object')\n",
      "(9288, 5)\n"
     ]
    }
   ],
   "source": [
    "# Step 2: Get metaKG and all predicates from Translator APIs\n",
    "metaKG = TCT.get_KP_metadata(APInames) # This only applies to the Translator APIs\n",
    "print(metaKG.columns)\n",
    "print(metaKG.shape)\n",
    "\n",
    "#metaKG.to_csv('../metaData/metaKG.csv', index=False)\n",
    "#metaKG = pd.read_csv('../metaData/metaKG.csv')\n",
    "All_predicates = list(set(metaKG['KG_category']))\n",
    "All_categories = list((set(list(set(metaKG['Subject']))+list(set(metaKG['Object'])))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NCBIGene:4869\n",
      "all relevant predicates in Translator:\n",
      "['biolink:has_part', 'biolink:directly_physically_interacts_with', 'biolink:regulates', 'biolink:genetically_associated_with', 'biolink:coexists_with', 'biolink:produced_by', 'biolink:positively_correlated_with', 'biolink:physically_interacts_with', 'biolink:affects', 'biolink:correlated_with', 'biolink:regulated_by', 'biolink:genetically_interacts_with', 'biolink:derives_from', 'biolink:affected_by', 'biolink:part_of', 'biolink:close_match', 'biolink:associated_with', 'biolink:derives_into', 'biolink:homologous_to', 'biolink:associated_with_resistance_to', 'biolink:related_to', 'biolink:negatively_correlated_with', 'biolink:associated_with_sensitivity_to', 'biolink:produces', 'biolink:interacts_with']\n",
      "all relevant APIs in Translator:\n",
      "['BioThings Explorer (BTE) TRAPI', 'Text Mining Targeted Association API', 'PharmGKB REST API', 'Biothings Explorer (BTE)', 'Connections Hypothesis Provider API', 'MolePro', 'BioThings SEMMEDDB API', 'Multiomics BigGIM-DrugResponse KP API', 'Service Provider TRAPI', 'MyGene.info API']\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "# Step 3: set input parameters\n",
    "# Test multiomics BigGIM Drug Response KP\n",
    "# Node1 for query\n",
    "input_node1 = 'NPM1'\n",
    "input_node1_id = TCT.get_curie(input_node1)\n",
    "print(input_node1_id)\n",
    "input_node1_list = [input_node1_id]\n",
    "input_node1_category = ['biolink:Gene'] # Node: this has to be in a format of biolink:xxx\n",
    "\n",
    "#Node2 for query\n",
    "input_node2_list = []\n",
    "#input_node2_category = ['biolink:Drug', 'biolink:SmallMolecule', 'biolink:ChemicalSubstance']\n",
    "input_node2_category = ['biolink:Gene']\n",
    "\n",
    "# Get all predicates for the input node1 and node2, user can furter select the predicates among this list\n",
    "sele_predicates = list(set(TCT.select_concept(sub_list=input_node1_category,\n",
    "                                              obj_list=input_node2_category,\n",
    "                                              metaKG=metaKG)))\n",
    "print(\"all relevant predicates in Translator:\")\n",
    "print(sele_predicates)\n",
    "# select predicates\n",
    "\n",
    "# Get all APIs for the input node1 and node2, user can furter select the APIs among this list\n",
    "sele_APIs = TCT.select_API(sub_list=input_node1_category,\n",
    "                           obj_list=input_node2_category,\n",
    "                           metaKG=metaKG)\n",
    "\n",
    "print(\"all relevant APIs in Translator:\")\n",
    "print(sele_APIs)\n",
    "print(len(sele_APIs))\n",
    "\n",
    "# get API URLs\n",
    "API_URLs = TCT.get_Translator_API_URL(sele_APIs, \n",
    "                                      APInames)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'https://api.bte.ncats.io/v1/smartapi/978fe380a147a8641caf72320862697b/query' generated an exception: HTTPSConnectionPool(host='api.bte.ncats.io', port=443): Max retries exceeded with url: /v1/smartapi/978fe380a147a8641caf72320862697b/query (Caused by SSLError(SSLCertVerificationError(1, \"[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for 'api.bte.ncats.io'. (_ssl.c:1000)\")))\n",
      "'https://api.bte.ncats.io/v1/smartapi/978fe380a147a8641caf72320862697b/query' generated an exception: HTTPSConnectionPool(host='api.bte.ncats.io', port=443): Max retries exceeded with url: /v1/smartapi/978fe380a147a8641caf72320862697b/query (Caused by SSLError(SSLCertVerificationError(1, \"[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for 'api.bte.ncats.io'. (_ssl.c:1000)\")))\n",
      "'https://api.bte.ncats.io/v1/bde72db681ec0b8f9eeb67bb6b8dd72c/query' generated an exception: HTTPSConnectionPool(host='api.bte.ncats.io', port=443): Max retries exceeded with url: /v1/bde72db681ec0b8f9eeb67bb6b8dd72c/query (Caused by SSLError(SSLCertVerificationError(1, \"[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for 'api.bte.ncats.io'. (_ssl.c:1000)\")))\n",
      "'https://api.bte.ncats.io/v1/smartapi/1d288b3a3caf75d541ffaae3aab386c8/query' generated an exception: HTTPSConnectionPool(host='api.bte.ncats.io', port=443): Max retries exceeded with url: /v1/smartapi/1d288b3a3caf75d541ffaae3aab386c8/query (Caused by SSLError(SSLCertVerificationError(1, \"[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for 'api.bte.ncats.io'. (_ssl.c:1000)\")))\n",
      "'https://api.bte.ncats.io/v1/smartapi/59dce17363dce279d389100834e43648/query' generated an exception: HTTPSConnectionPool(host='api.bte.ncats.io', port=443): Max retries exceeded with url: /v1/smartapi/59dce17363dce279d389100834e43648/query (Caused by SSLError(SSLCertVerificationError(1, \"[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for 'api.bte.ncats.io'. (_ssl.c:1000)\")))\n",
      "Warning Code:502:https://translator.broadinstitute.org/molepro/trapi/v1.4/query\n",
      "Success!https://chp-api.transltr.io/query\n",
      "Success!https://bte.transltr.io/v1/smartapi/adf20dd6ff23dfe18e8e012bde686e31/query\n",
      "Success!https://bte.transltr.io/v1/query\n",
      "Success!https://bte.transltr.io/v1/query\n",
      "NCBIGene:6052: no preferred name\n"
     ]
    }
   ],
   "source": [
    "# Step 4: Format query json\n",
    "query_json = TCT.format_query_json(input_node1_list,  # a list of identifiers for input node1\n",
    "                                   input_node2_list,  # it can be empty list if only want to query node1\n",
    "                                   input_node1_category,  # a list of categories of input node1\n",
    "                                   input_node2_category,  # a list of categories of input node2\n",
    "                                   sele_predicates) # a list of predicates\n",
    "\n",
    "# Step 5: Query Translator APIs and parse results\n",
    "result = TCT.parallel_api_query(API_URLs,query_json=query_json, max_workers=len(API_URLs))\n",
    "\n",
    "# Step 6: Parse results\n",
    "result_parsed = TCT.parse_KG(result)\n",
    "\n",
    "# Step 7: Ranking the results. This ranking method is based on the number of unique primary infores. It can only be used to rank the results with one defined node.\n",
    "result_ranked_by_primary_infores = TCT.rank_by_primary_infores(result_parsed, input_node1_id)   # input_node1_id is the curie id of the input node, such as \"NCBIGene:1017\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'message': {'query_graph': {'nodes': {'n0': {'ids': ['NCBIGene:4869'], 'categories': ['biolink:Gene']}, 'n1': {'categories': ['biolink:Gene']}}, 'edges': {'e1': {'subject': 'n0', 'object': 'n1', 'predicates': ['biolink:physically_interacts_with', 'biolink:has_part', 'biolink:genetic_association', 'biolink:regulated_by', 'biolink:correlated_with', 'biolink:homologous_to', 'biolink:coexpressed_with', 'biolink:negatively_correlated_with', 'biolink:orthologous_to', 'biolink:genetically_associated_with', 'biolink:binds', 'biolink:derives_from', 'biolink:affects', 'biolink:has_input', 'biolink:subclass_of', 'biolink:affected_by', 'biolink:part_of', 'biolink:coexists_with', 'biolink:biomarker_for', 'biolink:related_to', 'biolink:contributes_to', 'biolink:interacts_with', 'biolink:regulates', 'biolink:produced_by', 'biolink:derives_into', 'biolink:close_match', 'biolink:produces', 'biolink:genetically_interacts_with', 'biolink:directly_physically_interacts_with', 'biolink:has_phenotype', 'biolink:positively_correlated_with', 'biolink:causes']}}}}}\n"
     ]
    }
   ],
   "source": [
    "print(query_json)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NCBIGene:6052:no preferred name\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:title={'center':'Ranking of one-hop nodes by predicate'}, ylabel='Predicate'>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 236228 characters]",
      "text/plain": [
       "<Figure size 2040x1050 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 235524 characters]",
      "text/plain": [
       "<Figure size 2040x1050 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Step 8: Visualize the results\n",
    "TCT.visulization_one_hop_ranking(result_ranked_by_primary_infores, result_parsed, \n",
    "                                num_of_nodes = 30, input_query = input_node1_id, \n",
    "                                fontsize = 10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# End of the example"
   ]
  }
 ],
 "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": 4
}
