Skip to content

Commit e9e3b94

Browse files
committed
Fix compatibility with pyOpenMS 3.5+ for PeptideIdentificationList handling
1 parent 9b6605d commit e9e3b94

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

psm_utils/io/idxml.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434
import pyopenms as oms # type: ignore[import]
3535

3636
_has_openms = True
37+
# Check if we have pyOpenMS 3.5+ with PeptideIdentificationList
38+
_has_peptide_id_list = hasattr(oms, "PeptideIdentificationList")
3739
except ImportError:
3840
_has_openms = False
41+
_has_peptide_id_list = False
3942
oms = None # type: ignore[assignment]
4043

4144
logger = logging.getLogger(__name__)
@@ -157,8 +160,17 @@ def _parse_idxml(self) -> tuple[Any, Any]:
157160
158161
"""
159162
protein_ids: Any = [] # list[oms.ProteinIdentification]
160-
peptide_ids: Any = [] # list[oms.PeptideIdentification]
161-
oms.IdXMLFile().load(str(self.filename), protein_ids, peptide_ids) # type: ignore
163+
# In pyOpenMS 3.5+, peptide_ids must be a PeptideIdentificationList
164+
if _has_peptide_id_list:
165+
peptide_ids: Any = oms.PeptideIdentificationList() # type: ignore
166+
else:
167+
peptide_ids = [] # list[oms.PeptideIdentification] for pyOpenMS <3.5
168+
169+
# Load the idXML file - the lists will be populated by pyOpenMS
170+
idxml_file = oms.IdXMLFile() # type: ignore
171+
# Ensure filename is a string, not a Path object
172+
filename_str: str = str(self.filename)
173+
idxml_file.load(filename_str, protein_ids, peptide_ids)
162174

163175
if len(protein_ids) == 0:
164176
raise IdXMLReaderEmptyListException(
@@ -564,7 +576,10 @@ def _update_existing_ids(
564576

565577
peptide_id.setHits(updated_peptide_hits)
566578

567-
oms.IdXMLFile().store(str(self.filename), self.protein_ids, self.peptide_ids) # type: ignore
579+
# Store the idXML file
580+
idxml_file = oms.IdXMLFile() # type: ignore
581+
filename_str: str = str(self.filename)
582+
idxml_file.store(filename_str, self.protein_ids, self.peptide_ids)
568583

569584
def _update_peptide_hit(self, peptide_hit: Any, psm: PSM) -> None:
570585
"""Inplace update of PeptideHit with novel predicted features information from PSM."""
@@ -594,7 +609,11 @@ def _create_ids_for_collection(
594609
) -> None:
595610
"""Create ProteinIdentification and PeptideIdentification objects for a single collection."""
596611
self.protein_ids = [oms.ProteinIdentification()] # type: ignore
597-
self.peptide_ids = []
612+
# In pyOpenMS 3.5+, peptide_ids must be a PeptideIdentificationList
613+
if _has_peptide_id_list:
614+
self.peptide_ids = oms.PeptideIdentificationList() # type: ignore
615+
else:
616+
self.peptide_ids = [] # list[oms.PeptideIdentification] for pyOpenMS <3.5
598617

599618
# Set msrun filename with spectra_data meta value
600619
msrun_reference = [str(run).encode() for run in runs.keys()]
@@ -617,14 +636,19 @@ def _create_ids_for_collection(
617636
# Create PeptideHits
618637
peptide_hits = [self._create_peptide_hit(psm) for psm in psms]
619638
peptide_id.setHits(peptide_hits)
620-
self.peptide_ids.append(peptide_id)
639+
# Use push_back for pyOpenMS 3.5+, append for older versions
640+
if _has_peptide_id_list:
641+
self.peptide_ids.push_back(peptide_id) # type: ignore
642+
else:
643+
self.peptide_ids.append(peptide_id) # type: ignore[attr-defined]
621644

622645
# Create protein hits
623646
self._create_protein_hits(protein_list)
624647

625648
# Write idXML file
626-
filename = "/".join(filter(None, [collection, str(self.filename)]))
627-
oms.IdXMLFile().store(filename, self.protein_ids, self.peptide_ids) # type: ignore
649+
filename: str = "/".join(filter(None, [collection, str(self.filename)]))
650+
idxml_file = oms.IdXMLFile() # type: ignore
651+
idxml_file.store(filename, self.protein_ids, self.peptide_ids) # type: ignore
628652

629653
def _create_peptide_identification(
630654
self,

0 commit comments

Comments
 (0)