mgkit.graphs module

New in version 0.1.12.

Graph module

Sample edge_links for link_graph()

class mgkit.graphs.Reaction(kegg_id, substrates, products, reversible, orthologs, pathway)[source]

Bases: object

New in version 0.4.0.

Object used to hold information about a reaction entry in Kegg

__eq__(other)[source]

Tests equality by comparing the IDs and the compounds

cmp_compounds(other)[source]

Compares the substrates and products of the current instance with those of another one, using information about the reversibility of the reaction.

irreversible_paths = None
kegg_id = None
orthologs = None
pathways

Set which includes all the pathways in which the reaction was found

products = None
reversible

Property that returns the reversibility of the reaction according to the information in the pathways. Returns True if the number of pathways in which the reaction was observed as reversible is greater or equal than the number of pathwaysin which the reaction was observerd as irreversible.

reversible_paths = None
substrates = None
to_edges()[source]

Returns a generator of edges to be used when building a graph, along with an attribute that specify if the reaction is reversible.

to_edges_compounds()[source]
to_nodes()[source]

Returns a generator that returns the nodes associated with reaction, to be used in a graph, along with attributes about the type of node (reaction or compound).

update(other)[source]

Updates the current instance with information from another instance. the underlining sets that hold the information are update with those from the other instance.

Raises:ValueError – if the ID of the reaction is different
mgkit.graphs.add_module_compounds(graph, rn_defs)[source]

New in version 0.3.1.

Modify in-place a graph, by adding additional compounds from a dictionary of definitions. It uses the reversible/irreversible information for each reaction to add the correct number of edges to the graph.

Parameters:
  • graph (graph) – a graph to update with additional compounds
  • rn_defs (dict) – a dictionary, whose keys are reactions IDs and the values are instances of mgkit.kegg.KeggReaction
mgkit.graphs.annotate_graph_nodes(graph, attr, id_map, default=None, conv=None)[source]

New in version 0.1.12.

Changed in version 0.4.0: added conv parameter and reworked internals

Add/Changes nodes attribute attr using a dictionary of ids->values.

Note

If the id is not found in id_map:

  • default is None: no value added for that node
  • default is not None: the node attribute will be set to default
Parameters:
  • graph – the graph to annotate
  • attr (str) – the attribute to annotate
  • id_map (dict) – the dictionary with the values for each node
  • default – the value used in case an id is not found in id_map, if None, the attribute is not set for missing values
  • conv (func) – function to convert the value to another type
mgkit.graphs.build_graph(id_links, name, edge_type='', weight=0.5)[source]

New in version 0.1.12.

Builds a networkx graph from a dictionary of nodes, as outputted by mgkit.kegg.KeggClientRest.get_pathway_links(). The graph is undirected, and all edges weight are the same.

Parameters:
  • id_links (dict) – dictionary with the links
  • name (str) – name of the graph
  • edge_type (str) – an optional name for the edge_type attribute set for each edge
  • weight (float) – the weight assigned to each edge in the graph
Returns:

an instance of networkx.Graph

Return type:

graph

mgkit.graphs.build_weighted_graph(id_links, name, weights, edge_type='')[source]

New in version 0.1.14.

Builds a networkx graph from a dictionary of nodes, as outputted by mgkit.kegg.KeggClientRest.get_pathway_links(). The graph is undirected, and all edges weight are the same.

Parameters:
  • id_links (dict) – dictionary with the links
  • name (str) – name of the graph
  • edge_type (str) – an optional name for the edge_type attribute set for each edge
  • weight (float) – the weight assigned to each edge in the graph
Returns:

an instance of networkx.Graph

Return type:

graph

mgkit.graphs.copy_edges(g, graph1, name=None, **kwd)[source]

New in version 0.1.12.

Used by link_nodes() to copy edges

mgkit.graphs.copy_nodes(g, graph1, name=None, id_attr=None, **kwd)[source]

New in version 0.1.12.

Used by link_nodes() to copy nodes

mgkit.graphs.filter_graph(graph, id_list, filter_func=<function <lambda>>)[source]

New in version 0.1.12.

Filter a graph based on the id_list provided and the filter function used to test the id attribute of each node.

A node is removed if filter_func returns True on a node and its id attribute is not in id_list

Parameters:
  • graph – the graph to filter
  • id_list (iterable) – the list of nodes that are to remain in the graph
  • filter_func (func) – function which accept a single parameter and return a boolean
Returns:

an instance of networkx.Graph

Return type:

graph

mgkit.graphs.from_kgml(entry, graph=None, rn_ids=None)[source]

New in version 0.3.1.

Given a KGML file (as string), representing a pathway in Kegg, returns a networkx DiGraph, using reaction directionality included in the KGML. If a reaction is reversible, 2 edges (from and to) for each compound/reaction pair are added, giving the bidirectionality.

Note

substrate and products included in a KGML don’t represent the complete reaction, excluding in general cofactors or more general terms. Those can be added using add_module_compounds(), which may be more useful when used with a restricted number of reactions (e.g. a module)

Parameters:
  • entry (str) – KGML file as a string, or anything that can be passed to ElementTree
  • graph (graph) – an instance of a networkx DiGraph if the network is to be updated with a new KGML, if None a new one is created
  • rn_ids (set) – a set/list of reaction IDs that are to be included, if None all reactions are used
Returns:

a networkx DiGraph with the reaction/compounds

Return type:

graph

New in version 0.1.12.

Link nodes of a set of graphs using the specifics in edge_links. The resulting graph nodes are renamed, and the nodes that are shared between the graphs linked.

Parameters:
  • graphs – iterable of graphs
  • edge_links – iterable with function, edge_type and weight for the links between graphs
Returns:

an instance of networkx.Graph

Return type:

graph

New in version 0.1.12.

Used by link_graph() to link nodes with the same id

mgkit.graphs.merge_kgmls(kgmls)[source]

New in version 0.4.0.

Parses multiple KGMLs and merges the reactions from them.

Parameters:kgmls (iterable) – iterable of KGML files (content) to be passed to parse_kgml_reactions()
Returns:dictionary with the reactions from amm te KGML files
Return type:dict
mgkit.graphs.parse_kgml_reactions(kgml)[source]

New in version 0.4.0.

Parses a KGML for reactions, returning a dictionary with instances of Reaction as values and the IDs as keys.

Parameters:kgml (str) – the KGML file content as a string (to be passed)
Returns:dictionary of ID->Reaction
Return type:dict
mgkit.graphs.rename_graph_nodes(graph, name_func=None, exclude_ids=None)[source]