scimilarity.ontologies#

scimilarity.ontologies.all_pair_similarities(graph, nodes, restricted_set=None)[source]#

Get the ontology similarity of all pairs in a node list.

Parameters:
  • graph (networkx.DiGraph) – Node graph.

  • nodes (list, set) – List of nodes.

  • restricted_set (set) – Set of restricted nodes to remove from their common ancestors.

Returns:

A pandas dataframe showing similarity for all node pairs.

Return type:

pandas.DataFrame

Examples

>>> onto_sim = all_pair_similarities(onto, id1, id2)
scimilarity.ontologies.find_most_viable_parent(graph, node, node_list)[source]#

Get most viable parent of a given node among the node_list.

Parameters:
  • graph (networkx.DiGraph) – Node graph.

  • node (str) – ID of given node.

  • node_list (list, set, optional, default: None) – A restricted node list for filtering.

Returns:

Node graph of parents.

Return type:

networkx.DiGraph

Examples

>>> coarse_grained = find_most_viable_parent(onto, id, celltype_list)
scimilarity.ontologies.get_all_ancestors(graph, node, node_list=None, inclusive=False)[source]#

Get all ancestor nodes of a given node.

Parameters:
  • graph (networkx.DiGraph) – Node graph.

  • node (str) – ID of given node.

  • node_list (list, set, optional, default: None) – A restricted node list for filtering.

  • inclusive (bool, default: False) – Whether to include the given node in the results.

Returns:

Node graph of ancestors.

Return type:

networkx.DiGraph

Examples

>>> ancestors = get_all_ancestors(onto, id)
scimilarity.ontologies.get_all_descendants(graph, nodes, node_list=None, inclusive=False)[source]#

Get all descendant nodes of given node(s).

Parameters:
  • graph (networkx.DiGraph) – Node graph.

  • nodes (str, list) – ID of given node or a list of node IDs.

  • node_list (list, set, optional, default: None) – A restricted node list for filtering.

  • inclusive (bool, default: False) – Whether to include the given node in the results.

Returns:

Node graph of descendants.

Return type:

networkx.DiGraph

Examples

>>> descendants = get_all_descendants(onto, id)
scimilarity.ontologies.get_children(graph, node, node_list=None)[source]#

Get children nodes of a given node.

Parameters:
  • graph (networkx.DiGraph) – Node graph.

  • node (str) – ID of given node.

  • node_list (list, set, optional, default: None) – A restricted node list for filtering.

Returns:

Node graph of children.

Return type:

networkx.DiGraph

Examples

>>> children = get_children(onto, id)
scimilarity.ontologies.get_id_mapper(graph)[source]#

Mapping from term ID to name.

Parameters:

graph (networkx.DiGraph) – Node graph.

Returns:

The id to name mapping dictionary.

Return type:

dict

Examples

>>> id2name = get_id_mapper(onto)
scimilarity.ontologies.get_lowest_common_ancestor(graph, node1, node2)[source]#

Get the lowest common ancestor of two nodes.

Parameters:
  • graph (networkx.DiGraph) – Node graph.

  • node1 (str) – ID of node1.

  • node2 (str) – ID of node2.

Returns:

Node graph of descendants.

Return type:

networkx.DiGraph

Examples

>>> common_ancestor = get_lowest_common_ancestor(onto, id1, id2)
scimilarity.ontologies.get_parents(graph, node, node_list=None)[source]#

Get parent nodes of a given node.

Parameters:
  • graph (networkx.DiGraph) – Node graph.

  • node (str) – ID of given node.

  • node_list (list, set, optional, default: None) – A restricted node list for filtering.

Returns:

Node graph of parents.

Return type:

networkx.DiGraph

Examples

>>> parents = get_parents(onto, id)
scimilarity.ontologies.get_siblings(graph, node, node_list=None)[source]#

Get sibling nodes of a given node.

