5.1.1.1.4. cobramod.core.graph

Module for graph algorithm

This module creates the needed functions to find out the reaction order from a pathway. The vertex represents the reactions and the edges symbolize the order of the reactions. I.e. the relationship between reactions. The main function of this module:

build_graph: From given dictionary with Parent-reaction:children-reaction, return the corresponding non-cyclic directed graph.

5.1.1.1.4.1. Module Contents

5.1.1.1.4.1.1. Functions

find_missing(graph: dict)

Checks whether the given graph is missing a key.

find_cycle(graph: dict, key: str, visited: list)

Returns a list with the cycle in the graph or False is graph does not

return_cycles(graph: dict)

Returns a nested list of cyclic paths. These paths might repeat. If the

cut_cycle(graph: dict, key: str)

Changes value of the key to None in a given dictionary. It will raise an

cut_parents(graph: dict)

Checks if multiple parents shared a common child. If so, the graph will

back(graph: dict, value: str, path: list, stop_list: list = []) → list

Returns a list with a linear path. The function creates a sequence from

verify_paths(paths: list, graph: dict) → set

Returns the missing keys that are not present in the given paths list.

get_paths(graph: dict, stop_list: list) → list

Returns a list with the longest path in a graph. This only works with

get_mapping(graph: dict, stop_list: list, new: list) → list

Gets the mapping for given graph. The mapping defines the longest paths

build_graph(graph: dict) → list

Returns the mapping for the given graph. The mapping is defined as a list

cobramod.core.graph.find_missing(graph: dict)

Checks whether the given graph is missing a key.

Parameters

graph (dict) – Dictionary representing the relationships between nodes. A node can have several edges, which should be represented in the form of values.

Raises

KeyError – If keys are missing

cobramod.core.graph.find_cycle(graph: dict, key: str, visited: list)

Returns a list with the cycle in the graph or False is graph does not contain a cycle.

Parameters

graph (dict) –

Dictionary representing the relationships between nodes.

A node can have several edges, which should be represented in the form of values.

key (str): Key out of the dictionary, from which the search is

started.

visited (list): List with keys already visited.

Returns

Members of the graph that are in a cycle. False: If the graph does not contain a cycle.

Return type

List

Raises

GraphKeyError – If a key is missing its relationship. When this happens, there is probably a value missing.

cobramod.core.graph.return_cycles(graph: dict)

Returns a nested list of cyclic paths. These paths might repeat. If the graph does not contain a cycle, the list is empty.

Parameters

graph (dict) – Dictionary representing the relationships between nodes. A node can have several edges, which should be represented in the form of values.

Returns

Nested list with cyclic paths or an empty list if the graph

does not contain a cycle.

Return type

List

cobramod.core.graph.cut_cycle(graph: dict, key: str)

Changes value of the key to None in a given dictionary. It will raise an error if the value is a tuple.

cobramod.core.graph.cut_parents(graph: dict)

Checks if multiple parents shared a common child. If so, the graph will replace the values of these parents to a None and leave one of the parents normal.

Parameters

graph (dict) – Dictionary representing the relationships between nodes. A node can have several edges, which should be represented in the form of values.

cobramod.core.graph.back(graph: dict, value: str, path: list, stop_list: list = []) list

Returns a list with a linear path. The function creates a sequence from the graph dictionary until the given value is found in the sequence or in the given stop_list. The function does not work with graphs that contain some kind of cycle and will raise a recursion error.

Parameters
  • graph (dict) – Dictionary representing the relationships between nodes. A node can have several edges, which should be represented in the form of values.

  • value (str) – The value to be searched..

  • path (list) – The already-visited path.

  • stop_list (list) – Elements that trigger the function to stop, if found.

Returns

A list with the path that ends with the specified value or with

an element of the stop_list.

Return type

List

Raises

RecursionError – If an element is visited more than once due to a cycle.

cobramod.core.graph.verify_paths(paths: list, graph: dict) set

Returns the missing keys that are not present in the given paths list.

Parameters
  • paths (list) – Paths of the given graph.

  • graph (dict) – Dictionary representing the relationships between nodes. A node can have several edges, which should be represented in the form of values.

Returns

Either the missing nodes or an empty set.

Return type

set

cobramod.core.graph.get_paths(graph: dict, stop_list: list) list

Returns a list with the longest path in a graph. This only works with lineal directed paths.

Parameters

graph (dict) – Dictionary representing the relationships between nodes. A node can have several edges, which should be represented in the form of values.

Returns

Paths from given graph

Return type

List

Raises

RecursionError – if the graph is not lineal

cobramod.core.graph.get_mapping(graph: dict, stop_list: list, new: list) list

Gets the mapping for given graph. The mapping defines the longest paths for the graph without including the previous longest path. The function modifies given graph.

Parameters
  • graph (dict) – graph (dict): Dictionary representing the relationships between nodes. A node can have several edges, which should be represented in the form of values. This will be modified.

  • stop_list (list) – Elements that cause the function to stop when found.

  • new (list) – List with new mapping. Must be empty when called for the first time.

Returns

A list with the new mapping. Longest path for each recursion and

rest.

Return type

List

cobramod.core.graph.build_graph(graph: dict) list

Returns the mapping for the given graph. The mapping is defined as a list with a “core” path and its branches. Cyclic graphs will be cut to create a lineal direct graph

Note

If the first element of a branch is not in the “core”, then it is in one of the branches.

Returns

Mapping of the graph.

Return type

List

Raises

GraphKeyError – If graph is missing a value.