7.1.1.1.1.5. 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.

7.1.1.1.1.5.1. Exceptions

GraphKeyError

Simple Error that should be raised when a value is missing as key in a

7.1.1.1.1.5.2. Functions

find_missing(graph)

Checks whether the given graph is missing a key.

find_cycle(graph, key, visited)

return_cycles(graph)

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

cut_cycle(graph, key)

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

cut_parents(graph)

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

back(graph, value, path[, stop_list])

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

verify_paths(paths, graph)

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

get_paths(graph, stop_list)

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

get_mapping(graph, stop_list, new)

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

get_graph_dict(graph)

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

filter_graph(graph, avoid_list)

Returns a new graph, where items are removed if found in given avoid list.

build_lineal_graph(sequence)

Creates a returns a simple lineal directed graph from given sequence

7.1.1.1.1.5.3. Module Contents

exception cobramod.core.graph.GraphKeyError

Bases: Exception

Simple Error that should be raised when a value is missing as key in a graph

cobramod.core.graph.find_missing(graph)

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, key, visited)
Parameters:
Return type:

list[str]

cobramod.core.graph.return_cycles(graph)

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, key)

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

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

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, value, path, stop_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, graph)

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, stop_list)

Returns a list with the longest paths 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.

  • stop_list (list)

Returns:

Paths from given graph

Return type:

List

Raises:

RecursionError – if the graph is not lineal

cobramod.core.graph.get_mapping(graph, stop_list, new)

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.get_graph_dict(graph)

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.

Parameters:

graph (dict[str, Union[str, tuple[str]]])

cobramod.core.graph.filter_graph(graph, avoid_list)

Returns a new graph, where items are removed if found in given avoid list.

Parameters:
Return type:

dict

cobramod.core.graph.build_lineal_graph(sequence)

Creates a returns a simple lineal directed graph from given sequence

Parameters:

sequence (list[str])

Return type:

dict[str, Union[str, None]]