Reading RDF

Reading RDF graphs revolves around the from* and add_from* methods, where the wildcard * is one of string, url or filename. The from* methods are static methods and create a new RDF graph while the add_from* methods add to the existing RDF graph.

When reading RDF, you must pass the name of which parser you would like to use. The default is the “guess” parser, which will try to figure out which syntax you’ve given it. However, the guess parser is imperfect, so if you know which syntax you are reading, you can specify this as an argument. The accepted formats are:

  • rdfxml, ntriples, turtle, trig, rss-tag-soup, grddl, guess, rdfa, nquads, guess, ,

Reading from a string

Read from a string in Python
from pyomexmeta import RDF, eUriType

rdf_str = """@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
             @prefix OMEXlib: <http://omex-library.org/> .
             @prefix myOMEX: <http://omex-library.org/NewOmex.omex/> .
             @prefix local: <http://omex-library.org/NewOmex.omex/NewModel.rdf#> .

             <http://omex-library.org/NewOmex.omex/NewModel.xml#>
                 <http://purl.org/dc/terms/creator> <https://orcid.org/1234-1234-1234-1234> ."""

# read the annotations into RDF graph
rdf = RDF.from_string(rdf_str, syntax="turtle")

# serialize the string to rdfxml-abbrev
xml_string = rdf.to_string("rdfxml-abbrev")
print(f"RDF graph serialized to rdfxml abbreviated is:\n\n{xml_string}")
Output
RDF graph serialized to rdfxml abbreviated is:

<?xml version="1.1" encoding="utf-8"?>
<rdf:RDF xmlns:OMEXlib="http://omex-library.org/"
   xmlns:local="http://omex-library.org/NewOmex.omex/NewModel.rdf#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="http://omex-library.org/NewOmex.omex/NewModel.xml#">
    <ns1:creator xmlns:ns1="http://purl.org/dc/terms/"
       rdf:resource="https://orcid.org/1234-1234-1234-1234"/>
  </rdf:Description>
</rdf:RDF>

Adding from a string

Read from a string and add to existing graph in Python
from pyomexmeta import RDF, eUriType

rdf_str1 = """@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
              @prefix OMEXlib: <http://omex-library.org/> .
              @prefix myOMEX: <http://omex-library.org/NewOmex.omex/> .
              @prefix local: <http://omex-library.org/NewOmex.omex/NewModel.rdf#> .
 
              <http://omex-library.org/NewOmex.omex/NewModel.xml#>
                  <http://purl.org/dc/terms/creator> <https://orcid.org/1234-1234-1234-1234> ."""


rdf_str2 = """@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
              @prefix bqbiol: <http://biomodels.net/biology-qualifiers/> .
              @prefix OMEXlib: <http://omex-library.org/> .
              @prefix myOMEX: <http://omex-library.org/NewOmex.omex/> .
              @prefix local: <http://omex-library.org/NewOmex.omex/NewModel.rdf#> .
              
              <http://omex-library.org/NewOmex.omex/NewModel.xml#OmexMetaId0001>
                  bqbiol:is <https://identifiers.org/uniprot/PD12345> ."""

# read the first string into our RDF graph
rdf = RDF.from_string(rdf_str1, syntax="turtle")
# Add to our RDF graph by reading the second string
rdf.add_from_string(rdf_str2)

# serialize the string to rdfxml-abbrev
xml_string = rdf.to_string("rdfxml-abbrev")
print(f"RDF graph serialized to rdfxml abbreviated is:\n\n{xml_string}")
Output
RDF graph serialized to rdfxml abbreviated is:

<?xml version="1.1" encoding="utf-8"?>
<rdf:RDF xmlns:OMEXlib="http://omex-library.org/"
   xmlns:bqbiol="http://biomodels.net/biology-qualifiers/"
   xmlns:local="http://omex-library.org/NewOmex.omex/NewModel.rdf#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="http://omex-library.org/NewOmex.omex/NewModel.xml#">
    <ns1:creator xmlns:ns1="http://purl.org/dc/terms/"
       rdf:resource="https://orcid.org/1234-1234-1234-1234"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://omex-library.org/NewOmex.omex/NewModel.xml#OmexMetaId0001">
    <bqbiol:is rdf:resource="https://identifiers.org/uniprot/PD12345"/>
  </rdf:Description>
</rdf:RDF>

Reading from a file

Read from a file in Python
from pyomexmeta import RDF, eUriType
import os

rdf_str = """@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix bqbiol: <http://biomodels.net/biology-qualifiers/> .
@prefix OMEXlib: <http://omex-library.org/> .
@prefix myOMEX: <http://omex-library.org/NewOmex.omex/> .
@prefix local: <http://omex-library.org/NewOmex.omex/NewModel.rdf#> .

<http://omex-library.org/NewOmex.omex/NewModel.xml#OmexMetaId0000>
  bqbiol:is <https://identifiers.org/uniprot/PD12345> ."""

# we write the annotations to file, so we can read it in from file
#   filename is in same directory as your python script
fname = os.path.join(os.path.dirname(__file__), "annotations.rdf")
#   write turtle syntax to file
with open (fname, "w") as f:
    f.write(rdf_str)

# Add to our RDF graph by reading the second string
rdf = RDF.from_file(fname, "turtle")

print(f"{len(rdf)} triples read from file")

# remove the file we wrote
if os.path.isfile(fname):
    os.remove(fname)
Output
1 triples read from file

Adding from a file

Read from a file and add to existing graph in Python
from pyomexmeta import RDF, eUriType
import os

rdf_str = """@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix bqbiol: <http://biomodels.net/biology-qualifiers/> .
@prefix OMEXlib: <http://omex-library.org/> .
@prefix myOMEX: <http://omex-library.org/NewOmex.omex/> .
@prefix local: <http://omex-library.org/NewOmex.omex/NewModel.rdf#> .

<http://omex-library.org/NewOmex.omex/NewModel.xml#OmexMetaId0000>
  bqbiol:is <https://identifiers.org/uniprot/PD12345> ."""

# create an empty RDF graph
rdf = RDF()

# we write the annotations to file, so we can read it in from file
#   filename is in same directory as your python script
fname = os.path.join(os.path.dirname(__file__), "annotations.rdf")

#   write turtle syntax to file
with open (fname, "w") as f:
    f.write(rdf_str)

#   Add to our RDF graph by reading the second string
rdf.add_from_file(fname, "turtle")

# print out annotations in turtle syntax
rdfxml_abbrev_string = rdf.to_string("rdfxml-abbrev")
print(rdfxml_abbrev_string)

# remove the file we wrote
if os.path.isfile(fname):
    os.remove(fname)
Output
<?xml version="1.1" encoding="utf-8"?>
<rdf:RDF xmlns:OMEXlib="http://omex-library.org/"
   xmlns:bqbiol="http://biomodels.net/biology-qualifiers/"
   xmlns:local="http://omex-library.org/NewOmex.omex/NewModel.rdf#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="http://omex-library.org/NewOmex.omex/NewModel.xml#OmexMetaId0000">
    <bqbiol:is rdf:resource="https://identifiers.org/uniprot/PD12345"/>
  </rdf:Description>
</rdf:RDF>