def test_recursive_filtering(self): #Test default off via name, attributes, and materials with recursive_species_filtering = True s1 = Species("s1", material_type = "m1", attributes = ["a1"]) s2 = Species("s2", material_type = "m2", attributes = ["a2"]) c1 = Complex([s1, s2], name = "c1") c2 = Complex([c1, s2], name = "c2") fd = {"s1":True} #Filter based on name mech_default_on_fds1 = GlobalMechanism(name = self.mech_name, mechanism_type = "dummy", default_on = False, filter_dict = fd, recursive_species_filtering = True) self.assertTrue(mech_default_on_fds1.apply_filter(s1)) self.assertFalse(mech_default_on_fds1.apply_filter(s2)) self.assertTrue(mech_default_on_fds1.apply_filter(c1)) self.assertTrue(mech_default_on_fds1.apply_filter(c2)) fd = {"m1":True} #Filter based on material mech_default_on_fds1 = GlobalMechanism(name = self.mech_name, mechanism_type = "dummy", default_on = False, filter_dict = fd, recursive_species_filtering = True) self.assertTrue(mech_default_on_fds1.apply_filter(s1)) self.assertFalse(mech_default_on_fds1.apply_filter(s2)) self.assertTrue(mech_default_on_fds1.apply_filter(c1)) self.assertTrue(mech_default_on_fds1.apply_filter(c2)) fd = {"a1":True} #Filter based on attribute mech_default_on_fds1 = GlobalMechanism(name = self.mech_name, mechanism_type = "dummy", default_on = False, filter_dict = fd, recursive_species_filtering = True) self.assertTrue(mech_default_on_fds1.apply_filter(s1)) self.assertFalse(mech_default_on_fds1.apply_filter(s2)) self.assertTrue(mech_default_on_fds1.apply_filter(c1)) #attributes are not inherited through ComplexSpecies, but contained inside self.assertTrue(mech_default_on_fds1.apply_filter(c2))
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_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))
def test_complex_with_polymer_replacement(): #These tests show how the order of binding can matter. #Best practices is to put everything into a PolymerConformation before doing Complex if PolymerConformations are being used. a = Species('A') b = Species('B') s = Species("S") p = OrderedPolymerSpecies([a, b, a]) pc0 = PolymerConformation( polymer=p) #Put the polymer inside a conformation #Bind two monomers together pc = Complex([pc0.polymers[0][0], pc0.polymers[0][1]]).parent #get a PolymerConformation assert isinstance(pc, PolymerConformation) #create a Complex around an unbound element of p (from within pc) c = Complex([s, pc.polymers[0][2], s], ordered=True) assert str(c) == str( OrderedComplexSpecies([s, p[2], s], called_from_complex=True)) assert str(pc.polymers[0]) == str(c.parent.polymers[0]) assert len(c.parent.complexes) > len(pc.complexes) #Make the same thing with the replacement first #this gives a different final complex than doing things in the previous order p2 = OrderedPolymerSpecies([a, b, Complex([s, a, s], ordered=True)]) pc2 = PolymerConformation(polymer=p2) assert str(c.parent) != str(p2) assert c.parent.parent != Complex([pc2.polymers[0][0], pc2.polymers[0][1] ]).parent
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)
def test_combinatorial_enumeration_DNAconstruct(): P = Promoter("pconst") #constitutive promoter T = Terminator("term") parameters = { "cooperativity": 2, "kb": 100, "ku": 10, "ktx": .05, "ktl": .2, "kdeg": 2, "kint": .05 } mechs = { "transcription": Transcription_MM(Species("RNAP", material_type="protein")) } #circular construct x = DNA_construct([P, P, T], mechanisms=mechs, parameters=parameters, circular=True) y = x.combinatorial_enumeration() prom_positions = [] dna_species = {} for prom_comp in y: if (prom_comp.position not in dna_species): dna_species[prom_comp.position] = [prom_comp.dna_to_bind.parent] else: dna_species[prom_comp.position] += [prom_comp.dna_to_bind.parent] p1_bound = Complex( [x.get_species()[0], Species("RNAP", material_type="protein")]).parent p2_bound = Complex( [x.get_species()[1], Species("RNAP", material_type="protein")]).parent both_bound = Complex( [p1_bound[1], Species("RNAP", material_type="protein")]).parent assert (x.get_species() in dna_species[0] ) #unbound polymer found in combinatorial enumeration assert (p2_bound in dna_species[0] ) #proper bound polymer found in combinatorial enumeration assert (x.get_species() in dna_species[1] ) #unbound polymer found in combinatorial enumeration assert (p1_bound in dna_species[1] ) #proper bound polymer found in combinatorial enumeration assert ( both_bound not in dna_species[0] ) #dna with both promoters bound is not in combinatorial enumeration assert ( both_bound not in dna_species[1] ) #dna with both promoters bound is not in combinatorial enumeration assert (len(y) == 4)
def test_One_Step_Cooperative_Binding(): oscb = One_Step_Cooperative_Binding() binder = Species("S1") bindee = Species("S2") c_fake = Species("C") c1 = Complex([binder, bindee]) c2 = Complex([binder, binder, bindee]) #Test Update Species #Try Cooperativity 1 assert len(oscb.update_species(binder, bindee, cooperativity=1)) == 3 assert c1 in oscb.update_species(binder, bindee, cooperativity=1) assert c_fake in oscb.update_species(binder, bindee, cooperativity=1, complex_species=c_fake) #Try Cooperativity 2 assert len(oscb.update_species(binder, bindee, cooperativity=2)) == 3 assert c2 in oscb.update_species(binder, bindee, cooperativity=2) assert c_fake in oscb.update_species(binder, bindee, cooperativity=2, complex_species=c_fake) #Test Update Reactions #Try Cooperativity 1 assert len( oscb.update_reactions(binder, bindee, cooperativity=1, kb=1.0, ku=1.0)) == 1 assert len( oscb.update_reactions( binder, bindee, cooperativity=2, kb=1.0, ku=1.0, complex_species=c_fake, )) == 1 #Try Cooperativity 2 assert len( oscb.update_reactions(binder, bindee, cooperativity=2, kb=1.0, ku=1.0)) == 1 assert len( oscb.update_reactions( binder, bindee, cooperativity=2, kb=1.0, ku=1.0, complex_species=c_fake, )) == 1
def test_default_filtering(self): #No filter dictionary used in these tests s1 = Species("s1", material_type = "m1", attributes = ["a1"]) s2 = Species("s2", material_type = "m2", attributes = ["a2"]) c1 = Complex([s1, s2], name = "c1") c2 = Complex([c1, s2], name = "c2") #Always ON for all species mech_default_on = GlobalMechanism(name = self.mech_name, mechanism_type = "dummy", default_on = True) self.assertTrue(False not in [mech_default_on.apply_filter(s) for s in [s1, s2, c1, c2]]) #Always OFF for all species mech_default_off = GlobalMechanism(name = self.mech_name, mechanism_type = "dummy", default_on = False) self.assertTrue(True not in [mech_default_off.apply_filter(s) for s in [s1, s2, c1, c2]])
def test_degenerate_polymer_conformation(): S = Species("S") p = OrderedPolymerSpecies([S, S, S]) pc = PolymerConformation(polymer=p) c1 = Complex([pc.polymers[0][0], pc.polymers[0][1]]) pc1 = c1.parent c2 = Complex([pc.polymers[0][1], pc.polymers[0][2]]) pc2 = c2.parent c3 = Complex([pc.polymers[0][0], pc.polymers[0][2]]) pc3 = c3.parent assert pc1 != pc2 != pc3
def test_One_Step_Binding(): osb = One_Step_Binding() binder = Species("S1") bindee = Species("S2") c_fake = Species("C") c1 = Complex([binder, bindee]) #Test Update Species assert len(osb.update_species(binder, bindee, cooperativity=1)) == 3 assert c1 in osb.update_species(binder, bindee, cooperativity=1) assert c_fake in osb.update_species(binder, bindee, complex_species=c_fake) #Test Update Reactions assert len(osb.update_reactions(binder, bindee, kb=1.0, ku=1.0)) == 1 assert len( osb.update_reactions( binder, bindee, kb=1.0, ku=1.0, complex_species=c_fake, )) == 1 #Also works with lists assert len(osb.update_species([binder], [bindee])) == 3 assert len(osb.update_reactions([binder], [bindee], kb=1.0, ku=1.0)) == 1
def test_replace_species_with_a_non_massaction_reaction(self): c1 = Complex([self.s1, self.s_old]) prop_hill_old = ProportionalHillPositive(k=1., s1=self.s1, K=10, d=self.s_old, n=2) r1 = Reaction([self.s1, self.s_old], [c1], propensity_type=prop_hill_old) prop_hill_new = ProportionalHillPositive(k=1., s1=self.s1, K=10, d=self.s_new, n=2) r1_new = Reaction([self.s1, self.s_new], [c1.replace_species(self.s_old, self.s_new)], propensity_type=prop_hill_new) self.assertTrue(r1.replace_species(self.s_old, self.s_new) == r1_new)
def test_naming_convention(self): A = Species("A", material_type="a") B = Species('B', attributes="b") C = Complex([Species("S"), Species("S")]) p = OrderedPolymerSpecies([A, B, C], attributes=["a"]) print(str(p)) print( f"{p.material_type}_{str(A)}_{str(B)}_{str(C)}_{p.attributes[0]}_") self.assertTrue( str(p) == f"{p.material_type}_{str(A)}_{str(B)}_{str(C)}_{p.attributes[0]}_")
def test_complex_with_polymer(): a = Species('A') b = Species('B') c = Complex([a, b]) d = OrderedPolymerSpecies([a, b, a]) #Complex in an OrderedPolymerSpecies d_c = Complex([d[1], a]) assert (isinstance(d_c, ComplexSpecies)) truth = OrderedPolymerSpecies([a, Complex([b, a]), a]) assert (d_c.parent == truth) assert (d[1] not in d_c) #different parents assert (b in d_c) #b is unbound when put in the Complex #Ordered case d_co = Complex([d[1], a], ordered=True) assert isinstance(d_co, OrderedComplexSpecies) truth_o = OrderedPolymerSpecies([a, Complex([b, a], ordered=True), a]) assert d_co.parent == truth_o #Cannot complex two Species inside a PolymerSpecies without puting the PolymerSpecies into a Conformation first with pytest.raises(TypeError): c = Complex([Species("S"), d[0], d[2]])
def test_complex_with_a_complex_in_a_conformation(): #This occurs when Complexes are formed around Complexes in PolymerConformations. #In these cases, the Complexes are merged to prevent nested Complexes inside of PolymerConformations. #Using ordered = True to test that order is preserved a = Species('A') b = Species('B') c = Species('C') p = OrderedPolymerSpecies([a, b, c]) pc0 = PolymerConformation(polymer=p) pc = Complex([pc0.polymers[0][0], pc0.polymers[0][1]], ordered=True).parent #get a PolymerConformation c = pc.complexes[0] #get a complex from the PolymerConformation c2 = Complex([c, Species("S")], ordered=True) #Create a Complex with a Complex pc2 = c2.parent assert str(c2) == str(OrderedComplexSpecies([p[0], p[1], Species("S") ])) #merging done correctly assert pc2 == Complex( [pc0.polymers[0][0], pc0.polymers[0][1], Species("S")], ordered=True).parent #check the parent PolymerConformation assert len(pc2.complexes) == 1 #Create a PolymerConformation with two complexes c3 = Complex([pc2.polymers[0][0], pc2.polymers[0][2], Species("S2")], ordered=True) pc3 = c3.parent assert len(pc3.complexes) == 2 assert str(c3) == str( OrderedComplexSpecies([p[0], p[2], Species("S2")], called_from_complex=True)) #merge the two complexes in pc3 c4 = Complex([pc3.complexes[0], pc3.complexes[1], Species("S3")], ordered=True) assert len(c4.parent.complexes) == 1 assert str(c4) == str( Complex([ pc0.polymers[0][0], pc0.polymers[0][1], Species("S"), pc0.polymers[0][0], pc0.polymers[0][2], Species("S2"), Species("S3") ], ordered=True)) assert c4.parent == Complex([ pc0.polymers[0][0], pc0.polymers[0][1], Species("S"), pc0.polymers[0][0], pc0.polymers[0][2], Species("S2"), Species("S3") ], ordered=True).parent
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_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_update_species(self): from biocrnpyler import CombinatorialPromoter, Protein, Species, Combinatorial_Cooperative_Binding, \ DNAassembly,Transcription_MM, Translation_MM, Complex #make a complicated promoter newprom = CombinatorialPromoter("testprom",["treg1",Species("treg2",material_type="rna")],\ tx_capable_list = [["treg1","treg2"]],cooperativity={"testprom_treg2":1},leak=True) sp_rnap = Species("RNAP",material_type="protein") ribosome = Species("Ribo", material_type = "protein") newdna = DNAassembly("testDNA",promoter=newprom) newdna.add_mechanisms({"transcription":Transcription_MM(rnap = sp_rnap), "translation":Translation_MM(ribosome = ribosome)}) newdna.update_parameters(parameters={"cooperativity":2,"kb":100, "ku":10, "ktx":.05, "ktl":.2, "kdeg":2}) #Promoters are copied when added to DNAassemblies newprom_copy = newdna.promoter newprom_spec = newprom_copy.update_species() sp_treg1 = Species("treg1",material_type="protein") sp_treg2 = Species("treg2",material_type="rna") sp_dna = Species("testDNA",material_type="dna") sp_rna = Species("testDNA",material_type="rna") cp_dna_rnap = Complex([sp_dna,sp_rnap]) cp_dna_treg1 = Complex([sp_dna,sp_treg1,sp_treg1]) cp_dna_treg2 = Complex([sp_dna,sp_treg2]) cp_dna_treg1_rnap = Complex([cp_dna_treg1,sp_rnap]) cp_dna_treg2_rnap = Complex([cp_dna_treg2,sp_rnap]) cp_dna_treg1_treg2 = Complex([sp_dna,sp_treg1,sp_treg1,sp_treg2]) cp_dna_treg1_treg2_rnap = Complex([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,sp_treg1,sp_treg2] #these are the species that should come out test_set = set([str(a) for a in newprom_spec]) mistake_found = False error_txt = "" 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): error_txt += "\ncouldn't find "+str(known_spec) mistake_found = True if mistake_found: raise ValueError(f"Mistake found in Species output:{error_txt}. Returned Species: {test_set}.") #we should have the correct length of species self.assertTrue(len(test_set)==len(knownspecies)) self.assertTrue(not mistake_found)
def test_CombinatorialComplex_compute_species_to_add(): X, Y, Z = Species("X"), Species("Y"), Species("Z") C = Complex([X, X, Y, Z]) CC = CombinatorialComplex(final_states = [C]) #A single X, Y, and Z should be added species_to_add = CC.compute_species_to_add(X, C) assert set(species_to_add) == set([X, Y, Z]) assert species_to_add.count(X) == 1 and species_to_add.count(Y) == 1 and species_to_add.count(Z) == 1 #Two X's, and Z should be added species_to_add = CC.compute_species_to_add(Y, C) assert set(species_to_add) == set([X, Z]) assert species_to_add.count(X) == 2 and species_to_add.count(Z) == 1 #Try with s0 as a ComplexSpecies species_to_add = CC.compute_species_to_add(Complex([X, X]), C) assert set(species_to_add) == set([Y, Z]) assert species_to_add.count(Y) == 1 and species_to_add.count(Z) == 1 #If sf is not a ComplexSpecies, there is an error with pytest.raises(ValueError): species_to_add = CC.compute_species_to_add(C, X) #In the following cases, sf cannot be created by adding species to s0, so None is returned #C contains more species than Complex([X, X]) species_to_add = CC.compute_species_to_add(C, Complex([X, Y])) assert species_to_add is None #C contains more species than Complex([X, X]) species_to_add = CC.compute_species_to_add(C, Complex([X, X])) assert species_to_add is None #S is not in C species_to_add = CC.compute_species_to_add(Species("S"), C) assert species_to_add is None species_to_add = CC.compute_species_to_add(Complex([Species("S"), X]), C) assert species_to_add is None
def test_ordered_polymer_species_contains(self): a = Species("A") b = Species("B") bf = Species("B").set_dir("forward") c = Complex([a, a]) p = OrderedPolymerSpecies([bf, b, c]) #In these cases, parent doesn't matter self.assertTrue(a in p[2]) self.assertTrue(a in c) self.assertTrue(b in p) self.assertTrue(c in p) p2 = OrderedPolymerSpecies([bf, a, c]) #In these cases parents matter self.assertFalse(p[0] in p2) self.assertFalse(p2[0] in p) #In this case, direciton matters self.assertFalse(b in p2)
def test_complex(self): a = Species('A') b = Species('B') c = Complex([a, b]) d = OrderedPolymerSpecies([a, b, a]) #normal complex that makes complex species self.assertEqual(set(c.species), set([a, b])) self.assertTrue(type(c) == ComplexSpecies) #Polymer Complex testpoly = Complex([d[1], a]).parent truth = OrderedPolymerSpecies([a, Complex([b, a]), a]) self.assertEqual(testpoly, truth) #Ordered Complex truth = OrderedComplexSpecies([a, Complex([b, a]), a]) testcomplx = Complex([a, Complex([b, a]), a], ordered=True) self.assertEqual(truth, testcomplx)
def pass_test_CombinatorialConformation_compute_complexes_to_add_to_polymer(): X, Y, Z = Species("X"), Species("Y"), Species("Z") p0 = OrderedPolymerSpecies([X]) p1 = OrderedPolymerSpecies([X, Y, Z]) p2 = OrderedPolymerSpecies([Z, Y, X]) p3 = OrderedPolymerSpecies([Complex([X, X]), Y, Complex([X, Z])]) CC = CombinatorialConformation(initial_states=[], final_states=[]) #An empty list is returned if the Polymers are the same ("Nothing to add") complexes_to_add = CC.compute_complexes_to_add_to_polymer(p0, p0) assert len(complexes_to_add) == 0 #None is returned if the Polymers cannot be converted complexes_to_add = CC.compute_complexes_to_add_to_polymer(p0, p1) assert complexes_to_add == None complexes_to_add = CC.compute_complexes_to_add_to_polymer(p1, p2) assert complexes_to_add == None #Basic Case complexes_to_add = CC.compute_complexes_to_add_to_polymer(p1, p3) assert len(complexes_to_add) == 2 assert (0, [X]) in complexes_to_add and (2, [X]) in complexes_to_add #Nested Complex Case p4 = OrderedPolymerSpecies( [Complex([X, X]), Y, Complex([Z, Complex([X, X])])]) complexes_to_add = CC.compute_complexes_to_add_to_polymer(p1, p4) assert len(complexes_to_add) == 2 assert (0, [X]) in complexes_to_add and (2, [Complex([X, X]) ]) in complexes_to_add #These cases should give errors with pytest.raises(ValueError): CC.compute_complexes_to_add_to_polymer(X, p4) with pytest.raises(ValueError): CC.compute_complexes_to_add_to_polymer(p1, X)
def test_replace_Species_in_OrderedComplexSpecies(self): oc1 = Complex([self.s1, self.s_old], ordered=True) self.assertTrue( oc1.replace_species(self.s_old, self.s_new) == Complex( [self.s1, self.s_new], ordered=True))
def test_CombinatorialComplex_update_reactions(): mech_b = One_Step_Binding() ku, kb = 1, 1 params = {"ku":ku, "kb":kb} #shortcut to write this faster def R(inputs, outputs): return Reaction.from_massaction(inputs, outputs, k_forward = kb, k_reverse = ku) #Basic Case X, Y, Z = Species("X"), Species("Y"), Species("Z") CXYZ = Complex([X, Y, Z]) CXY = Complex([X, Y]) CXZ = Complex([X, Z]) CYZ = Complex([Y, Z]) CC1 = CombinatorialComplex(final_states = [CXYZ], mechanisms = [mech_b], parameters = params) r1 = CC1.update_reactions() r1_true = [R([X, Y], [CXY]), R([X, Z], [CXZ]), R([Y, Z], [CYZ]), R([CXY, Z], [CXYZ]), R([CXZ, Y], [CXYZ]), R([CYZ, X], [CXYZ])] assert all([r in r1_true for r in r1]) and all([r in r1 for r in r1_true]) #Set initial states CYZ = Complex([Y, Z]) CXX = Complex([X, X]) CXXY = Complex([X, X, Y]) CXXZ = Complex([X, X, Z]) CXXYZ = Complex([X, X, Y, Z]) CC2 = CombinatorialComplex(final_states = [CXXYZ], initial_states =[CXX, CYZ], mechanisms = [mech_b], parameters = params) r2 = CC2.update_reactions() r2_true = [R([CXX, Y], [CXXY]), R([CXX, Z], [CXXZ]), R([CXXY, Z], [CXXYZ]), R([CXXZ, Y], [CXXYZ]), R([CYZ, X], [CXYZ]), R([CXYZ, X], [CXXYZ])] assert all([r in r2_true for r in r2]) and all([r in r2 for r in r2_true]) #set intermediate states CC3 = CombinatorialComplex(final_states = [CXXYZ], intermediate_states =[CXX, CYZ], mechanisms = [mech_b], parameters = params) r3 = CC3.update_reactions() r3_true = [R([X, X], [CXX]), R([Y, Z], [CYZ])] + r2_true assert all([r in r3_true for r in r3]) and all([r in r3 for r in r3_true]) #set initial and intermediate states CXXYYZ = Complex([X, X, Y, Y, Z]) CXXYZZ = Complex([X, X, Y, Z, Z]) CXXYYZZ = Complex([X, X, Y, Y, Z, Z]) CC4 = CombinatorialComplex(final_states = [CXXYYZZ], intermediate_states =[CXXYZ], initial_states = [CXX, CYZ], mechanisms = [mech_b], parameters = params) r4 = CC4.update_reactions() r4_true = [R([CXX, Y], [CXXY]), R([CXX, Z], [CXXZ]), R([CYZ, X], [CXYZ]), R([CXYZ, X], [CXXYZ]), R([CXXZ, Y], [CXXYZ]), R([CXXY, Z], [CXXYZ]), R([CXXYZ, Y], [CXXYYZ]), R([CXXYZ, Z], [CXXYZZ]), R([CXXYYZ, Z], [CXXYYZZ]), R([CXXYZZ, Y], [CXXYYZZ])] assert all([r in r4_true for r in r4]) and all([r in r4 for r in r4_true]) #multiple final states CC5 = CombinatorialComplex(final_states = [CXXY, CXXZ], mechanisms = [mech_b], parameters = params) r5 = CC5.update_reactions() r5_true = [R([X, X], [CXX]), R([CXX, Y], [CXXY]), R([CXX, Z], [CXXZ]), R([X, Y], [CXY]), R([CXY, X], [CXXY]), R([X, Z], [CXZ]), R([CXZ, X], [CXXZ])] assert all([r in r5_true for r in r5]) and all([r in r5 for r in r5_true])
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, Complex, ParameterKey #make a relatively simple combinatorial promoter parameters={"cooperativity":2,"kb":100, "ku":10, "ktx":.05, ParameterKey(mechanism = None, part_id = "testprom_leak", name = "ktx"):.01, ParameterKey(mechanism = None, part_id = "testprom_leak", name = "ku"):50, ParameterKey(mechanism = None, part_id = "testprom_treg1_treg2_RNAP", name = "ku"):5} newprom = CombinatorialPromoter("testprom",["treg1","treg2"],tx_capable_list=[["treg2","treg1"]], leak = True, parameters = parameters) #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 sp_rnap = Species("RNAP",material_type="protein") ribosome = Species("Ribo", material_type = "protein") newdna.add_mechanisms({"transcription":Transcription_MM(rnap = sp_rnap), "translation":Translation_MM(ribosome = ribosome)}) #you have to do update_species first newprom_copy = newdna.promoter #Promoters are copied when added to DNAassemblies newprom_spec = newprom_copy.update_species() #now the test... does it produce the right reactions?? newprom_rxns = newprom_copy.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") #now the complexes sp_dna_treg1 = Complex([sp_dna,sp_treg1,sp_treg1]) sp_dna_treg2 = Complex([sp_dna,sp_treg2,sp_treg2]) sp_dna_treg1_treg2 = Complex([sp_dna,sp_treg2,sp_treg2,sp_treg1,sp_treg1]) sp_dna_rnap = Complex([sp_dna,sp_rnap]) sp_dna_treg1_rnap = Complex([sp_dna_treg1,sp_rnap]) sp_dna_treg2_rnap = Complex([sp_dna_treg2,sp_rnap]) sp_dna_treg1_treg2_rnap = Complex([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,None] #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,None] #leaky tx from single regulator r5 = [set([sp_dna_treg2_rnap]),set([sp_dna_treg2,sp_rnap,sp_rna]),.01,None] #leaky tx from single regulator r6 = [set([sp_dna_treg1_treg2_rnap]),set([sp_dna_treg1_treg2,sp_rnap,sp_rna]),.05,None] #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([i.species for i in rxn.inputs]), set([o.species for o in 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.propensity_type.k_forward == rset[2]) self.assertTrue(rxn.propensity_type.k_reverse == rset[3]) correctPick = True break self.assertTrue(correctPick) #12 reactions must be generated self.assertTrue(len(newprom_rxns)==len(truthlist)) #then we should also test what happens if you say that nothing can transcribe: newprom2 = CombinatorialPromoter("testprom",["treg1"], parameters = parameters,tx_capable_list = [],leak=False,\ mechanisms={"transcription":Transcription_MM(rnap = sp_rnap), "translation":Translation_MM(ribosome = ribosome)}) newdna2 = DNAassembly("testDNA",promoter=newprom2) #adding mechanisms because there is no mixture. I believe this is what the mixture does sp_rnap = Species("RNAP",material_type="protein") ribosome = Species("Ribo", material_type = "protein") self.assertWarnsRegex(UserWarning, 'nothing can transcribe',newdna2.update_reactions)
def test_Two_Step_Cooperative_Binding(): tscb = Two_Step_Cooperative_Binding() binder = Species("S1") bindee = Species("S2") c_fake = Species("C") nmer_fake = Species("n") c1 = Complex([nmer_fake, bindee]) c2 = Complex([Complex([binder, binder]), bindee]) #Test Update Species #Try Cooperativity 1 #The following will fail with pytest.raises(ValueError): tscb.update_species(binder, bindee, cooperativity=1) with pytest.raises(ValueError): tscb.update_species(binder, bindee, cooperativity=1, complex_species=c_fake) #These work due to passing in the nmer assert nmer_fake in tscb.update_species(binder, bindee, cooperativity=1, n_mer_species=nmer_fake) assert c1 in tscb.update_species(binder, bindee, cooperativity=1, n_mer_species=nmer_fake) assert len( tscb.update_species(binder, bindee, cooperativity=1, n_mer_species=nmer_fake)) == 4 #Try Cooperativity 2 assert len(tscb.update_species(binder, bindee, cooperativity=2)) == 4 assert c2 in tscb.update_species(binder, bindee, cooperativity=2) assert c_fake in tscb.update_species(binder, bindee, cooperativity=2, complex_species=c_fake) assert nmer_fake in tscb.update_species(binder, bindee, cooperativity=2, n_mer_species=nmer_fake) #Test Update Reactions assert len( tscb.update_reactions(binder, bindee, cooperativity=1, kb=(1.0, 1.0), ku=(1.0, 1.0), n_mer_species=nmer_fake, complex_species=c_fake)) == 2 assert len( tscb.update_reactions(binder, bindee, cooperativity=2, kb=(1.0, 1.0), ku=(1.0, 1.0))) == 2 #2 rates are required for kb and ku, each with pytest.raises(TypeError): assert tscb.update_reactions(binder, bindee, cooperativity=2, kb=1.0, ku=1.0)
def test_invalid_complex(): with pytest.raises(TypeError): Complex("A") with pytest.raises(TypeError): Complex(species="A")
def test_CombinatorialComplex_get_combinations_between(): X, Y, Z = Species("X"), Species("Y"), Species("Z") C = Complex([X, Y, Z]) CC = CombinatorialComplex(final_states = [C]) combos = CC.get_combinations_between(X, C) #Below conditions uniquely specify all the correct returns assert len(combos) == 4 #Assert first steps are in combos assert (Y, X, Complex([X, Y])) in combos and (Z, X, Complex([X, Z])) in combos #Second steps are in combos assert (Z, Complex([X, Y]), Complex([X, Y, Z])) in combos and (Y, Complex([X, Z]), Complex([X, Y, Z])) in combos #test again from a complex combos = CC.get_combinations_between(Complex([X, Y]), C) assert len(combos) == 1 assert (Z, Complex([X, Y]), Complex([X, Y, Z])) in combos #Test with duplicate species C2 = Complex([X, X, Z]) CC2 = CombinatorialComplex(final_states = [C2]) combos = CC2.get_combinations_between(X, C2) assert len(combos) == 4 assert (X, X, Complex([X, X])) in combos and (Z, X, Complex([X, Z])) in combos assert (Z, Complex([X, X]), C2) in combos and (X, Complex([X, Z]), C2) in combos #test again from a Complex combos = CC2.get_combinations_between(Complex([X, X]), C2) assert len(combos) == 1 assert (Z, Complex([X, X]), C2) in combos combos = CC2.get_combinations_between(Complex([X, Z]), C2) assert len(combos) == 1 assert (X, Complex([X, Z]), C2) in combos #Test with excluded states that are ComplexSpecies C3a = Complex([X, Y]) C3b = Complex([X, Z]) CC3 = CombinatorialComplex(final_states = [C], excluded_states = [C3a, C3b]) combos = CC3.get_combinations_between(X, C) combos_list = [c[0] for c in combos]+[c[1] for c in combos] + [c[2] for c in combos] assert C3a not in combos and C3b not in combos #Test with excluded states that are Species CC4 = CombinatorialComplex(final_states = [C], excluded_states = [Y, Z]) combos = CC4.get_combinations_between(X, C) combos_list = [c[0] for c in combos]+[c[1] for c in combos] + [c[2] for c in combos] assert Y not in combos and Z not in combos
def test_replace_Species_in_ComplexSpecies(self): c1 = Complex([self.s1, self.s_old]) c2 = Complex([self.s1, c1]) self.assertTrue(c1.replace_species(self.s2, self.s_new) == c1) self.assertTrue( c1.replace_species(self.s_old, self.s_new) == Complex( [self.s1, self.s_new])) self.assertTrue(c2 == Complex([self.s1, c1])) self.assertTrue( c2.replace_species(self.s_new, self.s_old) == Complex( [self.s1, Complex([self.s1, self.s_old])]))
def test_CombinatorialComplex_update_species(): mech_b = One_Step_Binding() #Basic Case X, Y, Z = Species("X"), Species("Y"), Species("Z") C1 = Complex([X, Y, Z]) CC1 = CombinatorialComplex(final_states = [C1], mechanisms = [mech_b]) s1 = CC1.update_species() assert X in s1 and Y in s1 and Z in s1 and C1 in s1 assert Complex([X, Y]) in s1 and Complex([X, Z]) in s1 and Complex([Y, Z]) in s1 #Set initial states C2i1 = Complex([X, X, Y]) C2i2 = Complex([Y, Z]) C2 = Complex([X, X, Y, Z]) CC2 = CombinatorialComplex(final_states = [C2], initial_states =[C2i1, C2i2], mechanisms = [mech_b]) s2 = CC2.update_species() assert X in s2 and Y not in s2 and Z in s2 assert C2i1 in s2 and C2i2 in s2 and C2 in s2 assert Complex([X, Y, Z]) in s2 assert Complex([X, X]) not in s2 and Complex([X, X, Z]) not in s2 assert Complex([X, X]) not in s2 and Complex([X, Y]) not in s2 and Complex([X, Z]) not in s2 #set intermediate states CC3 = CombinatorialComplex(final_states = [C2], intermediate_states =[C2i1, C2i2], mechanisms = [mech_b]) s3 = CC3.update_species() assert X in s3 and Y in s3 and Z in s3 assert C2i1 in s3 and C2i2 in s3 and C2 in s3 assert Complex([X, Y, Z]) in s3 and Complex([X, X]) in s3 and Complex([X, Y]) in s3 assert Complex([X, X, Z]) not in s3 and Complex([X, Z]) not in s3 #set initial and intermediate states C4 = Complex([X, Y, Y, Z, Z]) CC4 = CombinatorialComplex(final_states = [C4], intermediate_states =[Complex([X, Y, Y]), Complex([X, Z, Z])], initial_states = [Complex([X, Y]), Complex([X, Z])], mechanisms = [mech_b]) s4 = CC4.update_species() assert X not in s4 and Y in s4 and Z in s4 assert Complex([X, Y, Y]) in s4 and Complex([X, Z, Z]) in s4 and Complex([X, Y]) in s4 and Complex([X, Z]) in s4 assert Complex([Y, Y]) not in s4 and Complex([Z, Z]) not in s4 and Complex([X, Y, Z]) not in s4 assert Complex([X, Y, Y, Z]) in s4 and Complex([X, Y, Z, Z]) in s4 #multiple final states C5a = Complex([X, X, Y]) C5b = Complex([X, X, Z]) CC5 = CombinatorialComplex(final_states = [C5a, C5b], mechanisms = [mech_b]) s5 = CC5.update_species() assert X in s5 and Y in s5 and Z in s5 assert Complex([X, X]) in s5 and Complex([X, Z]) in s5 and Complex([X, Y]) in s5 assert Complex([Y, Z]) not in s5
def test_CombinatorialComplex_init_and_properties(): X, Y, Z = Species("X"), Species("Y"), Species("Z") #Single final_state case C1 = Complex([Y, Z, Z]) CC1 = CombinatorialComplex(final_states = [C1]) #tests getters and setters assert set(CC1.final_states) == set([C1]) assert set(CC1.sub_species) == set([Y, Z]) assert set(CC1.initial_states) == set([Y, Z]) assert CC1.intermediate_states is None #multiple final_state case C2 = Complex([X, X, Z, Z]) CC2 = CombinatorialComplex(final_states = [C1, C2]) #tests getters and setters assert set(CC2.final_states) == set([C1, C2]) assert set(CC2.sub_species) == set([X, Y, Z]) assert set(CC2.initial_states) == set([X, Y, Z]) assert CC2.intermediate_states is None #test with initial states CC3 = CombinatorialComplex(final_states = [C1, C2], initial_states = [Complex([Z, Z]), X]) assert set(CC3.final_states) == set([C1, C2]) assert set(CC3.sub_species) == set([X, Y, Z]) assert set(CC3.initial_states) == set([Complex([Z, Z]), X]) assert CC3.intermediate_states is None #Test with intermediate states CC4 = CombinatorialComplex(final_states = [C1, C2], intermediate_states = [Complex([Z, Z])]) assert set(CC4.final_states) == set([C1, C2]) assert set(CC4.sub_species) == set([X, Y, Z]) assert set(CC4.initial_states) == set([X, Y, Z]) assert set(CC4.intermediate_states) == set([Complex([Z, Z])]) #Test with excluded states CC5 = CombinatorialComplex(final_states = [C2], excluded_states = [C1]) #Test cases that should produce errors #final_states must be ComplexSpecies with pytest.raises(ValueError): CC = CombinatorialComplex(final_states = [Species("C1")]) #initial_states must be Species inside of final states with pytest.raises(ValueError): CC = CombinatorialComplex(final_states = [C1], initial_states = [Species("S")]) #initial states which are Complexes can only contain things in the final_states with pytest.raises(ValueError): CC = CombinatorialComplex(final_states = [C1], initial_states = [Complex([Species("S"), Species("S2")])]) #intermiade_states must be Complexes with pytest.raises(ValueError): CC = CombinatorialComplex(final_states = [C1], intermediate_states = [Species("S")]) #intermediate_states must contain only things in final states with pytest.raises(ValueError): CC = CombinatorialComplex(final_states = [C1], intermediate_states = [Complex([Species("S"), Species("S2")])])