moped tutorial¶
import reprlib
import pkg_resources
from moped import Model
from pathlib import Path
def pretty_print(name: str, content: object) -> None:
"""Print a prettier version of descriptor and an object."""
collection_ends = {
"set": "}",
"dict": "}",
"list": "]",
}
content_type = type(content).__name__
content = reprlib.repr(content)
if len(content) > 70:
content = content[:70] + " ... " + collection_ends.get(content_type, "")
print(f"{name:<20}: {content}")
print(pkg_resources.get_distribution("moped").version)
1.6.5
# Parsing metacyc can take a bit, so we cached the results here
metacyc_cache_path = Path("../data/metacyc/25.1/metacyc.pickle")
if metacyc_cache_path.exists():
metacyc = Model.load_from_pickle(metacyc_cache_path)
else:
metacyc = Model()
metacyc.read_from_pgdb(pgdb_path="../data/metacyc/25.1/data")
metacyc.to_pickle(metacyc_cache_path)
Inspecting the model¶
Before you start working with your model, you should always take the time to take a look at it. You can access the information stored in your model directly from the objects themselves or from the model object itself.
In both cases we would like to stress that there are certain terms we are using throughout the software to keep a consistent interface:
- the classes of reactions and compounds are called
Reaction
andCompound
- the objects themselves are called
reaction
andcompound
- the identifiers of those objects are called
reaction_id
andcompound_id
So whenever you see a function use the keyword reaction_id
, it expects a string, while a function that uses the keyword reaction
will use the object.
Both reactions and compounds also have a base_id
. This is the identifier without any prefixes and suffixes.
Getting information about compounds¶
Let's take a look at the compound ATP. Since this is a vital compound in any metabolic network, we can expect to find it in multiple compartments. Let's check this with the Model.get_compound_compartment_variants
method. This expects a compound_base_id
.
metacyc.get_compound_compartment_variants("ATP")
{'ATP_c', 'ATP_e', 'ATP_p'}
As you can see below, if you want to access the compound in most other use cases, you will have to access it with its right compartment. In this case we are using the cytosol, which will have the suffix _c
. To get all supported compartments and their suffixes you can take a look at Model.compartments
.
We decided to always include a suffix instead of implicitly assuming the cytosol to follow the Python mantra Explicit is better than implicit
.
compound = metacyc.compounds["ATP_c"]
pretty_print("Compartment", compound.compartment)
pretty_print("Formula", compound.formula)
pretty_print("Charge", compound.charge)
pretty_print("Compound rxns", compound.in_reaction)
pretty_print("Database links", compound.database_links)
Compartment : 'CYTOSOL' Formula : {'C': 10, 'H': 12, 'N': 5, 'O': 13, ...} Charge : -4 Compound rxns : {'1-PHOSPHATID...RXN__var__0_c', '1.13.12.7-RXN_c', '1.2.1.30-RXN__var ... } Database links : {'BIGG': {'atp'}, 'CAS': {'56-65-5'}, 'CHEBI': {'30616'}, 'CHEMSPIDER' ... }
Alternatively, you can use model functions to get the same information
compound_id = "ATP_c"
pretty_print("Compartment", metacyc.get_compound_compartment(compound_id))
pretty_print("Formula", metacyc.get_compound_formula(compound_id))
pretty_print("Charge", metacyc.get_compound_charge(compound_id))
pretty_print("Compound rxns", metacyc.get_reactions_of_compound(compound_id))
pretty_print("Database links", metacyc.get_compound_database_links(compound_id))
Compartment : 'CYTOSOL' Formula : {'C': 10, 'H': 12, 'N': 5, 'O': 13, ...} Charge : -4 Compound rxns : {'1-PHOSPHATID...RXN__var__0_c', '1.13.12.7-RXN_c', '1.2.1.30-RXN__var ... } Database links : {'BIGG': {'atp'}, 'CAS': {'56-65-5'}, 'CHEBI': {'30616'}, 'CHEMSPIDER' ... }
Getting information about reactions¶
Accessing reactions can be done similarly
metacyc.get_reaction_compartment_variants("ATPSYN-RXN")
{'ATPSYN-RXN_c_p'}
In this case we have the double suffix _c_p
. This means, that this is a transmembrane reaction, that has compounds in multiple compartments. Let's take a look at it:
reaction = metacyc.reactions["ATPSYN-RXN_c_p"]
pretty_print("Stoichiometries", reaction.compartment)
pretty_print("Gibbs 0", reaction.gibbs0)
pretty_print("Bounds", reaction.bounds)
pretty_print("Reversible", reaction.reversible)
pretty_print("Transmembrane", reaction.transmembrane)
pretty_print("Pathways", reaction.pathways)
pretty_print("Sequences", reaction.sequences)
pretty_print("Database links", reaction.database_links)
Stoichiometries : ('CYTOSOL', 'PERIPLASM') Gibbs 0 : -7.598816 Bounds : (-1000, 1000) Reversible : True Transmembrane : True Pathways : {'PWY-7980'} Sequences : {'ATPA-MONOMER': 'MQLNSTEISELI...KGILDSFKATQSW', 'ATPB-MONOMER': 'MASE ... } Database links : {'METANETX-RXN': {'MNXR96136'}, 'RHEA': {'20855'}}
Alternatively, you can use model functions to get the same information
reaction_id = "ATPSYN-RXN_c_p"
pretty_print("Stoichiometries", metacyc.get_reaction_compartment(reaction_id))
pretty_print("Gibbs 0", metacyc.get_reaction_gibbs0(reaction_id))
pretty_print("Bounds", metacyc.get_reaction_bounds(reaction_id))
pretty_print("Reversible", metacyc.get_reaction_reversibility(reaction_id))
pretty_print("Pathways", metacyc.get_reaction_pathways(reaction_id))
pretty_print("Sequences", metacyc.get_reaction_sequences(reaction_id))
pretty_print("Reaction type", metacyc.get_reaction_types(reaction_id))
pretty_print("Database links", metacyc.get_reaction_database_links(reaction_id))
Stoichiometries : ('CYTOSOL', 'PERIPLASM') Gibbs 0 : -7.598816 Bounds : (-1000, 1000) Reversible : True Pathways : {'PWY-7980'} Sequences : {'ATPA-MONOMER': 'MQLNSTEISELI...KGILDSFKATQSW', 'ATPB-MONOMER': 'MASE ... } Reaction type : ['Small-Molecule-Reactions', 'TR-13'] Database links : {'METANETX-RXN': {'MNXR96136'}, 'RHEA': {'20855'}}
Getting collections of objects¶
If you want to get all compounds or reactions of certain types, pathways or compartments, or want to now which types, pathways or compartments are in your model in the first place, we got you covered:
pretty_print("All compartments", metacyc.get_model_compartment_ids())
print()
pretty_print("Base compound ids", metacyc.get_base_compound_ids())
pretty_print("All compound types", metacyc.get_compound_type_ids())
print()
pretty_print("Base reaction ids", metacyc.get_base_reaction_ids())
pretty_print("All reaction types", metacyc.get_reaction_type_ids())
pretty_print("All pathways", metacyc.get_pathway_ids())
All compartments : {'CYTOSOL', 'EXTRACELLULAR', 'PERIPLASM'} Base compound ids : {'+-5-oxo-1-2-campholide', '+-bornyl-diphosphate', '+-epi-beta-santale ... } All compound types : {'1-2-Beta-D-Glucans', '1-5-Galactofuran', '1-Acyl-2-Ara...yl-Glycerol ... } Base reaction ids : {'+-BORNEOL-DEHYDROGENASE-RXN', '+-NEOMENTHOL...DROGENASE-RXN', '+-SAB ... } All reaction types : {'Binding-Reactions', 'Chemical-Reactions', 'Complex-Processes', 'Comp ... } All pathways : ['PWY66-201', 'PWY-6993', 'PWY-6659', 'PWY-7517', 'PWY-5846', 'PWY-760 ... ]
pretty_print("Cytosol compounds", metacyc.get_compounds_of_compartment("CYTOSOL"))
pretty_print("Type compounds", metacyc.get_compounds_of_type("All-Nucleosides"))
print()
pretty_print("Cytosol reactions", metacyc.get_reactions_of_compartment("CYTOSOL"))
pretty_print("Compound reactions", metacyc.get_reactions_of_compound("ATP_c"))
pretty_print("Pathway reactions", metacyc.get_reactions_of_pathway("PWY-101"))
pretty_print("Type reactions", metacyc.get_reactions_of_type("TR-13"))
Cytosol compounds : ['CHITOBIOSE_c', 'CPD-12274_c', 'Archeal-Dolichol-P_c', 'Archeal-Dil-G ... ] Type compounds : {'ADP_c', 'ADP_e', 'ADP_p', 'AMP_c', 'AMP_p', 'CAMP_c', ...} Cytosol reactions : {'+-BORNEOL-DE...OGENASE-RXN_c', '+-NEOMENTHOL...OGENASE-RXN_c', '+-SA ... } Compound reactions : {'1-PHOSPHATID...RXN__var__0_c', '1.13.12.7-RXN_c', '1.2.1.30-RXN__var ... } Pathway reactions : {'1.18.1.2-RXN_c', '1.18.1.2-RXN_e', 'PLASTOQUINOL...N__var__0_c_p', ' ... } Type reactions : {'3.6.3.1-RXN__var__0_c_e', '3.6.3.1-RXN__var__1_c_e', '3.6.3.11-RXN_c ... }