Esempio n. 1
0
def test_complex_with_single_polymer():
    a = Species('A')
    b = Species('B')
    p = OrderedPolymerSpecies([a, b, a])

    #This should just produce a ComplexSpecies around the PolymerSpecies
    c2 = Complex([p, Species("S")])
    assert c2 == ComplexSpecies([p, Species("S")], called_from_complex=True)

    #If the Polymer is in a Conformation, it cannot be Complexed.
    pc = PolymerConformation(polymer=p)
    assert str(pc.polymers[0]) == str(p)
    assert pc.polymers[0].parent == pc
    assert p.parent is None

    with pytest.raises(ValueError):
        c2 = Complex([pc.polymers[0], Species("S")])

    #A monomer form the polymer can still be complexed, however
    c2 = Complex([pc.polymers[0][0], Species("S")])
    assert isinstance(c2.parent, PolymerConformation)
    assert c2.parent != pc
    assert len(c2.parent.complexes) == 1
    assert len(c2.parent.polymers) == 1
    assert str(c2) == str(
        ComplexSpecies([pc.polymers[0][0], Species("S")],
                       called_from_complex=True))
Esempio n. 2
0
    def test_contains(self):
        s1 = Species(name='s1', material_type="m1")
        s2 = Species(name='s2', material_type="m2")
        s3 = Species("s3")

        c1 = ComplexSpecies([s1, s2, s1], called_from_complex=True)
        c2 = ComplexSpecies([c1, s3], called_from_complex=True)

        self.assertTrue(s1 in c1)
        self.assertTrue(s2 in c1)
        self.assertFalse(c1 in c1)
        self.assertTrue(s1 in c2)
        self.assertTrue(s2 in c2)
        self.assertTrue(c1 in c2)
        self.assertTrue(s3 in c2)

        #contains checks the direction, position, and parent as well.
        p = OrderedPolymerSpecies([s1, [s2, "reverse"]])
        p2 = OrderedPolymerSpecies([s2, [s2, "reverse"]])
        c3 = ComplexSpecies([p[0], p[1]], called_from_complex=True)
        assert not s1 in c3
        assert not s2 in c3
        assert p[0] in c3
        assert p[1] in c3
        assert not p2[1] in c3
        assert p[0] not in c1
Esempio n. 3
0
def test_complex_with_multiple_polymers():
    a = Species('A')
    b = Species('B')
    p = OrderedPolymerSpecies([a, b, a])
    pc0 = PolymerConformation(polymer=p)
    p2 = OrderedPolymerSpecies([b, a, b])
    pc2 = PolymerConformation(polymer=p2)

    #Bind one two monomers from one polymer
    #Polymers must be placed into a Conformation before being bound together
    with pytest.raises(TypeError):
        c = Complex([Species("S"), p[0], p[2]])

    c = Complex([Species("S"), pc0.polymers[0][0], pc0.polymers[0][2]])

    #Correct parent
    assert c.parent == PolymerConformation(
        [ComplexSpecies([Species("S"), p[0], p[2]], called_from_complex=True)])
    #correct complex returned
    assert str(c) == str(
        ComplexSpecies([Species("S"), p[0], p[2]], called_from_complex=True))

    #Ordered Case
    oc = Complex([Species("S"), pc0.polymers[0][0], pc0.polymers[0][2]],
                 ordered=True)
    #Correct parent
    assert oc.parent == PolymerConformation([
        OrderedComplexSpecies([Species("S"), p[0], p[2]],
                              called_from_complex=True)
    ])
    #correct complex returned
    assert str(oc) == str(
        OrderedComplexSpecies([Species("S"), p[0], p[2]],
                              called_from_complex=True))

    #Two polymers which bind together
    #Polymers must be placed into a Conformation before being bound together
    with pytest.raises(TypeError):
        c2 = Complex([Species("S"), p[0], p2[1]])

    c2 = Complex([Species("S"), pc0.polymers[0][0], pc2.polymers[0][1]])
    assert c2.parent == PolymerConformation([
        ComplexSpecies([Species("S"), p[0], p2[1]], called_from_complex=True)
    ])
    assert str(c2) == str(
        ComplexSpecies([Species("S"), p[0], p2[1]], called_from_complex=True))

    #Two polymers already bound together in a Conformation interacting at a new location
    l1 = c2.parent.polymers[0][1]
    l2 = c2.parent.polymers[1][0]
    c3 = Complex([l1, l2], ordered=True)
    assert c3.parent == PolymerConformation([
        ComplexSpecies([Species("S"), p[0], p2[1]], called_from_complex=True),
        OrderedComplexSpecies([p[1], p2[0]], called_from_complex=True)
    ])
    assert str(c3) == str(
        OrderedComplexSpecies([p[1], p2[0]], called_from_complex=True))
    def test_species_equality(self):

        s1 = Species(name='s1', material_type="m1")
        s2 = Species(name='s2', material_type="m2")

        c1 = ComplexSpecies([s1, s2])
        c2 = ComplexSpecies([s2, s1])
        # Check equality of differently ordered complexes
        self.assertEqual(c1, c2)

        oc1 = OrderedComplexSpecies([s2, s1])
        oc2 = OrderedComplexSpecies([s1, s2])
        self.assertFalse(oc1 == oc2)
