booldog.io.bnet

Additional functions to parse bnet format

Attributes

logger

BNET_LINE_REGEX

BNET_REGULATORS_REGEX

BNET_HEADER

Classes

BnetParser

Parse the body of a bnet-format string into per-node Boolean rules.

Functions

_is_bnet_header(line)

Check whether a line is a bnet header line.

read_bnet(bnet[, node_names])

Parse a Boolean network in BoolNet (bnet) format into the data

write_bnet(model[, outfile, from_primes, header, minimize])

Write a BoolDogModel object to a Boolean network in boolnet (bnet) format.

Module Contents

booldog.io.bnet.logger
booldog.io.bnet.BNET_LINE_REGEX = '^[a-zA-Z]+[a-zA-Z0-9_]*,\\s*.+$'
booldog.io.bnet.BNET_REGULATORS_REGEX = '\\b[A-Za-z_][A-Za-z0-9_]*\\b'
booldog.io.bnet.BNET_HEADER = 'targets, factors'
booldog.io.bnet._is_bnet_header(line)

Check whether a line is a bnet header line.

Mirrors BoolNet’s own tolerant loadNetwork() check (per-field, whitespace- and case-insensitive), rather than an exact match against BNET_HEADER: real bnet files vary in header spacing (e.g. "targets,factors" with no space, as used throughout the biodivine-boolean-models repository, versus BoolNet’s own documented "targets, factors"). Only the first two fields are checked; BoolNet also allows an optional third probabilities field for probabilistic Boolean networks, which is simply ignored here since BoolDog does not otherwise support parsing rules for those.

Parameters:

line (str) – A single, already-stripped line of bnet text.

Return type:

bool

class booldog.io.bnet.BnetParser(bnet)

Parse the body of a bnet-format string into per-node Boolean rules.

Parameters:

bnet (str) – Text in bnet format (one target, rule line per node; blank lines, a header line such as "targets, factors" (see _is_bnet_header() for the accepted variants), and lines starting with # are ignored).

bnet

The raw bnet text passed in at construction.

Type:

str

rules

Mapping of node identifier to its bnet-format rule string (see _get_rules()).

Type:

dict

_get_rules(bnet_str)

Parse bnet text into a dict of node identifier to rule string.

Any regulator referenced in a rule that does not itself appear as a target elsewhere in the text is also added to the returned dict, with an empty string as its rule (i.e. it is treated as an input node with no update function).

Parameters:

bnet_str (str) – Text in bnet format.

Returns:

rules – Mapping of node identifier (str) to rule (str). Nodes with no defined rule (regulator-only/input nodes) map to "".

Return type:

dict

Raises:

ValueError – If a non-blank, non-header, non-comment line does not match the expected target, rule bnet line format.

booldog.io.bnet.read_bnet(bnet, node_names=None)

Parse a Boolean network in BoolNet (bnet) format into the data needed to construct a BoolDogModel.

For complete documentation of the bnet format, see file_exchange.

Parameters:
  • bnet (str) – Either a path to a bnet file, or a string already containing the bnet-format text (checked with os.path.exists; if it isn’t an existing path, it is parsed directly as bnet text).

  • node_names (dict, optional) – Mapping of node identifier to a display name, used to populate BoolDogNode.name for each parsed node. Nodes not present in this dict (or if node_names is None) get no explicit name (see BoolDogNode, whose name defaults to the identifier).

Returns:

data – Dictionary with keys "nodes" (list of BoolDogNode), "modelinfo" (BoolDogModelInfo, with source_format set to "bnet"), and "primes" (None, since primes are not computed by this reader). Suitable for BoolDogModel(**data).

Return type:

dict

Notes

The format of the output file is described at file_exchange.

booldog.io.bnet.write_bnet(model, outfile=None, from_primes=False, header=True, minimize=False)

Write a BoolDogModel object to a Boolean network in boolnet (bnet) format.

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

  • outfile (str) – Path to the output file. If None, the output is returned as a string.

  • from_primes (bool, default False) – If True, rules are obtained by converting prime implicants to bnet format. Otherwise, node rules are written directly.

  • header (bool, default True) – If True, include a header line (“target, factors”).

  • minimize (bool, default False) – If True, minimize rules when converting from primes. Only relevant if from_primes is True.

Returns:

Returns the bnet string if outfile is None, otherwise writes it to outfile and returns None. This holds for both from_primes values.

Return type:

str or None

Notes

The output file will be overwritten if it already exists.

The format of the output file is described at file_exchange.