IndraOntology (indra.ontology)

class indra.ontology.ontology_graph.IndraOntology[source]

A directed graph representing entities and their properties as nodes and ontological relationships between the entities as edges.

name

A prefix/name for the ontology, used for the purposes of caching.

Type

str

version

A version for the ontology, used for the purposes of caching.

Type

str

get_children(ns, id, ns_filter=None)[source]

Return all isa or partof children of a given entity.

Importantly, isa and partof edges always point towards higher-level entities in the ontology but here “child” means lower-level entity i.e., ancestors in the graph.

Parameters
  • ns (str) – The name space of an entity.

  • id (str) – The ID of an entity.

  • ns_filter (Optional[set]) – If provided, only entities within the set of given name spaces are returned.

Returns

A list of entities (name space, ID pairs) that are the children of the given entity.

Return type

list

static get_id(node)[source]

Return the name ID a given node from its label.

Parameters

node (str) – A node’s label.

Returns

The node’s ID within its name space.

Return type

str

get_id_from_name(ns, name)[source]

Return an entity’s ID given its name space and standard name.

Parameters
  • ns (str) – The name space in which the standard name is defined.

  • name (str) – The standard name defined in the name space.

Returns

The ID corresponding to the given standard name in the given name space or None if it’s not available.

Return type

str

get_mappings(ns, id)[source]

Return entities that are xrefs of a given entity.

This function returns all mappings via xrefs edges from the given entity.

Parameters
  • ns (str) – An entity’s name space.

  • id (str) – An entity’s ID.

Returns

A list of entities (name space, ID pairs) that are direct or indirect xrefs of the given entity.

Return type

list

get_name(ns, id)[source]

Return the standard name of a given entity.

Parameters
  • ns (str) – An entity’s name space.

  • id (str) – An entity’s ID.

Returns

The name associated with the given entity or None if the node is not in the ontology or doesn’t have a standard name.

Return type

str or None

get_node_property(ns, id, property)[source]

Return a given property of a given entity.

Parameters
  • ns (str) – An entity’s name space.

  • id (str) – An entity’s ID.

  • property (str) – The property to look for on the given node.

Returns

The name associated with the given entity or None if the node is not in the ontology or doesn’t have the given property.

Return type

str or None

static get_ns(node)[source]

Return the name space of a given node from its label.

Parameters

node (str) – A node’s label.

Returns

The node’s name space.

Return type

str

static get_ns_id(node)[source]

Return the name space and ID of a given node from its label.

Parameters

node (str) – A node’s label.

Returns

A tuple of the node’s name space and ID.

Return type

tuple(str, str)

get_parents(ns, id)[source]

Return all isa or partof parents of a given entity.

Importantly, isa and partof edges always point towards higher-level entities in the ontology but here “parent” means higher-level entity i.e., descendants in the graph.

Parameters
  • ns (str) – The name space of an entity.

  • id (str) – The ID of an entity.

Returns

A list of entities (name space, ID pairs) that are the parents of the given entity.

Return type

list

get_polarity(ns, id)[source]

Return the polarity of a given entity.

Parameters
  • ns (str) – An entity’s name space.

  • id (str) – An entity’s ID.

Returns

The polarity associated with the given entity or None if the node is not in the ontology or doesn’t have a polarity.

Return type

str or None

get_top_level_parents(ns, id)[source]

Return all top-level isa or partof parents of a given entity.

Top level means that this function only returns parents which don’t have any further isa or partof parents above them. Importantly, isa and partof edges always point towards higher-level entities in the ontology but here “parent” means higher-level entity i.e., descendants in the graph.

Parameters
  • ns (str) – The name space of an entity.

  • id (str) – The ID of an entity.

Returns

A list of entities (name space, ID pairs) that are the top-level parents of the given entity.

Return type

list

initialize()[source]

Initialize the ontology by adding nodes and edges.

By convention, ontologies are implemented such that the constructor does not add all the nodes and edges, which can take a long time. This function is called automatically when any of the user-facing methods ot IndraOntology is called. This way, the ontology is only fully constructed if it is used.

