mgkit.graphs module

New in version 0.1.12.

Graph module

mgkit.graphs.add_module_compounds(graph, rn_defs)

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)

New in version 0.1.12.

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
mgkit.graphs.build_graph(id_links, name, edge_type='', weight=0.5)

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='')

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)

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)

New in version 0.1.12.

Used by link_nodes() to copy nodes

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

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)

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.rename_graph_nodes(graph, name_func=None, exclude_ids=None)