KGBN.model_parser

The model_parser module provides functions for merging, converting, and extending Boolean and Probabilistic Boolean Networks.

KGBN.model_parser.simplify_expression(expression)[source]

Simplifies the Boolean expression by removing redundant parentheses using boolean.py package.

Parameters:

expression – The Boolean expression as a string.

Returns:

The simplified expression.

KGBN.model_parser.parse_expression(expression: str)[source]

Parses a Boolean expression to separate activators and inhibitors.

KGBN.model_parser.parse_equations(equations)[source]
KGBN.model_parser.get_upstream(rule_str)[source]
KGBN.model_parser.find_direct_targets(target_nodes, rule_dict)[source]
KGBN.model_parser.check_inhibitor_wins_rule(expression)[source]

Ensures that the “Inhibitor Wins” rule is respected within a single expression.

KGBN.model_parser.merge_networks(BNs, method='OR', prob=0.9, descriptive=False)[source]

Merge multiple Boolean-network models using one of four strategies.

Parameters:
BNslist[BooleanNetwork]

List of Boolean-network objects

method{“OR”, “AND”, “Inhibitor Wins”, “PBN”}

Merge strategy.

probfloat, default 0.9

Only used for PBN. Probability given to equations from the first model

descriptivebool, default False

If True, prints a human-readable merge log.

Returns:
str

A newline-separated network definition string suitable for writing to file. • deterministic modes: gene = Boolean-expression • PBN mode: gene = expr1, p1

gene = expr2, p2 … (one line per rule)

KGBN.model_parser.BN2PBN(bn, prob=0.5, fixed_nodes=None)[source]

Expand the boolean network to a PBN by adding a self-loop as alternative function prob: probability of the equations from the original BN fixed_nodes: list of nodes that are fixed (e.g., output nodes)

Returns:

pbn_string: string of the PBN nodes_to_optimize: list of nodes excludes input nodes

KGBN.model_parser.extend_networks(original_network, new_network, nodes_to_extend, prob=0.5, descriptive=True)[source]

Extend the original network by adding the nodes_to_extend (and their rules) from the new network. This will return a PBN where: - rules for the nodes_to_extend are added to the original network with a probability of prob. - rules for the nodes_to_extend from the original network will have a probability of 1-prob. - in case the new rules for nodes_to_extend include new nodes, they will also be added with a probability of 1. - The direct targets of nodes_to_extend are also extended with both rules and probabilities. - The rest of the rules are kept the same with a probability of 1.

Example Usage

Merging Networks

import KGBN

# Load two networks
network1 = KGBN.load_network_from_file("network1.txt")
network2 = KGBN.load_network_from_file("network2.txt")

# Merge into a Boolean-network definition string
merged_bn_string = KGBN.merge_networks([network1, network2], method='Inhibitor Wins')
merged_bn = KGBN.load_network(merged_bn_string, network_type='bn')

# Or merge into a PBN definition string
merged_pbn_string = KGBN.merge_networks([network1, network2], method='PBN', prob=0.9)
merged_pbn = KGBN.load_network(merged_pbn_string, network_type='pbn')

Converting BN to PBN

import KGBN

# Load a Boolean Network
bn = KGBN.load_network_from_file("network.txt")

# Convert to PBN rules with equal probabilities for existing rules and self-loops
pbn_string, nodes_to_optimize = KGBN.BN2PBN(bn, prob=0.5)
pbn = KGBN.load_network(pbn_string, network_type='pbn')

Extending Networks

import KGBN

# Load original network and KG-derived network
original_bn = KGBN.load_network_from_file("original.txt")
kg_string, _ = KGBN.load_signor_network(gene_list=['TP53', 'MYC'])
kg_network = KGBN.load_network_from_string(kg_string)

# Extend original network with KG information
extended_pbn_string = KGBN.extend_networks(
    original_bn,
    kg_network,
    nodes_to_extend=['GENE1', 'GENE2'],
    prob=0.5, # probability of the rules from the KG
    descriptive=True
)
extended_pbn = KGBN.load_network(extended_pbn_string, network_type='pbn')