Skip to content

mio.py

MiomNetwork

A minimal class to store a metabolic network.

Attributes:

Name Type Description
S numpy.ndarray

stoichiometric matrix

R numpy.ndarray

Structured array with the reactions. The fields are: - id (int): reaction ID - lb (float): lower bound - ub (float): upper bound - subsystem (str): subsystem - gpr (str): gene-protein-reaction rule

M numpy.ndarray

Structured array with the metabolites. The fields are: - id (int): metabolite ID - name (str): metabolite name - formula (str): metabolite formula

num_reactions property readonly

Number of reactions in the network.

Returns:

Type Description
int

Number of reactions

find_reaction(self, rxn_id)

Find a particular reaction in the metabolic network.

Parameters:

Name Type Description Default
rxn_id str

Name of the reaction

required

Returns:

Type Description
numpy.ndarray

Structured array with the information of the reaction.

Source code in miom/mio.py
def find_reaction(self, rxn_id):
    """Find a particular reaction in the metabolic network.

    Args:
        rxn_id (str): Name of the reaction

    Returns:
        numpy.ndarray: Structured array with the information of the reaction.
    """
    return MiomNetwork._find_reaction(rxn_id, self.R)

export_gem(miom_network, path_to_exported_file)

Export a miom network to a file in the miom format.

Parameters:

Name Type Description Default
miom_network MiomNetwork

an instance of a MiomNetwork

required
path_to_exported_file str

Path to the exported file (e.g. /path/to/file.miom)

required
Source code in miom/mio.py
def export_gem(miom_network, path_to_exported_file):
    """Export a miom network to a file in the miom format.

    Args:
        miom_network (MiomNetwork): an instance of a MiomNetwork
        path_to_exported_file (str): Path to the exported file (e.g. /path/to/file.miom)
    """
    import lzma
    with io.BytesIO() as npz:
        np.savez_compressed(npz,
                            S=miom_network.S,
                            reactions=miom_network.R,
                            metabolites=miom_network.M)
        compressed = lzma.compress(npz.getbuffer())
        # Store to file
        with open(path_to_exported_file, 'wb') as f_out:
            f_out.write(compressed)

load_gem(model_or_path)

Load a metabolic network from a file or URL.

The method supports any format supported by cobrapy (.xml, .yml, .json, .mat) or a miom compressed model (.miom) from a url or a local file path. For the cobra supported formats, you need the cobrapy package installed, and for .mat files, you need both cobrapy and scipy installed.

Parameters:

Name Type Description Default
model_or_path str

Path to a local file or URL pointing to the metabolic network. If the string starts with '@', the file will be loaded from the default github repository.

required

Returns:

Type Description
MiomNetwork

A MiomNetwork instance with the minimal information of the metabolic network required for simulations. It includes the stoichiometric matrix, the list of reactions with the lower and upper bounds, the associated genes and GPR rules, and the list of metabolites.

Source code in miom/mio.py
def load_gem(model_or_path):
    """Load a metabolic network from a file or URL.

    The method supports any format supported by cobrapy (.xml, .yml, .json, .mat)
    or a miom compressed model (.miom) from a url or a local file path. For the cobra
    supported formats, you need the cobrapy package installed, and for .mat files, you
    need both cobrapy and scipy installed.

    Args:
        model_or_path (str): Path to a local file or URL pointing to the metabolic network.
            If the string starts with '@', the file will be loaded from the default github
            repository.

    Returns:
        MiomNetwork: A [MiomNetwork][miom.mio] instance with the minimal information 
            of the metabolic network required for simulations. It includes the stoichiometric
            matrix, the list of reactions with the lower and upper bounds, the associated
            genes and GPR rules, and the list of metabolites.
    """
    extensions = ['.xml', '.yml', '.json', '.mat', '.miom']
    if isinstance(model_or_path, str):
        file = model_or_path
        if model_or_path.startswith("@"):
            # Check if the file has a valid file extension
            if not any(file.endswith(ext) for ext in extensions):
                # Assume is a relative url pointing to a version
                file = _download(DEFAULT_REPOSITORY + model_or_path[1:] + "/default.miom")
            else:
                file = _download(DEFAULT_REPOSITORY + model_or_path[1:])
        elif _is_url(model_or_path):
            file = _download(model_or_path)
        ext = pathlib.Path(file).suffix
        if ext == '.miom' or ext == '.xz' or ext == '.npz':
            return _load_compressed_model(file)
        else:
            return cobra_to_miom(_read_cobra_model(file))
    else:
        return cobra_to_miom(model_or_path)