Skip to content

Commit 70eb83d

Browse files
committed
test: fix tests, allow for negative indices in slicing
1 parent 1d0d031 commit 70eb83d

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

openms_python/py_aasequence.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def __getitem__(self, index):
245245
# Handle negative indices
246246
if index < 0:
247247
index = len(self) + index
248-
if index < 0 or index >= len(self):
248+
if index >= len(self):
249249
raise IndexError(f"Index {index} out of range for sequence of length {len(self)}")
250250
residue = self._sequence.getResidue(index)
251251
residue_char = residue.getOneLetterCode()
@@ -394,10 +394,18 @@ def has_suffix(self, suffix: str) -> bool:
394394

395395

396396
# ===================== Exporting =======================
397-
def to_string(self, modified=True, mod_format: Optional[Literal['unimod', 'bracket']] = 'unimod') -> str:
397+
def to_string(self, modified=True, mod_format: Optional[Literal['default', 'unimod', 'bracket']] = 'default') -> str:
398398
"""
399399
Get string representation of the sequence.
400400
401+
Args:
402+
modified (bool): Whether to include modifications in the string.
403+
mod_format (Optional[Literal['default', 'unimod', 'bracket']]): Format for modifications.
404+
'default' for OpenMS format,
405+
'unimod' for UniMod format,
406+
'bracket' for bracket notation.
407+
Default is 'unimod'.
408+
401409
Returns:
402410
str: Amino acid sequence as string.
403411
@@ -409,7 +417,9 @@ def to_string(self, modified=True, mod_format: Optional[Literal['unimod', 'brack
409417
return self.unmodified_sequence
410418

411419
else:
412-
if mod_format == 'unimod':
420+
if mod_format == 'default':
421+
return self._sequence.toString()
422+
elif mod_format == 'unimod':
413423
return self._sequence.toUniModString()
414424
elif mod_format == 'bracket':
415425
return self._sequence.toBracketString()

tests/test_py_aasequence.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,22 @@ def test_py_aasequence_iteration():
118118
seq = Py_AASequence.from_string("PEPTIDE")
119119
residues = list(seq)
120120

121-
assert residues == ["P", "E", "P", "T", "I", "D", "E"]
121+
assert [res.sequence for res in residues] == ["P", "E", "P", "T", "I", "D", "E"]
122122
assert len(residues) == 7
123123

124124

125125
def test_py_aasequence_indexing():
126126
"""Test indexing into sequence."""
127127
seq = Py_AASequence.from_string("PEPTIDE")
128128

129-
assert seq[0] == "P"
130-
assert seq[1] == "E"
131-
assert seq[6] == "E"
129+
assert seq[0].sequence == "P"
130+
assert seq[1].sequence == "E"
131+
assert seq[6].sequence == "E"
132132

133133
# Test out of bounds
134134
with pytest.raises(IndexError):
135135
_ = seq[7]
136136

137-
with pytest.raises(IndexError):
138-
_ = seq[-1]
139-
140137

141138
def test_py_aasequence_string_representation():
142139
"""Test string representations."""
@@ -258,3 +255,37 @@ def test_py_aasequence_with_native_aasequence():
258255

259256
assert seq.sequence == "PEPTIDE"
260257
assert seq.native is native
258+
259+
260+
def test_py_aasequence_to_string():
261+
"""Test to_string method with different options."""
262+
seq = Py_AASequence.from_string("PEPTIDEM(Oxidation)")
263+
264+
# Default should return modified string in default format
265+
mod_str = seq.to_string()
266+
assert mod_str == "PEPTIDEM(Oxidation)"
267+
268+
# Unmodified should return unmodified sequence
269+
unmod_str = seq.to_string(modified=False)
270+
assert unmod_str == "PEPTIDEM"
271+
272+
# Bracket format
273+
bracket_str = seq.to_string(modified=True, mod_format='bracket')
274+
assert bracket_str == "PEPTIDEM[147]"
275+
276+
# unimod format
277+
unimod_str = seq.to_string(modified=True, mod_format='unimod')
278+
assert unimod_str == "PEPTIDEM(UniMod:35)"
279+
280+
# Invalid format should raise error
281+
with pytest.raises(ValueError):
282+
_ = seq.to_string(modified=True, mod_format='invalid_format')
283+
284+
285+
def test_slicing():
286+
aa_seq = Py_AASequence.from_string('PEPTIDEM(Oxidation)R')
287+
assert aa_seq[0].sequence == 'P'
288+
assert aa_seq[-1].sequence == 'R'
289+
assert aa_seq[1:4].sequence == 'EPT'
290+
assert aa_seq[-2:].sequence == 'M(Oxidation)R'
291+

0 commit comments

Comments
 (0)