booldog.io.circuit

Convert a BoolDogModel’s Boolean rules into a logic-circuit graph.

Each node’s Boolean rule (a string using &, |, !) is parsed and expanded into explicit “logical” AND/OR/NOT nodes wired up with edges, in addition to the original “species” nodes. This produces a networkx.DiGraph circuit representation of the model, as used by booldog2networkx/booldog2igraph when as_logic_circuit=True.

Attributes

logger

PAREN_RE

Classes

BooleanOperators

A simple class to define the Boolean operators as class attributes. This

BooleanDiGraph

A networkx.DiGraph subclass that also tracks how many AND, OR,

Functions

clean_line(line)

Strip a bnet-style line down to its Boolean-rule content.

booldog2circuit(model)

Export a BoolDog Boolean model to Networkx DiGraph

resolve_rule(rule, processor)

Parse a Boolean rule and replace it with "logical" nodes and edges in the

resolve_factor(factor, processor)

Parse a single factor of a Boolean rule.

Module Contents

booldog.io.circuit.logger
booldog.io.circuit.PAREN_RE
booldog.io.circuit.clean_line(line)

Strip a bnet-style line down to its Boolean-rule content.

Parameters:

line (str) – A single line of text (e.g. from a bnet file).

Returns:

None if, once stripped, line is exactly the bnet header line (“targets, factors”). Otherwise, the part of line before any trailing “#” comment, stripped of surrounding whitespace.

Return type:

str or None

class booldog.io.circuit.BooleanOperators

A simple class to define the Boolean operators as class attributes. This is used to make the code more readable and maintainable.

AND = '&'

symbol used for the Boolean AND operator in rule strings.

Type:

str

OR = '|'

symbol used for the Boolean OR operator in rule strings.

Type:

str

NOT = '!'

symbol used for the Boolean NOT operator in rule strings.

Type:

str

class booldog.io.circuit.BooleanDiGraph

Bases: networkx.DiGraph

A networkx.DiGraph subclass that also tracks how many AND, OR, and NOT logic nodes it contains, so that newly added logic nodes get unique, sequential labels (e.g. “and_0”, “and_1”, …).

and_count = 0

number of AND nodes added so far, used to generate the next unique “and_<n>” node id.

Type:

int

or_count = 0

number of OR nodes added so far, used to generate the next unique “or_<n>” node id.

Type:

int

not_count = 0

number of NOT nodes added so far, used to generate the next unique “not_<n>” node id.

Type:

int

add_and()

Add a new AND logic node (“and_<n>”) to the graph.

Returns:

node – The identifier of the newly added node (e.g. “and_0”).

Return type:

str

add_or()

Add a new OR logic node (“or_<n>”) to the graph.

Returns:

node – The identifier of the newly added node (e.g. “or_0”).

Return type:

str

add_not()

Add a new NOT logic node (“not_<n>”) to the graph.

Returns:

node – The identifier of the newly added node (e.g. “not_0”).

Return type:

str

booldog.io.circuit.booldog2circuit(model)

Export a BoolDog Boolean model to Networkx DiGraph

Parameters:

model (booldog:BoolDogModel) – A BoolDog object representing a Boolean network.

Returns:

graph – A plain networkx.DiGraph (built from an internal BooleanDiGraph) with one “species” node per model node, plus additional “logical” AND/OR/NOT nodes and edges representing each node’s Boolean rule. An edge of type “update_function” connects the final logic node of a rule to the species node it updates. Nodes whose rule is empty/falsy have no incoming update-function edge (a warning is logged instead).

Return type:

networkx.DiGraph

booldog.io.circuit.resolve_rule(rule, processor)

Parse a Boolean rule and replace it with “logical” nodes and edges in the graph.

The rule can be nested with brackets, which are resolved first.

Parameters:
  • rule (str) – A Boolean rule (e.g. “A & B | C”)

  • processor (function) – A function that takes a list of nodes and an operator, processes them as needed. The operator can be “AND”, “OR”, or “NOT”. The function should return the new node representing the result of applying the operator to the nodes.

Returns:

new_s – Replacement string (the final node)

Return type:

str

booldog.io.circuit.resolve_factor(factor, processor)

Parse a single factor of a Boolean rule.

The factor can be a list of nodes connected by ‘&’, ‘|’, and ‘!’ operators. The factor does not contain brackets.

Parameters:
  • factor (str) – A Boolean factor e.g. “A & B | C”

  • processor (function) – A function that takes a list of nodes and an operator, processes them as needed. The operator can be “AND”, “OR”, or “NOT”. The function should return the new node representing the result of applying the operator to the nodes.

Returns:

s – Replacement string (the final node representing this factor)

Return type:

str