Skip to content

Commit b3a397e

Browse files
committed
add pyresidue class
This class is a single amino acid, wrapper around Residue object
1 parent ac76123 commit b3a397e

File tree

5 files changed

+879
-11
lines changed

5 files changed

+879
-11
lines changed

openms_python/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from .py_consensusmap import Py_ConsensusMap
3131
from .py_experimentaldesign import Py_ExperimentalDesign
3232
from .py_aasequence import Py_AASequence
33+
from .py_residue import Py_Residue
3334
from .py_identifications import (
3435
ProteinIdentifications,
3536
PeptideIdentifications,
@@ -109,6 +110,7 @@ def get_example(name: str, *, load: bool = False, target_dir: Union[str, Path, N
109110
"Py_ConsensusMap",
110111
"Py_ExperimentalDesign",
111112
"Py_AASequence",
113+
"Py_Residue",
112114
"ProteinIdentifications",
113115
"PeptideIdentifications",
114116
"Identifications",

openms_python/py_aasequence.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Optional, Literal
66
import pyopenms as oms
77
import warnings
8+
from .py_residue import Py_Residue
89

910

1011
class Py_AASequence:
@@ -242,20 +243,20 @@ def __getitem__(self, index):
242243
if step != 1:
243244
raise ValueError("Step slicing is not supported for amino acid sequences")
244245
return Py_AASequence.from_native(self._sequence.getSubsequence(start, stop - start))
245-
else:
246+
else: # isinstance(index, int)
246247
# Handle negative indices
247248
if index < 0:
248249
index = len(self) + index
249250
if index >= len(self):
250251
raise IndexError(f"Index {index} out of range for sequence of length {len(self)}")
251-
residue = self._sequence.getSubsequence(index, 1)
252-
return Py_AASequence.from_native(residue)
252+
residue = self._sequence.getResidue(index)
253+
return Py_Residue.from_native(residue)
253254

254255
def __iter__(self):
255256
"""Iterate over residues."""
256257
for i in range(len(self)):
257258
yield self[i]
258-
def __add__(self, other: Py_AASequence | str) -> Py_AASequence:
259+
def __add__(self, other: Py_AASequence | str | Py_Residue) -> Py_AASequence:
259260
"""
260261
Concatenate sequences.
261262
@@ -279,6 +280,8 @@ def __add__(self, other: Py_AASequence | str) -> Py_AASequence:
279280
combined_str = self.sequence + other.sequence
280281
elif isinstance(other, str):
281282
combined_str = self.sequence + other
283+
elif isinstance(other, Py_Residue):
284+
combined_str = self.sequence + other.one_letter_code
282285
else:
283286
return NotImplemented
284287
return Py_AASequence.from_string(combined_str)
@@ -296,6 +299,9 @@ def __radd__(self, other: str) -> Py_AASequence:
296299
if isinstance(other, str):
297300
combined_str = other + self.sequence
298301
return Py_AASequence.from_string(combined_str)
302+
if isinstance(other, Py_Residue):
303+
combined_str = other.one_letter_code + self.sequence
304+
return Py_AASequence.from_string(combined_str)
299305
return NotImplemented
300306

301307
def __mul__(self, times: int) -> Py_AASequence:

0 commit comments

Comments
 (0)