1.2. Removing elements using COBRApy

1.2.1. Removal of reactions/metabolites

In the following example we show how individual reactions or metabolites can be removed from the model based on their ID. More information about the function remove_reactions can be found here and for function remove_metabolites here.

For demonstration purposes we add a reaction. The metabolites in this reactions are also added to the model.

[1]:
from cobramod.test import textbook_kegg
from cobramod import add_reactions
from pathlib import Path


test_model = textbook_kegg.copy()

# Defining directory
dir_data = Path.cwd().resolve().joinpath("memote-model-repository/data")

add_reactions(
    model=test_model,
    obj=["R04382, c"],
    database="KEGG",
    directory=dir_data,
)
test_model.reactions.get_by_id("R04382_c")
Nothing was specified in argument "genome". Reaction "R04382" will not include genes. Please modify if necessary.
Reaction "R04382_c" is already present in the model. Skipping additions.
[1]:
Reaction identifierR04382_c
Name4-(4-deoxy-alpha-D-galact-4-enuronosyl)-D-galacturonate lyase
Memory address 0x750b10288470
Stoichiometry

C06118_c <=> 2.0 C04053_c

Unsaturated digalacturonate <=> 2.0 (4S,5R)-4,5-Dihydroxy-2,6-dioxohexanoate

GPR
Lower bound-1000
Upper bound1000

Here, the metabolite with ID “C04053_c” will be removed.

[2]:
metabolite_to_be_removed = test_model.metabolites.get_by_id("C04053_c")
metabolite_to_be_removed
[2]:
Metabolite identifierC04053_c
Name(4S,5R)-4,5-Dihydroxy-2,6-dioxohexanoate
Memory address 0x750b0bf5f8f0
FormulaC6H8O6
Compartmentc
In 1 reaction(s) R04382_c
[3]:
%xmode Minimal
test_model.remove_metabolites([metabolite_to_be_removed])
test_model.metabolites.get_by_id("C04053_c")
%xmode Context
Exception reporting mode: Minimal
KeyError: 'C04053_c'

Now we remove a reaction. Please note that this reaction contained the previously removed metabolite, which is therefore now absent.

[4]:
reactions_to_be_removed = test_model.reactions.get_by_id("R04382_c")
reactions_to_be_removed
[4]:
Reaction identifierR04382_c
Name4-(4-deoxy-alpha-D-galact-4-enuronosyl)-D-galacturonate lyase
Memory address 0x750b10288470
Stoichiometry

C06118_c <=>

Unsaturated digalacturonate <=>

GPR
Lower bound-1000
Upper bound1000
[5]:
%xmode Minimal
test_model.remove_reactions([reactions_to_be_removed], remove_orphans=True)
test_model.reactions.get_by_id("R04382_c")
%xmode Context
Exception reporting mode: Minimal
KeyError: 'R04382_c'

1.2.2. Removal of pathways/groups

In the following example, we show how individual reactions can be removed from the model using their ID. More information about the function remove_reactions can be found here.

Please note that in Cobrapy reactions are defined as groups. Thus some of the functions in the following use the term ‘group’ instead of ‘pathway’.

For demonstration purposes, we add a pathway using CobraMod.

[6]:
from pathlib import Path
from cobramod import add_pathway
from cobramod.test import textbook_biocyc
# Defining directory
dir_data = Path.cwd().resolve().joinpath("memote-model-repository/data")

test_model = textbook_biocyc.copy()
# Defining database-specific identifiers
sequence = ["PEPDEPHOS-RXN", "PYRUVFORMLY-RXN", "FHLMULTI-RXN"]

print(f'Number of reaction prior addition: {len(test_model.reactions)}')

add_pathway(
    model=test_model,
    pathway=sequence,
    directory=dir_data,
    database="ECOLI",
    compartment="c",
    group="curated_pathway"
)

print(f'Number of reactions after addition: {len(test_model.reactions)}')
# Display in jupyter
test_model.groups.get_by_id("curated_pathway")
Gene-reaction rule for reaction "PEPDEPHOS-RXN" set to "OR". Please modify it if necessary.
Gene-reaction rule for reaction "PYRUVFORMLY-RXN" set to "OR". Please modify it if necessary.
Gene-reaction rule for reaction "FHLMULTI-RXN" set to "OR". Please modify it if necessary.
Non-zero flux test for reaction 'PEPDEPHOS_RXN_c' passed.
Reaction "PEPDEPHOS_RXN_c" added to group "curated_pathway".
The model cannot turnover the following metabolites ['FORMATE_c']. To overcome this, sink reactions were created to simulate their synthesis.
Non-zero flux test for reaction 'PYRUVFORMLY_RXN_c' passed.
Reaction "PYRUVFORMLY_RXN_c" added to group "curated_pathway".
Non-zero flux test for reaction 'FHLMULTI_RXN_c' passed.
Reaction "FHLMULTI_RXN_c" added to group "curated_pathway".
Auxiliary sink reaction for "SK_HYDROGEN_MOLECULE_c" created. Consider removing it and adding the synthesis reactions for the metabolite.
Auxiliary sink reaction for "SK_FORMATE_c" created. Consider removing it and adding the synthesis reactions for the metabolite.
Number of reaction prior addition: 95
Number of       new   | removed entities in
*=====================|===================*
Reactions        3    |    0
Metabolites      2    |    0
Exchange         0    |    0
Demand           0    |    0
Sinks            2    |    0
Genes           12    |    0
Groups           1    |    0

Number of reactions after addition: 100
[6]:
Pathway identifier curated_pathway
Name
Memory address 0x0128690305761248
Reactions involved

PEPDEPHOS_RXN_c, FHLMULTI_RXN_c, PYRUVFORMLY_RXN_c

Genes involved

EG10803, EG10804, EG10480, EG10478, EG10477, EG10476, EG10479, EG10475, EG10285, EG10701, G7627, EG11784

Visualization attributes
  • vertical = False
  • color_negative = None
  • color_positive = None
  • color_quantile = False

 

To remove a pathway completely, all of its members need to be removed first. Otherwise, they would remain in the model but would no longer be associated with the groups.

[7]:
test_model.groups.get_by_id("curated_pathway")
[7]:
Pathway identifier curated_pathway
Name
Memory address 0x0128690305761248
Reactions involved

PEPDEPHOS_RXN_c, FHLMULTI_RXN_c, PYRUVFORMLY_RXN_c

Genes involved

EG10803, EG10804, EG10480, EG10478, EG10477, EG10476, EG10479, EG10475, EG10285, EG10701, G7627, EG11784

Visualization attributes
  • vertical = False
  • color_negative = None
  • color_positive = None
  • color_quantile = False

 

[8]:
members = test_model.groups.get_by_id("curated_pathway").members
test_model.groups.get_by_id("curated_pathway").remove_members(members)

test_model.groups.get_by_id("curated_pathway")
[8]:
Pathway identifier curated_pathway
Name
Memory address 0x0128690305761248
Reactions involved

Genes involved

Visualization attributes
  • vertical = False
  • color_negative = None
  • color_positive = None
  • color_quantile = False

 

The now-empty group can be removed without further effort and without leaving any elements behind.

[9]:
%xmode Minimal
group_to_be_removed = test_model.groups.get_by_id("curated_pathway")
test_model.remove_groups([group_to_be_removed])
test_model.groups.get_by_id("curated_pathway")
%xmode Context
Exception reporting mode: Minimal
KeyError: 'curated_pathway'