parser

Parse csv files to build compound/reaction/graph objects.

rnets.parser.S

Type constraint for StrEnum.

Type:

type

rnets.parser.T

Type variable.

Type:

type

rnets.parser.O

Type variable.

Type:

type

class rnets.parser.CompoundCol(value, names=_not_given, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Posible values for the compound columns.

class rnets.parser.Direction(value, names=_not_given, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Direction of a reaction. Can be Left, Right or Bidirectional.

class rnets.parser.ReactionCol(value, names=_not_given, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Possible values for the reaction columns.

rnets.parser.apply_maybe(fn: Callable[[T], O], s: T | None) O | None[source]

Given a function fn that takes as an input a value of type T and returns a value of type an optional value of type T and O, apply fn only s is not None.

Parameters:
  • fn (function) -- Function that takes a value of type T as input and returns a value of Type O.

  • s (any value or None) -- Value to be used as the input value.

Returns:

Either the projected O value or None.

rnets.parser.assure_compound(cn: str, cs: Sequence[Compound]) Compound[source]

This function serves a wrapper to raise an error in case that the compound is not on the list.

Parameters:
  • cn (Compound) -- Candidate compound name.

  • cs (sequence of Compound) -- Available candidates.

Returns:

In case that the compound is in the sequence, return the

Compound with the given name.

Return type:

Compound

Raises:

ValueError -- If the name is not present in cs.

rnets.parser.parse_compound_line(idx: int, l: str, h: Sequence[CompoundCol] = tuple(CompoundCol)) Compound[source]

Parse a compound line.

Parameters:
  • idx (int) -- Index of the compound.

  • l (str) -- Compound line, with values separated by comma.

  • h (sequence of CompoundCol) -- Order of the columns. Defaults to the order of CompoundCol.

Returns:

with the given values.

Return type:

Compound

Note

Check Compound for possible values and REQ_COMP_COL for

required values.

rnets.parser.parse_compounds(s: str, req: set[CompoundCol] = REQ_COMP_COL) tuple[Compound, ...][source]

Parse compounds in a given string.

Parameters:
  • s (str) -- String containing the header and the compounds separated by new lines.

  • req (set of CompoundCol, optional) -- Required header values. Defaults to REQ_COMP_COL.

Returns:

A tuple containing the parsed compounds.

Return type:

tuple of Compound

rnets.parser.parse_compounds_from_file(f: str | Path) tuple[Compound, ...][source]

Wrapper of parse_compounds but using a file as input.

Parameters:

f (str or Path) -- Name of the file.

Returns:

A tuple containing the parsed Compound

rnets.parser.parse_conc(s: str) float | None[source]

Parse concentration value

Parameters:

s (str) -- String to parse.

Returns:

float with the parsed value or None

rnets.parser.parse_fflags(s: str) set[FFlags][source]

Parse format flags.

s (str): String to parse. Should contain the format options separated by

colons.

Returns:

Set containing the parsed format flags.

Return type:

set of FFlags

Note

The format flags are: 'i' for italics, 'b' for bold and 'u' for

underscore. E.g. i:b.

rnets.parser.parse_lines(s: str, h: type[S], r: set[S], fn: Callable[[int, str, list[S]], T]) tuple[T, ...][source]

Parse a string containing compounds or intermediates in multiple lines.

Parameters:
  • s (str) -- String to parse.

  • h (StrEnum) -- Possible column values.

  • r (set of str) -- Required column values.

  • fn (Callable[int, str, S] -> T) -- Function to parse a single line.

Returns:

Where T with the parsed values. Where T is the output type of :attr:fn.

Return type:

tuple of T

rnets.parser.parse_network(sc: str, sr: str) Network[source]

Parse two strings, one containing the compounds of the network and a second one containing the reactions of the network. To check how they will be parsed, the reader is redicted to the parse_compounds and parse_reactions functions.

Parameters:
  • sc (str) -- String containing the header and the compounds separated by new lines.

  • sr (str) -- String containing the header and the reactions separated by new lines.

Returns:

obj:Network: Network with the parsed compounds and reactions.

rnets.parser.parse_network_from_file(cf: str | Path, rf: str | Path) Network[source]

Wrapper of parse_network but using files as input.

Parameters:
  • cf (str or Path) -- Name of the file containing the compounds.

  • rf (str or Path) -- Name of the file containing the reactions.

Returns:

obj:Network: Network with the parsed compounds and reactions.

rnets.parser.parse_opts(s: str) dict[str, str] | None[source]

Parses the opts column associated with additional opt options.

Parameters:

s (str) -- String containing the pydot options sopearated by ;.

Returns:

keys and values with the opts parsed.

Return type:

dict of [str, str]

Note

Each option should have a label and a value separated by a '=', and

different options should be separated with a ':'.

rnets.parser.parse_reaction_line(idx: int, l: str, cs: Sequence[Compound], h: Sequence[ReactionCol] = tuple(ReactionCol)) tuple[Reaction, ...][source]

Parse a reaction line.

Parameters:
  • idx (int) -- Index of the reaction

  • l (str) -- Reaction line.

  • cs (sequence of Compound) -- Sequence of compounds. It will be used to get the Compound (by matching name) and reference in the reaction instead of using the name.

  • h (sequence of ReactionCol) -- Order of the columns. Defaults to the order of ReactionCol.

Returns:

With the parsed values.

Return type:

Reaction

Note

Check Reaction for possible values and REQ_REACT_COL for

required values.

Bidirectional reactions will return 2 reactions sharing the same

idx. idx is intended to track the order of the reaction in the reactions file, and thus, I decided to label both reactions with the same number.

rnets.parser.parse_reactions(s: str, cs: Sequence[Compound], req: set[ReactionCol] = REQ_REACT_COL) tuple[Reaction, ...][source]

Parse reactions in a given string.

Parameters:
  • s (str) -- String containing the header and the reactions separated by new lines.

  • req (set of ReactionCol, optional) -- Required header values. Defaults to REQ_COMP_COL

Returns:

Containing the parsed reactions.

Return type:

tuple of Reaction

rnets.parser.parse_reactions_from_file(f: str | Path, cs: Sequence[Compound]) tuple[Reaction, ...][source]

Wrapper of parse_reactions but using a file as input.

Parameters:

f (str or Path) -- Name of the file.

Returns:

A tuple containing the parsed Reaction

rnets.parser.parse_vis(s: str, default: Visibility = Visibility.TRUE) Visibility[source]

Parses a string representing the visibility of the node. It can be set to true, false or grey.

Parameters:
  • s (str) -- String to parse.

  • default (Visibility or None, optional) -- Default value to return if parsing fails. Defaults to Visibility.TRUE.

Returns:

Value corresponding to the parsed statement.

Return type:

Visibility

Note

The value is lowercased and compared before parsing. t and true and

parsed as Visibility.TRUE, f and false are parsed as Visibility.FALSE and g and grey are parsed as Visibility.GREY.