Esempio n. 5
0
    def test_species_equality(self):

        s1 = Species(name='s1', material_type="m1")
        s2 = Species(name='s2', material_type="m2")

        c1 = ComplexSpecies([s1, s2], called_from_complex=True)
        c2 = ComplexSpecies([s2, s1], called_from_complex=True)
        # Check equality of differently ordered complexes
        self.assertEqual(c1, c2)

        oc1 = OrderedComplexSpecies([s2, s1], called_from_complex=True)
        oc2 = OrderedComplexSpecies([s1, s2], called_from_complex=True)
        self.assertFalse(oc1 == oc2)
        #check of __contains__
        self.assertTrue(s1 in c1)
Esempio n. 6
0
    def test_contains_species_monomer(self):
        """Checks if the ComplexSpecies has a monomer (Species) inside of it, 
        but without checking Species.parent or Species.position. In effect, a
        less stringent version of __contains__. This is useful for checking
        complexes containing monomers from Polymers."""

        s1 = Species(name='s1', material_type="m1")
        s2 = Species(name='s2', material_type="m2")
        p = OrderedPolymerSpecies([s1, [s2, "reverse"]])
        p2 = OrderedPolymerSpecies([[s2, "reverse"], s1])
        c2 = ComplexSpecies([p[0], p[1]], called_from_complex=True)

        #Contains parent doesn't matter
        assert c2.contains_species_monomer(s1)
        assert c2.contains_species_monomer(p[0])
        assert c2.contains_species_monomer(p2[0])

        #position doesn't matter
        assert c2.contains_species_monomer(p2[1])

        #Direction does matter (by default)
        assert not c2.contains_species_monomer(s2)

        #Direction does matter (with keyword)
        print("*****")
        print(s2 is p2[0], s2, p2[0])
Esempio n. 7
0
def test_complex_no_polymer():
    a = Species('A')
    b = Species('B')
    #Create a ComplexSpecies
    c = Complex([a, b])
    assert (c == ComplexSpecies([a, b], called_from_complex=True))

    #Ordered Complex
    truth = OrderedComplexSpecies([a, Complex([b, a]), a])
    testcomplx = Complex([a, Complex([b, a]), a], ordered=True)
    assert (truth == testcomplx)
