Mutation

This module contains functions that mutate or make transformations on a network

pybel_tools.mutation.collapse_nodes(graph, survivor_mapping)[source]

Collapse all nodes in values to the key nodes, in place.

Parameters:
  • graph (pybel.BELGraph) – A BEL graph
  • survivor_mapping (Mapping[BaseEntity, Set[BaseEntity]]) – A dictionary with survivors as their keys, and iterables of the corresponding victims as values.
Return type:

None

pybel_tools.mutation.rewire_variants_to_genes(graph)[source]

Find all protein variants that are pointing to a gene and not a protein and fixes them by changing their function to be pybel.constants.GENE, in place

A use case is after running collapse_to_genes().

Return type:None
pybel_tools.mutation.collapse_gene_variants(graph)[source]

Collapse all gene’s variants’ edges to their parents, in-place.

Return type:None
pybel_tools.mutation.collapse_protein_variants(graph)[source]

Collapse all protein’s variants’ edges to their parents, in-place.

Return type:None
pybel_tools.mutation.collapse_consistent_edges(graph)[source]

Collapse consistent edges together.

Warning

This operation doesn’t preserve evidences or other annotations

pybel_tools.mutation.collapse_equivalencies_by_namespace(graph, victim_namespace, survivor_namespace)[source]

Collapse pairs of nodes with the given namespaces that have equivalence relationships.

Parameters:
  • graph (BELGraph) – A BEL graph
  • victim_namespace (Union[str, Iterable[str]]) – The namespace(s) of the node to collapse
  • survivor_namespace (str) – The namespace of the node to keep

To convert all ChEBI names to InChI keys, assuming there are appropriate equivalence relations between nodes with those namespaces:

>>> collapse_equivalencies_by_namespace(graph, 'CHEBI', 'CHEBIID')
>>> collapse_equivalencies_by_namespace(graph, 'CHEBIID', 'INCHI')
Return type:None
pybel_tools.mutation.collapse_orthologies_by_namespace(graph, victim_namespace, survivor_namespace)[source]

Collapse pairs of nodes with the given namespaces that have orthology relationships.

Parameters:
  • graph (BELGraph) – A BEL Graph
  • victim_namespace (Union[str, Iterable[str]]) – The namespace(s) of the node to collapse
  • survivor_namespace (str) – The namespace of the node to keep

To collapse all MGI nodes to their HGNC orthologs, use: >>> collapse_orthologies_by_namespace(‘MGI’, ‘HGNC’)

To collapse collapse both MGI and RGD nodes to their HGNC orthologs, use: >>> collapse_orthologies_by_namespace([‘MGI’, ‘RGD’], ‘HGNC’)

Return type:None
pybel_tools.mutation.collapse_to_protein_interactions(graph)[source]

Collapse to a graph made of only causal gene/protein edges.

Return type:BELGraph
pybel_tools.mutation.remove_inconsistent_edges(graph)[source]

Remove all edges between node pairs with inconsistent edges.

This is the all-or-nothing approach. It would be better to do more careful investigation of the evidences during curation.

Return type:None
pybel_tools.mutation.get_peripheral_successor_edges(graph, subgraph)[source]

Get the set of possible successor edges peripheral to the sub-graph.

The source nodes in this iterable are all inside the sub-graph, while the targets are outside.

Return type:Iterable[Tuple[BaseEntity, BaseEntity, str]]
pybel_tools.mutation.get_peripheral_predecessor_edges(graph, subgraph)[source]

Get the set of possible predecessor edges peripheral to the sub-graph.

The target nodes in this iterable are all inside the sub-graph, while the sources are outside.

Return type:Iterable[Tuple[BaseEntity, BaseEntity, str]]
pybel_tools.mutation.count_sources(edge_iter)[source]

Count the source nodes in an edge iterator with keys and data.

Return type:Counter
Returns:A counter of source nodes in the iterable
pybel_tools.mutation.count_targets(edge_iter)[source]

Count the target nodes in an edge iterator with keys and data.

Return type:Counter
Returns:A counter of target nodes in the iterable
pybel_tools.mutation.count_possible_successors(graph, subgraph)[source]
Parameters:
  • graph (BELGraph) – A BEL graph
  • subgraph (BELGraph) – An iterator of BEL nodes
Return type:

Counter

Returns:

A counter of possible successor nodes

pybel_tools.mutation.count_possible_predecessors(graph, subgraph)[source]
Parameters:
  • graph (BELGraph) – A BEL graph
  • subgraph (BELGraph) – An iterator of BEL nodes
Return type:

Counter

Returns:

A counter of possible predecessor nodes

pybel_tools.mutation.get_subgraph_edges(graph, annotation, value, source_filter=None, target_filter=None)[source]

Gets all edges from a given subgraph whose source and target nodes pass all of the given filters

Parameters:
  • graph (pybel.BELGraph) – A BEL graph
  • annotation (str) – The annotation to search
  • value (str) – The annotation value to search by
  • source_filter – Optional filter for source nodes (graph, node) -> bool
  • target_filter – Optional filter for target nodes (graph, node) -> bool
