def from_formulas(reaction_strings, arrow='<=>', has_reaction_ids=False, raise_exception=False): """ parses a list of reactions in KEGG format Arguments: reaction_strings - a list of reactions in KEGG format arrow - the string used as the 'arrow' in each reaction (default: '<=>') has_reaction_ids - a boolean flag indicating if there is a column of reaction IDs (separated from the reaction with whitespaces) Return values: S - a stoichiometric matrix cids - the KEGG compound IDs in the same order as the rows of S """ try: reactions = [] not_balanced_count = 0 for line in reaction_strings: rid = None if has_reaction_ids: tokens = re.findall('(\w+)\s+(.*)', line.strip())[0] rid = tokens[0] line = tokens[1] try: reaction = KeggReaction.parse_formula(line, arrow, rid) except KeggParseException as e: logging.warning(str(e)) reaction = KeggReaction({}) if not reaction.is_balanced(fix_water=True, raise_exception=raise_exception): not_balanced_count += 1 logging.warning('Model contains an unbalanced reaction: ' + line) reaction = KeggReaction({}) reactions.append(reaction) logging.debug('Adding reaction: ' + reaction.write_formula()) if not_balanced_count > 0: warning_str = '%d out of the %d reactions are not chemically balanced' % \ (not_balanced_count, len(reaction_strings)) logging.debug(warning_str) return KeggModel.from_kegg_reactions(reactions, has_reaction_ids) except ValueError as e: if raise_exception: raise e else: logging.debug(str(e)) return None