Parameters:
  • graph (networkx.DiGraph) – Node graph.

  • node (str) – ID of given node.

  • node_list (list, set, optional, default: None) – A restricted node list for filtering.

Returns:

Node graph of siblings.

Return type:

networkx.DiGraph

Examples

>>> siblings = get_siblings(onto, id)
scimilarity.ontologies.import_cell_ontology(url='http://purl.obolibrary.org/obo/cl/cl-basic.obo')[source]#

Import taxrank cell ontology.

Parameters:

url (str, default: "/gstore/data/omni/scdb/cell-ontology-2022-09-15/cl-basic.obo") – The url of the ontology obo file.

Returns:

Node graph of ontology.

Return type:

networkx.DiGraph

Examples

>>> onto = import_cell_ontology()
scimilarity.ontologies.import_doid_ontology(url='http://purl.obolibrary.org/obo/doid.obo')[source]#

Import doid disease ontology.

Parameters:

url (str, default: “http://purl.obolibrary.org/obo/doid.obo”) – The url of the ontology obo file.

Returns:

Node graph of ontology.

Return type:

networkx.DiGraph

Examples

>>> onto = import_doid_ontology()
scimilarity.ontologies.import_mondo_ontology(url='http://purl.obolibrary.org/obo/mondo.obo')[source]#

Import mondo disease ontology.

Parameters:

url (str, default: “http://purl.obolibrary.org/obo/mondo.obo”) – The url of the ontology obo file.

Returns:

Node graph of ontology.

Return type:

networkx.DiGraph

Examples

>>> onto = import_mondo_ontology()
scimilarity.ontologies.import_uberon_ontology(url='http://purl.obolibrary.org/obo/uberon/basic.obo')[source]#

Import uberon tissue ontology.

Parameters:

url (str, default: “http://purl.obolibrary.org/obo/uberon/basic.obo”) – The url of the ontology obo file.

Returns:

Node graph of ontology.

Return type:

networkx.DiGraph

Examples

>>> onto = import_uberon_ontology()
scimilarity.ontologies.ontology_silhouette_width(embeddings, labels, onto, name2id, metric='cosine')[source]#
Get the average silhouette width of celltypes, being aware of cell ontology such that

ancestors are not considered inter-cluster and descendants are considered intra-cluster.

Parameters:
  • embeddings (numpy.ndarray) – Cell embeddings.

  • labels (List[str]) – Celltype names.

  • onto (networkx.DiGraph) – Cell ontology graph object.

  • name2id (dict) – A mapping dictionary of celltype name to id

  • metric (str, default: "cosine") – The distance metric to use for scipy.spatial.distance.cdist().

Returns:

  • asw (float) – The average silhouette width.

  • asw_df (pandas.DataFrame) – A dataframe containing silhouette width as well as inter and intra cluster distances for all cell types.

Return type:

Tuple[float, pandas.DataFrame]

Examples

>>> asw, asw_df = ontology_silhouette_width(
        embeddings, labels, onto, name2id, metric="cosine"
    )
scimilarity.ontologies.ontology_similarity(graph, node1, node2, restricted_set=None)[source]#

Get the ontology similarity of two terms based on the number of common ancestors.

Parameters:
  • graph (networkx.DiGraph) – Node graph.

  • node1 (str) – ID of node1.

  • node2 (str) – ID of node2.

  • restricted_set (set) – Set of restricted nodes to remove from their common ancestors.

Returns:

Number of common ancestors.

Return type:

int

Examples

>>> onto_sim = ontology_similarity(onto, id1, id2)
scimilarity.ontologies.subset_nodes_to_set(nodes, restricted_set)[source]#

Restrict nodes to a given set.

Parameters:
  • nodes (networkx.DiGraph) – Node graph.

  • restricted_set (list, set) – Restricted node list.

Returns:

Node graph of restricted set.

Return type:

networkx.DiGraph

Examples

>>> subset_nodes_to_set(nodes, node_list)