Skip to content

Commit 14d1623

Browse files
committed
Fix protein_scale for window 1 and even window sizes
A window of 1 crashed on a division by zero in _weight_list, and an even window double-counted subsequence[window//2] (once as the back of the innermost mirrored pair, once as the 'middle'), making profiles depend on sequence direction. Even windows now skip the middle term and normalize by the sum of the two-sided weights. Fixes biopython#5340. I agree to dual license my contribution under both the Biopython License Agreement and the 3-Clause BSD License.
1 parent 6ac7388 commit 14d1623

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

‎Bio/SeqUtils/ProtParam.py‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def _weight_list(self, window, edge):
210210
For a window of size 9 and edge 0.4 you get a list of
211211
[0.4, 0.55, 0.7, 0.85].
212212
"""
213-
unit = 2 * (1.0 - edge) / (window - 1)
213+
unit = 2 * (1.0 - edge) / (window - 1) if window > 1 else 0.0
214214
weights = [0.0] * (window // 2)
215215

216216
for i in range(window // 2):
@@ -260,8 +260,9 @@ def protein_scale(self, param_dict, window, edge=1.0):
260260
scores = []
261261

262262
# the score in each Window is divided by the sum of weights
263-
# (* 2 + 1) since the weight list is one sided:
264-
sum_of_weights = sum(weights) * 2 + 1
263+
# (* 2 + 1) since the weight list is one sided. An even window
264+
# has no single center position, so its weights sum to 2*weights.
265+
sum_of_weights = sum(weights) * 2 + (window % 2)
265266

266267
for i in range(self.length - window + 1):
267268
subsequence = self.sequence[i : i + window]
@@ -282,11 +283,16 @@ def protein_scale(self, param_dict, window, edge=1.0):
282283
)
283284

284285
# Now add the middle value, which always has a weight of 1.
285-
middle = subsequence[window // 2]
286-
if middle in param_dict:
287-
score += param_dict[middle]
288-
else:
289-
sys.stderr.write(f"warning: {middle} is not a standard amino acid.\n")
286+
# For an even window there is no middle position: the two
287+
# center residues are covered by the innermost mirrored pair.
288+
if window % 2 == 1:
289+
middle = subsequence[window // 2]
290+
if middle in param_dict:
291+
score += param_dict[middle]
292+
else:
293+
sys.stderr.write(
294+
f"warning: {middle} is not a standard amino acid.\n"
295+
)
290296

291297
scores.append(score / sum_of_weights)
292298

‎Tests/test_ProtParam.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,26 @@ def test_protein_scale(self):
202202
# Expected values have 4 decimal places, so restrict to that exactness
203203
self.assertAlmostEqual(i, e, places=4)
204204

205+
def test_protein_scale_window_one(self):
206+
"""A window of 1 returns the scale value for each residue."""
207+
scale = {aa: float(i + 1) for i, aa in enumerate("ACDEFGHIK")}
208+
analysis = ProtParam.ProteinAnalysis("ACDEFGHIK")
209+
result = analysis.protein_scale(scale, 1, edge=1.0)
210+
self.assertEqual(result, [float(i + 1) for i in range(9)])
211+
212+
def test_protein_scale_even_window_direction_invariant(self):
213+
"""An even window must not double-count a residue: the profile of a
214+
reversed sequence is the reversed profile."""
215+
scale = {aa: float(i + 1) for i, aa in enumerate("ACDEFGHIK")}
216+
forward = ProtParam.ProteinAnalysis("ACDEFGHIK").protein_scale(
217+
scale, 4, edge=0.4
218+
)
219+
reverse = ProtParam.ProteinAnalysis("KIHGFEDCA").protein_scale(
220+
scale, 4, edge=0.4
221+
)[::-1]
222+
for f, r in zip(forward, reverse):
223+
self.assertAlmostEqual(f, r, places=7)
224+
205225
def test_gravy(self):
206226
"""Calculate gravy. Tests all pre-defined scales."""
207227
expected_values = {

0 commit comments

Comments
 (0)