{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import getpass\n",
    "import sys\n",
    "import time\n",
    "from pprint import pprint\n",
    "\n",
    "import requests\n",
    "\n",
    "\n",
    "import pybel\n",
    "from pybel import BELGraph\n",
    "from pybel.constants import DIRTY\n",
    "from pybel.io.line_utils import parse_lines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.7.7 (default, Mar 10 2020, 15:43:33) \n",
      "[Clang 11.0.0 (clang-1100.0.33.17)]\n"
     ]
    }
   ],
   "source": [
    "print(sys.version)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cthoyt\n"
     ]
    }
   ],
   "source": [
    "print(getpass.getuser())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wed Apr  1 21:43:32 2020\n"
     ]
    }
   ],
   "source": [
    "print(time.asctime())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.14.5\n"
     ]
    }
   ],
   "source": [
    "print(pybel.get_version())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "GILDA_URL = 'http://grounding.indra.bio/ground'\n",
    "\n",
    "def ground(text, context=None):\n",
    "    res = requests.post(GILDA_URL, json={'text': text, 'context': context})\n",
    "    return res.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For the purposes of the demo, the internal guts of pybel are used to have the BEL document embedded inline. Normally, you'd run something like `pybel.from_bel_script(path, allow_naked_names=True)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "graph = BELGraph()\n",
    "lines = [\n",
    "    'SET DOCUMENT Name = \"Test\"',\n",
    "    'SET DOCUMENT Version = \"20200401\"',\n",
    "    'SET DOCUMENT Authors = \"Test\"',\n",
    "    'SET DOCUMENT Description = \"Test\"',\n",
    "    'SET DOCUMENT ContactInfo = \"Test\"',\n",
    "    'DEFINE NAMESPACE hgnc AS PATTERN \"\\w+\"',\n",
    "    'SET Citation = {\"PubMed\", \"1235\"}',\n",
    "    'SET Evidence = \"Test\"',\n",
    "    'p(hgnc:TEST) increases p(RAS)',\n",
    "]\n",
    "parse_lines(graph, lines, allow_naked_names=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p(dirty:RAS)\n",
      "[{'match': {'cap_combos': [],\n",
      "            'dash_mismatches': [],\n",
      "            'exact': True,\n",
      "            'query': 'RAS',\n",
      "            'ref': 'RAS',\n",
      "            'space_mismatch': False},\n",
      "  'score': 1.0,\n",
      "  'term': {'db': 'FPLX',\n",
      "           'entry_name': 'RAS',\n",
      "           'id': 'RAS',\n",
      "           'norm_text': 'ras',\n",
      "           'source': 'famplex',\n",
      "           'status': 'assertion',\n",
      "           'text': 'RAS'}},\n",
      " {'match': {'cap_combos': [],\n",
      "            'dash_mismatches': [],\n",
      "            'exact': True,\n",
      "            'query': 'RAS',\n",
      "            'ref': 'RAS',\n",
      "            'space_mismatch': False},\n",
      "  'score': 0.5555555555555556,\n",
      "  'term': {'db': 'CHEBI',\n",
      "           'entry_name': 'rasagiline',\n",
      "           'id': 'CHEBI:63620',\n",
      "           'norm_text': 'ras',\n",
      "           'source': 'chebi',\n",
      "           'status': 'synonym',\n",
      "           'text': 'RAS'}}]\n"
     ]
    }
   ],
   "source": [
    "u, v, data = list(graph.edges(data=True))[0]\n",
    "\n",
    "for node in graph:\n",
    "    if node.namespace == DIRTY:\n",
    "        print(node)\n",
    "        pprint(ground(node.name))"
   ]
  }
 ],
 "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.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
