• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Edinburgh-Genome-Foundry / DnaChisel / 5190565251

pending completion
5190565251

push

github

veghp
Bump to v3.2.11

1 of 1 new or added line in 1 file covered. (100.0%)

2966 of 3299 relevant lines covered (89.91%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.23
/dnachisel/builtin_specifications/AvoidPattern.py
1
"""Implement AvoidPattern"""
2

3
from ..SequencePattern import SequencePattern
1✔
4
from ..Location import Location
1✔
5
from ..Specification.Specification import Specification
1✔
6
from ..Specification.SpecEvaluation import SpecEvaluation
1✔
7

8

9
class AvoidPattern(Specification):
1✔
10
    """Enforce that the given pattern is absent in the sequence.
11

12
    Shorthand for annotations: "no".
13

14
    Parameters
15
    ----------
16

17
    pattern
18
      A SequencePattern or DnaNotationPattern. If a ``str`` is given, it will
19
      be converted. Note that providing ``size`` may be necessary for certain
20
      patterns. See SequencePattern documentation for more details.
21

22
    location
23
      Location of the DNA segment on which to enforce the pattern e.g.
24
      ``Location(10, 45, 1)``. For patterns which are not palindromic,
25
      the strand matters! Use +1 for eliminating the pattern on the +1 strand
26
      only, -1 for eliminating the pattern on the -1 strand, and 0 for
27
      eliminating the pattern on both strands. Default ``None`` enforces on
28
      the whole sequence.
29

30
    strand
31
      Alternative way to set the strand, meant to be used in two cases only:
32
      (1) in a Genbank annotation by setting ``strand=both`` to indicate that
33
      the pattern should be avoided on both strands (otherwise, only the
34
      feature's strand will be considered).
35
      (2) if you want to create a specification without preset location, but
36
      with a set strand: ``AvoidPattern('BsmBI_site', strand=1)``.
37
      The default 'from_location' uses the strand specified in ``location``,
38
      or if that is ``None``, it sets both strands.
39
    """
40

41
    best_possible_score = 0
1✔
42
    priority = 1
1✔
43
    shorthand_name = "no"  # will appear as, for instance, @no(BsmBI_site)
1✔
44

45
    def __init__(self, pattern=None, location=None, strand="from_location", boost=1.0):
1✔
46
        """Initialize."""
47
        if isinstance(pattern, str):
1✔
48
            pattern = SequencePattern.from_string(pattern)
1✔
49
        self.pattern = pattern
1✔
50
        self.location = Location.from_data(location)
1✔
51

52
        if strand == "from_location":
1✔
53
            if self.location is None:
1✔
54
                self.strand = 0
1✔
55
            else:
56
                self.strand = self.location.strand
1✔
57
        elif strand == "both":
1✔
58
            self.strand = 0
1✔
59
        elif strand in [-1, 0, 1]:
1✔
60
            self.strand = strand
1✔
61
        else:
62
            raise ValueError("unknown strand: %s" % strand)
×
63

64
        self.boost = boost
1✔
65

66
    def evaluate(self, problem):
1✔
67
        """Return score=-number_of_occurences. And patterns locations."""
68
        locations = self.pattern.find_matches(problem.sequence, self.location)
1✔
69
        score = -len(locations)
1✔
70
        if score == 0:
1✔
71
            message = "Passed. Pattern not found !"
1✔
72
        else:
73
            message = "Failed. Pattern found at positions %s" % locations
1✔
74
        return SpecEvaluation(
1✔
75
            self, problem, score, locations=locations, message=message
76
        )
77

78
    def short_label(self):
1✔
79
        if self.pattern.name is not None:
1✔
80
            return "No %s" % self.pattern.name
1✔
81
        else:
82
            return "No %s" % self.pattern
×
83

84
    def breach_label(self):
1✔
85
        if self.pattern.name is not None:
1✔
86
            return str(self.pattern.name)
1✔
87
        else:
88
            return str(self.pattern)
×
89

90
    def initialized_on_problem(self, problem, role="constraint"):
1✔
91
        copy_of_constraint = self._copy_with_full_span_if_no_location(problem)
1✔
92
        copy_of_constraint.location.strand = self.strand
1✔
93
        return copy_of_constraint
1✔
94

95
    def localized(self, location, problem=None, with_righthand=True):
1✔
96
        """Localize the pattern to the given location. Taking into account the
97
        specification's own location, and the size of the pattern."""
98
        if self.location.overlap_region(location) is None:
1✔
99
            return None
1✔
100
        if self.pattern.size is None:
1✔
101
            return self
1✔
102
        extended_location = location.extended(
1✔
103
            self.pattern.size - 1, right=with_righthand
104
        )
105
        new_location = self.location.overlap_region(extended_location)
1✔
106
        return self.copy_with_changes(location=new_location)
1✔
107

108
    def label_parameters(self):
1✔
109
        return [("pattern", str(self.pattern))]
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc