API for padmet.utils

sbmlPlugin

padmet.utils.sbmlPlugin.ascii_replace(match)[source]

recover banned char from the integer ordinal in the reg.match

padmet.utils.sbmlPlugin.convert_from_coded_id(coded, pattern='__', compart_in_id=False, reaction_tag='R', species_tag='M')[source]

convert an id from sbml format to the original id. try to extract the type of the id and the compart using strong regular expression ex: M_METABOLITE__45__12_c => (‘METABOLITE-12’, ‘M’, ‘c’)

Parameters:
  • coded (str) – the encoded id

  • pattern (str) – pattern used to delimit interger ordinal

  • compart_in_id (bool) – if true: the last _* is not mean to be the compart is part of the id

  • reaciton_tag (str) – First letter used to tag a reaction

  • species_tag (str) – First letter used to tag a species

Returns:

  • str – the uncoded id

  • str, None – type of ID (ex: ‘M’ or ‘R’)

  • str, None – compart of the id

padmet.utils.sbmlPlugin.convert_to_coded_id(uncoded, _type=None, compart=None)[source]

convert an id to sbml valid format. First add type of id “R” for reaction “M” for compound at the start and the compart at the end. _type+”_”+uncoded+”_”+compart then replace not allowed char by integer ordinal :param uncoded: the original id to code :type uncoded: str :param _type: the type of the id (ex: ‘R’ or ‘M’) :type _type: str :param compart: the compartment of the id (ex: ‘c’ or ‘e’) :type compart: str

Returns:

the coded id

Return type:

str

padmet.utils.sbmlPlugin.extractFormula(elementR)[source]

From an SBML reaction_element will return the formula in a string ex: ‘1.0 FRUCTOSELYSINE_p => 1.0 FRUCTOSELYSINE_c’ :param elementR: a reaction from libsbml.element :type elementR: libsbml.element

Returns:

the formula

Return type:

str

padmet.utils.sbmlPlugin.get_all_decoded_version(element_id, _type)[source]

Use convert_from_coded function to convert a element_id (reaction or species) _type use define if element is a ‘reaction’ or un ‘species’. Try different decoding combination based on old and new sbml id encoding.

Parameters:
  • element_id (str) – the encoded id

  • _type (str) – _type is ‘reaction’ or ‘species’

Returns:

list of encoded id

Return type:

list

padmet.utils.sbmlPlugin.parseGeneAssoc(GeneAssocStr)[source]

Given a grammar of ‘and’, ‘or’ and ‘(’ ‘)’. Extracts genes ids to a list. (geneX and geneY) or geneW’ => [geneX,geneY,geneW] :param GeneAssocStr: the string containing genes ids :type GeneAssocStr: str

Returns:

the list of unique ids

Return type:

list

padmet.utils.sbmlPlugin.parseNotes(element)[source]

From an SBML element (ex: species or reaction) will return all the section note in a dictionary. ex: <notes>

<html:body>

<html:p>BIOCYC: |Alkylphosphonates|</html:p> <html:p>CHEBI: 60983</html:p>

</html:body>

</notes>

output: {‘BIOCYC’: |Alkylphosphonates|,’CHEBI’:’60983’} value is a list in case diff lines for the same type of info

Parameters:

element (libsbml.element) – an element from libsbml

Returns:

the dictionary of note

Return type:

dict

Grammar-boolean-rapsody

Aim of this script is to produce, for any pattern like:

a&(b|c)&(d|e)

the resulting lists of elements:

(a, b, d) (a, c, d) (a, b, e) (a, c, e)

Use a finite state machine (see generate_fsm() function) to parse the input,

produce a syntactic tree from the lexical table, that is finally used for find the expected ouput.

The API consist only in the compile_output(1) function. All other functions are used internally, or not used at all

but conserved for science. (only postfix(1) function is in this last case)

The compile_output(1) function is high-level and simple. You may want to

begin there if you want to read these code.

