def test_replace_species_in_Reaction(self): c1 = Complex([self.s1, self.s_old]) c2 = Complex([self.s1, self.s_new]) r1 = Reaction.from_massaction([self.s1, self.s_old], [c1], k_forward=1) self.assertTrue( r1.replace_species(self.s_old, self.s_new) == Reaction.from_massaction([self.s1, self.s_new], [c2], k_forward=1))
def test_compile_crn(self): a = Species(name='a') b = Species(name='b') species_list = [a, b] rxn = Reaction.from_massaction(inputs=[a], outputs=[b], k_forward=0.1) CRN = ChemicalReactionNetwork(species_list, [rxn]) # create a component component = Component("comp") # creating a mock update function to decouple the update process from the rest of the code def mock_update_reactions(): rxn = Reaction.from_massaction(inputs=[a], outputs=[b], k_forward=0.1) return [rxn] def mock_update_species(): return [a, b] component.update_species = mock_update_species component.update_reactions = mock_update_reactions mixture = Mixture(components=[component]) crn_from_mixture = mixture.compile_crn() # test that the mixture has the same species as the manually build CRN object self.assertEqual(set(CRN.species), set(crn_from_mixture.species)) # test that the mixture has the same reactions as the manually build CRN object self.assertEqual(CRN.reactions, crn_from_mixture.reactions)
def test_replace_in_a_chemical_reaction_network(self): c1 = Complex([self.s1, self.s_old]) c2 = Complex([self.s1, c1]) species = [self.s1, self.s_old, c1, c2] r1 = Reaction.from_massaction([self.s1, self.s_old], [c1], k_forward=1) crn = ChemicalReactionNetwork(species=species, reactions=[r1]) new_crn = crn.replace_species(self.s_old, self.s_new) self.assertTrue(self.s1 in new_crn.species) self.assertFalse(self.s_old in new_crn.species) self.assertTrue(self.s_new in new_crn.species) self.assertFalse(c1 in new_crn.species) self.assertFalse(c2 in new_crn.species) c1_new = Complex([self.s1, self.s_new]) c2_new = Complex([self.s1, c1_new]) self.assertTrue(c1_new in new_crn.species) self.assertTrue(c2_new in new_crn.species) r1_new = Reaction.from_massaction([self.s1, self.s_new], [c1_new], k_forward=1) self.assertFalse(r1 in new_crn.reactions) self.assertTrue(r1_new in new_crn.reactions)
def test_reaction_protection(self): #tests that Reactions cannot be changed once they are in a CRN S = Species("S") S2 = Species("S2") R = Reaction.from_massaction([S], [S2], k_forward=1.0) R2 = Reaction.from_massaction([S2], [S], k_forward=1.0) CRN = ChemicalReactionNetwork([S, S2], [R]) #Internal reactions copied correctly to return assert R in CRN.reactions assert not R is CRN._reactions[0] #Returned list does not effect internal reactions CRN.reactions[0] = R2 assert R2 not in CRN.reactions #add reactions effects internal reaction list CRN.add_reactions(R2) assert R2 in CRN.reactions assert not R2 is CRN._reactions[1] with self.assertRaisesRegex( AttributeError, "The reactions in a CRN cannot be removed or modified*"): CRN.reactions = [] #test bypassing reaction protection CRN = ChemicalReactionNetwork([], []) CRN.add_reactions([R], copy_reactions=False) assert R is CRN._reactions[0] assert S in CRN.species #test bypassing reaction protection CRN = ChemicalReactionNetwork([], []) CRN.add_reactions([R], add_species=False) assert not S in CRN.species
def test_compile_crn_directives(self): a = Species(name='a') b = Species(name='b') species_list = [a, b] rxn = Reaction.from_massaction(inputs=[a], outputs=[b], k_forward=0.1) CRN = ChemicalReactionNetwork(species_list, [rxn]) # create a component component = Component("comp") # creating a mock update function to decouple the update process from the rest of the code def mock_update_reactions(): rxn = Reaction.from_massaction(inputs=[a], outputs=[b], k_forward=0.1) return [rxn] def mock_update_species(): return [a, b] component.update_species = mock_update_species component.update_reactions = mock_update_reactions mixture = Mixture(components=[component]) #All the directives used below should not change the CRN. #They just remove some checks and safegaurds, but compilation should work the same in this simple case. #directives are best used in specific cases to compile very large models where speed is essential crn_from_mixture1 = mixture.compile_crn(copy_objects = False) # test that the mixture has the same species as the manually build CRN object self.assertEqual(set(CRN.species), set(crn_from_mixture1.species)) # test that the mixture has the same reactions as the manually build CRN object self.assertEqual(CRN.reactions, crn_from_mixture1.reactions) crn_from_mixture2 = mixture.compile_crn(add_reaction_species = False) # test that the mixture has the same species as the manually build CRN object self.assertEqual(set(CRN.species), set(crn_from_mixture2.species)) # test that the mixture has the same reactions as the manually build CRN object self.assertEqual(CRN.reactions, crn_from_mixture2.reactions) crn_from_mixture3 = mixture.compile_crn(initial_concentrations_at_end = True) # test that the mixture has the same species as the manually build CRN object self.assertEqual(set(CRN.species), set(crn_from_mixture3.species)) # test that the mixture has the same reactions as the manually build CRN object self.assertEqual(CRN.reactions, crn_from_mixture3.reactions)
def test_write_sbml_file(self): s1, s2 = Species("S1"), Species("S2") rx1 = Reaction.from_massaction(inputs=[s1], outputs=[s2], k_forward=0.1) crn = ChemicalReactionNetwork(species=[s1, s2], reactions=[rx1]) model_id = 'test_model' document, _ = crn.generate_sbml_model(model_id=model_id) sbml_string = libsbml.writeSBMLToString(document) file_name = 'test_sbml.xml' with patch("builtins.open", new=mock_open()) as _file: crn.write_sbml_file(file_name, model_id=model_id) _file.assert_called_once_with(file_name, 'w') _file().write.assert_called_once_with(sbml_string)
def setUp(self) -> None: """this method gets executed before every test""" self.s1 = Species(name='test_species1') self.s2 = Species(name='test_species2') self.s3 = Species(name='test_species3') self.s4 = Species(name='test_species4') self.s_old = Species("s_old") self.s_new = Species("s_new") self.species_list = [self.s1, self.s2] # creating a valid reaction two species self.rx1 = Reaction.from_massaction(inputs=[self.s1], outputs=[self.s2], k_forward=0.1) self.rxn_list = [self.rx1] self.crn = ChemicalReactionNetwork(species=self.species_list, reactions=self.rxn_list)
def test_reaction_initialization(self): # warns if both input and output species are empty mak = MassAction(k_forward=0.1) with self.assertWarns(Warning): Reaction(inputs=[], outputs=[], propensity_type=mak) # test for invalid propensity type with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], propensity_type=Species) # input must be a valid species object with self.assertRaises(TypeError): Reaction(inputs=['a'], outputs=[], propensity_type=mak) # output must be a valid species object with self.assertRaises(TypeError): Reaction(inputs=[], outputs=['b'], propensity_type=mak) rxn = Reaction.from_massaction(inputs=[], outputs=[], k_forward=0.1, k_reverse=1) # test whether the reaction is registered as reversible self.assertTrue(rxn.is_reversible) # test whether the reaction is registered as massaction self.assertTrue(isinstance(rxn.propensity_type, MassAction)) # test WeightedSpecies inputs sp1 = Species(name='test_species_a') sp2 = Species(name='test_species_b') chem_com_sp1 = WeightedSpecies(species=sp1, stoichiometry=2) chem_com_sp2 = WeightedSpecies(species=sp2, stoichiometry=1) Reaction(inputs=[chem_com_sp1], outputs=[chem_com_sp2], propensity_type=MassAction(k_forward=1)) # test different input and output lists Reaction(inputs=[chem_com_sp1], outputs=[sp2], propensity_type=MassAction(k_forward=1)) # mixing WeightedSpecies and Species is not allowed with self.assertRaises(TypeError): Reaction(inputs=[chem_com_sp1, sp2], outputs=[sp1], propensity_type=MassAction(k_forward=1))
def mock_update_reactions(): rxn = Reaction.from_massaction(inputs=[a], outputs=[b], k_forward=0.1) return [rxn]
def test_check_crn_validity(self): checked_reactions, checked_species = ChemicalReactionNetwork.check_crn_validity( reactions=self.rxn_list, species=self.species_list) # test that the returned species list is the same as the species list supplied self.assertEqual(self.species_list, checked_species) # test that the returned reaction list is the same as the reaction list supplied self.assertEqual(self.rxn_list, checked_reactions) species_list_with_none = self.species_list.copy() # injecting a None to the species list species_list_with_none.append(None) # test whether a non-species object is detected and Value error has been raised # A non-species object was used as a species: [test_species1, test_species2, None]!"' # A non-species object was used as a species: [test_species1, test_species2, None]!" with self.assertRaisesRegex( ValueError, "A non-species object was used as a species!"): ChemicalReactionNetwork.check_crn_validity( reactions=self.rxn_list, species=species_list_with_none) rxn_list_with_none = self.rxn_list.copy() # injecting a None to the reaction list rxn_list_with_none.append(None) # test whether a non-reaction object is detected and Value Error has been raised with self.assertRaisesRegex( ValueError, 'A non-reaction object was used as a reaction!'): ChemicalReactionNetwork.check_crn_validity( reactions=rxn_list_with_none, species=self.species_list) rxn2 = Reaction.from_massaction(inputs=[self.s1], outputs=[self.s3], k_forward=0.1) # test warning raised if a species (in the reaction outputs) is detected which is not part of the species list with self.assertWarnsRegex( Warning, f'are not part of any reactions in the CRN'): ChemicalReactionNetwork.check_crn_validity( reactions=[rxn2], species=self.species_list, show_warnings=True) rxn3 = Reaction.from_massaction(inputs=[self.s4], outputs=[self.s2], k_forward=0.1) # test warning raised if a species (in the reaction inputs) is detected which is not part of the species list with self.assertWarnsRegex( Warning, f'are not part of any reactions in the CRN'): ChemicalReactionNetwork.check_crn_validity( reactions=[rxn3], species=self.species_list, show_warnings=True) # test warning if reaction has unlisted species rxn4 = Reaction.from_massaction(inputs=[self.s4, self.s3], outputs=[self.s2], k_forward=0.1) with self.assertWarnsRegex( Warning, f'are not listed in the Species list, but part of the reactions' ): ChemicalReactionNetwork.check_crn_validity( reactions=[rxn4], species=[self.s4, self.s2], show_warnings=True) # test duplicate reactions are both added rxn_list = [self.rx1, self.rx1] CRN = ChemicalReactionNetwork(species=[self.s1, self.s2], reactions=rxn_list) self.assertTrue(CRN.reactions.count(self.rx1) == 2) with self.assertWarnsRegex(Warning, 'may be duplicated in CRN definitions'): ChemicalReactionNetwork.check_crn_validity( reactions=rxn_list, species=self.species_list, show_warnings=True) # test warning suppression with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. ChemicalReactionNetwork.check_crn_validity( reactions=rxn_list, species=self.species_list, show_warnings=False) assert not w
def R(inputs, outputs): return Reaction.from_massaction(inputs, outputs, k_forward = kb, k_reverse = ku)