Skip to content

Commit

Permalink
Merge branch 'main' into feature_write-idxml
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfG authored Oct 25, 2023
2 parents 7c81e52 + e7f0bfb commit cdeefdf
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions psm_utils/io/xtandem.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from __future__ import annotations

from pathlib import Path
from typing import Union

import numpy as np
from pyteomics import mass, tandem
Expand All @@ -53,11 +54,17 @@
from psm_utils.io._base_classes import ReaderBase
from psm_utils.peptidoform import Peptidoform
from psm_utils.psm import PSM
from psm_utils.psm_list import PSMList


class XTandemReader(ReaderBase):
def __init__(self, filename: str | Path, *args, decoy_prefix="DECOY_", **kwargs) -> None:
def __init__(
self,
filename: Union[str, Path],
*args,
decoy_prefix="DECOY_",
score_key="expect",
**kwargs,
) -> None:
"""
Reader for X!Tandem XML PSM files.
Expand All @@ -68,6 +75,11 @@ def __init__(self, filename: str | Path, *args, decoy_prefix="DECOY_", **kwargs)
decoy_prefix: str, optional
Protein name prefix used to denote decoy protein entries. Default:
``"DECOY_"``.
score_key: str, optional
Key of score to use as PSM score. One of ``"expect"``, ``"hyperscore"``,
``"delta"``, or ``"nextscore"``. Default: ``"expect"``. The ``"expect"`` score
(e-value) is converted to its negative natural logarithm to facilitate downstream
analysis.
Examples
--------
Expand All @@ -91,6 +103,7 @@ def __init__(self, filename: str | Path, *args, decoy_prefix="DECOY_", **kwargs)
"""
super().__init__(filename)
self.decoy_prefix = decoy_prefix
self.score_key = score_key

def __iter__(self):
"""Iterate over file and return PSMs one-by-one."""
Expand Down Expand Up @@ -144,7 +157,9 @@ def _parse_entry(self, entry) -> PSM:
peptidoform=self._parse_peptidoform(peptide_entry, entry["z"]),
spectrum_id=entry["support"]["fragment ion mass spectrum"]["note"],
is_decoy=entry["protein"][0]["label"].startswith(self.decoy_prefix),
score=-np.log(peptide_entry["expect"]),
score=-np.log(peptide_entry[self.score_key])
if self.score_key == "expect"
else peptide_entry[self.score_key],
precursor_mz=entry["mh"] - mass.nist_mass["H"][0][0],
retention_time=entry["rt"],
protein_list=[entry["protein"][0]["label"]],
Expand Down

0 comments on commit cdeefdf

Please sign in to comment.