Library Reference¶
The Faultless AST for Open Biomedical Ontologies.
Functions¶
fastobo.dump_graph
¶
-
fastobo.
dump_graph
(doc, fh)¶ Dump an OBO graph into the given writer or file handle, serialized into a compact JSON representation.
- Parameters
- Raises
TypeError – When the argument have invalid types.
ValueError – When the JSON serialization fails.
OSError – When an underlying OS error occurs.
Example
Use
fastobo
to convert an OBO file into an OBO graph:>>> doc = fastobo.load("plana.obo") >>> fastobo.dump_graph(doc, "plana.json")
fastobo.dump_owl
¶
-
fastobo.
dump_owl
(doc, fh, format='ofn')¶ Convert an OBO ontology to OWL and write it to the given handle.
- Parameters
fh (str or file-handle) – The path to a file, or a writable binary stream to write the serialized graph into. A binary stream needs a
write(b)
method that acceptsbytes
.doc (
OboDoc
) – The OBO document to be converted into an OWL Ontology.format (
str
) – The OWL format to serialize the converted OWL document into. Supported values are:ofn
for Functional-style syntax.
- Raises
TypeError – When the argument have invalid types.
ValueError – When the conversion to OWL fails.
OSError – When an underlying OS error occurs.
Example
Use
fastobo
to convert an OBO file into an OWL file:>>> doc = fastobo.load("ms.obo") >>> fastobo.dump_owl(doc, "ms.ofn", format="ofn")
Caution
This method is experimental. Conversion to OWL is provided on a best-effort basis using a dedicated Rust implementation of the OBO to OWL2-DL mapping. It should produce a document with correct syntax, but might fail to preserve the semantics. In such cases, consider opening an issue directly on the
fastobo-owl
issue tracker.Hint
To support serialization to OWL, an OBO document is required to declare an
ontology
clause in the header. Furthermore, every entity frame must have anamespace
clause, otherwise adefault-namespace
clause must be declared in the header. Failure to do both will result in aValueError
being thrown.
fastobo.iter
¶
-
fastobo.
iter
(fh, ordered=True, threads=0)¶ Iterate over the frames contained in an OBO document.
The header frame can be accessed with the
header
method of the returned object. Entity frames are yielded one after like any Python iterator. See the Examples section.- Parameters
fh (str or file-handle) – The path to an OBO file, or a binary stream that contains a serialized OBO document. A binary stream needs a
read(x)
method returningx
bytes.ordered (bool) – Whether or not to yield the frames in the same order they are declared in the source document.
threads (int) – The number of threads to use for parsing. Set to 0 to detect the number of logical cores, 1 to use the single threadeded parser, or to any positive integer value.
- Yields
AbstractFrame
– The individual frames contained in the OBO document.- Raises
TypeError – When the argument is not a
str
or a binary stream.SyntaxError – When the document is not in valid OBO syntax.
OSError – When an underlying OS error occurs.
Example
Use
fastobo.iter
to load an ontology frame-by-frame, even a larger one:>>> reader = fastobo.iter('ms.obo') >>> reader.header() HeaderFrame([...]) >>> next(reader) TermFrame(PrefixedIdent('MS', '0000000')) >>> list(reader) [TermFrame(PrefixedIdent('MS', '1000001')), ...]
fastobo.load
¶
-
fastobo.
load
(fh, threads=0)¶ Load an OBO document from the given path or file handle.
- Parameters
fh (str or file-handle) – The path to an OBO file, or a binary stream that contains a serialized OBO document. A binary stream needs a
read(x)
method returningx
bytes.ordered (bool) – Whether or not to yield the frames in the same order they are declared in the source document.
threads (int) – The number of threads to use for parsing. Set to 0 to detect the number of logical cores, 1 to use the single threadeded parser, or to any positive integer value.
- Returns
OboDoc
– The OBO document deserialized into an Abstract Syntax Tree.- Raises
TypeError – When the argument is not a
str
or a binary stream.SyntaxError – When the document is not in valid OBO syntax.
OSError – When an underlying OS error occurs.
Example
Use
urlopen
andfastobo.load
to parse an ontology downloaded from the OBO Library:>>> from urllib.request import urlopen >>> url = "http://purl.obolibrary.org/obo/po.obo" >>> doc = fastobo.load(urlopen(url)) >>> doc.header[3] SubsetdefClause(UnprefixedIdent('Angiosperm'), 'Term for angiosperms')
fastobo.loads
¶
-
fastobo.
loads
(document)¶ Load an OBO document from a string.
- Parameters
document (str) – A string containing an OBO document.
ordered (bool) – Whether or not to yield the frames in the same order they are declared in the source document.
threads (int) – The number of threads to use for parsing. Set to 0 to detect the number of logical cores, 1 to use the single threadeded parser, or to any positive integer value.
- Returns
OboDoc
– The OBO document deserialized into an Abstract Syntax Tree.- Raises
SyntaxError – When the document is not in valid OBO syntax.
Example
Use
fastobo.loads
to deserialize a literal OBO frame into the corresponding syntax tree:>>> doc = fastobo.loads(textwrap.dedent( ... """ ... [Term] ... id: TST:001 ... name: test item ... """ ... )) >>> doc[0].id PrefixedIdent('TST', '001') >>> doc[0][0] NameClause('test item')
fastobo.load_graph
¶
-
fastobo.
load_graph
(fh)¶ Load an OBO graph from the given path or file handle.
Both JSON and YAML formats are supported. Actually, since YAML is a superset of JSON, all graphs are in YAML format.
- Parameters
fh (str or file-handle) – The path to an OBO graph file, or a binary stream that contains a serialized OBO document. A binary stream needs a
read(x)
method returningx
bytes.- Returns
OboDoc
– The first graph of the OBO graph converted to an OBO document. The schema allows for more than one graph but this is only used when merging imports into the same document.- Raises
TypeError – When the argument is not a
str
or a binary stream.ValueError – When the JSON is not a valid OBO Graph.
SyntaxError – When the document contains invalid OBO identifiers.
OSError – When an underlying OS error occurs.
Example
Use
fastobo
to parse an ontology graph in JSON format:>>> graph = fastobo.load_graph("pato.json") >>> terms = [ ... term for term in graph ... if isinstance(term.id, fastobo.id.PrefixedIdent) ... and term.id.prefix == "PATO" ... ] >>> min(terms, key=lambda term: str(term.id)) TermFrame(PrefixedIdent('PATO', '0000000'))
fastobo.id.is_valid
¶
-
fastobo.id.
is_valid
(s)¶ Check whether or not a string is a valid OBO identifier.
- Arguments
s (
str
): the identifier to validate.
- Returns
bool
– whether or not the string is valid as an OBO identifier.
- Example
>>> fastobo.id.is_valid("MS:1000031") True >>> fastobo.id.is_valid("https://purl.obolibrary.org/obo/MS_1000031") True >>> fastobo.id.is_valid("related_to") True >>> fastobo.id.is_valid("definitely not an identifier") False
fastobo.id.parse
¶
-
fastobo.id.
parse
(s)¶ Parse a string into an OBO identifier.
- Parameters
s (
str
) – the string representation of an OBO identifier- Returns
BaseIdent
– the appropriate concrete subclass that can store the given identifier.- Raises
ValueError – when the string could not be parsed as a valid OBO identifier.
Example
>>> fastobo.id.parse("MS:1000031") PrefixedIdent('MS', '1000031') >>> fastobo.id.parse("part_of") UnprefixedIdent('part_of') >>> fastobo.id.parse("http://purl.obolibrary.org/obo/IAO_0000231") Url('http://purl.obolibrary.org/obo/IAO_0000231')
Data structures¶
Document (fastobo.doc
)¶
The abstract syntax tree corresponding to an OBO document. |
Abstract Base Classes (fastobo.abc
)¶
An abstract clause. |
|
An abstract entity clause. |
|
An abstract OBO frame, storing a sequence of various clauses. |
|
An abstract entity frame, which clauses define an entity. |
Identifier (fastobo.id
)¶
A sequence of character used to refer to an OBO entity. |
|
An identifier with a prefix. |
|
An identifier without a prefix. |
|
A URL used as an identifier. |
Header (fastobo.header
)¶
A header clause, appearing in the OBO header frame. |
|
A header clause indicating the format version of the OBO document. |
|
A header clause indicating the version of the data in the OBO document. |
|
A header clause indicating the date the document was last modified. |
|
A header clause containing the name of the person who saved the document. |
|
A header clause indicating the software that generated the document. |
|
A clause with a URL or ontology ID referencing another OBO document. |
|
A header clause declaring a subset in the OBO document. |
|
A header clause declaring a synonym type in the OBO document. |
|
A clause declaring the default namespace for the rest of the document. |
|
A clause giving the mapping between a “local” and a “global” ID space. |
|
A macro to treats xrefs coming from an ID space as equivalence statements. |
|
A macro to treats xrefs from an ID space as genus-differentia definitions. |
|
A macro to treats xrefs from an ID space as reverse genus-differentia definitions. |
|
A macro to treats xrefs from an ID space as being relationships. |
|
A macro to treats xrefs from an ID space as being subclassing relations. |
|
A macro to treats xrefs from an ID space as being superclassing relations. |
|
A clause that binds a property to a value in the OBO document. |
|
A header clause storing general comments for the current OBO file. |
|
The ontology ID of the current OBO file. |
|
A header clause containing untranslatable OWL axioms. |
|
A tag/value pair not reserved in the OBO specification. |
Term (fastobo.term
)¶
A term clause, appearing in an OBO term frame. |
|
A clause defines an alternate id for this term. |
|
A clause declaring whether or not this term is built-in to the OBO format. |
|
A clause storing a comment for this term. |
|
A clause giving a potential substitute for an obsolete term. |
|
A term clause stating the name of the creator of this term. |
|
A clause declaring the date (and optionally time) a term was created. |
|
A clause giving a human-readable definition of the term. |
|
A clause stating this term has no instances in common with another term. |
|
A clause indicating the term is exactly equivalent to another term. |
|
A clause stating this term is equivalent to the intersection of other terms. |
|
A clause declaring this term is a subclass of another term. |
|
A clause declaring whether or not the current term has an anonymous id. |
|
A clause indicating whether or not this term is obsolete. |
|
A term clause declaring the human-readable name of this term. |
|
A term clause declaring the namespace of this term. |
|
A clause that binds a property to a value in the term. |
|
A clause describing a typed relationship between this term and another term. |
|
A clause giving a term which replaces this obsolete term. |
|
A clause declaring a subset to which this term belongs. |
|
A clause giving a synonym for this term, with some cross-references. |
|
A clause indicating the term represents the union of several other terms. |
|
A cross-reference that describes an analogous term in another vocabulary. |
Typedef (fastobo.typedef
)¶
A clause defines an alternate id for this relationship. |
|
A clause declaring whether this relation is built-in to the OBO format. |
|
A clause storing a comment for this relationship. |
|
A clause giving a potential substitute for an obsolete typedef. |
|
A term clause stating the name of the creator of this relationship. |
|
A clause declaring the date (and optionally time) a typedef was created. |
|
A clause giving a human-readable definition of the relationship. |
|
A clause stating is disjoint from another relationship. |
|
A clause declaring a relationship this relationship is disjoint over. |
|
A clause declaring the domain of the relationship, if any. |
|
A clause declaring a property chain this relationship is equivalent to. |
|
A clause indicating the relation is exactly equivalent to another relation. |
|
An OWL macro that adds an |
|
An OWL macro that adds an |
|
An extension of the |
|
Declares this relation is equivalent to the intersection of other relations. |
|
A clause declaring the inverse of this relationship type. |
|
A clause declaring this relation is a subproperty of another relation. |
|
A clause declaring whether or not the relationship has an anonymous id. |
|
A clause declaring whether the relationship if anti-symmetric or not. |
|
A clause declaring whether the relationship is asymmetric or not. |
|
A clause declaring wether this relationship is class level or not. |
|
A clause declaring whether the relationship if cyclic or not. |
|
A clause declaring whether the relationship if functional or not. |
|
A clause declaring whether the relationship if inverse-functional or not. |
|
A clause declaring whether this relationship is a metadata tag or not. |
|
A clause indicating whether or not this relationship is obsolete. |
|
A clause declaring whether the relationship if reflexive or not. |
|
A clause declaring whether the relationship if symmetric or not. |
|
A clause declaring whether the relationship if transitive or not. |
|
A clause declaring the human-readable name of this relationship. |
|
A term clause declaring the namespace of this relationship. |
|
A clause that binds a property to a value in the relationship. |
|
A clause declaring the range of the relationship, if any. |
|
A clause declaring a relationship this relation has to another relation. |
|
A clause giving a relation which replaces this obsolete relation. |
|
A clause declaring a subset to which this relationship belongs. |
|
A clause giving a synonym for this relation, with some cross-references. |
|
A clause declaring another relation that this relation is transitive over. |
|
Declares the relation represents the union of several other relations. |
|
A cross-reference describing an analogous relation in another vocabulary. |
Property Value (fastobo.pv
)¶
Xref (fastobo.xref
)¶
A cross-reference to another entity or an external resource. |
|
A list of cross-references. |