Transformations

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

pybel.struct.mutation.collapse_to_genes(graph)[source]

Collapse all protein, RNA, and miRNA nodes to their corresponding gene nodes.

Parameters

graph (pybel.BELGraph) – A BEL graph

pybel.struct.mutation.collapse_pair(graph, survivor, victim)[source]

Rewire all edges from the synonymous node to the survivor node, then deletes the synonymous node.

Does not keep edges between the two nodes.

Parameters
  • graph (pybel.BELGraph) – A BEL graph

  • survivor (BaseEntity) – The BEL node to collapse all edges on the synonym to

  • victim (BaseEntity) – The BEL node to collapse into the surviving node

Return type

None

pybel.struct.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.struct.mutation.collapse_all_variants(graph)[source]

Collapse all genes’, RNAs’, miRNAs’, and proteins’ variants to their parents.

Parameters

graph (pybel.BELGraph) – A BEL Graph

Return type

None

pybel.struct.mutation.surviors_are_inconsistent(survivor_mapping)[source]

Check that there’s no transitive shit going on.

Return type

Set[BaseEntity]

pybel.struct.mutation.prune_protein_rna_origins(graph)[source]

Delete genes that are only connected to one node, their correspond RNA, by a translation edge.

Parameters

graph (pybel.BELGraph) – A BEL graph

pybel.struct.mutation.remove_filtered_edges(graph, edge_predicates=None)[source]

Remove edges passing the given edge predicates.

Parameters
Returns

pybel.struct.mutation.remove_filtered_nodes(graph, node_predicates=None)[source]

Remove nodes passing the given node predicates.

Parameters

graph (pybel.BELGraph) – A BEL graph

pybel.struct.mutation.remove_associations(graph)[source]

Remove all associative relationships from the graph.

Parameters

graph (pybel.BELGraph) – A BEL graph

pybel.struct.mutation.remove_pathologies(graph)[source]

Remove pathology nodes from the graph.

Parameters

graph (pybel.BELGraph) – A BEL graph

pybel.struct.mutation.remove_biological_processes(graph)[source]

Remove biological process nodes from the graph.

Parameters

graph (pybel.BELGraph) – A BEL graph

pybel.struct.mutation.remove_isolated_list_abundances(graph)[source]

Remove isolated list abundances from the graph.

Parameters

graph (pybel.BELGraph) – A BEL graph

pybel.struct.mutation.remove_non_causal_edges(graph)[source]

Remove non-causal edges from the graph.

pybel.struct.mutation.expand_node_predecessors(universe, graph, node)[source]

Expand around the predecessors of the given node in the result graph.

Parameters
  • universe (pybel.BELGraph) – The graph containing the stuff to add

  • graph (pybel.BELGraph) – The graph to add stuff to

  • node (BaseEntity) – A BEL node

Return type

None

pybel.struct.mutation.expand_node_successors(universe, graph, node)[source]

Expand around the successors of the given node in the result graph.

Parameters
  • universe (pybel.BELGraph) – The graph containing the stuff to add

  • graph (pybel.BELGraph) – The graph to add stuff to

  • node (BaseEntity) – A BEL node

Return type

None

pybel.struct.mutation.expand_node_neighborhood(universe, graph, node)[source]

Expand around the neighborhoods of the given node in the result graph.

Note: expands complexes’ members

Parameters
  • universe (pybel.BELGraph) – The graph containing the stuff to add

  • graph (pybel.BELGraph) – The graph to add stuff to

  • node (BaseEntity) – A BEL node

Return type

None

pybel.struct.mutation.expand_nodes_neighborhoods(universe, graph, nodes)[source]

Expand around the neighborhoods of the given node in the result graph.

Parameters
  • universe (pybel.BELGraph) – The graph containing the stuff to add

  • graph (pybel.BELGraph) – The graph to add stuff to

  • nodes (Iterable[BaseEntity]) – Nodes from the query graph

Return type

None

pybel.struct.mutation.expand_all_node_neighborhoods(universe, graph, filter_pathologies=False)[source]

Expand the neighborhoods of all nodes in the given graph.

Parameters
  • universe (pybel.BELGraph) – The graph containing the stuff to add

  • graph (pybel.BELGraph) – The graph to add stuff to

  • filter_pathologies (bool) – Should expansion take place around pathologies?

Return type

None

pybel.struct.mutation.expand_upstream_causal(universe, graph)[source]

Add the upstream causal relations to the given sub-graph.

Parameters
  • universe (pybel.BELGraph) – A BEL graph representing the universe of all knowledge

  • graph (pybel.BELGraph) – The target BEL graph to enrich with upstream causal controllers of contained nodes

pybel.struct.mutation.expand_downstream_causal(universe, graph)[source]

