RDF
- class pyomexmeta.RDF(storage_type: str = 'memory', storage_name: str = 'libOmexMetaStore', storage_options: Optional[str] = None, model_options: Optional[str] = None, rdf_ptr: Optional[ctypes.c_longlong] = None)
-
- __eq__(other: pyomexmeta.pyomexmeta.RDF)
Equality operator for
RDF
- __init__(storage_type: str = 'memory', storage_name: str = 'libOmexMetaStore', storage_options: Optional[str] = None, model_options: Optional[str] = None, rdf_ptr: Optional[ctypes.c_longlong] = None)
Create an
RDF
empty graph.This is a central object in libOmexMeta and pyomexmeta from which all annotations are created, stored, read, written and queried. Arguments to
RDF
are passed on to the redland libraries and affect how theRDF
graph is stored. The default is to store theRDF
graph in memory.- Parameters
storage_type – memory (default), hashes, file, uri or sqlite. See https://librdf.org/docs/api/redland-storage-modules.html
storage_name – An arbitrary name for your storage. Not important when storage_type is memory but will be the name of a file or database if storage_type is on disk
storage_options – Options forwarded onto the redland libraries backend. Unfortunately, the redland library docs are not brilliant at describing these options and so neither are we. Please see https://librdf.org/docs/api/redland-storage.html
model_options – Options that are forwarded onto the redland libraries librdf_model* object. Similar to storage_options these are not described in great detail in the redland lib docs. Please see https://librdf.org/docs/api/redland-model.html#librdf-new-model
rdf_ptr – A pointer (or memory address), stored as ctypes c_int64, that points to a preexisting instance of an
RDF
in memory. todo consider whether to remove this option in favour of using onlyRDF
._set_rdf_ptr.
- __len__() → int
Returns the number of individual Triples stored in the rdf model
- __ne__(other: pyomexmeta.pyomexmeta.RDF)
Inequality operator for
RDF
- __str__() → str
Defaults to rdfxml-abbrev syntax
- __weakref__
list of weak references to the object (if defined)
- add_from_file(filename: str, syntax: str) → None
Add to this existing
RDF
class from RDF serialized as syntax in filename- Parameters
filename – Location of the file on disk containing RDF to add to existing RDF graph
syntax – The syntax used by filename for storing RDF.
Returns: None
Examples
>>> rdf = RDF.from_file("annot.rdf") # read from file on disk into new RDF graph >>> rdf.add_from_file("another_annot.rdf") # add to the existing rdf graph
- add_from_string(rdf_string: str, syntax: str = 'guess') → None
Add annotations to the existing
RDF
graph (self) from rdf_string- Parameters
rdf_string – The string containing the annotations that you want to add to the current
RDF
graphsyntax – The syntax used by rdf_string (default=`guess`). Other options are rdfxml, ntriples, turtle, nquads, guess.
Returns: None
Examples
>>> rdf = RDF() >>> rdf.add_from_string("insert rdf string here")
- add_from_uri(uri_string: str, syntax: str = 'guess') → None
Add RDF from a uri to an existing
RDF
graph- Parameters
uri_string – The uri from which to download rdf from
syntax – The syntax of the rdf content pointed to by uri_string
Returns: None
- delete() → None
destructor. Delete the dynamically allocated rdf object
- draw(filename: str, **kwargs)
render an graph of RDF and save to filename
- Parameters
filename – where to write. The extension determines the format. See
https – //graphviz.readthedocs.io/en/stable/index.html for more
about accepted formats. (details) –
Returns:
- static equals_rdf_vs_rdf(first_rdf: pyomexmeta.pyomexmeta.RDF, second_rdf: pyomexmeta.pyomexmeta.RDF, syntax: str = 'turtle', verbose: bool = False) → bool
Equality operator for comparing
RDF
objects.This is the “default” equality operator used by
RDF.__eq__()
. first_rdf and second_rdf are considered equal if all triples contained in each are identical. The only exception is regarding blank nodes, which are considered equal even if their label is different. For developers: this condition for blank nodes was necessary, because they are usually automatically assigned random labels when reading from an rdf graph.This method is static - it does not require an instance of
RDF
to use. See Examples.- Parameters
first_rdf – Left operand to equality operator
second_rdf – Right operand to equality operator
syntax – When verbose is True, use this syntax for displaying the two different gaphs
verbose – When True, output both left and right operands as a string to console encoded as syntax
Returns: bool
- static equals_rdf_vs_string(rdf: pyomexmeta.pyomexmeta.RDF, string: str, syntax: str = 'turtle', verbose: bool = False) → bool
Equality operator comparing an RDF graph (left operand) to a serialized RDF graph encoded in syntax
This method is the same as
equals_rdf_vs_rdf()
excapt that the second argument string is not an instance ofRDF
but a str.- Parameters
rdf – The
RDF
object to compare with string. Left operand.string – An rdf graph encoded in syntax to compare with rdf. Right operand.
syntax – Syntax for right operand, string. This syntax is also used for displaying left and right operands to console when verbose is True
verbose – Display left and right operands using syntax for inspection.
Returns: bool
- static equals_string_vs_string(first_string: str, second_string: str, first_syntax: str = 'turtle', second_syntax: str = 'turtle', verbose: bool = False) → bool
Equality operator comparing two rdf graphs both of which are serialized to strings.
This method is the same as
equals_rdf_vs_rdf()
except that both left and right operands are strings, encoded in first_syntax and second_syntax respectively.- Parameters
first_string – The rdf graph for the left operand, encoded in first_syntax
second_string – The rdf graph for the right operand, encoded in second_syntax
first_syntax – The syntax used by first_string
second_syntax – The syntax used by second_string
verbose – When True, print out rdf graphs encoded by first_string and second_string to console using first_syntax and second_syntax respectively.
Returns: bool
- static from_file(filename: str, syntax: str = 'guess', storage_type: str = 'hashes', storage_name: str = 'pyomexmeta_storage', storage_options: Optional[str] = None, model_options: Optional[str] = None) → pyomexmeta.pyomexmeta.RDF
Create an
RDF
graph from a file on disk.This is a static method and is therefore called without an instance of
RDF
(see example)- Parameters
filename – The location on disk of the file containing rdf for parsing
syntax – The syntax for the rdf content in filename. See
RDF
.storage_type – See
RDF.__init__()
storage_name – See
RDF.__init__()
storage_options – See
RDF.__init__()
model_options – See
RDF.__init__()
Returns:
Examples
>>> rdf = RDF.from_file("/path/to/rdf_file.rdf", "turtle")
See also
- static from_string(rdf_string: str, syntax: str = 'guess', storage_type: str = 'hashes', storage_name: str = 'pyomexmeta_storage', storage_options: Optional[str] = None, model_options: Optional[str] = None) → pyomexmeta.pyomexmeta.RDF
Read RDF from a string and create an
RDF
object with it.This is a static method, i.e. it should be used without an instantiated instance of
RDF
.- Parameters
rdf_string – The rdf string to parse. It can be in any supported syntax.
syntax – guess is default which will try to make an educated guess as to what syntax is contained in rdf_string. However, sometimes the syntax cannot be identified and this will need to be specified manually. Other options are rdfxml, ntriples, turtle, nquads, guess. Note: the easiest way to get a list of available syntax options is to run your program with deliberately the wrong syntax argument
storage_type – See
RDF.__init__()
storage_name – See
RDF.__init__()
storage_options – See
RDF.__init__()
model_options – See
RDF.__init__()
Returns: An
RDF
object that contains the RDF graph described by rdf_string.Examples
>>> rdf_string = "RDF string encoded in turtle syntax" >>> rdf = RDF.from_string(rdf_string, "turtle")
See also
- static from_uri(uri_string: str, syntax: str = 'guess', storage_type: str = 'hashes', storage_name: str = 'pyomexmeta_storage', storage_options: Optional[str] = None, model_options: Optional[str] = None) → pyomexmeta.pyomexmeta.RDF
Create an
RDF
object from content in a URI.This is a static method i.e. called without an instance of
RDF
.- Parameters
uri_string – The uri from which to download an
RDF
graph from.syntax – The syntax of the graph pointed to by uri_string.
storage_type – See
RDF.__init__()
storage_name – See
RDF.__init__()
storage_options – See
RDF.__init__()
model_options – See
RDF.__init__()
Returns:
RDF
Examples
>>> rdf = RDF.from_uri("http://myrdfgraph.org/rdfspecial")
- get_archive_uri() → str
Returns the string used for archive (i.e. omex) uri
- get_local_uri() → str
Returns the string used for local uri.
This is set indirectly through the model_uri
- get_model_level_annotation_uri() → str
Returns the uri that is used for model level annotations as a string
This string is the “model_uri” concatonated with the metaid for the “model” element.
Returns: (str) the uri
Examples
>>> rdf = RDF.from_file("annot.rdf") >>> rdf.get_model_metaid() NewModel >>> rdf.get_model_level_annotation_uri() http://omex-library.org/NewOmex.omex/NewModel.xml#NewModel >>> rdf.set_model_metaid("ModelMetaid") >>> rdf.get_model_metaid() ModelMetaid >>> rdf.get_model_level_annotation_uri() http://omex-library.org/NewOmex.omex/NewModel.xml#ModelMetaid
- get_model_metaid() → str
return the current string being used for the model elements metaid.
This defaults to the string “NewModel” but is replaced with the metaid for the first “model” element it finds when an XML is opened in the
Editor()
.Examples
>>> rdf = RDF.from_file("annot.rdf") >>> rdf.get_model_metaid() NewModel >>> rdf.get_model_level_annotation_uri() http://omex-library.org/NewOmex.omex/NewModel.xml#NewModel >>> rdf.set_model_metaid("ModelMetaid") >>> rdf.get_model_metaid() ModelMetaid >>> rdf.get_model_level_annotation_uri() http://omex-library.org/NewOmex.omex/NewModel.xml#ModelMetaid
Returns: str
- get_model_uri() → str
Returns the string used for model uri.
This string also gets used for local uri
- get_repository_uri() → str
Returns the string used for repository uri
- query_results_as_dict(query_str: str) → dict
Query the
RDF
using sparqlReturns the results as a dict[variable_name] = list(results)
- Parameters
query_str – (str) A SPAQRL query
Returns: dict
Examples
>>> rdf = RDF.from_file("annot.rdf") # read from file on disk >>> rdf.query_results_as_dict("SELECT *?x ?y ?z WHERE { ?x ?y ?z }") # selects everything
See also
query_results_as_str
- query_results_as_string(query_str: str, results_syntax: str) → str
Query the
RDF
using sparql.Returns the results as a string in the specified format.
- Parameters
query_str – (str) A SPAQRL query
results_syntax – What syntax to use for the results? Options are: xml, json, table, csv, mkr, tsv, html, turtle, rdfxml If incorrect string given, options are presented to user.
Returns: string. Results
Examples
>>> rdf = RDF.from_file("annot.rdf") # read from file on disk >>> rdf.query_results_as_string("SELECT *?x ?y ?z WHERE { ?x ?y ?z }", results_syntax="csv") # selects everything
See also
query_results_as_dict
- set_archive_uri(archive_url: str) → None
Set the “archive” portion of model and local uri’s.
- Parameters
archive_url – (str) default is “NewOmex.omex”. String to use for replacement.
Returns: None
Examples
>>> rdf = RDF.from_file("annot.rdf") >>> rdf.set_repository_uri("https://Repositorious.org") >>> rdf.set_archive_uri("Arch.omex") >>> rdf.set_model_uri("SignallingNetwork.sbml") >>> rdf.get_model_uri() https://Repositorious.org/Arch.omex/SignallingNetwork.sbml >>> rdf.get_local_uri() https://Repositorious.org/Arch.omex/SignallingNetwork.rdf
- set_model_metaid(model_metaid: str) → None
Set the string to use for the model level annotation uri.
Model level annotations should be placed on the
model
element that has a metaid. This method provides a way of changing whichmodel
element is annotated, in case there is more than one. The default is “NewModel” which gets changed to the name of the metaid of the firstmodel
element that is found, once an xml is loaded into the Editor class.- Parameters
model_metaid – (str) a new name for a model metaid. It is the users responsibility to ensure this name is accurate.
Examples
>>> rdf = RDF.from_file("annot.rdf") >>> rdf.get_model_metaid() NewModel >>> rdf.get_model_level_annotation_uri() http://omex-library.org/NewOmex.omex/NewModel.xml#NewModel >>> rdf.set_model_metaid("ModelMetaid") >>> rdf.get_model_metaid() ModelMetaid >>> rdf.get_model_level_annotation_uri() http://omex-library.org/NewOmex.omex/NewModel.xml#ModelMetaid
Returns: None
- set_model_uri(model_uri: str) → None
Sets the string used for model filename.
This string is also used for the local portion of the local uri.
- Parameters
model_uri – (str) default is NewModel.xml. String to use for model uri.
Returns: None
Examples
>>> rdf = RDF.from_file("annot.rdf") >>> rdf.set_repository_uri("https://Repositorious.org") >>> rdf.set_archive_uri("Arch.omex") >>> rdf.set_model_uri("SignallingNetwork.sbml") >>> rdf.get_model_uri() https://Repositorious.org/Arch.omex/SignallingNetwork.sbml >>> rdf.get_local_uri() https://Repositorious.org/Arch.omex/SignallingNetwork.rdf
- set_repository_uri(repository_uri: str) → None
Set the respository uri.
This is used to construct the local and model uri’s.
- Parameters
repository_uri – (str) uri to use for replacing the default respository uri (http://omex-library.org/)
Examples
>>> rdf = RDF.from_file("annot.rdf") >>> rdf.set_repository_uri("https://Repositorious.org") >>> rdf.set_archive_uri("Arch.omex") >>> rdf.set_model_uri("SignallingNetwork.sbml") >>> rdf.get_model_uri() https://Repositorious.org/Arch.omex/SignallingNetwork.sbml >>> rdf.get_local_uri() https://Repositorious.org/Arch.omex/SignallingNetwork.rdf
- to_editor(xml: str, generate_new_metaids: bool = False, sbml_semantic_extraction: bool = True) → pyomexmeta.pyomexmeta.Editor
Create an Editor object which is the interface for creating and removing annotations associated with a given xml.
- Parameters
xml – The xml string for annotation (creating/deleting/amending)
generate_new_metaids – (bool) When True, if metaids are not available on xml elements, this flag automatically creates them
sbml_semantic_extraction – When xml is an sbml string, this flag determines whether to automatically extract semantic information from the sbml. Does nothing if xml is not sbml.
Returns:
Editor
Examples
>>> rdf = RDF() >>> editor = rdf.to_editor("<insert sbml string here>", True, True) >>> editor.add_description("Insulin signalling network")
See also
- to_file(filename: str, syntax: str) → None
Serialize the annotations contained in this
RDF()
graph to a file on disk- Parameters
Returns: None
- to_string(syntax: str = 'turtle') → str
Serialize this :class”:RDF graph using syntax syntax.
This method is used by
RDF.__str__()
for printing an RDF graph as default “turtle” syntax- Parameters
syntax –
- The syntax to use for serializing content contained in this
RDF
graph. Options are ntriples, turtle, rdfxml-abbrev, rdfxml, dot, json-triples, json, nquads, html
- The syntax to use for serializing content contained in this
- Returns
(str) serialized RDF graph as string.
Examples
>>> rdf = RDF.from_file("annot.rdf", "rdfxml") >>> rdf.to_string("turtle")
See also