示例#1
0
    def test_get_mechanism(self):
        M1_comp = Mechanism(name = "m1_comp", mechanism_type = "shared")
        M1_mix = Mechanism(name = "m1_mix", mechanism_type = "shared")
        M2_comp = Mechanism(name = "m2_comp", mechanism_type = "comp")
        M2_mix = Mechanism(name = "m2_mix", mechanism_type = "mixture")

        #Create a Mixture and Component with the above mechanisms
        C = Component(name = "comp", mechanisms = [M1_comp, M2_comp])
        M = Mixture(mechanisms = [M1_mix, M2_mix], components = [C])

        #Get the copy of C in M
        C_copy = M.get_component(component = C)

        self.assertTrue(C_copy.get_mechanism("shared").name == "m1_comp")
        self.assertTrue(M.get_mechanism("shared").name == "m1_mix")
        self.assertTrue(C_copy.get_mechanism("comp").name == "m2_comp")
        self.assertTrue(C_copy.get_mechanism("mixture").name == "m2_mix")

        #Make sure the Mixture get_mechanism works as well, just in case.
        self.assertTrue(M.get_mechanism("comp") is None)

        #test get Mechanism with no_key_error = False (Default)
        with self.assertRaisesRegex(KeyError, "Unable to find mechanism of type"):
            C_copy.get_mechanism("DNE")

        #test get_mechanism with no_key_error = True
        self.assertTrue(C_copy.get_mechanism("DNE", optional_mechanism = True) is None)
示例#2
0
    def test_compile_crn(self):
        from biocrnpyler import ChemicalReactionNetwork
        from biocrnpyler import Species
        from biocrnpyler import Reaction
        from biocrnpyler import Mixture

        a = Species(name='a')
        b = Species(name='b')

        species_list = [a, b]

        def mock_update_reactions():
            rxn = Reaction(inputs=[a], outputs=[b], k=0.1)
            return [rxn]

        rxn = Reaction(inputs=[a], outputs=[b], k=0.1)

        CRN = ChemicalReactionNetwork(species_list, [rxn])

        mixture = Mixture(species=species_list)
        mixture.update_reactions = mock_update_reactions

        crn_from_mixture = mixture.compile_crn()
        self.assertEqual(CRN.species, crn_from_mixture.species)
        self.assertEqual(CRN.reactions, crn_from_mixture.reactions)
示例#3
0
    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_species_initial_condition_in_mixture():

    S1 = Species("S1")
    S2 = Species("S2")

    C = ChemicalComplex([S1, S2], name="C")
    S3 = C.get_species()

    mixture_name = "M"
    key1 = ParameterKey(mechanism="initial concentration",
                        part_id=mixture_name,
                        name=str(S1))
    key2 = ParameterKey(mechanism="initial concentration",
                        part_id=mixture_name,
                        name=C.name)

    M = Mixture(name=mixture_name,
                components=[C],
                parameters={
                    key1: 10,
                    key2: 2.2
                })

    #Initial condition found under the Species name
    assert parameter_to_value(M.get_initial_concentration(S1)[S1]) == 10

    #Initial condition defaults to 0
    assert parameter_to_value(M.get_initial_concentration(S2)[S2]) == 0

    #Initial condition found under the Component name
    assert parameter_to_value(M.get_initial_concentration(S3, C)[S3]) == 2.2
示例#5
0
    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)
示例#6
0
    def test_compoents_in_multiple_mixtures(self):
        C = Component("comp")
        M1 = Mixture(components = [C])

        #M1 should have a copy of C, not C itself
        self.assertTrue(type(M1.get_component(component = C)) == Component)
        self.assertTrue(C not in M1._components)

        #C.mixture should not be set, only it's copy
        self.assertTrue(C.mixture is None)
        self.assertTrue(M1.get_component(component = C).mixture is M1)

        #Add C to another Mixture
        M2 = Mixture(components = [C])
        self.assertTrue(type(M2.get_component(component = C))  == Component)
        self.assertTrue(M2.get_component(component = C).mixture is M2)
        self.assertTrue(M1.get_component(component = C) != M2.get_component(component = C))
示例#7
0
    def test_add_species_to_crn(self):
        species = Species(name='H2O')
        mixture = Mixture(species=[species])
        mixture.add_species_to_crn(species, None)

        self.assertTrue(species in mixture.crn.species)

        dna = DNA(name='test_DNA')
        mixture.add_components(dna)

        mixture.add_species_to_crn(dna.update_species(), dna)

        for s_dna in dna.update_species():
            self.assertTrue(s_dna in mixture.crn.species)
