{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install sparqlslurper --upgrade  -q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Graph length: 0\n",
      "\n",
      "http://www.wikidata.org/prop/P3937 : http://www.wikidata.org/entity/statement/Q29017194-a78136ed-49ba-4604-1d9b-63230dbba9d6\n",
      "http://www.wikidata.org/prop/P527 : http://www.wikidata.org/entity/statement/Q29017194-57B876E0-01F4-4529-9EBE-816FE7E054C2\n",
      "http://www.wikidata.org/prop/direct/P31 : http://www.wikidata.org/entity/Q4915012\n",
      "http://www.wikidata.org/prop/P527 : http://www.wikidata.org/entity/statement/Q29017194-483A24A4-2E2B-4F43-89EA-403BD7352B10\n",
      "http://www.wikidata.org/prop/P527 : http://www.wikidata.org/entity/statement/Q29017194-B0DD604D-A548-4C4F-AC1D-CF4AC11C2590\n",
      "http://www.wikidata.org/prop/P31 : http://www.wikidata.org/entity/statement/Q29017194-c50f2d33-4ea1-5782-c147-2ab8e76c24b7\n",
      "http://www.wikidata.org/prop/P527 : http://www.wikidata.org/entity/statement/Q29017194-4C524AC2-8F23-4323-A341-93467707649A\n",
      "http://schema.org/description : An instance of the biological pathway N-glycan trimming in the ER and Calnexin/Calreticulin cycle in Homo sapiens\n",
      "http://www.wikidata.org/prop/direct/P2888 : https://identifiers.org/reactome:R-HSA-532668\n",
      "http://wikiba.se/ontology#sitelinks : 0\n",
      "http://www.wikidata.org/prop/direct/P2860 : http://www.wikidata.org/entity/Q24646392\n",
      "http://www.wikidata.org/prop/direct/P527 : http://www.wikidata.org/entity/Q50296256\n",
      "http://www.wikidata.org/prop/P703 : http://www.wikidata.org/entity/statement/Q29017194-7CCBE861-89A9-4770-9D29-646E4A94456B\n",
      "http://schema.org/dateModified : 2018-06-27T17:19:58+00:00\n",
      "http://www.wikidata.org/prop/P2888 : http://www.wikidata.org/entity/statement/Q29017194-5DBCCA3A-8CC0-456E-9DD8-3B5A52C3A581\n",
      "http://www.wikidata.org/prop/P2860 : http://www.wikidata.org/entity/statement/Q29017194-5302F6D9-9E3A-4738-BFEF-432F38DA2BA3\n",
      "http://www.wikidata.org/prop/direct/P527 : http://www.wikidata.org/entity/Q50296274\n",
      "http://www.wikidata.org/prop/direct/P361 : http://www.wikidata.org/entity/Q45317649\n",
      "http://www.wikidata.org/prop/direct/P527 : http://www.wikidata.org/entity/Q50296258\n",
      "http://www.wikidata.org/prop/direct/P527 : http://www.wikidata.org/entity/Q45317659\n",
      "http://www.wikidata.org/prop/P527 : http://www.wikidata.org/entity/statement/Q29017194-45E6B804-988F-42CA-A319-7AC42903A248\n",
      "http://schema.org/version : 703455833\n",
      "http://www.wikidata.org/prop/direct/P31 : http://www.wikidata.org/entity/Q847556\n",
      "http://www.wikidata.org/prop/direct/P3937 : R-HSA-532668\n",
      "http://www.wikidata.org/prop/direct/P527 : http://www.wikidata.org/entity/Q50296275\n",
      "http://wikiba.se/ontology#statements : 13\n",
      "http://www.wikidata.org/prop/P361 : http://www.wikidata.org/entity/statement/Q29017194-6479876C-4ABA-407D-8DE7-864CF4A2260D\n",
      "http://www.w3.org/2000/01/rdf-schema#label : N-glycan trimming in the ER and Calnexin/Calreticulin cycle\n",
      "http://wikiba.se/ontology#identifiers : 1\n",
      "http://www.wikidata.org/prop/P31 : http://www.wikidata.org/entity/statement/Q29017194-AB50625F-7FBE-4F00-8447-04DF17F2340A\n",
      "http://www.wikidata.org/prop/P527 : http://www.wikidata.org/entity/statement/Q29017194-6109D9D6-6A87-4F44-8F9D-6EA4C11EC711\n",
      "http://www.wikidata.org/prop/direct/P527 : http://www.wikidata.org/entity/Q50296257\n",
      "http://www.wikidata.org/prop/direct/P703 : http://www.wikidata.org/entity/Q15978631\n",
      "Graph length: 33\n",
      "\n",
      "Graph length: 33 - unchanged\n",
      "SPARQL: (SELECT ?s ?p ?o {<http://www.wikidata.org/entity/statement/Q29017194-c50f2d33-4ea1-5782-c147-2ab8e76c24b7> ?p ?o}) (0.25 secs) - 4 triples\n",
      "SPARQL: (SELECT ?s ?p ?o {<http://www.wikidata.org/entity/statement/Q29017194-AB50625F-7FBE-4F00-8447-04DF17F2340A> ?p ?o}) (0.27 secs) - 4 triples\n",
      "Graph length: 41\n",
      "Invocations: 5\n",
      "Number of SPARQL queries: 3\n",
      "Number of triples retrieved: 41\n",
      "Clock time for SPARQL calls: 0.7728228569030762 msec\n"
     ]
    }
   ],
   "source": [
    "from rdflib import Namespace\n",
    "from sparqlslurper import SlurpyGraph\n",
    "\n",
    "\n",
    "WD = Namespace(\"http://www.wikidata.org/entity/\")\n",
    "P = Namespace(\"http://www.wikidata.org/prop/\")\n",
    "\n",
    "endpoint = 'https://query.wikidata.org/sparql'\n",
    "g = SlurpyGraph(endpoint)\n",
    "# Graph is empty when nothing has been slurped\n",
    "print(f\"Graph length: {len(g)}\")\n",
    "print()\n",
    "# Fetch a collection of triples\n",
    "for p, o in g.predicate_objects(WD.Q29017194):\n",
    "    print(f\"{p} : {o}\")\n",
    "    \n",
    "# Graph now contains collections\n",
    "print(f\"Graph length: {len(g)}\")\n",
    "print()\n",
    "\n",
    "# A re-fetch doesn't change the size\n",
    "print(f\"Graph length: {len(list(g.predicate_objects(WD.Q29017194)))} - unchanged\")\n",
    "\n",
    "# Additial triples can be retrieved as needed\n",
    "g.debug_slurps = True                       # Trace what is happening\n",
    "for s in g.objects(WD.Q29017194, P.P31):\n",
    "    _ = list(g.predicate_objects(s))\n",
    "\n",
    "# Graph contains everything that was added\n",
    "print(f\"Graph length: {len(g)}\")\n",
    "\n",
    "# Diagnostic parameters\n",
    "print(f\"Invocations: {g.total_calls}\")\n",
    "print(f\"Number of SPARQL queries: {g.total_queries}\")\n",
    "print(f\"Number of triples retrieved: {g.total_triples}\")\n",
    "print(f\"Clock time for SPARQL calls: {g.total_slurptime} msec\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}