@@ -58,14 +58,8 @@ def _get_peptide_spectrum_match(self, psm_dict) -> PSM:
5858 except KeyError :
5959 continue
6060
61- # If ion mobility is not 0.0 (not present), add it to the rescoring features
62- if "ion_mobility" in psm_dict : # Older versions of Sage have no ion mobility column
63- if float (psm_dict ['ion_mobility' ]):
64- rescoring_features .update ({
65- 'ion_mobility' : float (psm_dict ['ion_mobility' ]),
66- 'predicted_mobility' : float (psm_dict ['predicted_mobility' ]),
67- 'delta_mobility' : float (psm_dict ['delta_mobility' ])
68- })
61+ ion_mobility_features = self ._extract_ion_mobility_features (psm_dict )
62+ rescoring_features .update (ion_mobility_features )
6963
7064 return PSM (
7165 peptidoform = self ._parse_peptidoform (
@@ -79,7 +73,7 @@ def _get_peptide_spectrum_match(self, psm_dict) -> PSM:
7973 score = float (psm_dict [self .score_column ]),
8074 precursor_mz = self ._parse_precursor_mz (psm_dict ["expmass" ], psm_dict ["charge" ]),
8175 retention_time = float (psm_dict ["rt" ]),
82- ion_mobility = self . _parse_ion_mobility ( psm_dict ),
76+ ion_mobility = rescoring_features . get ( "ion_mobility" , None ),
8377 protein_list = psm_dict ["proteins" ].split (";" ),
8478 source = "sage" ,
8579 rank = int (float (psm_dict ["rank" ])),
@@ -104,15 +98,22 @@ def _parse_precursor_mz(expmass: str, charge: Optional[str]) -> Optional[float]:
10498 return None
10599
106100 @staticmethod
107- def _parse_ion_mobility (psm_dict : dict ) -> Optional [ float ] :
101+ def _extract_ion_mobility_features (psm_dict : dict ) -> dict :
108102 """
109- Parse ion mobility from PSM dictionary.
110- Returns None if not present .
103+ Extract ion mobility features from the PSM dictionary if present and non-zero .
104+ Returns a dict with the relevant keys or an empty dict .
111105 """
112- if "ion_mobility" in psm_dict : # Older versions of Sage have no ion mobility column
113- if float (psm_dict ["ion_mobility" ]): # If ion mobility is not 0.0 (not present)
114- return float (psm_dict ["ion_mobility" ])
115- return None
106+ try :
107+ ion_mob = float (psm_dict ["ion_mobility" ])
108+ if ion_mob :
109+ return {
110+ "ion_mobility" : ion_mob ,
111+ "predicted_mobility" : float (psm_dict ["predicted_mobility" ]),
112+ "delta_mobility" : float (psm_dict ["delta_mobility" ]),
113+ }
114+ except (KeyError , ValueError ):
115+ pass
116+ return {}
116117
117118 @classmethod
118119 def from_dataframe (cls , dataframe ) -> PSMList :
0 commit comments