示例#8
0
    def test_update_reactions(self):
        mixture = Mixture()
        with self.assertRaisesRegexp(AttributeError, 'Mixture.crn_species not defined.'):
            mixture.update_reactions()

        component = Component(name='test_component')

        # creating a mock update function to decouple the update process from the rest of the code
        def mock_update_reactions():
            rxn = Reaction(inputs=[], outputs=[], k=0.1)
            return [rxn]

        component.update_reactions = mock_update_reactions

        mixture.add_components(component)
        mixture.update_species()
        crn_rxn = mixture.update_reactions()
        crn_rxn_mock = mock_update_reactions()
        self.assertEqual(crn_rxn, crn_rxn_mock)
def test_species_initial_condition_defaulting():

    S1 = Species("S1")
    S2 = Species("S2")

    C = ChemicalComplex([S1, S2], name="C")
    S3 = C.get_species()

    mixture_name = "M"
    key1 = ParameterKey(mechanism="initial concentration",
                        part_id=mixture_name,
                        name=str(S1))
    key2 = ParameterKey(mechanism="initial concentration",
                        part_id=mixture_name,
                        name=str(S2))
    key3 = ParameterKey(mechanism="initial concentration",
                        part_id=mixture_name,
                        name=str(S3))

    C = ChemicalComplex([S1, S2],
                        name="C",
                        parameters={
                            key1: .11,
                            key2: .22
                        },
                        initial_concentration=.33)
    M = Mixture(name=mixture_name,
                components=[C],
                parameters={
                    key1: 1.1,
                    key2: 2.2,
                    key3: 3.3
                })

    #Initial condition found under the Species name, in the Component, not Mixture
    assert parameter_to_value(M.get_initial_concentration(S1, C)[S1]) == .11
    assert parameter_to_value(M.get_initial_concentration(S2, C)[S2]) == .22
    assert parameter_to_value(M.get_initial_concentration(S3, C)[S3]) == .33
示例#10
0
    def test_compile_crn(self):
        a = Species(name='a')
        b = Species(name='b')

        species_list = [a, b]

        # creating a mock update function to decouple the update process from the rest of the code
        def mock_update_reactions():
            rxn = Reaction(inputs=[a], outputs=[b], k=0.1)
            return [rxn]

        rxn = Reaction(inputs=[a], outputs=[b], k=0.1)

        CRN = ChemicalReactionNetwork(species_list, [rxn])

        mixture = Mixture(species=species_list)
        mixture.update_reactions = mock_update_reactions

        crn_from_mixture = mixture.compile_crn()
        # test that the mixture has the same species as the manually build CRN object
        self.assertEqual(CRN.species, 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)
示例#11
0
    def test_add_species(self):
        species = Species('test_species')
        mixture = Mixture()
        mixture.add_species(species)

        # test that the new mixture only contain one species
        self.assertEqual([species], mixture.added_species)

        # adding invalid species
        with self.assertRaisesRegex(AssertionError,'only Species type is accepted!'):
            mixture.add_species(['ok', 'ok'])
示例#12
0
    def test_update_reactions(self):
        from biocrnpyler import Mixture
        from biocrnpyler import Reaction
        from biocrnpyler import Component

        mixture = Mixture()
        with self.assertRaises(AttributeError):
            mixture.update_reactions()

        component = Component(name='test_component')

        def mock_update_reactions():
            rxn = Reaction(inputs=[], outputs=[], k=0.1)
            return [rxn]

        component.update_reactions = mock_update_reactions

        mixture.add_components(component)
        mixture.update_species()
        crn_rxn = mixture.update_reactions()
        crn_rxn_mock = mock_update_reactions()
        self.assertEqual(crn_rxn, crn_rxn_mock)
示例#13
0
    def test_add_species(self):
        from biocrnpyler import Mixture
        from biocrnpyler import Species

        species = Species('test_species')
        mixture = Mixture()
        mixture.add_species(species)

        self.assertEqual([species], mixture.added_species)

        with self.assertRaises(AssertionError):
            mixture.add_species(['ok', 'ok'])
示例#14
0
    def test_add_global_mechanism(self):
        mixture = Mixture()
        GM = GlobalMechanism(name = "gm", mechanism_type = "global")

        mixture.add_global_mechanism(GM)
        #Global Mechanisms should be copied!
        self.assertTrue(mixture.global_mechanisms["global"] != GM)
        self.assertTrue(mixture.global_mechanisms["global"].name == GM.name)

        #test setter to clear
        mixture.global_mechanisms = {}
        self.assertTrue("global" not in mixture.global_mechanisms)
        #test add via add_mechanisms with different key
        mixture.add_mechanisms({"key2":GM})
        self.assertTrue("global" not in mixture.global_mechanisms)
        self.assertTrue("key2" in mixture.global_mechanisms)
