{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This notebook is used to find a network from a list of nodes in Translator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('../src')\n",
    "import TCT as TCT\n",
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "98\n",
      "['Automat-ctd(Trapi v1.4.0)', 'Automat-sri-reference-kg(Trapi v1.4.0)', 'Autonomous Relay System (ARS) TRAPI', 'BioLink API', 'BioThings AGR API', 'BioThings BioPlanet Pathway-Gene API', 'BioThings DDInter API', 'BioThings Explorer (BTE) TRAPI', 'BioThings FooDB API', 'BioThings FoodData Central API', 'BioThings GO Biological Process API', 'BioThings InnateDB API', 'BioThings RARe-SOURCE API', 'BioThings repoDB API', 'Biolink Lookup', 'Biothings Therapeutic Target Database API', 'COHD TRAPI', 'Complex Portal Web Service', 'Curated Query Service', 'EBI Proteins API', 'Gene-List Network Enrichment Analysis', 'Knowledge Collaboratory API', 'LitVar API', 'RaMP API v1.0.1', 'SmartAPI API', 'Sri-answer-appraiser(Trapi v1.4.0)', 'Sri-name-resolver', 'Sri-node-normalizer(Trapi v1.3.0)', 'Sri-node-normalizer(Trapi v1.4.0)', 'Translator Annotation Service', 'Workflow-runner(Trapi v1.4.0)', 'imProving Agent for TRAPI 1.4', 'Aragorn(Trapi v1.4.0)', 'ARAX Translator Reasoner - TRAPI 1.4.0', 'RTX KG2 - TRAPI 1.4.0', 'SPOKE KP for TRAPI 1.4', 'Multiomics BigGIM-DrugResponse KP API', 'Multiomics ClinicalTrials KP', 'Multiomics Wellness KP API', 'Multiomics EHR Risk KP API', 'Biothings Explorer (BTE)', 'Service Provider TRAPI', 'Explanatory-agent', 'MolePro', 'Genetics KP', 'medikanren-unsecret', 'Text Mined Cooccurrence API', 'OpenPredict API', 'Agrkb(Trapi v1.4.0)', 'Automat-biolink(Trapi v1.4.0)', 'Automat-cam-kp(Trapi v1.4.0)', 'Automat-drug-central(Trapi v1.4.0)', 'Automat-gtex(Trapi v1.4.0)', 'Automat-gtopdb(Trapi v1.4.0)', 'Automat-gwas-catalog(Trapi v1.4.0)', 'Automat-hetio(Trapi v1.4.0)', 'Automat-hgnc(Trapi v1.4.0)', 'Automat-hmdb(Trapi v1.4.0)', 'Automat-human-goa(Trapi v1.4.0)', 'Automat-icees-kg(Trapi v1.4.0)', 'Automat-intact(Trapi v1.4.0)', 'Automat-panther(Trapi v1.4.0)', 'Automat-pharos(Trapi v1.4.0)', 'Automat-robokop(Trapi v1.4.0)', 'Automat-sri-reference-kp(Trapi v1.4.0)', 'Automat-string-db(Trapi v1.4.0)', 'Automat-ubergraph(Trapi v1.4.0)', 'Automat-ubergraph-nonredundant(Trapi v1.4.0)', 'Automat-viral-proteome(Trapi v1.4.0)', 'CTD API', 'Connections Hypothesis Provider API', 'MyGene.info API', 'MyDisease.info API', 'MyChem.info API', 'MyVariant.info API', 'Ontology Lookup Service API', 'PharmGKB REST API', 'QuickGO API', 'Text Mining Targeted Association API', 'BioThings BindingDB API', 'BioThings BioPlanet Pathway-Disease API', 'BioThings DDinter API', 'BioThings DGIdb API', 'BioThings DISEASES API', 'BioThings EBIgene2phenotype API', 'BioThings Biological Process API', 'BioThings GO Cellular Component API', 'BioThings GO Molecular Function API', 'BioThings GTRx API', 'BioThings HPO API', 'BioThings IDISK API', 'BioThings MGIgene2phenotype API', 'BioThings PFOCR API', 'Biothings RARe-SOURCE API', 'BioThings Rhea API', 'BioThings SEMMEDDB API', 'BioThings SuppKG API', 'BioThings UBERON API']\n"
     ]
    }
   ],
   "source": [
    "# Step1: List all the APIs in the translator system\n",
    "APInames = TCT.list_Translator_APIs()\n",
    "print(len(APInames))\n",
    "print(list(APInames.keys()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "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']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_node_list = [TCT.get_curie('NPM1'), TCT.get_curie('FLT3'), TCT.get_curie('NRAS'), TCT.get_curie('BCL2')]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_node1_category = ['biolink:Gene'] # Node: this has to be in a format of biolink:xxx\n",
    "input_node2_category = ['biolink:Gene']\n",
    "sele_predicates = list(set(TCT.select_concept(sub_list=input_node1_category,obj_list=input_node2_category,metaKG=metaKG)))\n",
    "sele_APIs = TCT.select_API(sub_list=input_node1_category,obj_list=input_node2_category,metaKG=metaKG)\n",
    "API_URLs = TCT.get_Translator_API_URL(sele_APIs, APInames)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "query_json = TCT.format_query_json(input_node_list,  # a list of identifiers for input node1\n",
    "                                   [],  # 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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'' generated an exception: Invalid URL '': No scheme supplied. Perhaps you meant https://?\n",
      "'' generated an exception: Invalid URL '': No scheme supplied. Perhaps you meant https://?\n",
      "'' generated an exception: Invalid URL '': No scheme supplied. Perhaps you meant https://?\n",
      "'' generated an exception: Invalid URL '': No scheme supplied. Perhaps you meant https://?\n",
      "'' generated an exception: Invalid URL '': No scheme supplied. Perhaps you meant https://?\n",
      "'' generated an exception: Invalid URL '': No scheme supplied. Perhaps you meant https://?\n",
      "Warning Code:404:https://api.bte.ncats.io/v1/bde72db681ec0b8f9eeb67bb6b8dd72c/query\n",
      "Warning Code:404:https://automat.ci.transltr.io/ubergraph-nonredundant/1.4/query\n",
      "Warning Code:405:https://automat.ci.transltr.io/cam-kp/1.4/query?limit=100Warning Code:404:https://automat.ci.transltr.io/sri-reference-kp/1.4/queryWarning Code:405:https://automat.ci.transltr.io/hetio/1.4/queryWarning Code:503:https://bte.test.transltr.io/v1/smartapi/adf20dd6ff23dfe18e8e012bde686e31/queryWarning Code:405:https://automat.ci.transltr.io/ubergraph/1.4/query\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "Warning Code:404:https://ars-prod.transltr.io/ara-robokop/api/runquery\n",
      "Warning Code:404:https://automat.renci.org/biolink/1.4/queryWarning Code:405:https://automat.renci.org/gtopdb/1.4/query\n",
      "Warning Code:405:https://automat.renci.org/gwas-catalog/1.4/queryWarning Code:405:https://automat.renci.org/hgnc/1.4/query\n",
      "\n",
      "Warning Code:405:https://automat.renci.org/panther/1.4/query\n",
      "\n",
      "Warning Code:405:https://automat.renci.org/hmdb/1.4/query\n",
      "Warning Code:405:https://automat.renci.org/pharos/1.4/query\n",
      "Warning Code:405:https://automat.renci.org/gtex/1.4/query\n",
      "Success!https://automat.transltr.io/genome-alliance/1.4/query\n",
      "Warning Code:500:https://spokekp.transltr.io/api/v1.4/query\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/59dce17363dce279d389100834e43648/query\n",
      "Success!https://translator.broadinstitute.org/molepro/trapi/v1.4/query\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/1d288b3a3caf75d541ffaae3aab386c8/query\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/978fe380a147a8641caf72320862697b/query\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/978fe380a147a8641caf72320862697b/query\n",
      "Success!https://bte.transltr.io/v1/query\n"
     ]
    }
   ],
   "source": [
    "result = TCT.parallel_api_query(API_URLs,query_json=query_json, max_workers=len(API_URLs))\n",
    "pairs_found = TCT.get_pair_annotation(result, input_node_list)\n",
    "edge_list = TCT.parse_pair_annotation(pairs_found,input_node_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "#edge_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1aee130ae4db4d3a9ca2f5cb3ae9ce30",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "CytoscapeWidget(cytoscape_layout={'name': 'cola', 'title': 'Path', 'nodeSpacing': 80, 'edgeLengthVal': 50}, cy…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "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>Subject</th>\n",
       "      <th>Object</th>\n",
       "      <th>Predicates</th>\n",
       "      <th>Subject_name</th>\n",
       "      <th>Object_name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NCBIGene:4869</td>\n",
       "      <td>NCBIGene:3320</td>\n",
       "      <td>associated_with::infores:string</td>\n",
       "      <td>NPM1</td>\n",
       "      <td>HSP90AA1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NCBIGene:4869</td>\n",
       "      <td>NCBIGene:3320</td>\n",
       "      <td>interacts_with::infores:biogrid</td>\n",
       "      <td>NPM1</td>\n",
       "      <td>HSP90AA1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NCBIGene:4869</td>\n",
       "      <td>NCBIGene:3320</td>\n",
       "      <td>physically_interacts_with::infores:biogrid</td>\n",
       "      <td>NPM1</td>\n",
       "      <td>HSP90AA1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NCBIGene:2322</td>\n",
       "      <td>NCBIGene:3320</td>\n",
       "      <td>interacts_with::infores:biogrid</td>\n",
       "      <td>FLT3</td>\n",
       "      <td>HSP90AA1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NCBIGene:2322</td>\n",
       "      <td>NCBIGene:3320</td>\n",
       "      <td>physically_interacts_with::infores:biogrid</td>\n",
       "      <td>FLT3</td>\n",
       "      <td>HSP90AA1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Subject         Object                                  Predicates  \\\n",
       "0  NCBIGene:4869  NCBIGene:3320             associated_with::infores:string   \n",
       "1  NCBIGene:4869  NCBIGene:3320             interacts_with::infores:biogrid   \n",
       "2  NCBIGene:4869  NCBIGene:3320  physically_interacts_with::infores:biogrid   \n",
       "3  NCBIGene:2322  NCBIGene:3320             interacts_with::infores:biogrid   \n",
       "4  NCBIGene:2322  NCBIGene:3320  physically_interacts_with::infores:biogrid   \n",
       "\n",
       "  Subject_name Object_name  \n",
       "0         NPM1    HSP90AA1  \n",
       "1         NPM1    HSP90AA1  \n",
       "2         NPM1    HSP90AA1  \n",
       "3         FLT3    HSP90AA1  \n",
       "4         FLT3    HSP90AA1  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "TCT.visulize_path(TCT.get_curie(\"NPM1\"), \"NCBIGene:3320\", TCT.get_curie(\"FLT3\"), result, result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# results can be visualized in cytoscpe or networkx"
   ]
  }
 ],
 "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.10.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