Esempio n. 8
0
    def test_species_initialization(self):

        s1 = Species(name='s1')
        s2 = Species(name='s2', material_type="m2")

        # Check invalidity of ComplexSpecies with fewer than 2 component
        with self.assertRaisesRegexp(ValueError,'chemical_reaction_network.complex requires '
                                                '2 or more species in its constructor'):
            ComplexSpecies([s1])
        
        # Check invalidity of OrderedComplexSpecies with fewer than 2 component
        with self.assertRaisesRegexp(ValueError, 'chemical_reaction_network.complex requires 2 '
                                                 'or more species in its constructor.'):
            OrderedComplexSpecies([s1])
        
        # Check invalidity of multimers with fewer than 2 component
        with self.assertRaisesRegexp(ValueError, 'chemical_reaction_network.complex requires 2 '
                                                 'or more species in its constructor'):
            Multimer(s1, 1)

        # Check the naming conventions
        oc1 = OrderedComplexSpecies([s2, s1])
        c1 = ComplexSpecies([s2, s1])
        m1 = Multimer(s1, 2)
        c3 = ComplexSpecies([s1, s1])

        # Check validity of ComplexSpecies, Multimers and OrderedComplexSpecies with strings instead of species
        self.assertEqual(OrderedComplexSpecies([s2, "s1"]), oc1)
        self.assertEqual(ComplexSpecies([s2, "s1"]), c1)
        self.assertEqual(Multimer("s1", 2), m1)
        
        # ComplexSpecies should sort the species added alphabetically by representation
        self.assertEqual(repr(c1), "complex_"+repr(s2)+"_"+repr(s1))
        
        # OrderedComplexSpecies do not sort their internal species
        self.assertEqual(repr(oc1), "ordered_complex_"+repr(s2)+"_"+repr(s1))
        
        # Multimers are just complexes with multiplicity
        self.assertEqual(repr(m1), "complex_2x_"+repr(s1))
        self.assertEqual(repr(c3), repr(m1))
    def test_update_species(self):
        from biocrnpyler import CombinatorialPromoter, Protein, Species, Combinatorial_Cooperative_Binding, \
                                    DNAassembly,Transcription_MM, Translation_MM, Multimer, ComplexSpecies
        #make a complicated promoter
        newprom = CombinatorialPromoter("testprom",["treg1",Species("treg2",material_type="rna")],\
                                tx_capable_list = [["treg1","treg2"]],cooperativity={"testprom_treg2":1},leak=True)

        newdna = DNAassembly("testDNA", promoter=newprom)
        newdna.update_mechanisms(mechanisms={
            "transcription": Transcription_MM(),
            "translation": Translation_MM()
        })
        newdna.update_parameters(
            parameters={
                "cooperativity": 2,
                "kb": 100,
                "ku": 10,
                "ktx": .05,
                "ktl": .2,
                "kdeg": 2
            })
        newprom_spec = newprom.update_species()

        sp_treg1 = Species("treg1", material_type="protein")
        sp_treg2 = Species("treg2", material_type="rna")
        #mu_treg2 = Multimer(sp_treg2,2)

        sp_dna = Species("testDNA", material_type="dna")
        sp_rnap = Species("RNAP", material_type="protein")
        sp_rna = Species("testDNA", material_type="rna")
        cp_dna_rnap = ComplexSpecies([sp_dna, sp_rnap])
        cp_dna_treg1 = ComplexSpecies([sp_dna, sp_treg1, sp_treg1])
        cp_dna_treg2 = ComplexSpecies([sp_dna, sp_treg2])
        cp_dna_treg1_rnap = ComplexSpecies([cp_dna_treg1, sp_rnap])
        cp_dna_treg2_rnap = ComplexSpecies([cp_dna_treg2, sp_rnap])
        cp_dna_treg1_treg2 = ComplexSpecies(
            [sp_dna, sp_treg1, sp_treg1, sp_treg2])
        cp_dna_treg1_treg2_rnap = ComplexSpecies([cp_dna_treg1_treg2, sp_rnap])

        knownspecies = [sp_dna,sp_rnap,sp_rna,cp_dna_rnap,cp_dna_treg1,\
                            cp_dna_treg2,cp_dna_treg1_treg2,cp_dna_treg1_rnap, \
                                cp_dna_treg2_rnap,cp_dna_treg1_treg2_rnap]

        #these are the species that should come out
        test_set = set([str(a) for a in newprom_spec])
        mistake_found = False
        #we should have the correct length of species
        self.assertTrue(len(test_set) == len(knownspecies))
        for known_spec in knownspecies:
            #go through and check each species if it's in the set generated by the promoter
            if (str(known_spec) not in test_set):
                print("couldn't find " + str(known_spec))
                mistake_found = True
                break
        self.assertTrue(not mistake_found)