示例#15
0
    def test_update_species(self):
        species = Species(name='H2O')
        mixture = Mixture(species=[species])

        self.assertTrue(species in mixture.update_species())

        dna = DNA(name='test_DNA')
        mixture.add_components(dna)

        crn_list = mixture.update_species()

        for s_dna in dna.update_species():
            self.assertTrue(s_dna in crn_list)
示例#16
0
    def test_add_components(self):

        mixture = Mixture()
        # test that new mixture has no components
        self.assertTrue(len(mixture.components) == 0)
        component = Component('test_comp')
        mixture.add_components(component)
        # test that the new components was added to the mixture
        self.assertTrue(component in mixture.components)

        species = Species('test_species')
        # species are invalid components
        with self.assertRaisesRegexp(AssertionError,f'the object: {species} passed into mixture as component must be of the class Component'):
            mixture.add_components(species)
示例#17
0
    def test_add_components(self):
        from biocrnpyler import Mixture
        from biocrnpyler import Component
        from biocrnpyler import Species

        mixture = Mixture()
        self.assertTrue(len(mixture.components) == 0)
        component = Component('test_comp')
        mixture.add_components(component)

        self.assertTrue(component in mixture.components)

        species = Species('test_species')
        with self.assertRaises(AssertionError):
            mixture.add_components(species)
示例#18
0
    def test_update_species(self):
        from biocrnpyler import Mixture
        from biocrnpyler import Species
        from biocrnpyler import DNA
        # from biocrnpyler import Dilution

        species = Species(name='H2O')
        mixture = Mixture(species=[species])

        self.assertTrue(species in mixture.update_species())

        dna = DNA(name='test_DNA')
        mixture.add_components(dna)

        crn_list = mixture.update_species()

        for s_dna in dna.update_species():
            self.assertTrue(s_dna in crn_list)
示例#19
0
    def test_deg_tagged_degredation(self):

        M = Mixture(parameters = {"kdeg":1, "kb":1, "ku":1})

        protease = Species("P")
        tagged_protein = Species("X", attributes = ["degtagged"])
        untagged_protein = Species("Y")

        #compare deg_Tagged_Degredation to MichaelisMenten
        MM = MichaelisMenten(name = "name", mechanism_type = "type")
        
        dtd = Deg_Tagged_Degredation(protease)
        #test default global mechanism parameters
        self.assertTrue(dtd.recursive_species_filtering is False)
        self.assertTrue(dtd.default_on is False)
        self.assertTrue("degtagged" in dtd.filter_dict)
        self.assertTrue(dtd.filter_dict["degtagged"] is True)
        self.assertTrue(len(dtd.filter_dict)==1)

        #test default functionality on a tagged species
        species = dtd.update_species(tagged_protein, M)
        rxns = dtd.update_reactions(tagged_protein, M)
        species_mm = MM.update_species(Enzyme = protease, Sub = tagged_protein, Prod = None)
        rxns_mm = MM.update_reactions(Enzyme = protease, Sub = tagged_protein, Prod = None, kb=1, ku=1, kcat=1)

        #species
        self.assertTrue(species_mm == species)
        #reactions
        self.assertTrue(all([str(rxns[i]) == str(rxns_mm[i]) for i in range(len(rxns))]))

        #Overwrite default_on, recursive_species_filtering, and filter_dict
        dtd2 = Deg_Tagged_Degredation(protease, default_on = True, recursive_species_filtering = True, filter_dict = {"test":True})
        self.assertTrue(dtd2.recursive_species_filtering is True)
        self.assertTrue(dtd2.default_on is True)
        self.assertTrue("degtagged" not in dtd2.filter_dict)
        self.assertTrue("test" in dtd2.filter_dict and dtd2.filter_dict["test"] is True)
        self.assertTrue(len(dtd2.filter_dict)==1)
示例#20
0
    def test_rna_degredation_mm(self):

        M = Mixture(parameters = {"kdeg":1, "kb":1, "ku":1})

        rnaase = Species("P")
        rna = Species("X", material_type = "rna")

        #compare deg_Tagged_Degredation to MichaelisMenten
        MM = MichaelisMenten(name = "name", mechanism_type = "type")
        
        rdmm = Degredation_mRNA_MM(rnaase)
        #test default global mechanism parameters
        self.assertTrue(rdmm.recursive_species_filtering is True)
        self.assertTrue(rdmm.default_on is False)
        self.assertTrue("rna" in rdmm.filter_dict)
        self.assertTrue(rdmm.filter_dict["rna"] is True)
        self.assertTrue(len(rdmm.filter_dict)==2)

        #test default functionality on a tagged species
        species = rdmm.update_species(rna, M)
        rxns = rdmm.update_reactions(rna, M)
        species_mm = MM.update_species(Enzyme = rnaase, Sub = rna, Prod = None)
        rxns_mm = MM.update_reactions(Enzyme = rnaase, Sub = rna, Prod = None, kb=1, ku=1, kcat=1)

        #species
        self.assertTrue(species_mm == species)
        #reactions
        self.assertTrue(all([str(rxns[i]) == str(rxns_mm[i]) for i in range(len(rxns))]))

        #Overwrite default_on, recursive_species_filtering, and filter_dict
        rdmm2 = Degredation_mRNA_MM(rnaase, default_on = True, recursive_species_filtering = False, filter_dict = {"test":True})
        self.assertTrue(rdmm2.recursive_species_filtering is False)
        self.assertTrue(rdmm2.default_on is True)
        self.assertTrue("rna" not in rdmm2.filter_dict)
        self.assertTrue("test" in rdmm2.filter_dict and rdmm2.filter_dict["test"] is True)
        self.assertTrue(len(rdmm2.filter_dict)==1)