PEP8 is completely busted at some point, but its mainly because of

readability of big lines.

Principles: - lexical analysis of the input string, given the lexems (id, operators, parenthesis) - generate the polish notation of the lexical table - syntactic tree construction - walks in the syntactic tree to determine the possible paths to leafs

Idea is, mainly, that a syntree is simple to store (dict {node:successors}),

and represent well the input data. The walks is performed by the eval_tree function, and simply consist of a recursive DFS with generation of the whole path since the root each time a leaf is hit. Behavior of AND and OR operators are hardcoded.

Note that there is no real error handling for parenthesis.

usage:
    padmet gbr --string=FILE

options:
    -h --help     Show help.
    --string=STR    A string to parse
class padmet.utils.gbr.Command(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Finish = -2
Stack = -1
class padmet.utils.gbr.Error(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
UnexpectedChar = 'unexpected character'
UnexpectedClosing = 'unexpected closing parenthesis'
UnexpectedLetter = 'unexpected letter'
UnexpectedOp = 'unexpected operator'
UnexpectedOpening = 'unexpected opening parenthesis'
class padmet.utils.gbr.Type(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Closing = '\\)'
EndOfFile = '\x00'
Letter = '[a-zA-Z0-9_\\.:-]'
Op = '[&|]'
Opening = '\\('
Other = ''
padmet.utils.gbr.command_help()[source]

Show help for analysis command.

padmet.utils.gbr.compile_input(string, combine_or: bool = False)[source]

Yields the possible combinations parsed from given string. See module docstring for more explanations.

combine_or – if True, will also yield paths (a, b) from ‘a|b’

padmet.utils.gbr.eval_tree(tree: dict, root: int = 1, combine_or: bool = False) iter[source]

Yield paths from input syntree, from given root.

combine_or – if True, will also yield paths (a, b) from ‘a|b’

padmet.utils.gbr.gbr_cli(command_args)[source]
padmet.utils.gbr.generate_fsm()[source]

Return a dict {previous type: {current type: command/error}}

padmet.utils.gbr.generate_syntree(pretable)[source]

Return the syntactic tree of given prefix lexical table

padmet.utils.gbr.lexical_analysis(string)[source]

Generate the lexical table of given string

padmet.utils.gbr.postfix(lextable)[source]

Return the postfix representation of the given lexical table

Wikipedia definition: While there are tokens to be read:

Read a token. If the token is a number, then add it to the output queue.

If the token is an operator, o1, then:
while there is an operator token, o2, at the top of the operator stack, and either

o1 is left-associative and its precedence is less than or equal to that of o2, or o1 is right associative, and has precedence less than that of o2,

then pop o2 off the operator stack, onto the output queue;

push o1 onto the operator stack.

If the token is a left parenthesis (i.e. “(“), then push it onto the stack. If the token is a right parenthesis (i.e. “)”):

Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output queue. Pop the left parenthesis from the stack, but not onto the output queue. If the token at the top of the stack is a function token, pop it onto the output queue. If the stack runs out without finding a left parenthesis, then there are mismatched parentheses.

When there are no more tokens to read:
While there are still operator tokens in the stack:

If the operator token on the top of the stack is a parenthesis, then there are mismatched parentheses. Pop the operator onto the output queue.

Exit.

padmet.utils.gbr.precedence(op1, op2)[source]

True if op1 have precedence on op2. Here AND have an higher precedence than OR.

padmet.utils.gbr.prefix(lextable)[source]
  • Scan input in reversed order:
    • If token is an operand yield it

    • If token is a ) push it to stack

    • If token is an operator o1:
      • pop from stack and yield each operator o2 that have same or higher precedence than o1.

      • puth o2 to stack

    • If token is a (:
      • pop from stack and yield each operator until a ) is encountered.

      • Remove the (

padmet.utils.gbr.type_of(character)[source]

Return the Type value for given character

padmet.utils.gbr.well_parenthesed(lextable)[source]

True iff given lexical table is well parenthesed