Example #1
0
def test_EnforceRegionsCompatibility():
    # Two enzymes, BsmBI(CGTCTC) is GC-rich, EcoRI(GAATTC) is GC-poor, which
    # enzyme will be chosen and inserted in the sequence depends on the other
    # constraint on GC content
    numpy.random.seed(123)

    def compatibility_condition(location1, location2, problem):
        seq1 = location1.extract_sequence(problem.sequence)
        seq2 = location2.extract_sequence(problem.sequence)
        return sequences_differences(seq1, seq2) >= 2

    locations = [(0, 4), (50, 54), (100, 104), (150, 154)]
    problem = DnaOptimizationProblem(
        sequence=random_dna_sequence(200, seed=123),
        constraints=[
            EnforceRegionsCompatibility(
                locations=locations,
                compatibility_condition=compatibility_condition,
                condition_label="2bp difference",
            ),
            EnforceGCContent(mini=0.4, maxi=0.6, window=40),
        ],
        logger=None,
    )
    assert not any([e.passes for e in problem.constraints_evaluations()])
    problem.resolve_constraints()
    assert problem.all_constraints_pass()
    seq = problem.sequence
    assert [
        sequences_differences(seq[s1:e1], seq[s2:e2]) >= 2
        for (s1, e1), (s2, e2) in itertools.combinations(locations, 2)
    ]
def test_avoid_change_as_objectives_basics():
    numpy.random.seed(123)
    results = []
    for boost in (0, 0.1, 0.2, 1):
        sequence = random_dna_sequence(1000, seed=123)
        problem = DnaOptimizationProblem(
            sequence=sequence,
            objectives=[
                EnforceGCContent(
                    mini=0.45, maxi=0.55,
                    window=80).copy_with_changes(locations_span=300),
                AvoidChanges(boost=boost).as_passive_objective()
            ])

        problem.optimize()
        differences = sequences_differences(problem.sequence,
                                            problem.sequence_before)
        results.append(differences)
    assert results[0] > 40
    assert (results[0] > results[1] > results[2] > results[3])
    assert results[-1] == 0
Example #3
0
def compatibility_condition(location1, location2, problem):

    seq1 = location1.extract_sequence(problem.sequence)
    seq2 = location2.extract_sequence(problem.sequence)
    return sequences_differences(seq1, seq2) >= 2
Example #4
0
"""Example of use of the AvoidChanges as an objective to minimize modifications
of a sequence."""

from dnachisel import (DnaOptimizationProblem, random_dna_sequence,
                       AvoidPattern, AvoidChanges, sequences_differences,
                       EnforceGCContent)

# Note: we are not providing a location for AvoidChanges: it applies globally

for boost in (0, 0.1, 1, 10.0):
    sequence = random_dna_sequence(1000, seed=123)
    problem = DnaOptimizationProblem(
        sequence=sequence,
        objectives=[
            EnforceGCContent(mini=0.45, maxi=0.55, window=80),
            AvoidChanges(boost=boost).as_passive_objective()
        ])

    problem.optimize()
    differences = sequences_differences(problem.sequence,
                                        problem.sequence_before)

    print("%d nucleotides modified for boost=%.1f" % (differences, boost))