示例#21
0
    def test_add_components(self):

        mixture = Mixture()
        # test that new mixture has no components
        self.assertTrue(len(mixture.components) == 0)
        component = Component('test_comp')
        mixture.add_component(component)

        # test that the new components was added to the mixture
        self.assertTrue(mixture.get_component(component = component) is not None)
        #test that it was added by copying
        self.assertTrue(component not in mixture._components)
        #test that the same component cannot be added again
        with self.assertRaisesRegex(ValueError,f'{component} of the same type and name already in Mixture!'):
            mixture.add_component(component)


        #use the constructor the other way
        mixture = Mixture(components = [component])
        # test that the new components was added to the mixture
        self.assertTrue(mixture.get_component(component = component) is not None)
        #test that it was added by copying
        self.assertTrue(component not in mixture._components)


        species = Species('test_species')
        # species are invalid components
        with self.assertRaisesRegex(ValueError,f'add_components expected a list of Components.'):
            mixture.add_components(species)
示例#22
0
    def test_add_mechanisms(self):

        mixture = Mixture()

        tx = SimpleTranscription()
        tl = SimpleTranslation()

        test_mech = {tx.mechanism_type: tx, tl.mechanism_type: tl}

        # test that mixture has no mechanism
        self.assertTrue(isinstance(mixture.mechanisms, dict) and len(mixture.mechanisms) == 0)

        #test mechanism setter
        mixture.mechanisms = test_mech
        self.assertEqual(mixture.mechanisms.keys(), test_mech.keys())

        #test mechanisms are copied
        self.assertEqual(type(mixture.mechanisms[tx.mechanism_type]),  type(test_mech[tx.mechanism_type]))
        self.assertFalse(mixture.mechanisms[tx.mechanism_type] == test_mech[tx.mechanism_type])

        #remove all mechanisms
        mixture.mechanisms = {}
        self.assertEqual(mixture.mechanisms, {})

        #test add_mechanism
        mixture.add_mechanism(tx, tx.mechanism_type)
        mixture.add_mechanism(tl)
        self.assertEqual(mixture.mechanisms.keys(), test_mech.keys())

        #test add_mechanisms with list
        mixture.mechanisms = {}
        test_mech_list = list(test_mech.values())
        mixture.add_mechanisms(test_mech_list)
        self.assertEqual(mixture.mechanisms.keys(), test_mech.keys())
示例#23
0
    def test_get_component(self):
        c1 = Component('c1')
        c2 = Component('c2')
        c3 = DNA('c1')
        c4 = DNA("c2")
        mixture = Mixture()
        mixture.add_components([c1, c2, c3])

        #Test with name
        #nothing named c3 is in the mixture
        self.assertTrue(mixture.get_component(name = "c3") is None)
        #two things named c2 are in the mixture
        self.assertTrue(type(mixture.get_component(name = "c1")) is list)
        #one thing named c1 is in the mixture
        self.assertTrue(type(mixture.get_component(name = "c2")) is Component)

        #test with component
        self.assertTrue(type(mixture.get_component(component = c1)) is Component)
        self.assertTrue(type(mixture.get_component(component = c2)) is Component)
        self.assertTrue(type(mixture.get_component(component = c3)) is DNA)
        self.assertTrue(mixture.get_component(component = c4) is None)

        #Test with index
        self.assertTrue(type(mixture.get_component(index = 1)) is Component)
        self.assertTrue(type(mixture.get_component(index = 2)) is DNA)
        with self.assertRaisesRegex(IndexError,""):
            mixture.get_component(index = 3)

        with self.assertRaisesRegex(ValueError,""):
            mixture.get_component(index = 3, component = c1)

        with self.assertRaisesRegex(ValueError,""):
            mixture.get_component(component = "c1")

        with self.assertRaisesRegex(ValueError,""):
            mixture.get_component(name = c1)

        with self.assertRaisesRegex(ValueError,""):
            mixture.get_component(index = c1)