Skip to content

Commit 251d33e

Browse files
committed
Fix #86
1 parent c429d82 commit 251d33e

File tree

8 files changed

+56
-52
lines changed

8 files changed

+56
-52
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ The example below will generate a random sequence and optimize it so that:
6464
EnforceTranslation(location=(500, 1400))
6565
],
6666
objectives=[CodonOptimize(species='e_coli', location=(500, 1400))]
67-
)
67+
) # Note: always use a codon optimisation specification with EnforceTranslation
6868
6969
# SOLVE THE CONSTRAINTS, OPTIMIZE WITH RESPECT TO THE OBJECTIVE
7070

dnachisel/builtin_specifications/EnforceTranslation.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class EnforceTranslation(CodonSpecification):
2020
2121
Shorthand for annotations: "cds".
2222
23+
Note: always use a codon optimisation specification with EnforceTranslation.
24+
25+
2326
Parameters
2427
-----------
2528
@@ -98,10 +101,7 @@ def set_location(self, location):
98101
len(location) != 3 * len(self.translation)
99102
):
100103
raise ValueError(
101-
(
102-
"Window size (%d bp) incompatible with translation "
103-
"(%d aa)"
104-
)
104+
("Window size (%d bp) incompatible with translation " "(%d aa)")
105105
% (len(location), len(self.translation))
106106
)
107107
self.location = location
@@ -119,10 +119,7 @@ def initialized_on_problem(self, problem, role):
119119
result = result.copy_with_changes(translation=translation)
120120
if len(result.location) != 3 * len(result.translation):
121121
raise ValueError(
122-
(
123-
"Window size (%d bp) incompatible with translation "
124-
"(%d aa)"
125-
)
122+
("Window size (%d bp) incompatible with translation " "(%d aa)")
126123
% (len(result.location), len(result.translation))
127124
)
128125
if (result.start_codon is not None) and result.translation[0] != "M":
@@ -163,9 +160,11 @@ def evaluate(self, problem):
163160
problem,
164161
score=-len(errors_locations),
165162
locations=errors_locations,
166-
message="All OK."
167-
if len(errors_locations) == 0
168-
else "Wrong translation at locations %s" % errors_locations,
163+
message=(
164+
"All OK."
165+
if len(errors_locations) == 0
166+
else "Wrong translation at locations %s" % errors_locations
167+
),
169168
)
170169

171170
def localized_on_window(self, new_location, start_codon, end_codon):
@@ -179,7 +178,7 @@ def localized_on_window(self, new_location, start_codon, end_codon):
179178
translation=new_translation,
180179
boost=self.boost,
181180
genetic_table=self.genetic_table,
182-
start_codon=self.start_codon if location_is_at_start else None
181+
start_codon=self.start_codon if location_is_at_start else None,
183182
# has_start_codon=self.has_start_codon and location_is_at_start,
184183
)
185184

@@ -196,11 +195,10 @@ def get_first_codon_choices(first_codon):
196195
return [first_codon]
197196
else:
198197
return [self.start_codon] # "ATG"
198+
199199
first_codon_location = self.codon_index_to_location(0)
200200
first_codon = first_codon_location.extract_sequence(sequence)
201-
choices = [
202-
(first_codon_location, get_first_codon_choices(first_codon))
203-
] + [
201+
choices = [(first_codon_location, get_first_codon_choices(first_codon))] + [
204202
(self.codon_index_to_location(i), self.backtranslation_table[aa])
205203
for i, aa in list(enumerate(self.translation))[1:]
206204
]
@@ -221,6 +219,6 @@ def __str__(self):
221219

222220
def short_label(self):
223221
return "cds"
224-
222+
225223
def breach_label(self):
226224
return "protein sequence changed"

dnachisel/builtin_specifications/codon_optimization/AvoidRareCodons.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class AvoidRareCodons(BaseCodonOptimizationClass):
1111
This can be seen as a "mild" form of codon optimization where only rare
1212
codons (which slow down protein synthesis) are considered.
1313
14-
WARNING: Make sure to always use this specification with EnforceTranslation
15-
to preserve the amino-acid sequence.
14+
Warning: always use this specification with an EnforceTranslation constraint
15+
defined over the same location, to preserve the amino acid sequence.
1616
1717
Shorthand for annotations: "no_rare_codons".
1818
@@ -25,7 +25,7 @@ class AvoidRareCodons(BaseCodonOptimizationClass):
2525
Name or TaxID of the species for which to optimize the sequence. A custom
2626
codon_usage_table can be provided instead (or in addition, for species
2727
names whose codon usage table cannot be imported).
28-
28+
2929
codon_usage_table
3030
Optional codon usage table of the species for which the sequence will be
3131
codon-optimized, which can be provided instead of ``species``. A dict of
@@ -111,17 +111,17 @@ def evaluate(self, problem):
111111
problem,
112112
score=score,
113113
locations=locations,
114-
message="All OK."
115-
if len(locations) == 0
116-
else "Rare codons at locations %s" % locations,
114+
message=(
115+
"All OK."
116+
if len(locations) == 0
117+
else "Rare codons at locations %s" % locations
118+
),
117119
)
118120

