7.1.2.8. pta.utils

General utility functions.

7.1.2.8.1. Module Contents

pta.utils.WATER_OR_PROTON_IDS[source]
pta.utils.enable_all_logging(level: int = logging.INFO)[source]

Enable detailed logging in PTA and its dependencies. This is useful to debug possible problems.

Parameters:

level (int, optional) – The desired logging level, by default logging.INFO

pta.utils.floor(value: float, decimals: int = 0) float[source]

Same as math.floor, except that it rounds to a given number of decimals.

Parameters:
  • value (float) – Value to round.

  • decimals (int, optional) – Number of decimals to round to, by default 0.

Returns:

The rounded value.

Return type:

float

pta.utils.ceil(value: float, decimals: int = 0)[source]

Same as math.ceil, except that it rounds to a given number of decimals.

Parameters:
  • value (float) – Value to round.

  • decimals (int, optional) – Number of decimals to round to, by default 0.

Returns:

The rounded value.

Return type:

float

pta.utils.get_internal_reactions(model: cobra.Model) List[str][source]

Gets the identifiers of all internal (non boundary) reactions in the model.

Parameters:

model (cobra.Model) – The target model.

Returns:

List of identifiers.

Return type:

List[str]

pta.utils.tighten_model_bounds(model: cobra.Model, margin: float = 0.0001, round_to_digits: int = 6, prevent_loops: bool = False, fva_result: pandas.DataFrame = None)[source]

Apply FVA to a model to reduce the range of the flux bounds. The FVA result (plus a margin) is applied to each reaction if it is tighter than the bounds in the model.

Parameters:
  • model (cobra.Model) – The model to analyze.

  • margin (float, optional) – Margin to be applied to the FVA result when the computed bounds are tighter than the original ones. This avoids overconstraining the model in case of numerical inaccuracies in the solution. By default 1e-4.

  • round_to_digits (int, optional) – Number of digits to which tighter bounds should be rounded to. This limits the range of the coefficients for optimization problems. By default 6.

  • prevent_loops (bool, optional) – If no FVA solution is provided, determines whether regular or loopless FVA will be used.

  • fva_result (pd.DataFrame, optional) – Precomputed FVA result. If specified, FVA will not be run again, by default None.

pta.utils.find_blocked_reactions_from_fva_solution(model: cobra.Model, prevent_loops: bool = False, zero_cutoff: Optional[float] = None, fva_result: pandas.DataFrame = None) List[str][source]

Similar to cobrapy’s find_blocked_reactions(), but optionally takes a precomputed FVA solution as input.

Parameters:
  • model (cobra.Model) – The model to analyze.

  • prevent_loops (bool, optional) – If no FVA solution is provided, determines whether regular or loopless FVA will be used.

  • zero_cutoff (Optional[float], optional) – Flux value which is considered to effectively be zero. The default is set to use model.tolerance (default None).

  • fva_result (pd.DataFrame, optional) – Precomputed FVA result. If specified, FVA will not be run again, by default None.

Returns:

List of identifiers of the blocked reactions.

Return type:

List[str]

pta.utils.get_candidate_thermodynamic_constraints(model: cobra.Model, metabolites_namespace: str = None, exclude_compartments: List[str] = None) List[str][source]

Selects reactions in the flux space that are suitable to be used as thermodynamic constraints. By default this method selects all internal reactions, excluding water transport all boundary and exchange reactions. Optionally, it can exclude reactions where at least one metabolite belongs to certain compartments.

Parameters:
  • model (Union[FluxSpace, cobra.Model]) – The target model.

  • metabolite_interpreter (MetaboliteInterpreter, optional) – Specifies how to parse metabolite identifiers.

  • exclude_compartments (List[str], optional) – List of identifiers of the compartment to exclude from the candidates.

  • ccache (CompoundCache, optional) – eQuilibrator’s CompoundCache object, used to identify compounds using identifiers from different namespaces. For performance reasons, it is recommended to use a single instance of CompoundCache for all functions in PTA.

Returns:

The identifiers of the candidate reactions.

Return type:

List[str]

pta.utils.get_reactions_in_internal_cycles(model: cobra.Model, biomass_name: Optional[str] = None, atpm_name: str = 'ATPM') List[str][source]