Add the downstream causal relations to the given sub-graph.

Parameters
  • universe (pybel.BELGraph) – A BEL graph representing the universe of all knowledge

  • graph (pybel.BELGraph) – The target BEL graph to enrich with upstream causal controllers of contained nodes

pybel.struct.mutation.get_subgraph_by_annotation_value(graph, annotation, values)[source]

Induce a sub-graph over all edges whose annotations match the given key and value.

Parameters
  • graph (pybel.BELGraph) – A BEL graph

  • annotation (str) – The annotation to group by

  • values (str or iter[str]) – The value(s) for the annotation

Returns

A subgraph of the original BEL graph

Return type

pybel.BELGraph

pybel.struct.mutation.get_subgraph_by_annotations(graph, annotations, or_=None)[source]

Induce a sub-graph given an annotations filter.

Parameters
  • graph – pybel.BELGraph graph: A BEL graph

  • annotations (dict[str,iter[str]]) – Annotation filters (match all with pybel.utils.subdict_matches())

  • or (boolean) – if True any annotation should be present, if False all annotations should be present in the edge. Defaults to True.

Returns

A subgraph of the original BEL graph

Return type

pybel.BELGraph

pybel.struct.mutation.get_subgraph_by_pubmed(graph, pubmed_identifiers)[source]

Induce a sub-graph over the edges retrieved from the given PubMed identifier(s).

Parameters
  • graph (pybel.BELGraph) – A BEL graph

  • or list[str] pubmed_identifiers (str) – A PubMed identifier or list of PubMed identifiers

Return type

pybel.BELGraph

pybel.struct.mutation.get_subgraph_by_authors(graph, authors)[source]

Induce a sub-graph over the edges retrieved publications by the given author(s).

Parameters
  • graph (pybel.BELGraph) – A BEL graph

  • or list[str] authors (str) – An author or list of authors

Return type

pybel.BELGraph

pybel.struct.mutation.get_subgraph_by_neighborhood(graph, nodes)[source]

Get a BEL graph around the neighborhoods of the given nodes.

Returns none if no nodes are in the graph.

Parameters
Returns

A BEL graph induced around the neighborhoods of the given nodes

Return type

Optional[pybel.BELGraph]

pybel.struct.mutation.get_nodes_in_all_shortest_paths(graph, nodes, weight=None, remove_pathologies=False)[source]

Get a set of nodes in all shortest paths between the given nodes.

Thinly wraps networkx.all_shortest_paths().

Parameters
  • graph (pybel.BELGraph) – A BEL graph

  • nodes (Iterable[BaseEntity]) – The list of nodes to use to use to find all shortest paths

  • weight (Optional[str]) – Edge data key corresponding to the edge weight. If none, uses unweighted search.

  • remove_pathologies (bool) – Should pathology nodes be removed first?

Returns

A set of nodes appearing in the shortest paths between nodes in the BEL graph

Note

This can be trivially parallelized using networkx.single_source_shortest_path()

pybel.struct.mutation.get_subgraph_by_all_shortest_paths(graph, nodes, weight=None, remove_pathologies=False)[source]

Induce a subgraph over the nodes in the pairwise shortest paths between all of the nodes in the given list.

Parameters
  • graph (pybel.BELGraph) – A BEL graph

  • nodes (Iterable[BaseEntity]) – A set of nodes over which to calculate shortest paths

  • weight (Optional[str]) – Edge data key corresponding to the edge weight. If None, performs unweighted search

  • remove_pathologies (bool) – Should the pathology nodes be deleted before getting shortest paths?

Returns

A BEL graph induced over the nodes appearing in the shortest paths between the given nodes

Return type

Optional[pybel.BELGraph]

pybel.struct.mutation.get_random_path(graph)[source]

Get a random path from the graph as a list of nodes.

Parameters

graph (pybel.BELGraph) – A BEL graph

Return type

List[BaseEntity]

pybel.struct.mutation.get_graph_with_random_edges(graph, n_edges)[source]

Build a new graph from a seeding of edges.

Parameters

n_edges (int) – Number of edges to randomly select from the given graph

Return type

pybel.BELGraph

pybel.struct.mutation.get_random_node(graph, node_blacklist, invert_degrees=None)[source]

Choose a node from the graph with probabilities based on their degrees.

Parameters
  • node_blacklist (Set[BaseEntity]) – Nodes to filter out

  • invert_degrees (Optional[bool]) – Should the degrees be inverted? Defaults to true.

Return type

Optional[BaseEntity]

pybel.struct.mutation.get_random_subgraph(graph, number_edges=None, number_seed_edges=None, seed=None, invert_degrees=None)[source]

Generate a random subgraph based on weighted random walks from random seed edges.