119121
def restrict_nucleotides(self, sequence, location=None):
120122
nonrare_codons = list(self.nonrare_codons)
121123
if self.location.strand == -1:
122-
nonrare_codons = sorted(
123-
[reverse_complement(c) for c in nonrare_codons]
124-
)
124+
nonrare_codons = sorted([reverse_complement(c) for c in nonrare_codons])
125125
return [
126126
((i, i + 3), nonrare_codons)
127127
for i in range(self.location.start, self.location.end, 3)

dnachisel/builtin_specifications/codon_optimization/CodonOptimize.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def CodonOptimize(
1010
codon_usage_table=None,
1111
original_species=None,
1212
original_codon_usage_table=None,
13-
boost=1.0
13+
boost=1.0,
1414
):
1515
"""Codon-optimize a coding sequence using a user-selected method.
1616
@@ -28,6 +28,10 @@ def CodonOptimize(
2828
codon whose usage in the target organism matches the usage of the
2929
original codon in its host organism (as per Claassens 2017).
3030
31+
Warning: always use this specification with an EnforceTranslation constraint
32+
defined over the same location, to preserve the amino acid sequence.
33+
34+
3135
Parameters
3236
==========
3337
species
@@ -105,5 +109,7 @@ def CodonOptimize(
105109
original_codon_usage_table=original_codon_usage_table,
106110
boost=boost,
107111
)
108-
raise ValueError("`method` must be 'use_best_codon', 'match_codon_usage' "
109-
f"or 'harmonize_rca', not {method!r}")
112+
raise ValueError(
113+
"`method` must be 'use_best_codon', 'match_codon_usage' "
114+
f"or 'harmonize_rca', not {method!r}"
115+
)

dnachisel/builtin_specifications/codon_optimization/HarmonizeRCA.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class HarmonizeRCA(BaseCodonOptimizationClass):
2727
algorithm (Angov 2008), which was much more complicated as it involved
2828
predicting "ribosome pausing" sites in the sequence.
2929
30-
Warning: always use with an EnforceTranslation constraint.
30+
Warning: always use this specification with an EnforceTranslation constraint
31+
defined over the same location, to preserve the amino acid sequence.
3132
3233
3334
Parameters

dnachisel/builtin_specifications/codon_optimization/MatchTargetCodonUsage.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class MatchTargetCodonUsage(BaseCodonOptimizationClass):
2020
host-to-target codon harmonization. See DnaChisel's HarmonizeRCA class
2121
for Codon Harmonization.
2222
23+
Warning: always use this specification with an EnforceTranslation constraint
24+
defined over the same location, to preserve the amino acid sequence.
25+
26+
2327
Parameters
2428
----------
2529
@@ -71,9 +75,7 @@ class MatchTargetCodonUsage(BaseCodonOptimizationClass):
7175

7276
shorthand_name = "match_codon_usage"
7377

74-
def __init__(
75-
self, species=None, location=None, codon_usage_table=None, boost=1.0
76-
):
78+
def __init__(self, species=None, location=None, codon_usage_table=None, boost=1.0):
7779
BaseCodonOptimizationClass.__init__(
7880
self,
7981
species=species,
@@ -130,8 +132,7 @@ def evaluate(self, problem):
130132
problem,
131133
score=score,
132134
locations=locations,
133-
message="Codon opt. on window %s scored %.02E"
134-
% (self.location, score),
135+
message="Codon opt. on window %s scored %.02E" % (self.location, score),
135136
)
136137

137138
def localized_on_window(self, new_location, start_codon, end_codon):
@@ -177,9 +178,7 @@ def compare_frequencies(self, codons, text_mode=False):
177178
for i, codon in enumerate(codons):
178179
codons_positions[codon].append(i)
179180
# aa: amino-acid
180-
codons_frequencies = {
181-
aa: {"total": 0} for aa in self.codon_usage_table
182-
}
181+
codons_frequencies = {aa: {"total": 0} for aa in self.codon_usage_table}
183182
for codon, positions in codons_positions.items():
184183
count = len(positions)
185184
aa = self.codons_translations[codon]
@@ -191,9 +190,7 @@ def compare_frequencies(self, codons, text_mode=False):
191190
if codon != "total":
192191
data[codon] = 1.0 * value / total
193192
codons_frequencies = {
194-
aa: data
195-
for aa, data in codons_frequencies.items()
196-
if data["total"]
193+
aa: data for aa, data in codons_frequencies.items() if data["total"]
197194
}
198195
comparisons = {
199196
aa: {
@@ -209,8 +206,9 @@ def compare_frequencies(self, codons, text_mode=False):
209206
return dict_to_pretty_string(comparisons)
210207
else:
211208
return codons_positions, comparisons
209+
212210
def short_label(self):
213211
result = "match-codon-usage"
214212
if self.species is not None:
215213
result += " (%s)" % self.species
216-
return result
214+
return result

dnachisel/builtin_specifications/codon_optimization/MaximizeCAI.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class MaximizeCAI(BaseCodonOptimizationClass):
2020
2121
This score is between -inf. and 0 (0 meaning a perfectly optimal sequence).
2222
23+
Warning: always use this specification with an EnforceTranslation constraint
24+
defined over the same location, to preserve the amino acid sequence.
25+
26+
2327
Parameters
2428
----------
2529
@@ -65,9 +69,7 @@ class MaximizeCAI(BaseCodonOptimizationClass):
6569

6670
shorthand_name = "use_best_codon"
6771

68-
def __init__(
69-
self, species=None, location=None, codon_usage_table=None, boost=1.0
70-
):
72+
def __init__(self, species=None, location=None, codon_usage_table=None, boost=1.0):
7173
BaseCodonOptimizationClass.__init__(
7274
self,
7375
species=species,
@@ -105,12 +107,10 @@ def evaluate(self, problem):
105107
problem,
106108
score=freq - optimal,
107109
locations=[] if (freq == optimal) else [self.location],
108-
message="Codon opt. on window %s scored %.02E"
109-
% (self.location, score),
110+
message="Codon opt. on window %s scored %.02E" % (self.location, score),
110111
)
111112
current_usage = [
112-
self.codon_usage_table["log_codons_frequencies"][codon]
113-
for codon in codons
113+
self.codon_usage_table["log_codons_frequencies"][codon] for codon in codons
114114
]
115115
optimal_usage = [
116116
self.codon_usage_table["log_best_frequencies"][ct[codon]]
@@ -125,8 +125,7 @@ def evaluate(self, problem):
125125
problem,
126126
score=score,
127127
locations=locations,
128-
message="Codon opt. on window %s scored %.02E"
129-
% (self.location, score),
128+
message="Codon opt. on window %s scored %.02E" % (self.location, score),
130129
)
131130

132131
def label_parameters(self):
@@ -137,4 +136,3 @@ def short_label(self):
137136
if self.species is not None:
138137
result += " (%s)" % self.species
139138
return result
140-

dnachisel/builtin_specifications/codon_optimization/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ optimization that one can find in the literature.
1212

1313
Finally, ``CodonOptimize`` is a generic pseudo-specification-class which uses a "mode"
1414
parameter to return a specification of one of the above classes.
15+
16+
Warning: always use this specification with an EnforceTranslation constraint defined
17+
over the same location, to preserve the amino acid sequence.

0 commit comments

Comments
 (0)