Find all the reactions in a model involved in one or more internal cycles.

Parameters:
  • model (cobra.Model) – The target model.

  • biomass_name (Optional[str], optional) – Identifier of the biomass reaction.

  • atpm_name (str, optional) – Identifier of the ATP maintenance reaction.

Returns:

The identifiers of the reactions involved in internal cycles.

Return type:

List[str]

pta.utils.get_internal_cycles(model: cobra.Model, biomass_name: Optional[str] = None, atpm_name: str = 'ATPM') numpy.ndarray[source]

Uses efmtool to enumerate all the internal cycles in the network.

modelcobra.Model

The target model.

biomass_nameOptional[str], optional

Identifier of the biomass reaction.

atpm_namestr, optional

Identifier of the ATP maintenance reaction.

Returns:

A n-by-e matrix, where e in the number of EFMs and n is the number of reactions, where each column is an EFM representing an internal cycle.

Return type:

np.ndarray

pta.utils.to_reactions_idxs(reactions: Union[List[int], List[str], cobra.DictList, List[cobra.Reaction]], model: cobra.Model) List[int][source]

Utility function to obtain a list of reaction indices from different representations.

Parameters:
  • reactions (Union[List[int], List[str], cobra.DictList, List[cobra.Reaction]]) – Input list of reactions. Reactions can be defined through their index in the model, their identifiers, or with the reactions themselves.

  • model (cobra.Model) – The model in which the reactions are defined.

Returns:

List of reaction indices.

Return type:

List[int]

Raises:

Exception – If the list is not of one of the expected formats.

pta.utils.to_reactions_ids(reactions: Union[List[int], List[str], cobra.DictList, List[cobra.Reaction]], model: cobra.Model) List[str][source]

Utility function to obtain a list of reaction identifiers from different representations.

Parameters:
  • reactions (Union[List[int], List[str], cobra.DictList, List[cobra.Reaction]]) – Input list of reactions. Reactions can be defined through their index in the model, their identifiers, or with the reactions themselves.

  • model (cobra.Model) – The model in which the reactions are defined.

Returns:

List of reaction identifiers.

Return type:

List[str]

Raises:

Exception – If the list is not of one of the expected formats.

pta.utils.apply_transform(value: numpy.ndarray, transform: Tuple[numpy.ndarray, numpy.ndarray]) numpy.ndarray[source]

Applies an affine transform to a matrix.

Parameters:
  • value (np.ndarray) – The matrix to be transformed.

  • transform (Tuple[np.ndarray, np.ndarray]) – Tuple describing the linear (transformation matrix) and affine (translation vector) of the transform.

Returns:

The transformed matrix.

Return type:

np.ndarray

pta.utils.get_path(path: Union[pathlib.Path, str]) pathlib.Path[source]

Gets a Path object from different representations.

Parameters:

path (Union[Path, str]) – A Path object or a string describing the path.

Returns:

A Path object.

Return type:

Path

Raises:

Exception – If the type of the input is not supported.

pta.utils.covariance_square_root(covariance: numpy.ndarray, min_eigenvalue: float = default_min_eigenvalue_tds_basis) numpy.ndarray[source]

Gets a full-rank square root of a covariance matrix.

Parameters:
  • covariance (np.ndarray) – The input covariance matrix.

  • min_eigenvalue (float, optional) – Minimum eigenvalue to keep during the truncated EVD.

Returns:

A full-rank square root of the covariance.

Return type:

np.ndarray

pta.utils.load_example_model(model_name: str) cobra.Model[source]

Load an example SBML model in cobrapy.

Parameters:

model_name (str) – Name of the model file (without extension). This can be any of the models in pta/data/models.

Returns:

The loaded model in cobrapy format.

Return type:

cobra.Model

pta.utils.find_rotation_matrix(x: numpy.ndarray, y: numpy.ndarray) numpy.ndarray[source]

Compute a matrix that rotates the vector x onto vector y (without scaling).

Parameters:
  • x (np.ndarray) – The starting vector.

  • y (np.ndarray) – The destination vector.

Returns:

The computed rotation matrix.

Return type:

np.ndarray