Utils

This module contains some utility function related to cobrapy community models.

pycomo.helper.utils.check_annotation_overlap_of_metabolites_with_identical_id(model_1, model_2)

Retrieves all metabolites with identical IDs in both models, that do not share a single overlap in any of their metabolite annotation key / value pairs.

Parameters:
  • model_1 – Model 1

  • model_2 – Model 2

Returns:

Metabolites with identical IDs in both models, but no overlap in their annotation

pycomo.helper.utils.check_mass_balance_fomula_safe(model)

Checks a model’s reactions for mass and charge balance. In case the mass / charge balance check fails due to wrongly formatted metabolite formulae, add reaction and metabolites to unbalanced reactions and throw a warning.

Parameters:

model – The model to check

Returns:

Dictionary of key reaction and value: dictionary (metabolite: float)

pycomo.helper.utils.check_mass_balance_of_metabolites_with_identical_id(model_1, model_2)

Checks whether metabolites with identical IDs in each of the models has identical mass and charge.

Parameters:
  • model_1 – Model 1

  • model_2 – Model 2

Returns:

List of metabolite IDs of metabolites that do not have identical mass and charge in both models

pycomo.helper.utils.check_metabolite_equal_mass(met1, met2)

This function compares mass and charge of two metabolites. It returns True if the metabolites have equal mass and charge and False if they do not.

Parameters:
  • met1 – metabolite 1

  • met2 – metabolite 2

Returns:

True, if the metabolites have equal mass and charge, else False

pycomo.helper.utils.close_to_zero(num, t=None)

Checks whether a number is within threshold t of 0. Default threshold t is the solver tolerance.

Parameters:
  • num – The number to be checked

  • t – The threshold around 0

Returns:

True if the number is within threshold t of 0, otherwise False

pycomo.helper.utils.create_abundance_parameter(sbml_model, member_id, abundance=None)

Create a parameter for the abundance of a community member.

Parameters:
  • sbml_model – libsbml model

  • member_id – ID of the community member

  • abundance – Abundance fraction of the community member

pycomo.helper.utils.create_parameter_in_sbml_model(sbml_model, pid, is_constant, value=None, as_name=False)

Helper function to set a parameter with ID and value to a SBML model.

Parameters:
  • sbml_model – libsbml model

  • pid – Parameter ID

  • is_constant – Flag whether the parameter is constant

  • value – Value of the parameter

  • as_name – Set the value of the parameter as name of the parameter instead of value

pycomo.helper.utils.find_incoherent_bounds(model)

Find incoherent bounds (lb > ub, bounds with NaN) and return them. :param model: a cobrapy model :return: A list of reactions with incoherent bounds. If none are found, returns an empty list

pycomo.helper.utils.find_loops_in_model(model, processes=None, time_out=30, max_time_out=300)

This function finds thermodynamically infeasible cycles in models. This is accomplished by setting the medium to contain nothing and relax all constraints to allow a flux of 0. Then, FVA is run on all reactions.

Parameters:
  • model – Model to be searched for thermodynamically infeasible cycles

  • processes – The number of processes to use

Returns:

A dataframe of reactions and their flux range, if they can carry non-zero flux without metabolite input

pycomo.helper.utils.find_matching_annotations(met1, met2)

Finds keys of the metabolite annotation dictionary that are present in both metabolites and whose values are identical

Parameters:
  • met1 – Metabolite 1

  • met2 – Metabolite 2

Returns:

A dictionary of identical annotations with annotation keys as keys and their values as values

pycomo.helper.utils.get_abundance_parameters_from_sbml_doc(sbml_model)

Extract the community member abundance fractions from the libsbml model.

Parameters:

sbml_model – libsbml model

Returns:

Dictionary of community member abundances with community member names as keys and abundance fraction as values

pycomo.helper.utils.get_abundance_parameters_from_sbml_file(sbml_file)

Retrieve the community member abundance fractions from a sbml file.

Parameters:

sbml_file – Path to the SBML model file

Returns:

Dictionary of community member abundances with community member names as keys and abundance fraction as values

pycomo.helper.utils.get_exchange_metabolites(model)

Retrieve all metabolites of a model that are part of exchange reactions.

Parameters:

model – The model whose exchange metabolite should be extracted

Returns:

Dictionary of exchange metabolite with metabolite IDs as keys and COBRApy metabolites as values

pycomo.helper.utils.get_f_reactions(model)

Get the IDs of all fraction reactionsm in a PyCoMo community metabolic model.

Parameters:

model – A PyCoMo community metabolic model

Returns:

A set of all fraction reactions

pycomo.helper.utils.get_flags_and_muc_from_sbml_file(sbml_file)

Retrieve the community growth rate and the flags for fixed abundance state, fixed growth state as well as the ID of the shared exchange compartment from a sbml file.

Parameters:

sbml_file – Path to the SBML model file

Returns:

Dictionary of flags and parameters as keys and their values as values

pycomo.helper.utils.get_metabolite_id_without_compartment(metabolite)

Get the ID of a metabolite without the compartment string, should it be included in the metabolite ID.

Parameters:

metabolite – The metabolite whose ID should be extracted

Returns:

The metabolite ID without the compartment string

pycomo.helper.utils.get_model_biomass_compound(model, shared_compartment_name, expected_biomass_id='', generate_if_none=False, return_biomass_rxn=False)

Finds the biomass metabolite in a model. A biomass metabolite can also be created, if the biomass reaction (objective) does not produce any metabolites.