Parameters
  • number_edges (Optional[int]) – Maximum number of edges. Defaults to pybel_tools.constants.SAMPLE_RANDOM_EDGE_COUNT (250).

  • number_seed_edges (Optional[int]) – Number of nodes to start with (which likely results in different components in large graphs). Defaults to SAMPLE_RANDOM_EDGE_SEED_COUNT (5).

  • seed (Optional[int]) – A seed for the random state

  • invert_degrees (Optional[bool]) – Should the degrees be inverted? Defaults to true.

Return type

pybel.BELGraph

pybel.struct.mutation.get_upstream_causal_subgraph(graph, nbunch)[source]

Induce a sub-graph from all of the upstream causal entities of the nodes in the nbunch.

Return type

pybel.BELGraph

pybel.struct.mutation.get_downstream_causal_subgraph(graph, nbunch)[source]

Induce a sub-graph from all of the downstream causal entities of the nodes in the nbunch.

Return type

pybel.BELGraph

pybel.struct.mutation.get_subgraph_by_edge_filter(graph, edge_predicates=None)[source]

Induce a sub-graph on all edges that pass the given filters.

Parameters
Returns

A BEL sub-graph induced over the edges passing the given filters

Return type

pybel.BELGraph

pybel.struct.mutation.get_subgraph_by_induction(graph, nodes)[source]

Induce a sub-graph over the given nodes or return None if none of the nodes are in the given graph.

Parameters
Return type

Optional[pybel.BELGraph]

pybel.struct.mutation.get_multi_causal_upstream(graph, nbunch)[source]

Get the union of all the 2-level deep causal upstream subgraphs from the nbunch.

Parameters
Returns

A subgraph of the original BEL graph

Return type

pybel.BELGraph

pybel.struct.mutation.get_multi_causal_downstream(graph, nbunch)[source]

Get the union of all of the 2-level deep causal downstream subgraphs from the nbunch.

Parameters
Returns

A subgraph of the original BEL graph

Return type

pybel.BELGraph

pybel.struct.mutation.get_subgraph_by_second_neighbors(graph, nodes, filter_pathologies=False)[source]

Get a graph around the neighborhoods of the given nodes and expand to the neighborhood of those nodes.

Returns none if none of the nodes are in the graph.

Parameters
  • graph (pybel.BELGraph) – A BEL graph

  • nodes (Iterable[BaseEntity]) – An iterable of BEL nodes

  • filter_pathologies (bool) – Should expansion take place around pathologies?

Returns

A BEL graph induced around the neighborhoods of the given nodes

Return type

Optional[pybel.BELGraph]

pybel.struct.mutation.enrich_rnas_with_genes(graph)[source]

Add the corresponding gene node for each RNA/miRNA node and connect them with a transcription edge.

Parameters

graph (pybel.BELGraph) – A BEL graph

Return type

None

pybel.struct.mutation.enrich_proteins_with_rnas(graph)[source]

Add the corresponding RNA node for each protein node and connect them with a translation edge.

Parameters

graph (pybel.BELGraph) – A BEL graph

Return type

None

pybel.struct.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

Return type

None

pybel.struct.mutation.strip_annotations(graph)[source]

Strip all the annotations from a BEL graph.

Parameters

graph (pybel.BELGraph) – A BEL graph

Return type

None

pybel.struct.mutation.add_annotation_value(graph, annotation, value, strict=True)[source]

Add the given annotation/value pair to all qualified edges.

Parameters
  • graph (pybel.BELGraph) –

  • annotation (str) –

  • value (str) –

  • strict (bool) – Should the function ensure the annotation has already been defined?

Return type

None

pybel.struct.mutation.remove_annotation_value(graph, annotation, value)[source]

Remove the given annotation/value pair to all qualified edges.

Parameters
Return type

None

pybel.struct.mutation.remove_citation_metadata(graph)[source]

Remove the metadata associated with a citation.

Best practice is to add this information programmatically.

Return type

None

pybel.struct.mutation.infer_child_relations(graph, node)[source]

Propagate causal relations to children.

Return type

List[str]

pybel.struct.mutation.remove_isolated_nodes(graph)[source]

Remove isolated nodes from the network, in place.

Parameters

graph (pybel.BELGraph) – A BEL graph

pybel.struct.mutation.remove_isolated_nodes_op(graph)[source]

Build a new graph excluding the isolated nodes.

Parameters

graph (pybel.BELGraph) – A BEL graph

Return type

pybel.BELGraph

pybel.struct.mutation.expand_by_edge_filter(source, target, edge_predicates)[source]

Expand a target graph by edges in the source matching the given predicates.

Parameters
Returns

A BEL sub-graph induced over the edges passing the given filters

Return type

pybel.BELGraph