{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Key steps:\n",
    "### 1. Convert a user's question into a TRAPI json using a query template (LLM facilitated)\n",
    "### 2. Validate the TRAPI json format\n",
    "### 3. Refine the TRAPI json format by selecting the similar categories and predicates  (LLM facilitated) and user's selection\n",
    "### 4. ID formating\n",
    "### 5. Query, rank, and visualization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "![image.png](../Figures/LLM_TRAPI_converting2.png)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('../TCT')\n",
    "import TCT as TCT\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import openai\n",
    "import json\n",
    "import ipywidgets as widgets\n",
    "from IPython.display import display\n",
    "\n",
    "\n",
    "confi = json.loads(open(\"../../openAPI_key.json\").read()) # replace with your api key\n",
    "openai.organization = confi['Organization']\n",
    "openai.api_key = confi['API_key']\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "69\n",
      "{'message': {'query_graph': {'nodes': {'n0': {'ids': [], 'categories': ['biolink:category']}, 'n1': {'categories': ['biolink:category']}}, 'edges': {'e1': {'subject': 'n0', 'object': 'n1', 'predicates': ['biolink:predicates']}}}}}\n"
     ]
    }
   ],
   "source": [
    "# Step1: List all the APIs in the translator system\n",
    "APInames = TCT.list_Translator_APIs()\n",
    "print(len(APInames))\n",
    "#print(APInames.keys())\n",
    "\n",
    "# Step 2: Get metaKG 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",
    "\n",
    "KG_category = list(set( list(metaKG['Subject'].unique()) + list(metaKG['Object'].unique())))\n",
    "KG_predicates = list(metaKG['KG_category'].unique())\n",
    "\n",
    "\n",
    "metaKG_triplets = metaKG[['Subject', 'KG_category', 'Object']]\n",
    "metaKG_triplets_dic = metaKG_triplets.to_dict('records')\n",
    "\n",
    "# Step 3: Load the query template\n",
    "query_json_temp = TCT.load_json_template()\n",
    "query_json = str(query_json_temp)\n",
    "print(query_json)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convert_Question2Query(question):\n",
    "    # formatting query text\n",
    "    input_text = \"We know the avaiable predicates in the KG are: \" + ','.join(list(set(KG_predicates))) + \". We also know the avaiable categories in the KGs are \"+ ','.join(list(set(KG_category))) +\". We also know a TRAPI message temple is \" +query_json+ \". With the question of \" + question + \" What is the json format of message to represent this question? The following rules for the output: 1) The result must be just a json format with the same structure with template; 2)categories should be replaced from the categories in the KG; 3)predicts can be replaced from the predicates in the KG; 4) can use the name to fill the ids; 4)the output must start with '{' and end with '}', and be a standard json format.  At least one ids should be given and No annotations are needed!\"\n",
    "    query_json_cur = TCT.ask_chatGPT4(input_text)\n",
    "    query_json_cur_clean = TCT.extract_json(query_json_cur)\n",
    "    return query_json_cur_clean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example questions\n",
    "question = \"What genes or proteins interact with KRAS?\"  #successful running\n",
    "#question2 = \"What drugs may treat Type 2 diabetes?\" #successful running\n",
    "#question3 = \"what disease we can consider to treat with drug Olaparib?\" #successful running\n",
    "#question4 = \"What could be potential targets for ovarian cancer?\" \n",
    "#question5 = \"What are the drugs or small moleculaes that target the gene KRAS?\" #successful running\n",
    "#question6 = \"What diseases co-occurence with covid-19?\"  #successful running\n",
    "#question7 = \"What symptoms are associated with long covid?\" # no results\n",
    "#question8 = \"What genes are associated with aptosis?\"  # successful running\n",
    "#question9 = \"What drug increase the risk of liver cancer?\" # not successful, may because of the direction of the edge\n",
    "#question10 = \"which drugs are in clinical trial for liver cancer?\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ASK a question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "#question = \"What genes or proteins interact with KRAS?\"  #successful running\n",
    "#question = \"What drugs may treat Type 2 diabetes?\"  #successful running\n",
    "#question = \"What are the drugs or small moleculaes that target  KRAS?\"  #successful running\n",
    "#question = \"What diseases co-occurence with covid-19?\"  #successful running\n",
    "question = \"What drug increase the risk of liver cancer?\"  # successful running\n",
    "#question = \"What drugs may treat acute myeloid leukemia?\" # successful running\n",
    "#question = \"What symptoms are associated with long covid?\" # no results\n",
    "#question = \"what genes mutations are relevant to Myelodysplastic Syndrome?\"\n",
    "#question = \"What class of drugs can be used to treat depression?\"\n",
    "#question = \"Which drug used for treating disease X has the fewest side effects?\"\n",
    "#question = \"Which drugs can be used to treat both neurological and cardiovascular diseases (those are categories)?\"\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Initial query converting\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'message': {'query_graph': {'nodes': {'n0': {'ids': ['liver cancer'],\n",
       "     'categories': ['biolink:Disease']},\n",
       "    'n1': {'categories': ['biolink:Drug']}},\n",
       "   'edges': {'e1': {'subject': 'n0',\n",
       "     'object': 'n1',\n",
       "     'predicates': ['biolink:increases_response_to']}}}}}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_json_cur_clean = convert_Question2Query(question)\n",
    "query_json_cur_clean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reverse_nodes(query_json):\n",
    "    orginal_subject = query_json['message']['query_graph']['nodes']['n0']\n",
    "    orginal_object = query_json['message']['query_graph']['nodes']['n1']\n",
    "    temp_subject = orginal_subject\n",
    "    temp_object = orginal_object\n",
    "\n",
    "    query_json['message']['query_graph']['nodes']['n0'] = temp_object\n",
    "    query_json['message']['query_graph']['nodes']['n1'] = temp_subject\n",
    "    return query_json\n",
    "\n",
    "def check_json(query_json):\n",
    "    Label = False\n",
    "    subject_category = []\n",
    "    object_category = []\n",
    "    predicate = []\n",
    "    triple_pass = 0\n",
    "    if 'message' in query_json:\n",
    "        if 'query_graph' in query_json['message']:\n",
    "            if 'nodes' in query_json['message']['query_graph']:\n",
    "                if 'n0' in query_json['message']['query_graph']['nodes']:\n",
    "                    if 'categories' in query_json['message']['query_graph']['nodes']['n0']:\n",
    "                        if len(set(query_json['message']['query_graph']['nodes']['n0']['categories']).intersection(set(list(set(metaKG['Subject']))+list(set(metaKG['Object']))))) > 0:\n",
    "                            subject_category = list(set(query_json['message']['query_graph']['nodes']['n0']['categories']).intersection(set(list(set(metaKG['Subject']))+list(set(metaKG['Object'])))))\n",
    "                        else:\n",
    "                            Label = False\n",
    "                    else:\n",
    "                        Label = False\n",
    "\n",
    "                \n",
    "                if 'n1' in query_json['message']['query_graph']['nodes']:\n",
    "                    if 'categories' in query_json['message']['query_graph']['nodes']['n1']:\n",
    "                        if len(set(query_json['message']['query_graph']['nodes']['n1']['categories']).intersection(set(list(set(metaKG['Subject']))+list(set(metaKG['Object']))))) > 0:\n",
    "                            object_category = list(set(query_json['message']['query_graph']['nodes']['n1']['categories']).intersection(set(list(set(metaKG['Subject']))+list(set(metaKG['Object'])))))\n",
    "                        else:\n",
    "                            Label = False\n",
    "                    else:\n",
    "                        Label = False\n",
    "                        \n",
    "                    \n",
    "            if 'edges' in query_json['message']['query_graph']:\n",
    "                if 'e1' in query_json['message']['query_graph']['edges']:\n",
    "                    if 'predicates' in query_json['message']['query_graph']['edges']['e1']:\n",
    "                        if len(set(query_json['message']['query_graph']['edges']['e1']['predicates']).intersection(set(list(set(metaKG['KG_category']))))) > 0:\n",
    "                            predicate = list(set(query_json['message']['query_graph']['edges']['e1']['predicates']).intersection(set(list(set(metaKG['KG_category'])))))\n",
    "                        else:\n",
    "                            Label = False\n",
    "                    else:\n",
    "                        Label = False\n",
    "    if (len(subject_category)>0) & (len(object_category)>0) & (len(predicate)>0):\n",
    "        for subject_cur_category in subject_category:\n",
    "            for object_cur_category in object_category:\n",
    "                for predicate_cur in predicate:\n",
    "                    if {'Subject': subject_cur_category,\n",
    "                        'KG_category': predicate_cur,\n",
    "                        'Object': object_cur_category} in metaKG_triplets_dic:\n",
    "                        triple_pass += 1\n",
    "                    \n",
    "                    if {'Subject': object_cur_category,\n",
    "                        'KG_category': predicate_cur,\n",
    "                        'Object': subject_cur_category} in metaKG_triplets_dic:\n",
    "                        triple_pass += 1\n",
    "                        query_json = reverse_nodes(query_json)\n",
    "                        print(\"The reverse direction of the edge is  correct! Subject and Object are reversed!\")\n",
    "    if triple_pass > 0:\n",
    "        Label = True\n",
    "\n",
    "                                            \n",
    "    if Label == True:\n",
    "        print(\"The json format is correct!\")\n",
    "    else:\n",
    "        print(\"!!Incorrect input file format\")\n",
    "    return(query_json)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "!!Incorrect input file format\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'message': {'query_graph': {'nodes': {'n0': {'ids': ['liver cancer'],\n",
       "     'categories': ['biolink:Disease']},\n",
       "    'n1': {'categories': ['biolink:Drug']}},\n",
       "   'edges': {'e1': {'subject': 'n0',\n",
       "     'object': 'n1',\n",
       "     'predicates': ['biolink:increases_response_to']}}}}}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "check_json(query_json_cur_clean)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Refine the query?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ed2e5292d43b4310823d1e85183fb7e7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "RadioButtons(description='switch subject and object?', index=1, options=('Yes', 'No'), value='No')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "switch_subject_object = widgets.RadioButtons( options=['Yes', 'No'], \n",
    "                                       value='No', \n",
    "                                       description='switch subject and object?', disabled=False)\n",
    "display(switch_subject_object)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'message': {'query_graph': {'nodes': {'n0': {'categories': ['biolink:Drug']}, 'n1': {'ids': ['Disease X'], 'categories': ['biolink:Disease']}}, 'edges': {'e1': {'subject': 'n0', 'object': 'n1', 'predicates': ['biolink:treats']}}}}}\n"
     ]
    }
   ],
   "source": [
    "if switch_subject_object.value == 'Yes':\n",
    "    query_json_cur_clean = reverse_nodes(query_json_cur_clean)\n",
    "\n",
    "print(query_json_cur_clean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ccf0aebff69a4a96bf5897e174adcaf3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "RadioButtons(description='Refine category?', index=1, options=('Yes', 'No'), value='No')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# add a widget to ask whether a user would like to refine the category or predicate\n",
    "refine_category = widgets.RadioButtons( options=['Yes', 'No'], \n",
    "                                       value='No', \n",
    "                                       description='Refine category?', disabled=False)\n",
    "display(refine_category)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "if refine_category.value == 'Yes':\n",
    "\n",
    "    similar_category = TCT.get_similar_category(query_json_cur_clean, KG_category)\n",
    "    #print(TCT.find_similar_category(query_json_cur_clean, KG_category))\n",
    "\n",
    "    # add a widget to select the category for n0\n",
    "    \n",
    "    print(query_json_cur_clean)\n",
    "    category_n1 = widgets.SelectMultiple(\n",
    "            options=similar_category,\n",
    "            value=[],\n",
    "            #rows=10,\n",
    "            description='Node 0',\n",
    "            disabled=False,\n",
    "            layout=widgets.Layout(width='80%', height='300px')\n",
    "        )\n",
    "    display(category_n1)\n",
    "\n",
    "    # add a widget to select the category for n1\n",
    "    category_n2 = widgets.SelectMultiple(\n",
    "        options=similar_category,\n",
    "        value=[],\n",
    "        #rows=10,\n",
    "        description='Node 1',\n",
    "        disabled=False,\n",
    "        layout=widgets.Layout(width='80%', height='300px')\n",
    "    )\n",
    "    display(category_n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'message': {'query_graph': {'nodes': {'n0': {'categories': ['biolink:Drug']}, 'n1': {'ids': ['Disease X'], 'categories': ['biolink:Disease']}}, 'edges': {'e1': {'subject': 'n0', 'object': 'n1', 'predicates': ['biolink:treats']}}}}}\n"
     ]
    }
   ],
   "source": [
    "# update categories\n",
    "if refine_category.value == 'Yes':\n",
    "    if len(category_n1.value) > 0:\n",
    "        print(\"updated node 1!\")\n",
    "        query_json_cur_clean['message']['query_graph']['nodes']['n0']['categories'] = list(category_n1.value)\n",
    "    if len(category_n2.value) > 0:\n",
    "        print(\"updated node 2!\")\n",
    "        query_json_cur_clean['message']['query_graph']['nodes']['n1']['categories'] = list(category_n2.value)\n",
    "print(query_json_cur_clean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "520afecb594542f69a00a96981430ed3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "RadioButtons(description='Refine predicates?', index=1, options=('Yes', 'No'), value='No')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# add a widget to ask whether a user would like to refine the category or predicate\n",
    "refine_predicates = widgets.RadioButtons( options=['Yes', 'No'], \n",
    "                                       value='No', \n",
    "                                       description='Refine predicates?', disabled=False)\n",
    "display(refine_predicates)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "if refine_predicates.value == 'Yes':\n",
    "    print(question)\n",
    "    print(query_json_cur_clean)\n",
    "    similar_predicate = TCT.get_similar_predicate(query_json_cur_clean, All_predicates)\n",
    "    #print(TCT.find_similar_predicate(query_json_cur_clean, All_predicates))\n",
    "\n",
    "    # add a widget to select the category for n0\n",
    "    predicate_e01 = widgets.SelectMultiple(\n",
    "            options=similar_predicate,\n",
    "            value=[],\n",
    "            #rows=10,\n",
    "            description='Predicates',\n",
    "            disabled=False,\n",
    "            layout=widgets.Layout(width='80%', height='300px')\n",
    "        )\n",
    "    display(predicate_e01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# update predicates\n",
    "if refine_predicates.value == 'Yes':\n",
    "    query_json_cur_clean['message']['query_graph']['edges']['e1']['predicates'] = list(predicate_e01.value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Validate the format of the json\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The current json format is: \n",
      "{'message': {'query_graph': {'nodes': {'n0': {'categories': ['biolink:Drug']}, 'n1': {'ids': ['Disease X'], 'categories': ['biolink:Disease']}}, 'edges': {'e1': {'subject': 'n0', 'object': 'n1', 'predicates': ['biolink:treats']}}}}}\n",
      "Predicates ok!\n",
      "node0 category OK!\n",
      "node1 category OK!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Validate the format of the json\n",
    "print(\"The current json format is: \")\n",
    "print(query_json_cur_clean)\n",
    "TCT.TRAPI_json_validation(query_json_cur_clean, All_predicates, KG_category)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['MONDO:0018881']\n",
      "{'message': {'query_graph': {'nodes': {'n0': {'categories': ['biolink:Gene']}, 'n1': {'ids': ['MONDO:0018881'], 'categories': ['biolink:Disease']}}, 'edges': {'e1': {'subject': 'n0', 'object': 'n1', 'predicates': ['biolink:related_to']}}}}}\n"
     ]
    }
   ],
   "source": [
    "# id formatting\n",
    "query_json_cur_clean = TCT.format_id(query_json_cur_clean)\n",
    "print(query_json_cur_clean)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Query the Translator system\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "all relevant APIs in Translator:\n",
      "['Service Provider TRAPI', 'BioThings SEMMEDDB API', 'MyVariant.info API', 'MolePro', 'BioThings EBIgene2phenotype API', 'Automat-ctd(Trapi v1.4.0)', 'Automat-ubergraph(Trapi v1.4.0)', 'BioThings DISEASES API', 'Multiomics BigGIM-DrugResponse KP API', 'BioThings PFOCR API', 'Genetics KP', 'Automat-hetio(Trapi v1.4.0)', 'BioThings Explorer (BTE) TRAPI', 'Biothings Therapeutic Target Database API', 'Biothings Explorer (BTE)', 'MyGene.info API', 'PharmGKB REST API', 'MyDisease.info API', 'BioThings MGIgene2phenotype API', 'Automat-robokop(Trapi v1.4.0)', 'Biothings RARe-SOURCE API', 'CTD API', 'SPOKE KP for TRAPI 1.4', 'Automat-pharos(Trapi v1.4.0)', 'Automat-icees-kg(Trapi v1.4.0)']\n",
      "25\n"
     ]
    }
   ],
   "source": [
    "# Step Select APIs\n",
    "input_node1_category = query_json_cur_clean['message']['query_graph']['nodes']['n0']['categories']\n",
    "input_node2_category = query_json_cur_clean['message']['query_graph']['nodes']['n1']['categories']\n",
    "\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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Still need to revise id?\n",
    "#query_json_cur_clean['message']['query_graph']['nodes']['n0']['ids'] = ['MONDO:0008170']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['https://api.bte.ncats.io/v1/smartapi/978fe380a147a8641caf72320862697b/query', 'https://api.bte.ncats.io/v1/smartapi/1d288b3a3caf75d541ffaae3aab386c8/query', 'https://api.bte.ncats.io/v1/59dce17363dce279d389100834e43648/query', 'https://translator.broadinstitute.org/molepro/trapi/v1.4/query', 'https://api.bte.ncats.io/v1/smartapi/1f47552dabd67351d4c625adb0a10d00/query', 'https://automat.transltr.io/ctd/1.4/query', 'https://automat.ci.transltr.io/ubergraph/1.4/query', 'https://api.bte.ncats.io/v1/smartapi/a7f784626a426d054885a5f33f17d3f8/query', 'https://bte.test.transltr.io/v1/smartapi/adf20dd6ff23dfe18e8e012bde686e31/query', 'https://api.bte.ncats.io/v1/smartapi/edeb26858bd27d0322af93e7a9e08761/query', 'https://genetics-kp.transltr.io/genetics_provider/trapi/v1.4/query', 'https://automat.ci.transltr.io/hetio/1.4/query', 'https://bte.transltr.io/v1/query', 'https://biothings.ncats.io/ttd/query', 'https://bte.transltr.io/v1/query', 'https://api.bte.ncats.io/v1/smartapi/59dce17363dce279d389100834e43648/query', 'https://api.bte.ncats.io/v1/bde72db681ec0b8f9eeb67bb6b8dd72c/query', 'https://api.bte.ncats.io/v1/smartapi/671b45c0301c8624abbd26ae78449ca2/query', 'https://api.bte.ncats.io/v1/smartapi/77ed27f111262d0289ed4f4071faa619/query', 'https://ars-prod.transltr.io/ara-robokop/api/runquery', 'https://api.bte.ncats.io/v1/smartapi/b772ebfbfa536bba37764d7fddb11d6f/query', 'https://automat.ci.transltr.io/ctd/1.4/query', 'https://spokekp.transltr.io/api/v1.4/query', 'https://automat.renci.org/pharos/1.4/query', 'https://automat.renci.org/icees-kg/1.4/query']\n",
      "Warning Code:404:https://api.bte.ncats.io/v1/bde72db681ec0b8f9eeb67bb6b8dd72c/query\n",
      "Warning Code:404:https://api.bte.ncats.io/v1/59dce17363dce279d389100834e43648/query\n",
      "Warning Code:405:https://automat.renci.org/pharos/1.4/queryWarning Code:405:https://automat.renci.org/icees-kg/1.4/query\n",
      "\n",
      "Warning Code:404:https://ars-prod.transltr.io/ara-robokop/api/runquery\n",
      "Warning Code:400:https://biothings.ncats.io/ttd/query\n",
      "Warning Code:405:https://automat.ci.transltr.io/ubergraph/1.4/queryWarning Code:405:https://automat.ci.transltr.io/ctd/1.4/query\n",
      "\n",
      "Warning Code:405:https://automat.ci.transltr.io/hetio/1.4/query\n",
      "Warning Code:500:https://spokekp.transltr.io/api/v1.4/query\n",
      "Success!https://genetics-kp.transltr.io/genetics_provider/trapi/v1.4/query\n",
      "Success!https://automat.transltr.io/ctd/1.4/query\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/978fe380a147a8641caf72320862697b/query\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/59dce17363dce279d389100834e43648/querySuccess!https://bte.test.transltr.io/v1/smartapi/adf20dd6ff23dfe18e8e012bde686e31/query\n",
      "\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/1f47552dabd67351d4c625adb0a10d00/query\n",
      "Success!https://translator.broadinstitute.org/molepro/trapi/v1.4/query\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/a7f784626a426d054885a5f33f17d3f8/query\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/77ed27f111262d0289ed4f4071faa619/query\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/edeb26858bd27d0322af93e7a9e08761/query\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/671b45c0301c8624abbd26ae78449ca2/query\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/b772ebfbfa536bba37764d7fddb11d6f/query\n",
      "Success!https://api.bte.ncats.io/v1/smartapi/1d288b3a3caf75d541ffaae3aab386c8/query\n",
      "Success!https://bte.transltr.io/v1/query\n",
      "Success!https://bte.transltr.io/v1/query\n"
     ]
    }
   ],
   "source": [
    "# get API URLs\n",
    "API_URLs = TCT.get_Translator_API_URL(sele_APIs, \n",
    "                                      APInames)\n",
    "\n",
    "print(API_URLs)\n",
    "# Step 5: Query Translator APIs and parse results\n",
    "result = TCT.parallel_api_query(API_URLs,query_json=query_json_cur_clean, max_workers=len(API_URLs))\n",
    "\n",
    "# Step 6: Parse results\n",
    "result_parsed = TCT.parse_KG(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'message': {'query_graph': {'nodes': {'n0': {'categories': ['biolink:Gene']},\n",
       "    'n1': {'ids': ['MONDO:0018881'], 'categories': ['biolink:Disease']}},\n",
       "   'edges': {'e1': {'subject': 'n0',\n",
       "     'object': 'n1',\n",
       "     'predicates': ['biolink:related_to']}}}}}"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_json_cur_clean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ENSEMBL:ENSP00000478289: no preferred name\n",
      "NCBIGene:8163: no preferred name\n",
      "NodeNorm does not know about these identifiers: ENSEMBL:ENSP00000273158,ENSEMBL:ENSP00000363683,ENSEMBL:hsa-miR-608,ENSEMBL:hsa-miR-612,ENSEMBL:hsa-miR-145-5p,ENSEMBL:hsa-miR-146a-5p,MGI:4357685,MGI:4357687,MGI:5558102,MGI:6315192,NCBIGene:100862683,NCBIGene:619465,NCBIGene:100616101,NCBIGene:8213,NCBIGene:100616102,NCBIGene:100775105,NCBIGene:100616103,NCBIGene:28874,NCBIGene:105376906,NCBIGene:50951,NCBIGene:100862684,NCBIGene:10306,NCBIGene:266710,NCBIGene:7808,NCBIGene:252842,NCBIGene:3342,NCBIGene:100124696,NCBIGene:100310785,NCBIGene:470,NCBIGene:260431,NCBIGene:7361,NCBIGene:50947,NCBIGene:112577459,RGD:9167298,RGD:9404643,RGD:6487887,WB:WBGene00001249,WB:WBGene00003220,WB:WBGene00003842,WB:WBGene00003843,WB:WBGene00003909,WB:WBGene00004037,WB:WBGene00004704,WB:WBGene00003930,WB:WBGene00006618,WB:WBGene00008119,WB:WBGene00018734,WB:WBGene00000201,WB:WBGene00001773,WB:WBGene00004187,WB:WBGene00001679,WB:WBGene00001749,WB:WBGene00001753,WB:WBGene00001758,WB:WBGene00001759,WB:WBGene00001771,WB:WBGene00001789,WB:WBGene00002335,WB:WBGene00004701,WB:WBGene00000366,WB:WBGene00000517,WB:WBGene00007772,WB:WBGene00013258,WB:WBGene00018384,WB:WBGene00018911,WB:WBGene00021566,WB:WBGene00021752,WB:WBGene00043705,WB:WBGene00006510,WB:WBGene00010317,WB:WBGene00010882,WB:WBGene00011605,WB:WBGene00017601,WB:WBGene00003480,WB:WBGene00004483\n"
     ]
    }
   ],
   "source": [
    "# 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",
    "if 'ids' in query_json_cur_clean['message']['query_graph']['nodes']['n0']:\n",
    "    input_node1_id = query_json_cur_clean['message']['query_graph']['nodes']['n0']['ids'][0]\n",
    "elif 'ids' in query_json_cur_clean['message']['query_graph']['nodes']['n1']:\n",
    "    input_node1_id = query_json_cur_clean['message']['query_graph']['nodes']['n1']['ids'][0]\n",
    "else:\n",
    "    input_node1_id = None\n",
    "\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": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1647, 5)\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>output_node</th>\n",
       "      <th>Name</th>\n",
       "      <th>Num_of_primary_infores</th>\n",
       "      <th>type_of_nodes</th>\n",
       "      <th>unique_predicates</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NCBIGene:171023</td>\n",
       "      <td>ASXL1</td>\n",
       "      <td>9</td>\n",
       "      <td>subject</td>\n",
       "      <td>[biolink:gene_associated_with_condition, bioli...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NCBIGene:23451</td>\n",
       "      <td>SF3B1</td>\n",
       "      <td>9</td>\n",
       "      <td>subject</td>\n",
       "      <td>[biolink:gene_associated_with_condition, bioli...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NCBIGene:54790</td>\n",
       "      <td>TET2</td>\n",
       "      <td>9</td>\n",
       "      <td>subject</td>\n",
       "      <td>[biolink:gene_associated_with_condition, bioli...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NCBIGene:2624</td>\n",
       "      <td>GATA2</td>\n",
       "      <td>8</td>\n",
       "      <td>subject</td>\n",
       "      <td>[biolink:gene_associated_with_condition, bioli...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>262</th>\n",
       "      <td>NCBIGene:1788</td>\n",
       "      <td>DNMT3A</td>\n",
       "      <td>7</td>\n",
       "      <td>subject</td>\n",
       "      <td>[biolink:occurs_together_in_literature_with, b...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         output_node    Name  Num_of_primary_infores type_of_nodes  \\\n",
       "0    NCBIGene:171023   ASXL1                       9       subject   \n",
       "3     NCBIGene:23451   SF3B1                       9       subject   \n",
       "4     NCBIGene:54790    TET2                       9       subject   \n",
       "1      NCBIGene:2624   GATA2                       8       subject   \n",
       "262    NCBIGene:1788  DNMT3A                       7       subject   \n",
       "\n",
       "                                     unique_predicates  \n",
       "0    [biolink:gene_associated_with_condition, bioli...  \n",
       "3    [biolink:gene_associated_with_condition, bioli...  \n",
       "4    [biolink:gene_associated_with_condition, bioli...  \n",
       "1    [biolink:gene_associated_with_condition, bioli...  \n",
       "262  [biolink:occurs_together_in_literature_with, b...  "
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# print results \n",
    "print(result_ranked_by_primary_infores.shape)\n",
    "result_ranked_by_primary_infores.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NCBIGene:8163: no preferred name\n",
      "ENSEMBL:ENSP00000478289: no preferred name\n",
      "NodeNorm does not know about these identifiers: NCBIGene:470,NCBIGene:7361,NCBIGene:260431,WB:WBGene00017601,WB:WBGene00011605,WB:WBGene00010882,WB:WBGene00010317,WB:WBGene00006510,WB:WBGene00043705,WB:WBGene00021752,WB:WBGene00004483,WB:WBGene00003480,WB:WBGene00021566,WB:WBGene00018911,WB:WBGene00018384,WB:WBGene00013258,WB:WBGene00003220,WB:WBGene00001249,WB:WBGene00003842,WB:WBGene00003843,WB:WBGene00003909,WB:WBGene00001753,WB:WBGene00007772,WB:WBGene00000517,WB:WBGene00000366,WB:WBGene00004701,WB:WBGene00002335,WB:WBGene00001789,WB:WBGene00001771,WB:WBGene00001759,WB:WBGene00001758,WB:WBGene00001749,WB:WBGene00004037,WB:WBGene00001679,WB:WBGene00004187,WB:WBGene00001773,WB:WBGene00000201,WB:WBGene00018734,WB:WBGene00008119,WB:WBGene00006618,WB:WBGene00003930,WB:WBGene00004704,NCBIGene:112577459,NCBIGene:50947,RGD:6487887,RGD:9404643,RGD:9167298,NCBIGene:100775105,NCBIGene:100616102,NCBIGene:8213,NCBIGene:100616101,NCBIGene:619465,NCBIGene:100862683,NCBIGene:100862684,NCBIGene:50951,NCBIGene:105376906,NCBIGene:28874,NCBIGene:100616103,NCBIGene:7808,NCBIGene:266710,NCBIGene:10306,ENSEMBL:ENSP00000273158,ENSEMBL:ENSP00000363683,ENSEMBL:hsa-miR-608,ENSEMBL:hsa-miR-612,ENSEMBL:hsa-miR-145-5p,MGI:6315192,MGI:5558102,MGI:4357687,MGI:4357685,ENSEMBL:hsa-miR-146a-5p,NCBIGene:100310785,NCBIGene:100124696,NCBIGene:252842,NCBIGene:3342\n",
      "[Text(0.5, 0, 'ASXL1'), Text(2.5, 0, 'TET2'), Text(4.5, 0, 'DNMT3A'), Text(6.5, 0, 'NRAS'), Text(8.5, 0, 'U2AF1'), Text(10.5, 0, 'GATA1'), Text(12.5, 0, 'RUNX1'), Text(14.5, 0, 'CDKN2B'), Text(16.5, 0, 'SRSF2'), Text(18.5, 0, 'EPO'), Text(20.5, 0, 'ETV6'), Text(22.5, 0, 'IDH2'), Text(24.5, 0, 'ATRX'), Text(26.5, 0, 'GSTM1'), Text(28.5, 0, 'THPO')]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 192200 characters]",
      "text/plain": [
       "<Figure size 1140x1050 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Text(0.5, 0, 'ASXL1'), Text(2.5, 0, 'TET2'), Text(4.5, 0, 'DNMT3A'), Text(6.5, 0, 'NRAS'), Text(8.5, 0, 'U2AF1'), Text(10.5, 0, 'GATA1'), Text(12.5, 0, 'RUNX1'), Text(14.5, 0, 'CDKN2B'), Text(16.5, 0, 'SRSF2'), Text(18.5, 0, 'EPO'), Text(20.5, 0, 'ETV6'), Text(22.5, 0, 'IDH2'), Text(24.5, 0, 'ATRX'), Text(26.5, 0, 'GSTM1'), Text(28.5, 0, 'THPO')]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 200408 characters]",
      "text/plain": [
       "<Figure size 1140x1050 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "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>ASXL1</th>\n",
       "      <th>SF3B1</th>\n",
       "      <th>TET2</th>\n",
       "      <th>GATA2</th>\n",
       "      <th>DNMT3A</th>\n",
       "      <th>TP53</th>\n",
       "      <th>NRAS</th>\n",
       "      <th>GNB1</th>\n",
       "      <th>U2AF1</th>\n",
       "      <th>IDH1</th>\n",
       "      <th>...</th>\n",
       "      <th>IL12B</th>\n",
       "      <th>TTN</th>\n",
       "      <th>THOP1</th>\n",
       "      <th>SPARC</th>\n",
       "      <th>ATXN1</th>\n",
       "      <th>RARS1</th>\n",
       "      <th>TRPM1</th>\n",
       "      <th>KITLG</th>\n",
       "      <th>IRF1</th>\n",
       "      <th>COX1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>biolink:treats</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>biolink:part_of</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>biolink:contributes_to</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>biolink:related_to</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>biolink:causes</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>biolink:biomarker_for</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>biolink:gene_associated_with_condition</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>biolink:occurs_together_in_literature_with</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>biolink:subclass_of</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>biolink:affects</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>biolink:participates_in</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>11 rows × 1647 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                            ASXL1  SF3B1  TET2  GATA2  DNMT3A  \\\n",
       "biolink:treats                                  0      0     0      0       0   \n",
       "biolink:part_of                                 0      0     0      0       0   \n",
       "biolink:contributes_to                          0      0     0      1       0   \n",
       "biolink:related_to                              1      1     1      1       1   \n",
       "biolink:causes                                  1      1     1      0       0   \n",
       "biolink:biomarker_for                           0      0     0      1       0   \n",
       "biolink:gene_associated_with_condition          1      1     1      1       1   \n",
       "biolink:occurs_together_in_literature_with      1      1     1      0       1   \n",
       "biolink:subclass_of                             0      0     0      0       0   \n",
       "biolink:affects                                 1      1     1      0       1   \n",
       "biolink:participates_in                         1      1     1      1       0   \n",
       "\n",
       "                                            TP53  NRAS  GNB1  U2AF1  IDH1  \\\n",
       "biolink:treats                                 0     0     0      0     0   \n",
       "biolink:part_of                                1     0     0      0     0   \n",
       "biolink:contributes_to                         0     0     0      0     0   \n",
       "biolink:related_to                             1     1     1      1     1   \n",
       "biolink:causes                                 0     0     1      0     0   \n",
       "biolink:biomarker_for                          1     0     0      0     0   \n",
       "biolink:gene_associated_with_condition         1     1     1      1     1   \n",
       "biolink:occurs_together_in_literature_with     0     0     1      0     0   \n",
       "biolink:subclass_of                            0     0     0      0     0   \n",
       "biolink:affects                                1     0     0      1     1   \n",
       "biolink:participates_in                        1     0     1      0     0   \n",
       "\n",
       "                                            ...  IL12B  TTN  THOP1  SPARC  \\\n",
       "biolink:treats                              ...      0    0      0      0   \n",
       "biolink:part_of                             ...      0    0      0      0   \n",
       "biolink:contributes_to                      ...      0    0      0      0   \n",
       "biolink:related_to                          ...      0    0      0      0   \n",
       "biolink:causes                              ...      0    0      0      0   \n",
       "biolink:biomarker_for                       ...      0    0      0      0   \n",
       "biolink:gene_associated_with_condition      ...      1    1      1      1   \n",
       "biolink:occurs_together_in_literature_with  ...      0    0      0      0   \n",
       "biolink:subclass_of                         ...      0    0      0      0   \n",
       "biolink:affects                             ...      0    0      0      0   \n",
       "biolink:participates_in                     ...      0    0      0      0   \n",
       "\n",
       "                                            ATXN1  RARS1  TRPM1  KITLG  IRF1  \\\n",
       "biolink:treats                                  0      0      0      0     0   \n",
       "biolink:part_of                                 0      0      0      0     0   \n",
       "biolink:contributes_to                          0      0      0      0     0   \n",
       "biolink:related_to                              0      0      0      0     0   \n",
       "biolink:causes                                  0      0      0      0     0   \n",
       "biolink:biomarker_for                           0      0      0      0     0   \n",
       "biolink:gene_associated_with_condition          1      1      1      1     1   \n",
       "biolink:occurs_together_in_literature_with      0      0      0      0     0   \n",
       "biolink:subclass_of                             0      0      0      0     0   \n",
       "biolink:affects                                 0      0      0      0     0   \n",
       "biolink:participates_in                         0      0      0      0     0   \n",
       "\n",
       "                                            COX1  \n",
       "biolink:treats                                 0  \n",
       "biolink:part_of                                0  \n",
       "biolink:contributes_to                         0  \n",
       "biolink:related_to                             1  \n",
       "biolink:causes                                 0  \n",
       "biolink:biomarker_for                          0  \n",
       "biolink:gene_associated_with_condition         0  \n",
       "biolink:occurs_together_in_literature_with     0  \n",
       "biolink:subclass_of                            0  \n",
       "biolink:affects                                0  \n",
       "biolink:participates_in                        0  \n",
       "\n",
       "[11 rows x 1647 columns]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "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 = 8)"
   ]
  }
 ],
 "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
}