Returns:

An iterable of (source node, target node, key, data) for all edges that match the annotation/value and node filters

Return type:

iter[tuple]

pybel_tools.mutation.get_subgraph_peripheral_nodes(graph, subgraph, node_predicates=None, edge_predicates=None)[source]

Get a summary dictionary of all peripheral nodes to a given sub-graph.

Returns:A dictionary of {external node: {‘successor’: {internal node: list of (key, dict)}, ‘predecessor’: {internal node: list of (key, dict)}}}
Return type:dict

For example, it might be useful to quantify the number of predecessors and successors:

>>> from pybel.struct.filters import exclude_pathology_filter
>>> value = 'Blood vessel dilation subgraph'
>>> sg = get_subgraph_by_annotation_value(graph, annotation='Subgraph', value=value)
>>> p = get_subgraph_peripheral_nodes(graph, sg, node_predicates=exclude_pathology_filter)
>>> for node in sorted(p, key=lambda n: len(set(p[n]['successor']) | set(p[n]['predecessor'])), reverse=True):
>>>     if 1 == len(p[value][node]['successor']) or 1 == len(p[value][node]['predecessor']):
>>>         continue
>>>     print(node,
>>>           len(p[node]['successor']),
>>>           len(p[node]['predecessor']),
>>>           len(set(p[node]['successor']) | set(p[node]['predecessor'])))
pybel_tools.mutation.expand_periphery(universe, graph, node_predicates=None, edge_predicates=None, threshold=2)[source]

Iterates over all possible edges, peripheral to a given subgraph, that could be added from the given graph. Edges could be added if they go to nodes that are involved in relationships that occur with more than the threshold (default 2) number of nodes in the subgraph.

Parameters:
  • universe (BELGraph) – The universe of BEL knowledge
  • graph (BELGraph) – The (sub)graph to expand
  • threshold (int) – Minimum frequency of betweenness occurrence to add a gap node

A reasonable edge filter to use is pybel_tools.filters.keep_causal_edges() because this function can allow for huge expansions if there happen to be hub nodes.

Return type:None
pybel_tools.mutation.enrich_complexes(graph)[source]

Add all of the members of the complex abundances to the graph.

Return type:None
pybel_tools.mutation.enrich_composites(graph)[source]

Adds all of the members of the composite abundances to the graph.

pybel_tools.mutation.enrich_reactions(graph)[source]

Adds all of the reactants and products of reactions to the graph.

pybel_tools.mutation.enrich_variants(graph, func=None)[source]

Add the reference nodes for all variants of the given function.

Parameters:
  • graph (BELGraph) – The target BEL graph to enrich
  • func (Union[None, str, Iterable[str]]) – The function by which the subject of each triple is filtered. Defaults to the set of protein, rna, mirna, and gene.
pybel_tools.mutation.enrich_unqualified(graph)[source]

Enrich the sub-graph with the unqualified edges from the graph.

The reason you might want to do this is you induce a sub-graph from the original graph based on an annotation filter, but the unqualified edges that don’t have annotations that most likely connect elements within your graph are not included.

See also

This function thinly wraps the successive application of the following functions:

Equivalent to:

>>> enrich_complexes(graph)
>>> enrich_composites(graph)
>>> enrich_reactions(graph)
>>> enrich_variants(graph)
pybel_tools.mutation.expand_internal(universe, graph, edge_predicates=None)[source]

Edges between entities in the sub-graph that pass the given filters.

Parameters:
  • universe (BELGraph) – The full graph
  • graph (BELGraph) – A sub-graph to find the upstream information
  • edge_predicates (Union[Callable[[BELGraph, BaseEntity, BaseEntity, str], bool], Iterable[Callable[[BELGraph, BaseEntity, BaseEntity, str], bool]], None]) – Optional list of edge filter functions (graph, node, node, key, data) -> bool
Return type:

None

pybel_tools.mutation.expand_internal_causal(universe, graph)[source]

Add causal edges between entities in the sub-graph.

Is an extremely thin wrapper around expand_internal().

Parameters:
  • universe (BELGraph) – A BEL graph representing the universe of all knowledge
  • graph (BELGraph) – The target BEL graph to enrich with causal relations between contained nodes

Equivalent to:

>>> from pybel_tools.mutation import expand_internal
>>> from pybel.struct.filters.edge_predicates import is_causal_relation
>>> expand_internal(universe, graph, edge_predicates=is_causal_relation)
Return type:None
pybel_tools.mutation.is_node_highlighted(graph, node)[source]

Returns if the given node is highlighted.

Parameters:
  • graph (BELGraph) – A BEL graph
  • node (tuple) – A BEL node
Returns:

Does the node contain highlight information?

Return type:

bool

pybel_tools.mutation.highlight_nodes(graph, nodes=None, color=None)[source]

Adds a highlight tag to the given nodes.

Parameters:
  • graph (BELGraph) – A BEL graph
  • nodes (Optional[Iterable[BaseEntity]]) – The nodes to add a highlight tag on
  • color (Optional[str]) – The color to highlight (use something that works with CSS)