Parameters:
  • model – The model to be searched for a biomass metabolite

  • shared_compartment_name – The ID of the shared exchange compartment

  • expected_biomass_id – The ID of the biomass metabolite if already known or suspected

  • generate_if_none – If True, a biomass metabolite is created if the objective function does not produce a metabolite

  • return_biomass_rxn – If True, return the identified biomass reaction as well

Raises:

KeyError – This error is raised if the biomass metabolite cannot be determined

Returns:

The biomass metabolite as COBRApy metabolite

pycomo.helper.utils.list_contains_unique_strings(str_list)

Checks if a list contains only unique strings

Parameters:

str_list – List of strings to be checked

Returns:

True, if all strings in the list are unique, else False

pycomo.helper.utils.list_of_strings_is_self_contained(str_list)

Checks if a list of strings contains strings that are substrings of other string within the list.

Parameters:

str_list – The list of strings to be checked

Returns:

True, if strings in the list are substrings of others, else False

pycomo.helper.utils.list_without_element(list_var, element)

Returns a copy of a list without the specified element.

Parameters:
  • list_var – List whose element should be excluded

  • element – Element to be excluded

Returns:

A list without target element

pycomo.helper.utils.load_named_model(file, file_format='sbml')

Loads a metabolic model from file and returns the model together with the model file name as name.

Parameters:
  • file – Path to the model file

  • file_format – The file format of the model. Supported formats are sbml, json, mat and yaml/yml

Returns:

the metabolic model as COBRApy model and the model name

pycomo.helper.utils.load_named_models_from_dir(path, file_format='sbml')

Loads all metabolic models from file in the specified directory and of given format. Supported formats are sbml, json, mat and yaml/yml.

Parameters:
  • path – Path to the model file directory

  • file_format – The file format of the models. Supported formats are sbml, json, mat and yaml/yml

Returns:

A dictionary with model names as keys and metabolic models (COBRApy model objects) as values

pycomo.helper.utils.make_model_ids_sbml_conform(model)

Reformat the IDs of metabolites, reactions and genes to ensure they are SBML conform. Also removes any ASCII escapes in the form of digits surrounded by two underscores each.

Parameters:

model – The model to be reformatted

Returns:

The reformatted model

pycomo.helper.utils.make_string_sbml_id_compatible(string, remove_ascii_escapes=False, remove_trailing_underscore=False)

This function converts a string into SBML id compatible format. This format only allows alphanumeric characters a-z, A-Z, 0-9 and the underscore character _.

Parameters:
  • string – The string to be reformatted

  • remove_ascii_escapes – Remove any ascii escapes in the form of digits surrounded by two underscores

  • remove_trailing_underscore – Remove any trailing underscore characters

Returns:

The formatted string

pycomo.helper.utils.read_abundance_from_file(file)

Read community member abundance from file. The file needs to be a csv file, with comma (,) as separator. It needs to contains two columns, model and fraction. Model is the name of the community member model and fraction is the abundance fraction for the respective communtiy member.

Parameters:

file – Path to the abundance csv file

Returns:

An abundance dictionary with community members as keys and fractions as values

pycomo.helper.utils.read_medium_from_file(file, comp)

The file needs to be in the following format: A csv file with two columns separated by a comma (,). The two columns are named compounds and maxFlux. The compounds columns contains the metabolite IDs of the boundary metabolites as in the community metabolic model. The maxFlux column contains the maximum influx of the respective value as an integer or float.

Parameters:
  • file – Path to the medium file

  • comp – The name of the shared exchange compartment (required to convert metabolite IDs into exchange reaction IDs)

Returns:

A dictionary containing the medium with exchange reactions as keys and flux constraints as values

pycomo.helper.utils.read_sbml_model_from_file(file_path)

Reads a metabolic model from file and return it in libsbml format.

Parameters:

file_path – Path to the metabolic model file

Returns:

libsbml model

pycomo.helper.utils.relax_reaction_constraints_for_zero_flux(model)

This function relaxes all constraints of a model to allow a flux of 0 in all reactions.

Parameters:

model – Model to be changed

pycomo.helper.utils.remove_ascii_escape_from_string(text)

Remove any ascii escapes in the form of digits surrounded by two underscores, by removing the outer underscore characters until no more ascii escape patterns remain.

Parameters:

text – The string to be reformatted

Returns:

The formatted string

pycomo.helper.utils.remove_dunder_from_ascii_escape(match_obj)

Remove any ascii escapes in the form of digits surrounded by two underscores, by removing the outer underscore characters

Parameters:

match_obj – The match object containing the ascii escape

Returns:

The match object with the outer underscore characters removed

pycomo.helper.utils.replace_metabolite_stoichiometry(rxn, new_stoich)

This reaction allows for safely and reversibly assigning new reaction stoichiometry. Assigning stoichiometry in add/subtract_metabolites method of cobrapy with combine set to False results in a KeyError, if the metabolite was not previously part of the reaction. As with the cobrapy add/subtract_metabolites method, the stoichiometry of metabolites that are not part of the new stoichiometry, are left unchanged.

Example: r1: a -> b; new_stoich: {c: -1}; => r1: a + c -> b Example: r1: a -> b; new_stoich: {a: 0}; => r1: -> b

Parameters:
  • rxn – Target reaction

  • new_stoich – Dictionary with metabolites (string ID or cobra.Metabolite) as keys and floats as values.