def test_EnforceTranslation_bacterial_valine_antisense(): table_name = "Bacterial" protein = "LLTMMVTTTTVMVL" protein_sequence = reverse_translate(protein, table=table_name) for first_codon_before, first_codon_after in [ ("ATG", "ATG"), # methionine stays the only methionine codon ("GTG", "GTG"), # valine-start-codon stays the only valine-start-codon ]: sequence = first_codon_before + protein_sequence cds_constraint = EnforceTranslation( genetic_table="Bacterial", start_codon="keep", location=Location(0, len(sequence), -1), ) problem = DnaOptimizationProblem( sequence=reverse_complement(sequence), constraints=[cds_constraint], objectives=[EnforceChanges()], logger=None, ) assert problem.constraints[0].translation == "MLLTMMVTTTTVMVL" problem.optimize() problem_sequence_rv = reverse_complement(problem.sequence) protein_after = translate( problem_sequence_rv, table_name, assume_start_codon=True ) assert protein_after == "M" + protein assert problem_sequence_rv[:3] == first_codon_after
def test_AvoidRareCodons_as_constraint_reversed(): numpy.random.seed(123) sequence = "ATG" "TTT" "ATA" "CCA" "CTT" "TAG" rev_sequence = reverse_complement(sequence) location = (0, len(sequence), -1) problem = DnaOptimizationProblem( sequence=rev_sequence, constraints=[ EnforceTranslation(location=location), AvoidRareCodons(0.11, "e_coli", location=location), ], ) assert problem.all_constraints_pass() assert problem.sequence_edits_as_array().sum() == 4 new_sequence = reverse_complement(problem.sequence) assert translate(new_sequence) == translate(sequence)
def test_EnforceTranlationReversed(): numpy.random.seed(1234) sequence = reverse_translate(random_protein_sequence(50, seed=123)) rev_sequence = reverse_complement(sequence) problem = DnaOptimizationProblem( sequence=rev_sequence, constraints=[ AvoidPattern("AGC"), EnforceTranslation(location=(0, len(sequence), -1)) ], ) assert not problem.all_constraints_pass() problem.resolve_constraints() assert problem.all_constraints_pass()
def test_AvoidRareCodons_as_objective_reversed(): numpy.random.seed(123) sequence = "ATG" "TTT" "ATA" "CCA" "CTT" "TAG" rev_sequence = reverse_complement(sequence) location = (0, len(sequence), -1) problem = DnaOptimizationProblem( sequence=rev_sequence, constraints=[EnforceTranslation(location=location)], objectives=[AvoidRareCodons(0.11, "e_coli", location=location)], ) assert abs(problem.objective_scores_sum() + 0.09) < 0.001 problem.optimize() assert problem.objective_scores_sum() == 0
def test_avoid_hairpin_basics(): numpy.random.seed(123) random_sequences = [random_dna_sequence(30) for i in range(10)] full_sequence = "".join([ seq for sequence in random_sequences for seq in (random_dna_sequence(50), sequence, random_dna_sequence(50), reverse_complement(sequence), random_dna_sequence(50)) ]) problem = DnaOptimizationProblem(full_sequence, constraints=[AvoidHairpins()]) assert not problem.all_constraints_pass() problem.resolve_constraints() assert problem.all_constraints_pass()
"""Example of use of the AvoidPAttern specification""" from dnachisel import (DnaOptimizationProblem, random_dna_sequence, reverse_complement, AvoidHairpins) random_sequences = [random_dna_sequence(30) for i in range(10)] full_sequence = "".join([ seq for sequence in random_sequences for seq in (random_dna_sequence(50), sequence, random_dna_sequence(50), reverse_complement(sequence), random_dna_sequence(50)) ]) problem = DnaOptimizationProblem(full_sequence, constraints=[AvoidHairpins()]) print("\nBefore optimization:\n") print(problem.constraints_text_summary()) problem.resolve_constraints() print("\nAfter optimization:\n") print(problem.constraints_text_summary())
def test_enforce_pattern_options(): # Checks for Github issue #53 # Test 6 cases: location yes/no, 3 strand options sequence = "A" * 10 pattern = "C" * 4 # location=None problem = dc.DnaOptimizationProblem( sequence=sequence, constraints=[ dc.EnforcePatternOccurence(pattern, occurences=1, strand="from_location"), ], logger=None, ) problem.resolve_constraints() assert problem.all_constraints_pass() assert pattern in problem.sequence problem = dc.DnaOptimizationProblem( sequence=sequence, constraints=[ dc.EnforcePatternOccurence(pattern, occurences=1, strand="both") ], logger=None, ) problem.resolve_constraints() assert problem.all_constraints_pass() assert pattern in problem.sequence problem = dc.DnaOptimizationProblem( sequence=sequence, constraints=[ dc.EnforcePatternOccurence(pattern, occurences=1, strand=-1) ], logger=None, ) assert problem.constraints[0].evaluate(problem).score == -1 problem.resolve_constraints() assert problem.all_constraints_pass() assert dc.reverse_complement( pattern) in problem.sequence # other strand used # location specificed # Use -1 strand from location: problem = dc.DnaOptimizationProblem( sequence=sequence, constraints=[ dc.EnforcePatternOccurence( pattern, occurences=1, strand="from_location", location=Location(1, 6, strand=-1), ) ], logger=None, ) problem.resolve_constraints() assert problem.all_constraints_pass() assert dc.reverse_complement(pattern) in problem.sequence # Overwrite -1 strand to "both": problem = dc.DnaOptimizationProblem( sequence=sequence, constraints=[ dc.EnforcePatternOccurence( pattern, occurences=1, strand="both", location=Location(1, 6, strand=-1), ) ], logger=None, ) problem.resolve_constraints() assert problem.all_constraints_pass() assert pattern in problem.sequence # uses +1 strand by default # Overwrite -1 strand to +1: problem = dc.DnaOptimizationProblem( sequence=sequence, constraints=[ dc.EnforcePatternOccurence( pattern, occurences=1, strand=1, location=Location(1, 6, strand=-1), ) ], logger=None, ) problem.resolve_constraints() assert problem.all_constraints_pass() assert pattern in problem.sequence # uses +1 strand