def test_complicated_model(): """Test a complicated model. Difficult model since the online mean calculation is numerically unstable so many samples weakly violate the equality constraints. """ model = Model('flux_split') reaction1 = Reaction('V1') reaction2 = Reaction('V2') reaction3 = Reaction('V3') reaction1.bounds = (0, 6) reaction2.bounds = (0, 8) reaction3.bounds = (0, 10) A = Metabolite('A') reaction1.add_metabolites({A: -1}) reaction2.add_metabolites({A: -1}) reaction3.add_metabolites({A: 1}) model.add_reactions([reaction1, reaction2, reaction3]) optgp = OptGPSampler(model, 1, seed=42) achr = ACHRSampler(model, seed=42) optgp_samples = optgp.sample(100) achr_samples = achr.sample(100) assert any(optgp_samples.corr().abs() < 1.0) assert any(achr_samples.corr().abs() < 1.0) # > 95% are valid assert sum(optgp.validate(optgp_samples) == "v") > 95 assert sum(achr.validate(achr_samples) == "v") > 95
def test_find_boundary_types_sink(model: Model) -> None: """Test boundary type identification for sinks.""" sn = Reaction("sink") model.add_reaction(sn) sn.build_reaction_from_string("atp_c <->") sn.bounds = -1000, 1000 sn = model.sinks assert len(sn) == 1 assert "sink" in [r.id for r in sn]
def add_bigg_reactions(bigg_list, model, ignore_pseudo_reactions=True): """ Create a COBRA reaction from a BiGG reaction. Parameters ---------- bigg_list : list of dict List of dictionaries with BiGG reaction data model : cobra.core.Model Model to add reactions to ignore_pseudo_reactions : bool, optional When True, do not include pseudo reactions """ # Create a Reaction object for each BiGG reaction. reactions = DictList() for bigg_reaction in bigg_list: if bigg_reaction['pseudoreaction'] and ignore_pseudo_reactions: continue reaction = Reaction(id=bigg_reaction['bigg_id'], name=bigg_reaction['name']) reaction.notes['aliases'] = bigg_reaction['database_links'] metabolites = dict() for met in bigg_reaction['metabolites']: metabolite = model.metabolites.get_by_id('{0}_{1}'.format( met['bigg_id'], met['compartment_bigg_id'])) metabolites[metabolite] = met['stoichiometry'] reaction.add_metabolites(metabolites) try: reaction.bounds = (bigg_reaction['results'][0]['lower_bound'], bigg_reaction['results'][0]['upper_bound']) except KeyError: if '⇌' in bigg_reaction['reaction_string']: reaction.bounds = (-1000.0, 1000.0) else: warn('Unknown direction symbol in reaction string {0}'.format( bigg_reaction['reaction_string'])) reactions.append(reaction) # Add all of the reactions to the model. model.add_reactions(reactions) return
def test_model_medium(self, model): # Add a dummy 'malformed' import reaction bad_import = Reaction('bad_import') bad_import.add_metabolites({model.metabolites.pyr_c: 1}) bad_import.bounds = (0, 42) model.add_reaction(bad_import) # Test basic setting and getting methods medium = model.medium model.medium = medium assert model.medium == medium # Test context management with model: # Ensure the bounds are correct beforehand assert model.reactions.EX_glc__D_e.lower_bound == -10 assert model.reactions.bad_import.upper_bound == 42 assert model.reactions.EX_co2_e.lower_bound == -1000 # Make changes to the media new_medium = model.medium new_medium['EX_glc__D_e'] = 20 new_medium['bad_import'] = 24 del new_medium['EX_co2_e'] # Change the medium, make sure changes work model.medium = new_medium assert model.reactions.EX_glc__D_e.lower_bound == -20 assert model.reactions.bad_import.upper_bound == 24 assert model.reactions.EX_co2_e.lower_bound == 0 # Make sure changes revert after the contex assert model.reactions.EX_glc__D_e.lower_bound == -10 assert model.reactions.bad_import.upper_bound == 42 assert model.reactions.EX_co2_e.lower_bound == -1000 new_medium['bogus_rxn'] = 0 with pytest.raises(KeyError): model.medium = new_medium
def test_make_lhs_irreversible_reversible(model: Model) -> None: """Test reaction LHS irreversibility to reversibility.""" rxn = Reaction("test") rxn.add_metabolites({ model.metabolites[0]: -1.0, model.metabolites[1]: 1.0 }) rxn.bounds = (-1000.0, -100) model.add_reaction(rxn) assert rxn.lower_bound == -1000.0 assert rxn.upper_bound == -100.0 assert rxn.forward_variable.lb == 0.0 assert rxn.forward_variable.ub == 0.0 assert rxn.reverse_variable.lb == 100.0 assert rxn.reverse_variable.ub == 1000.0 rxn.upper_bound = 666.0 assert rxn.lower_bound == -1000.0 assert rxn.upper_bound == 666.0 assert rxn.forward_variable.lb == 0.0 assert rxn.forward_variable.ub == 666 assert rxn.reverse_variable.lb == 0.0 assert rxn.reverse_variable.ub == 1000.0