Esempio n. 10
0
    def test_species_initialization(self):

        s1 = Species(name='s1')
        s2 = Species(name='s2', material_type="m2")

        # Check invalidity of ComplexSpecies with fewer than 2 component
        with self.assertRaisesRegex(
                ValueError, 'chemical_reaction_network.complex requires '
                '2 or more species in its constructor'):
            ComplexSpecies([s1], called_from_complex=True)

        # Check invalidity of OrderedComplexSpecies with fewer than 2 component
        with self.assertRaisesRegex(
                ValueError, 'chemical_reaction_network.complex requires 2 '
                'or more species in its constructor.'):
            OrderedComplexSpecies([s1], called_from_complex=True)

        # Check invalidity of multimers with fewer than 2 component
        with self.assertRaisesRegex(
                ValueError, 'chemical_reaction_network.complex requires 2 '
                'or more species in its constructor'):
            Multimer(s1, 1, called_from_complex=True)

        # Check the naming conventions
        oc1 = OrderedComplexSpecies([s2, s1], called_from_complex=True)
        c1 = ComplexSpecies([s2, s1], called_from_complex=True)
        m1 = Multimer(s1, 2, called_from_complex=True)
        c3 = ComplexSpecies([s1, s1], called_from_complex=True)

        # Check invalidity of ComplexSpecies, Multimers and OrderedComplexSpecies with strings instead of species
        with self.assertRaisesRegex(
                TypeError,
                'recieved a non-species as a member of the list species'):
            self.assertEqual(OrderedComplexSpecies([s2, "s1"]), oc1)
            self.assertEqual(ComplexSpecies([s2, "s1"]), c1)
            self.assertEqual(Multimer("s1", 2), m1)

        # ComplexSpecies should sort the species added alphabetically by representation
        #an extra underscore is added at the end of the representation to deal with embedded complexes.
        self.assertEqual(repr(c1),
                         "complex_" + repr(s2) + "_" + repr(s1) + "_")

        # OrderedComplexSpecies do not sort their internal species
        self.assertEqual(repr(oc1),
                         "ordered_complex_" + repr(s2) + "_" + repr(s1) + "_")

        # Multimers are just complexes with multiplicity
        self.assertEqual(repr(m1), "complex_" + repr(s1) + "_2x_")
        self.assertEqual(repr(c3), repr(m1))

        # Nested list creation of ComplexSpecies
        c1 = ComplexSpecies([s1, [s2, s1]], called_from_complex=True)
        c2 = ComplexSpecies([s1, s2, s1], called_from_complex=True)
        self.assertEqual(c1, c2)

        c1 = OrderedComplexSpecies([s1, [s2, s1]], called_from_complex=True)
        c2 = OrderedComplexSpecies([s1, s2, s1], called_from_complex=True)
        c3 = OrderedComplexSpecies([s1, [s1, s2]], called_from_complex=True)
        self.assertEqual(c1, c2)
        self.assertFalse(c1 == c3)
    def test_update_reactions(self):
        """this function tests the CombinatorialPromoter for the ability to make
        reactions with the proper inputs and outputs."""
        from biocrnpyler import CombinatorialPromoter, Protein, Species, Combinatorial_Cooperative_Binding, \
                                    DNAassembly,Transcription_MM, Translation_MM, ComplexSpecies, Multimer

        #make a relatively simple combinatorial promoter
        newprom = CombinatorialPromoter("testprom", ["treg1", "treg2"],
                                        tx_capable_list=[["treg2", "treg1"]],
                                        leak=True)
        #you have to put it into an assembly
        newdna = DNAassembly("testDNA", promoter=newprom)
        #adding mechanisms because there is no mixture. I believe this is what the mixture does
        newdna.update_mechanisms(mechanisms={
            "transcription": Transcription_MM(),
            "translation": Translation_MM()
        })
        newdna.update_parameters(parameters={"cooperativity":2,"kb":100, "ku":10, "ktx":.05, ("testprom_leak","ktx"):.01,\
                                                                ("testprom_leak","ku"):50,("testprom_treg1_treg2_RNAP","ku"):5})
        #you have to do update_species first
        newprom_spec = newprom.update_species()
        #now the test... does it produce the right reactions??
        newprom_rxns = newprom.update_reactions()
        #here i am generating the species manually
        sp_dna = Species("testDNA", material_type="dna")
        sp_rna = Species("testDNA", material_type="rna")
        sp_treg1 = Species("treg1", material_type="protein")
        sp_treg2 = Species("treg2", material_type="protein")
        sp_rnap = Species("RNAP", material_type="protein")
        #now the complexes

        sp_dna_treg1 = ComplexSpecies([sp_dna, sp_treg1, sp_treg1])
        sp_dna_treg2 = ComplexSpecies([sp_dna, sp_treg2, sp_treg2])
        sp_dna_treg1_treg2 = ComplexSpecies(
            [sp_dna, sp_treg2, sp_treg2, sp_treg1, sp_treg1])
        sp_dna_rnap = ComplexSpecies([sp_dna, sp_rnap])
        sp_dna_treg1_rnap = ComplexSpecies([sp_dna_treg1, sp_rnap])
        sp_dna_treg2_rnap = ComplexSpecies([sp_dna_treg2, sp_rnap])
        sp_dna_treg1_treg2_rnap = ComplexSpecies([sp_dna_treg1_treg2, sp_rnap])
        #print('\n\n'.join([str(a) for a in newprom_rxns]))
        #now, we generate the reaction input outputs manually
        r0 = [set([sp_rnap, sp_dna]),
              set([sp_dna_rnap]), 100, 50]  #RNAP binding to DNA
        r1 = [set([sp_dna_rnap]),
              set([sp_dna, sp_rna, sp_rnap]), .01, 0]  #leaky transcription
        r2 = [set([sp_dna, sp_treg1]),
              set([sp_dna_treg1]), 100, 10]  #treg1 binding
        r3 = [set([sp_dna_treg1, sp_rnap]),
              set([sp_dna_treg1_rnap]), 100,
              50]  #rnap binding to treg1 bound dna
        r4 = [
            set([sp_dna_treg1_rnap]),
            set([sp_dna_treg1, sp_rnap, sp_rna]), .01, 0
        ]  #leaky tx from single regulator
        r5 = [
            set([sp_dna_treg2_rnap]),
            set([sp_dna_treg2, sp_rnap, sp_rna]), .01, 0
        ]  #leaky tx from single regulator
        r6 = [
            set([sp_dna_treg1_treg2_rnap]),
            set([sp_dna_treg1_treg2, sp_rnap, sp_rna]), .05, 0
        ]  #ktx for regular tx
        r7 = [set([sp_dna_treg2, sp_rnap]),
              set([sp_dna_treg2_rnap]), 100,
              50]  #rnap binding to treg2 bound dna
        r8 = [set([sp_dna, sp_treg2]),
              set([sp_dna_treg2]), 100, 10]  #treg2 binding
        r9 = [
            set([sp_dna_treg1, sp_treg2]),
            set([sp_dna_treg1_treg2]), 100, 10
        ]  #treg2 binding to dna that already has treg1
        r10 = [
            set([sp_dna_treg2, sp_treg1]),
            set([sp_dna_treg1_treg2]), 100, 10
        ]  #treg1 binding to dna that already has treg2
        r11 = [
            set([sp_dna_treg1_treg2, sp_rnap]),
            set([sp_dna_treg1_treg2_rnap]), 100, 5
        ]  #rnap binding to full complex
        truthlist = [r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11]
        #print('\n\n'.join([str(a) for a in newprom_rxns]))
        for rxn in newprom_rxns:
            in_outs = [set(rxn.inputs), set(rxn.outputs)]
            correctPick = False
            for rset in truthlist:
                #go through all the reactions and make sure that
                #they have the correct inputs, outputs, and constants.
                if (rset[0] == in_outs[0] and rset[1] == in_outs[1]):
                    self.assertTrue(rxn.k == rset[2])
                    self.assertTrue(rxn.k_r == rset[3])
                    correctPick = True
                    break

            self.assertTrue(correctPick)
        #12 reactions must be generated
        self.assertTrue(len(newprom_rxns) == len(truthlist))
        #TODO: tests below this are questionable
        #then we should also test what happens if you say that nothing can transcribe:
        newprom2 = CombinatorialPromoter("testprom",
                                         ["treg1"])  #,tx_capable_list = [])
        newdna2 = DNAassembly("testDNA", promoter=newprom2)
        #adding mechanisms because there is no mixture. I believe this is what the mixture does
        newdna2.update_mechanisms(mechanisms={
            "transcription": Transcription_MM(),
            "translation": Translation_MM()
        })
        newdna2.update_parameters(parameters={
            "cooperativity": 2,
            "kb": 100,
            "ku": 10,
            "ktx": .05
        })
        with self.assertWarns(UserWarning):
            #TODO fix this with a warning detection system that actually checks for the proper warning
            newprom_rxns = newprom2.update_reactions()