is_opposite(ns1, id1, ns2, id2)[source]

Return True if the two entities are opposites of each other.

Parameters
  • ns1 (str) – The first entity’s name space.

  • id1 (str) – The first entity’s ID.

  • ns2 (str) – The second entity’s name space.

  • id2 (str) – The second entity’s ID.

Returns

True if the first entity is in an is_opposite relationship with the second. False otherwise.

Return type

bool

isa(ns1, id1, ns2, id2)[source]

Return True if the first entity is related to the second as ‘isa’.

Parameters
  • ns1 (str) – The first entity’s name space.

  • id1 (str) – The first entity’s ID.

  • ns2 (str) – The second entity’s name space.

  • id2 (str) – The second entity’s ID.

Returns

True if the first entity is related to the second with a directed path containing edges with type isa. Otherwise False.

Return type

bool

isa_or_partof(ns1, id1, ns2, id2)[source]

Return True if the first entity is related to the second as ‘isa’ or partof.

Parameters
  • ns1 (str) – The first entity’s name space.

  • id1 (str) – The first entity’s ID.

  • ns2 (str) – The second entity’s name space.

  • id2 (str) – The second entity’s ID.

Returns

True if the first entity is related to the second with a directed path containing edges with type isa or partof. Otherwise False.

Return type

bool

isrel(ns1, id1, ns2, id2, rels)[source]

Return True if the two entities are related with a given rel.

Parameters
  • ns1 (str) – The first entity’s name space.

  • id1 (str) – The first entity’s ID.

  • ns2 (str) – The second entity’s name space.

  • id2 (str) – The second entity’s ID.

  • rels (iterable of str) – A set of edge types to traverse when determining if the first entity is related to the second entity.

Returns

True if the first entity is related to the second with a directed path containing edges with types in rels . Otherwise False.

Return type

bool

static label(ns, id)[source]

Return the label corresponding to a given entity.

This is mostly useful for constructing the ontology or when adding new nodes/edges. It can be overriden in subclasses to change the default mapping from ns / id to a label.

Parameters
  • ns (str) – An entity’s name space.

  • id (str) – An entity’s ID.

Returns

The label corresponding to the given entity.

Return type

str

map_to(ns1, id1, ns2)[source]

Return an entity that is a unique xref of an entity in a given name space.

This function first finds all mappings via xrefs edges from the given first entity to the given second name space. If exactly one such mapping target is found, the target is returned. Otherwise, None is returned.

Parameters
  • ns1 (str) – The first entity’s name space.

  • id1 (str) – The first entity’s ID.

  • ns2 (str) – The second entity’s name space.

Returns

  • str – The name space of the second entity

  • str – The ID of the second entity in the given name space.

maps_to(ns1, id1, ns2, id2)[source]

Return True if the first entity has an xref to the second.

Parameters
  • ns1 (str) – The first entity’s name space.

  • id1 (str) – The first entity’s ID.

  • ns2 (str) – The second entity’s name space.

  • id2 (str) – The second entity’s ID.

Returns

True if the first entity is related to the second with a directed path containing edges with type xref. Otherwise False.

Return type

bool

nodes_from_suffix(suffix)[source]

Return all node labels which have a given suffix.

This is useful for finding entities in ontologies where the IDs consist of paths like a/b/c/…

Parameters

suffix (str) – A label suffix.

Returns

A list of node labels that have the given suffix.

Return type

list

partof(ns1, id1, ns2, id2)[source]

Return True if the first entity is related to the second as ‘partof’.

Parameters
  • ns1 (str) – The first entity’s name space.

  • id1 (str) – The first entity’s ID.

  • ns2 (str) – The second entity’s name space.

  • id2 (str) – The second entity’s ID.

Returns

True if the first entity is related to the second with a directed path containing edges with type partof. Otherwise False.

Return type

bool

static reverse_label(label)[source]

Return the name space and ID from a given label.

This is the complement of the label method which reverses a label into a name space and ID.

Parameters

label – A node label.

Returns

  • str – The name space corresponding to the label.

  • str – The ID corresponding to the label.