# vermouth.graph_utils module¶

class vermouth.graph_utils.MappingGraphMatcher(*args, edge_match=None, node_match=None, **kwargs)[source]

Bases: networkx.algorithms.isomorphism.isomorphvf2.GraphMatcher

semantic_feasibility(G1_node, G2_node)[source]

Returns True if mapping G1_node to G2_node is semantically feasible. Adapted from networkx.algorithms.isomorphism.vf2userfunc._semantic_feasibility.

vermouth.graph_utils.add_element_attr(molecule)[source]

Adds an element attribute to every node in molecule, based on that node’s atomname attribute.

Parameters: molecule (networkx.Graph) – The graph of which nodes should get an element attribute. ValueError – If no element could be guessed for a node.
vermouth.graph_utils.categorical_cartesian_product(graph1, graph2, attributes=())[source]
vermouth.graph_utils.categorical_maximum_common_subgraph(graph1, graph2, attributes=())[source]
vermouth.graph_utils.categorical_modular_product(graph1, graph2, attributes=())[source]
vermouth.graph_utils.collect_residues(graph, attrs=('chain', 'resid', 'resname', 'insertion_code'))[source]

Creates groups of indices based on the node attributes with keys attrs. All nodes in graph will be part of exactly one group.

Parameters: graph (networkx.Graph) – The graph whose node indices should be grouped. attrs (Sequence) – The attribute keys that should be used to group node indices. The associated values should be hashable. The keys are the found node attributes, the values the associated node indices. dict[tuple, set]
vermouth.graph_utils.get_attrs(node, attrs)[source]

Returns multiple values from a dictionary in order.

Parameters: node (dict) – The dict from which items should be taken. attrs (collections.abc.Iterable) – The keys which values should be taken. A tuple containing the value of every key in attrs in the same order, where missing values are None. tuple
vermouth.graph_utils.make_residue_graph(graph, attrs=('chain', 'resid', 'resname', 'insertion_code'))[source]

Create a new graph based on graph, where nodes with identical attribute values for the attribute names in attrs will be contracted into a single, coarser node. With the default arguments it will create a graph with one node per residue. Resulting (coarse) nodes will have the same attributes as the constructing nodes, but only those that have identical values. In addition, they’ll have attributes ‘graph’, ‘nnodes’, ‘nedges’ and ‘density’.

Parameters: graph (networkx.Graph) – The graph to condense. attrs (collections.abc.Iterable[collections.abc.Hashable]) – The node attributes that determine node equivalence. The resulting coarser graph, where equivalent nodes are contracted to a single node. networkx.Graph
vermouth.graph_utils.partition_graph(graph, partitions)[source]

Create a new graph based on graph, where nodes are aggregated based on partitions, similar to quotient_graph(), except that it only accepts pre-made partitions, and edges are not given a ‘weight’ attribute. Much fast than the quotient_graph, since it creates edges based on existing edges rather than trying all possible combinations.

Parameters: graph (networkx.Graph) – The graph to partition partitions (collections.abc.Iterable[collections.abc.Iterable[collections.abc.Hashable]]) – E.g. a list of lists of node indices, describing the partitions. Will be sorted by lowest index. The coarser graph. networkx.Graph
vermouth.graph_utils.rate_match(residue, bead, match)[source]

A helper function which rates how well match describes the isomorphism between residue and bead based on the number of matching atomnames.

Parameters:
• residue (networkx.Graph) –

A graph. Required node attributes:

atomname: The name of an atom.
A subgraph of residue where the isomorphism is described by match. Required node attributes:
The number of entries in match where the atomname in residue matches the atomname in bead.