{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "# Example: Custom Battery Cell Metadata\n", "\n", "Let's describe two instances of custom R2032 coin cells with different materials!\n", "\n", "This example covers a few topics: \n", "\n", "- How to describe a resource using ontology terms and JSON-LD \n", "- How machines convert JSON-LD into triples \n", "- How to filter your cells based on some criteria **[Moderate]**\n", "- How to use the ontology to fetch more information from other sources **[Advanced]** \n", "\n", "A live version of this notebook is available on Google Colab [here](https://colab.research.google.com/drive/1k3dGZTz4bDeH4JPToqXsN0svCUkswPDN?usp=sharing)\n" ], "metadata": { "id": "1wseTQGaB4x9" } }, { "cell_type": "markdown", "source": [ "## Describe the powder using ontology terms in JSON-LD format\n", "The JSON-LD data that we will use is:" ], "metadata": { "id": "jcTVz9-DEh3m" } }, { "cell_type": "code", "source": [ "jsonld_LFPGr = {\n", " \"@context\": \"https://raw.githubusercontent.com/emmo-repo/domain-battery/master/context.json\",\n", " \"@type\": \"BatteryCell\",\n", " \"schema:name\": \"My LFP-Graphite R2032 Coin Cell\",\n", " \"schema:manufacturer\": {\n", " \"@id\": \"https://www.wikidata.org/wiki/Q3041255\",\n", " \"schema:name\": \"SINTEF\"\n", " },\n", " \"hasPositiveElectrode\": {\n", " \"@type\": \"Electrode\",\n", " \"hasActiveMaterial\": {\n", " \"@type\": \"LithiumIronPhosphate\"\n", " }\n", " },\n", " \"hasNegativeElectrode\": {\n", " \"@type\": \"Electrode\",\n", " \"hasActiveMaterial\": {\n", " \"@type\": \"Graphite\"\n", " }\n", " },\n", " \"hasCase\": {\n", " \"@type\": \"R2032\"\n", " },\n", " \"hasProperty\": {\n", " \"@type\": [\"NominalVoltage\", \"ConventionalProperty\"],\n", " \"hasNumericalPart\": {\n", " \"@type\": \"Real\",\n", " \"hasNumericalValue\": 3.2\n", " },\n", " \"hasMeasurementUnit\": \"emmo:Volt\"\n", " }\n", " }\n", "\n", "jsonld_LNOGr = {\n", " \"@context\": \"https://raw.githubusercontent.com/emmo-repo/domain-battery/master/context.json\",\n", " \"@type\": \"BatteryCell\",\n", " \"schema:name\": \"My LNO-Graphite R2032 Coin Cell\",\n", " \"schema:manufacturer\": {\n", " \"@id\": \"https://www.wikidata.org/wiki/Q3041255\",\n", " \"schema:name\": \"SINTEF\"\n", " },\n", " \"hasPositiveElectrode\": {\n", " \"@type\": \"Electrode\",\n", " \"hasActiveMaterial\": {\n", " \"@type\": \"LithiumNickelOxide\"\n", " }\n", " },\n", " \"hasNegativeElectrode\": {\n", " \"@type\": \"Electrode\",\n", " \"hasActiveMaterial\": {\n", " \"@type\": \"Graphite\"\n", " }\n", " },\n", " \"hasCase\": {\n", " \"@type\": \"R2032\"\n", " },\n", " \"hasProperty\": {\n", " \"@type\": [\"NominalVoltage\", \"ConventionalProperty\"],\n", " \"hasNumericalPart\": {\n", " \"@type\": \"Real\",\n", " \"hasNumericalValue\": 3.6\n", " },\n", " \"hasMeasurementUnit\": \"emmo:Volt\"\n", " }\n", " }" ], "metadata": { "id": "gohQKEBrF2QP" }, "execution_count": 48, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Parse this description into a graph\n", "Now let's see how a machine would process this data by reading it into a Graph!\n", "\n", "First, we install and import the python dependencies that we need for this example." ], "metadata": { "id": "in30p-x4H91Y" } }, { "cell_type": "code", "source": [ "# Install and import dependencies\n", "!pip install jsonschema rdflib requests matplotlib > /dev/null\n", "\n", "import json\n", "import rdflib\n", "import requests\n", "import sys\n", "from IPython.display import Image, display\n", "import matplotlib.pyplot as plt" ], "metadata": { "id": "wk4sFl_eA2ML" }, "execution_count": 49, "outputs": [] }, { "cell_type": "markdown", "source": [ "We create the graph using a very handy python package called [rdflib](https://rdflib.readthedocs.io/en/stable/), which provides us a way to parse our json-ld data, run some queries using the language [SPARQL](https://en.wikipedia.org/wiki/SPARQL), and serialize the graph in any RDF compatible format (e.g. JSON-LD, Turtle, etc.)." ], "metadata": { "id": "lotp-0QABV-2" } }, { "cell_type": "code", "source": [ "# Create a new graph\n", "g = rdflib.Graph()\n", "\n", "# Parse our json-ld data into the graph\n", "g.parse(data=json.dumps(jsonld_LFPGr), format=\"json-ld\")\n", "g.parse(data=json.dumps(jsonld_LNOGr), format=\"json-ld\")\n", "\n", "# Create a SPARQL query to return all the triples in the graph\n", "query_all = \"\"\"\n", "SELECT ?subject ?predicate ?object\n", "WHERE {\n", " ?subject ?predicate ?object\n", "}\n", "\"\"\"\n", "\n", "# Execute the SPARQL query\n", "all_the_things = g.query(query_all)\n", "\n", "# Print the results\n", "for row in all_the_things:\n", " print(row)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "zWibLw6NIrrq", "outputId": "785ba3d8-9dbc-4b89-df62-931c96bb8216" }, "execution_count": 50, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "(rdflib.term.BNode('N1fa58317b5c04a40bdc836b0e29a051d'), rdflib.term.URIRef('https://schema.org/name'), rdflib.term.Literal('My LFP-Graphite R2032 Coin Cell'))\n", "(rdflib.term.BNode('Na8888f67843f40fa8bc9efe32a39bd02'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_860aa941_5ff9_4452_8a16_7856fad07bee'), rdflib.term.BNode('N1fe8535096ed436e92cc736bb203262f'))\n", "(rdflib.term.BNode('N8a25bf6bf45743e0bbbd9401567d5dc4'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_bed1d005_b04e_4a90_94cf_02bc678a8569'), rdflib.term.URIRef('http://emmo.info/emmo#Volt'))\n", "(rdflib.term.BNode('Ne6858b3f64a9483f925ccd464076ab04'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/battery#battery_68ed592a_7924_45d0_a108_94d6275d57f0'))\n", "(rdflib.term.BNode('Nabed07743a4948009530f6c30a72a556'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_18d180e4_5e3e_42f7_820c_e08951223486'))\n", "(rdflib.term.BNode('N8a25bf6bf45743e0bbbd9401567d5dc4'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_d8aa8e1f_b650_416d_88a0_5118de945456'))\n", "(rdflib.term.BNode('N8a25bf6bf45743e0bbbd9401567d5dc4'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_8ef3cd6d_ae58_4a8d_9fc0_ad8f49015cd0'), rdflib.term.BNode('Nabed07743a4948009530f6c30a72a556'))\n", "(rdflib.term.BNode('Nf303668168c149ee9db9d3c1f077f92f'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_639b844a_e801_436b_985d_28926129ead6'))\n", "(rdflib.term.BNode('Nc84b874ca480485c82214ff777e4d5b0'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('https://w3id.org/emmo/domain/chemicalsubstance#substance_c28a0967_ed23_48cc_a14e_a651d75a19db'))\n", "(rdflib.term.BNode('N9d60b680f9a548f79d824e66f9f3f3a0'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_0f007072-a8dd-4798-b865-1bf9363be627'))\n", "(rdflib.term.BNode('Ne6858b3f64a9483f925ccd464076ab04'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_5d299271_3f68_494f_ab96_3db9acdd3138'), rdflib.term.BNode('N9d60b680f9a548f79d824e66f9f3f3a0'))\n", "(rdflib.term.BNode('Ne6858b3f64a9483f925ccd464076ab04'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_e1097637_70d2_4895_973f_2396f04fa204'), rdflib.term.BNode('N8a25bf6bf45743e0bbbd9401567d5dc4'))\n", "(rdflib.term.BNode('Ndeb892b2acee4a638d8f785513faa6d7'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_0f007072-a8dd-4798-b865-1bf9363be627'))\n", "(rdflib.term.BNode('Ne6858b3f64a9483f925ccd464076ab04'), rdflib.term.URIRef('https://schema.org/name'), rdflib.term.Literal('My LNO-Graphite R2032 Coin Cell'))\n", "(rdflib.term.BNode('N1fa58317b5c04a40bdc836b0e29a051d'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_3dcfe33d_6825_43c0_a798_68e871a68d39'), rdflib.term.BNode('N345c98843d9f46c89e490dbf667eacc6'))\n", "(rdflib.term.URIRef('https://www.wikidata.org/wiki/Q3041255'), rdflib.term.URIRef('https://schema.org/name'), rdflib.term.Literal('SINTEF'))\n", "(rdflib.term.BNode('N1fa58317b5c04a40bdc836b0e29a051d'), rdflib.term.URIRef('https://schema.org/manufacturer'), rdflib.term.URIRef('https://www.wikidata.org/wiki/Q3041255'))\n", "(rdflib.term.BNode('Ne6858b3f64a9483f925ccd464076ab04'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_8e9cf965_9f92_46e8_b678_b50410ce3616'), rdflib.term.BNode('Ndeb892b2acee4a638d8f785513faa6d7'))\n", "(rdflib.term.BNode('Ne6858b3f64a9483f925ccd464076ab04'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_3dcfe33d_6825_43c0_a798_68e871a68d39'), rdflib.term.BNode('N87fd0510c5d342d0b6d1ec1efab55efc'))\n", "(rdflib.term.BNode('N318dda34a11b4390946ce22a6278c83c'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_860aa941_5ff9_4452_8a16_7856fad07bee'), rdflib.term.BNode('N607b1c335401415da79f95196fce8dd5'))\n", "(rdflib.term.BNode('Nd522762dfe0c4e7aa318d88f5435c081'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_18d180e4_5e3e_42f7_820c_e08951223486'))\n", "(rdflib.term.BNode('N87fd0510c5d342d0b6d1ec1efab55efc'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_94497aca_52a0_48e3_9b76_157b050e35b3'))\n", "(rdflib.term.BNode('Nf303668168c149ee9db9d3c1f077f92f'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_bed1d005_b04e_4a90_94cf_02bc678a8569'), rdflib.term.URIRef('http://emmo.info/emmo#Volt'))\n", "(rdflib.term.BNode('N1fa58317b5c04a40bdc836b0e29a051d'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/battery#battery_68ed592a_7924_45d0_a108_94d6275d57f0'))\n", "(rdflib.term.BNode('Nbc45ccd0825c4ae9b894d0ae1d293482'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('https://w3id.org/emmo/domain/chemicalsubstance#substance_d53259a7_0d9c_48b9_a6c1_4418169df303'))\n", "(rdflib.term.BNode('N1fa58317b5c04a40bdc836b0e29a051d'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_8e9cf965_9f92_46e8_b678_b50410ce3616'), rdflib.term.BNode('Na8888f67843f40fa8bc9efe32a39bd02'))\n", "(rdflib.term.BNode('N8a25bf6bf45743e0bbbd9401567d5dc4'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_639b844a_e801_436b_985d_28926129ead6'))\n", "(rdflib.term.BNode('N9d60b680f9a548f79d824e66f9f3f3a0'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_860aa941_5ff9_4452_8a16_7856fad07bee'), rdflib.term.BNode('Nbc45ccd0825c4ae9b894d0ae1d293482'))\n", "(rdflib.term.BNode('Nf303668168c149ee9db9d3c1f077f92f'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_d8aa8e1f_b650_416d_88a0_5118de945456'))\n", "(rdflib.term.BNode('Nabed07743a4948009530f6c30a72a556'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_faf79f53_749d_40b2_807c_d34244c192f4'), rdflib.term.Literal('3.6', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#double')))\n", "(rdflib.term.BNode('N318dda34a11b4390946ce22a6278c83c'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_0f007072-a8dd-4798-b865-1bf9363be627'))\n", "(rdflib.term.BNode('Na8888f67843f40fa8bc9efe32a39bd02'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_0f007072-a8dd-4798-b865-1bf9363be627'))\n", "(rdflib.term.BNode('N607b1c335401415da79f95196fce8dd5'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('https://w3id.org/emmo/domain/chemicalsubstance#substance_d53259a7_0d9c_48b9_a6c1_4418169df303'))\n", "(rdflib.term.BNode('Nd522762dfe0c4e7aa318d88f5435c081'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_faf79f53_749d_40b2_807c_d34244c192f4'), rdflib.term.Literal('3.2', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#double')))\n", "(rdflib.term.BNode('N1fe8535096ed436e92cc736bb203262f'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('https://w3id.org/emmo/domain/chemicalsubstance#substance_aa8e9cc4_5f66_4307_b1c8_26fac7653a90'))\n", "(rdflib.term.BNode('N1fa58317b5c04a40bdc836b0e29a051d'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_e1097637_70d2_4895_973f_2396f04fa204'), rdflib.term.BNode('Nf303668168c149ee9db9d3c1f077f92f'))\n", "(rdflib.term.BNode('Nf303668168c149ee9db9d3c1f077f92f'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_8ef3cd6d_ae58_4a8d_9fc0_ad8f49015cd0'), rdflib.term.BNode('Nd522762dfe0c4e7aa318d88f5435c081'))\n", "(rdflib.term.BNode('N345c98843d9f46c89e490dbf667eacc6'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_94497aca_52a0_48e3_9b76_157b050e35b3'))\n", "(rdflib.term.BNode('Ne6858b3f64a9483f925ccd464076ab04'), rdflib.term.URIRef('https://schema.org/manufacturer'), rdflib.term.URIRef('https://www.wikidata.org/wiki/Q3041255'))\n", "(rdflib.term.BNode('Ndeb892b2acee4a638d8f785513faa6d7'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_860aa941_5ff9_4452_8a16_7856fad07bee'), rdflib.term.BNode('Nc84b874ca480485c82214ff777e4d5b0'))\n", "(rdflib.term.BNode('N1fa58317b5c04a40bdc836b0e29a051d'), rdflib.term.URIRef('http://emmo.info/electrochemistry#electrochemistry_5d299271_3f68_494f_ab96_3db9acdd3138'), rdflib.term.BNode('N318dda34a11b4390946ce22a6278c83c'))\n" ] } ] }, { "cell_type": "markdown", "source": [ "You can see that our human-readable JSON-LD file has been transformed into some nasty looking (but machine-readable!) triples.\n", "\n", "## Query the Graph to select instances with certain properties [Advanced]\n", "\n", "Now, let's write a SPARQL query to return the names of cells that have a nominal voltage greater than 3.5 V?" ], "metadata": { "id": "C-w1TbxkI4W5" } }, { "cell_type": "code", "source": [ "# Fetch the context\n", "context_url = 'https://raw.githubusercontent.com/emmo-repo/domain-battery/master/context.json'\n", "response = requests.get(context_url)\n", "context_data = response.json()\n", "\n", "# Look for the relevant IRIs in the context\n", "BatteryCell_iri = context_data.get('@context', {}).get('BatteryCell')\n", "NominalVoltage_iri = context_data.get('@context', {}).get('NominalVoltage')\n", "hasProperty_iri = context_data.get('@context', {}).get('hasProperty').get('@id')\n", "hasNumericalPart_iri = context_data.get('@context', {}).get('hasNumericalPart').get('@id')\n", "hasNumericalValue_iri = context_data.get('@context', {}).get('hasNumericalValue')\n", "hasMeasurementUnit_iri = context_data.get('@context', {}).get('hasMeasurementUnit').get('@id')\n", "\n", "query = f\"\"\"\n", "PREFIX schema: \n", "PREFIX emmo: \n", "\n", "SELECT ?cellName WHERE {{\n", " ?cell a <{BatteryCell_iri}>;\n", " schema:name ?cellName;\n", " <{hasProperty_iri}> ?property.\n", "\n", " ?property a <{NominalVoltage_iri}>;\n", " <{hasNumericalPart_iri}> ?numericalPart.\n", "\n", " ?numericalPart <{hasNumericalValue_iri}> ?voltage.\n", "\n", " FILTER (?voltage > 3.5)\n", "}}\n", "\"\"\"\n", "\n", "# Execute the SPARQL query\n", "results = g.query(query)\n", "\n", "# Print the results\n", "for row in results:\n", " print(row)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "6bXHGG4cI-kr", "outputId": "d74c6a2b-9dc7-4c7f-b5b2-7db4643f310d" }, "execution_count": 51, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "(rdflib.term.Literal('My LNO-Graphite R2032 Coin Cell'),)\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Fetch additional information from other sources [Advanced]\n", "\n", "Ontologies contain a lot of information about the meaning of things, but they don't always contain an exhaustive list of all the properties. Instead, they often point to other sources where that information exists rather than duplicating it. Let's see how you can use the ontology to fetch additional information from other sources." ], "metadata": { "id": "b7LJC8BubFce" } }, { "cell_type": "code", "source": [ "# Parse the ontology into the knowledge graph\n", "ontology = \"https://raw.githubusercontent.com/emmo-repo/domain-electrochemistry/master/electrochemistry-inferred.ttl\"\n", "g.parse(ontology, format='turtle')\n", "\n", "# Fetch the context\n", "context_url = 'https://raw.githubusercontent.com/emmo-repo/domain-battery/master/context.json'\n", "response = requests.get(context_url)\n", "context_data = response.json()\n", "\n", "# Look for the IRI of LithiumNickelOxide in the context\n", "LithiumNickelOxide_iri = context_data.get('@context', {}).get('LithiumNickelOxide')\n", "wikidata_iri = context_data.get('@context', {}).get('wikidataReference')\n", "\n", "# Query the ontology to find the wikidata id for LithiumNickelOxide\n", "query = \"\"\"\n", "SELECT ?wikidataId\n", "WHERE {\n", " <%s> <%s> ?wikidataId .\n", "}\n", "\"\"\" % (LithiumNickelOxide_iri, wikidata_iri)\n", "\n", "qres = g.query(query)\n", "for row in qres:\n", " wikidata_id = row.wikidataId.split('/')[-1]\n", "\n", "print(f\"The PubChem ID of Lithiun Nickel Oxide is: {wikidata_id}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ntT1Rf_yM6sZ", "outputId": "8945b2dc-8573-4d8e-e1b7-a0d2709569e5" }, "execution_count": 52, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The PubChem ID of Lithiun Nickel Oxide is: Q81988484\n" ] } ] }, { "cell_type": "markdown", "source": [ "Finally, let's retireve more information about Lithium Nickel Oxide from Wikidata and PubChem" ], "metadata": { "id": "mRcFo-MBDVBW" } }, { "cell_type": "code", "source": [ "# Query the Wikidata knowledge graph for more information\n", "wikidata_endpoint = \"https://query.wikidata.org/sparql\"\n", "\n", "# SPARQL query to get the PubChem ID\n", "query = \"\"\"\n", "SELECT ?id WHERE {\n", " wd:%s wdt:P662 ?id .\n", "}\n", "\"\"\" % wikidata_id\n", "\n", "# Execute the request\n", "response = requests.get(wikidata_endpoint, params={'query': query, 'format': 'json'})\n", "data = response.json()\n", "\n", "# Extract and display the PubChem ID\n", "if data['results']['bindings']:\n", " PubChemId = data['results']['bindings'][0]['id']['value']\n", " print(f\"The PubChem ID for a LithiumNickelOxide cell: {PubChemId}\")\n", "\n", "else:\n", " print(\"None found.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "nAAC5bo8FLD6", "outputId": "7bc59e2f-2892-4163-c1fa-d129b96f3433" }, "execution_count": 53, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The PubChem ID for a LithiumNickelOxide cell: 138395181\n" ] } ] }, { "cell_type": "code", "source": [ "def get_pubchem_compound_data(cid):\n", " base_url = \"https://pubchem.ncbi.nlm.nih.gov/rest/pug\"\n", " compound_url = f\"{base_url}/compound/cid/{cid}/JSON\"\n", " response = requests.get(compound_url)\n", " if response.status_code == 200:\n", " return response.json()\n", " else:\n", " return None\n", "\n", "# Fetch data for the compound with CID 138395181\n", "compound_data = get_pubchem_compound_data(PubChemId)\n", "if compound_data:\n", " pretty_json = json.dumps(compound_data, indent=4) # Pretty-print the JSON data\n", " print(pretty_json)\n", "else:\n", " print(\"Data not found or error in API request.\")" ], "metadata": { "id": "T1qUAeCDVNq3", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "9fabdf0b-adda-4d94-e85c-067efea39029" }, "execution_count": 54, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{\n", " \"PC_Compounds\": [\n", " {\n", " \"id\": {\n", " \"id\": {\n", " \"cid\": 138395181\n", " }\n", " },\n", " \"atoms\": {\n", " \"aid\": [\n", " 1,\n", " 2,\n", " 3,\n", " 4\n", " ],\n", " \"element\": [\n", " 28,\n", " 8,\n", " 8,\n", " 3\n", " ],\n", " \"charge\": [\n", " {\n", " \"aid\": 1,\n", " \"value\": 2\n", " },\n", " {\n", " \"aid\": 2,\n", " \"value\": -2\n", " },\n", " {\n", " \"aid\": 3,\n", " \"value\": -2\n", " },\n", " {\n", " \"aid\": 4,\n", " \"value\": 1\n", " }\n", " ]\n", " },\n", " \"coords\": [\n", " {\n", " \"type\": [\n", " 1,\n", " 5,\n", " 255\n", " ],\n", " \"aid\": [\n", " 1,\n", " 2,\n", " 3,\n", " 4\n", " ],\n", " \"conformers\": [\n", " {\n", " \"x\": [\n", " 3.732,\n", " 2.866,\n", " 4.5981,\n", " 2\n", " ],\n", " \"y\": [\n", " 0.25,\n", " -0.25,\n", " -0.25,\n", " 0.25\n", " ]\n", " }\n", " ]\n", " }\n", " ],\n", " \"charge\": -1,\n", " \"props\": [\n", " {\n", " \"urn\": {\n", " \"label\": \"Compound\",\n", " \"name\": \"Canonicalized\",\n", " \"datatype\": 5,\n", " \"release\": \"2019.04.19\"\n", " },\n", " \"value\": {\n", " \"ival\": 1\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"Compound Complexity\",\n", " \"datatype\": 7,\n", " \"implementation\": \"E_COMPLEXITY\",\n", " \"version\": \"3.4.6.11\",\n", " \"software\": \"Cactvs\",\n", " \"source\": \"xemistry.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"fval\": 0\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"Count\",\n", " \"name\": \"Hydrogen Bond Acceptor\",\n", " \"datatype\": 5,\n", " \"implementation\": \"E_NHACCEPTORS\",\n", " \"version\": \"3.4.6.11\",\n", " \"software\": \"Cactvs\",\n", " \"source\": \"xemistry.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"ival\": 2\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"Count\",\n", " \"name\": \"Hydrogen Bond Donor\",\n", " \"datatype\": 5,\n", " \"implementation\": \"E_NHDONORS\",\n", " \"version\": \"3.4.6.11\",\n", " \"software\": \"Cactvs\",\n", " \"source\": \"xemistry.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"ival\": 0\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"Count\",\n", " \"name\": \"Rotatable Bond\",\n", " \"datatype\": 5,\n", " \"implementation\": \"E_NROTBONDS\",\n", " \"version\": \"3.4.6.11\",\n", " \"software\": \"Cactvs\",\n", " \"source\": \"xemistry.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"ival\": 0\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"Fingerprint\",\n", " \"name\": \"SubStructure Keys\",\n", " \"datatype\": 16,\n", " \"parameters\": \"extended 2\",\n", " \"implementation\": \"E_SCREEN\",\n", " \"version\": \"3.4.6.11\",\n", " \"software\": \"Cactvs\",\n", " \"source\": \"xemistry.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"binary\": \"00000371080030000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"IUPAC Name\",\n", " \"name\": \"Allowed\",\n", " \"datatype\": 1,\n", " \"version\": \"2.6.6\",\n", " \"software\": \"LexiChem\",\n", " \"source\": \"openeye.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"sval\": \"lithium;nickelous;oxygen(2-)\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"IUPAC Name\",\n", " \"name\": \"CAS-like Style\",\n", " \"datatype\": 1,\n", " \"version\": \"2.6.6\",\n", " \"software\": \"LexiChem\",\n", " \"source\": \"openeye.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"sval\": \"lithium;nickel(2+);oxygen(2-)\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"IUPAC Name\",\n", " \"name\": \"Markup\",\n", " \"datatype\": 1,\n", " \"version\": \"2.6.6\",\n", " \"software\": \"LexiChem\",\n", " \"source\": \"openeye.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"sval\": \"lithium;nickel(2+);oxygen(2-)\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"IUPAC Name\",\n", " \"name\": \"Preferred\",\n", " \"datatype\": 1,\n", " \"version\": \"2.6.6\",\n", " \"software\": \"LexiChem\",\n", " \"source\": \"openeye.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"sval\": \"lithium;nickel(2+);oxygen(2-)\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"IUPAC Name\",\n", " \"name\": \"Systematic\",\n", " \"datatype\": 1,\n", " \"version\": \"2.6.6\",\n", " \"software\": \"LexiChem\",\n", " \"source\": \"openeye.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"sval\": \"lithium;nickel(2+);oxygen(2-)\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"IUPAC Name\",\n", " \"name\": \"Traditional\",\n", " \"datatype\": 1,\n", " \"version\": \"2.6.6\",\n", " \"software\": \"LexiChem\",\n", " \"source\": \"openeye.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"sval\": \"lithium;nickelous;oxygen(2-)\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"InChI\",\n", " \"name\": \"Standard\",\n", " \"datatype\": 1,\n", " \"version\": \"1.0.5\",\n", " \"software\": \"InChI\",\n", " \"source\": \"iupac.org\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"sval\": \"InChI=1S/Li.Ni.2O/q+1;+2;2*-2\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"InChIKey\",\n", " \"name\": \"Standard\",\n", " \"datatype\": 1,\n", " \"version\": \"1.0.5\",\n", " \"software\": \"InChI\",\n", " \"source\": \"iupac.org\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"sval\": \"FDVOICKSQXHDAQ-UHFFFAOYSA-N\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"Mass\",\n", " \"name\": \"Exact\",\n", " \"datatype\": 1,\n", " \"version\": \"2.1\",\n", " \"software\": \"PubChem\",\n", " \"source\": \"ncbi.nlm.nih.gov\",\n", " \"release\": \"2021.05.07\"\n", " },\n", " \"value\": {\n", " \"sval\": \"96.941174\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"Molecular Formula\",\n", " \"datatype\": 1,\n", " \"version\": \"2.1\",\n", " \"software\": \"PubChem\",\n", " \"source\": \"ncbi.nlm.nih.gov\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"sval\": \"LiNiO2-\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"Molecular Weight\",\n", " \"datatype\": 1,\n", " \"version\": \"2.1\",\n", " \"software\": \"PubChem\",\n", " \"source\": \"ncbi.nlm.nih.gov\",\n", " \"release\": \"2021.05.07\"\n", " },\n", " \"value\": {\n", " \"sval\": \"97.7\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"SMILES\",\n", " \"name\": \"Canonical\",\n", " \"datatype\": 1,\n", " \"version\": \"2.1.5\",\n", " \"software\": \"OEChem\",\n", " \"source\": \"openeye.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"sval\": \"[Li+].[O-2].[O-2].[Ni+2]\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"SMILES\",\n", " \"name\": \"Isomeric\",\n", " \"datatype\": 1,\n", " \"version\": \"2.1.5\",\n", " \"software\": \"OEChem\",\n", " \"source\": \"openeye.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"sval\": \"[Li+].[O-2].[O-2].[Ni+2]\"\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"Topological\",\n", " \"name\": \"Polar Surface Area\",\n", " \"datatype\": 7,\n", " \"implementation\": \"E_TPSA\",\n", " \"version\": \"3.4.6.11\",\n", " \"software\": \"Cactvs\",\n", " \"source\": \"xemistry.com\",\n", " \"release\": \"2019.06.18\"\n", " },\n", " \"value\": {\n", " \"fval\": 2\n", " }\n", " },\n", " {\n", " \"urn\": {\n", " \"label\": \"Weight\",\n", " \"name\": \"MonoIsotopic\",\n", " \"datatype\": 1,\n", " \"version\": \"2.1\",\n", " \"software\": \"PubChem\",\n", " \"source\": \"ncbi.nlm.nih.gov\",\n", " \"release\": \"2021.05.07\"\n", " },\n", " \"value\": {\n", " \"sval\": \"96.941174\"\n", " }\n", " }\n", " ],\n", " \"count\": {\n", " \"heavy_atom\": 4,\n", " \"atom_chiral\": 0,\n", " \"atom_chiral_def\": 0,\n", " \"atom_chiral_undef\": 0,\n", " \"bond_chiral\": 0,\n", " \"bond_chiral_def\": 0,\n", " \"bond_chiral_undef\": 0,\n", " \"isotope_atom\": 0,\n", " \"covalent_unit\": 4,\n", " \"tautomers\": -1\n", " }\n", " }\n", " ]\n", "}\n" ] } ] } ] }