• 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

93.75
/dnachisel/builtin_specifications/codon_optimization/AvoidRareCodons.py
1
"Implement AvoidRareCodons."
2

3
from ...Specification import SpecEvaluation
1✔
4
from ...biotools import reverse_complement
1✔
5
from .BaseCodonOptimizationClass import BaseCodonOptimizationClass
1✔
6

7

8
class AvoidRareCodons(BaseCodonOptimizationClass):
1✔
9
    """Avoid the use of codons with low frequency.
10

11
    This can be seen as a "mild" form of codon optimization where only rare
12
    codons (which slow down protein synthesis) are considered.
13

14
    WARNING: Make sure to always use this specification with EnforceTranslation
15
    to preserve the amino-acid sequence.
16

17
    Shorthand for annotations: "no_rare_codons".
18

19
    Parameters
20
    -----------
21
    min_frequency
22
      Minimal frequency accepted for a given codon.
23

24
    species
25
      Name or TaxID of the species for which to optimize the sequence. A custom
26
      codon_usage_table can be provided instead (or in addition, for species
27
      names whose codon usage table cannot be imported).
28
    
29
    codon_usage_table
30
      Optional codon usage table of the species for which the sequence will be
31
      codon-optimized, which can be provided instead of ``species``. A dict of
32
      the form ``{'*': {"TGA": 0.112, "TAA": 0.68}, 'K': ...}`` giving the codon
33
      frequency table (relative usage of each codon; frequencies add up to 1,
34
      separately for each amino acid). See parameter ``species`` above.
35

36
    location
37
      Either a DnaChisel Location or a tuple of the form (start, end, strand)
38
      or just (start, end), with strand defaulting to +1, indicating the
39
      position of the gene to codon-optimize. If not provided, the whole
40
      sequence is considered as the gene. The location should have a length
41
      that is a multiple of 3. The location strand is either 1 if the gene is
42
      encoded on the (+) strand, or -1 for antisense.
43

44
    boost
45
      Score multiplicator (=weight) for when the specification is used as an
46
      optimization objective alongside competing objectives.
47
    """
48

49
    best_possible_score = 0
1✔
50
    enforced_by_nucleotide_restrictions = True
1✔
51
    shorthand_name = "no_rare_codons"
1✔
52

53
    def __init__(
1✔
54
        self,
55
        min_frequency,
56
        species=None,
57
        codon_usage_table=None,
58
        location=None,
59
        boost=1.0,
60
    ):
61
        """Initialize."""
62

63
        BaseCodonOptimizationClass.__init__(
1✔
64
            self,
65
            species=species,
66
            codon_usage_table=codon_usage_table,
67
            location=location,
68
            boost=boost,
69
        )
70
        self.min_frequency = min_frequency
1✔
71
        self.codons_frequencies = {
1✔
72
            codon: freq
73
            for aa, aa_data in self.codon_usage_table.items()
74
            if len(aa) == 1
75
            for codon, freq in aa_data.items()
76
        }
77
        self.rare_codons = sorted(
1✔
78
            [
79
                codon
80
                for codon, frequency in self.codons_frequencies.items()
81
                if frequency < min_frequency
82
            ]
83
        )
84
        self.nonrare_codons = sorted(
1✔
85
            [
86
                codon
87
                for codon, frequency in self.codons_frequencies.items()
88
                if frequency >= min_frequency
89
            ]
90
        )
91

92
    def evaluate(self, problem):
1✔
93
        """Score is the sum of (freq - min_frequency) for all rare codons."""
94
        # Note: this method is actually very little used as this specification
95
        # class sets the enforced_by_nucleotide_restrictions attribute.
96
        codons = self.get_codons(problem)
1✔
97
        rare_codons_indices = [
1✔
98
            i for i, codon in enumerate(codons) if codon in self.rare_codons
99
        ]
100
        locations = self.codons_indices_to_locations(rare_codons_indices)
1✔
101
        score = (
1✔
102
            0
103
            if (len(locations) == 0)
104
            else sum(
105
                (self.codons_frequencies[codons[i]] - self.min_frequency)
106
                for i in rare_codons_indices
107
            )
108
        )
109
        return SpecEvaluation(
1✔
110
            self,
111
            problem,
112
            score=score,
113
            locations=locations,
114
            message="All OK."
115
            if len(locations) == 0
116
            else "Rare codons at locations %s" % locations,
117
        )
118

119
    def restrict_nucleotides(self, sequence, location=None):
1✔
120
        nonrare_codons = list(self.nonrare_codons)
1✔
121
        if self.location.strand == -1:
1✔
122
            nonrare_codons = sorted(
1✔
123
                [reverse_complement(c) for c in nonrare_codons]
124
            )
125
        return [
1✔
126
            ((i, i + 3), nonrare_codons)
127
            for i in range(self.location.start, self.location.end, 3)
128
        ]
129

130
    def _params_string(self):
1✔
131
        """Parameters representation used in __repr__, __str__, etc."""
132
        return "%d%%, %s" % (100 * self.min_frequency, str(self.species))
1✔
133

134
    def __repr__(self):
1✔
135
        return "AvoidRareCodons(%s)" % self._params_string()
×
136

137
    def __str__(self):
1✔
138
        return "AvoidRareCodons(%s)" % self._params_string()
1✔
139

140
    def short_label(self):
1✔
141
        return "no_rare_codons(%s)" % self._params_string()
×
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