pybel_tools.mutation.remove_highlight_nodes(graph, nodes=None)[source]

Removes the highlight from the given nodes, or all nodes if none given.

Parameters:
  • graph (BELGraph) – A BEL graph
  • nodes (Optional[Iterable[BaseEntity]]) – The list of nodes to un-highlight
Return type:

None

pybel_tools.mutation.is_edge_highlighted(graph, u, v, k)[source]

Returns if the given edge is highlighted.

Parameters:graph (BELGraph) – A BEL graph
Returns:Does the edge contain highlight information?
Return type:bool
pybel_tools.mutation.highlight_edges(graph, edges=None, color=None)[source]

Adds a highlight tag to the given edges.

Parameters:
  • graph (BELGraph) – A BEL graph
  • edges (iter[tuple]) – The edges (4-tuples of u, v, k, d) to add a highlight tag on
  • color (str) – The color to highlight (use something that works with CSS)
Return type:

None

pybel_tools.mutation.remove_highlight_edges(graph, edges=None)[source]

Remove the highlight from the given edges, or all edges if none given.

Parameters:
  • graph (BELGraph) – A BEL graph
  • edges (iter[tuple]) – The edges (4-tuple of u,v,k,d) to remove the highlight from)
pybel_tools.mutation.highlight_subgraph(universe, graph)[source]

Highlight all nodes/edges in the universe that in the given graph.

Parameters:
  • universe (BELGraph) – The universe of knowledge
  • graph (BELGraph) – The BEL graph to mutate
pybel_tools.mutation.remove_highlight_subgraph(graph, subgraph)[source]

Remove the highlight from all nodes/edges in the graph that are in the subgraph.

Parameters:
  • graph (BELGraph) – The BEL graph to mutate
  • subgraph (BELGraph) – The subgraph from which to remove the highlighting
pybel_tools.mutation.enrich_protein_and_rna_origins(graph)[source]

Add the corresponding RNA for each protein then the corresponding gene for each RNA/miRNA.

Parameters:graph (pybel.BELGraph) – A BEL graph
pybel_tools.mutation.infer_missing_two_way_edges(graph)[source]

Add edges to the graph when a two way edge exists, and the opposite direction doesn’t exist.

Use: two way edges from BEL definition and/or axiomatic inverses of membership relations

Parameters:graph (pybel.BELGraph) – A BEL graph
pybel_tools.mutation.infer_missing_backwards_edge(graph, u, v, k)[source]

Add the same edge, but in the opposite direction if not already present.

pybel_tools.mutation.enrich_internal_unqualified_edges(graph, subgraph)[source]

Add the missing unqualified edges between entities in the subgraph that are contained within the full graph.

Parameters:
pybel_tools.mutation.enrich_pubmed_citations(graph, stringify_authors=False, manager=None)[source]

Overwrites all PubMed citations with values from NCBI’s eUtils lookup service.

Sets authors as list, so probably a good idea to run pybel_tools.mutation.serialize_authors() before exporting.

Parameters:
  • graph (pybel.BELGraph) – A BEL graph
  • stringify_authors (bool) – Converts all author lists to author strings using pybel_tools.mutation.serialize_authors(). Defaults to False.
  • manager (None or str or Manager) – An RFC-1738 database connection string, a pre-built pybel.manager.Manager, or None for default connection
Return type:

Set[str]

Returns:

A set of PMIDs for which the eUtils service crashed

pybel_tools.mutation.random_by_nodes(graph, percentage=None)[source]

Get a random graph by inducing over a percentage of the original nodes.

Parameters:
  • graph (BELGraph) – A BEL graph
  • percentage (Optional[float]) – The percentage of edges to keep
Return type:

BELGraph

pybel_tools.mutation.random_by_edges(graph, percentage=None)[source]

Get a random graph by keeping a certain percentage of original edges.

Parameters:
  • graph (BELGraph) – A BEL graph
  • percentage (Optional[float]) – What percentage of eges to take
Return type:

BELGraph

pybel_tools.mutation.shuffle_node_data(graph, key, percentage=None)[source]

Shuffle the node’s data.

Useful for permutation testing.

Parameters:
  • graph (BELGraph) – A BEL graph
  • key (str) – The node data dictionary key
  • percentage (Optional[float]) – What percentage of possible swaps to make
Return type:

BELGraph

pybel_tools.mutation.shuffle_relations(graph, percentage=None)[source]

Shuffle the relations.

Useful for permutation testing.

Parameters:
  • graph (BELGraph) – A BEL graph
  • percentage (Optional[str]) – What percentage of possible swaps to make
Return type:

BELGraph

pybel_tools.mutation.build_expand_node_neighborhood_by_hash(manager)[source]

Make an expand function that’s bound to the manager.

Return type:Callable[[BELGraph, BELGraph, str], None]
pybel_tools.mutation.build_delete_node_by_hash(manager)[source]

Make a delete function that’s bound to the manager.

Return type:Callable[[BELGraph, str], None]