Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion psm_utils/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,16 @@ def _supports_write_psm(writer: type[WriterBase]) -> bool:
temp_file.close()
Path(temp_file.name).unlink()
example_psm = PSM(peptidoform="ACDE", spectrum_id="0")

# Prepare writer-specific kwargs for writers that need them
writer_kwargs = {}
if writer == percolator.PercolatorTabWriter:
writer_kwargs["style"] = "pin"

try:
with writer(temp_file.name, example_psm=example_psm) as writer_instance:
with writer(
temp_file.name, example_psm=example_psm, **writer_kwargs
) as writer_instance:
writer_instance.write_psm(example_psm)
except NotImplementedError:
supports_write_psm = False
Expand Down
46 changes: 28 additions & 18 deletions psm_utils/io/pepxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
"mzFidelity",
]

KNOWN_METADATA_KEYS = [
"num_matched_ions",
"tot_num_ions",
"num_missed_cleavages",
]


class PepXMLReader(ReaderBase):
"""Reader for pepXML PSM files."""
Expand Down Expand Up @@ -127,47 +133,51 @@ def _parse_peptidoform(

def _parse_psm(self, spectrum_query: dict[str, Any], search_hit: dict[str, Any]) -> PSM:
"""Parse pepXML PSM to PSM."""
metadata = {
"num_matched_ions": str(search_hit["num_matched_ions"]),
"tot_num_ions": str(search_hit["tot_num_ions"]),
"num_missed_cleavages": str(search_hit["num_missed_cleavages"]),
}
# Build metadata from optional search hit fields
metadata = {key: str(search_hit[key]) for key in KNOWN_METADATA_KEYS if key in search_hit}

# Add all search scores to metadata
metadata.update(
{
f"search_score_{key.lower()}": str(search_hit["search_score"][key])
for key in search_hit["search_score"]
f"search_score_{key.lower()}": str(value)
for key, value in search_hit["search_score"].items()
}
)

# Build provenance data from optional spectrum query fields
provenance_data = {
k: str(v)
for k, v in {
"pepxml_index": spectrum_query.get("index"),
"start_scan": spectrum_query.get("start_scan"),
"end_scan": spectrum_query.get("end_scan"),
}.items()
if v is not None
}

return PSM(
peptidoform=self._parse_peptidoform(
search_hit["peptide"],
search_hit["modifications"],
spectrum_query["assumed_charge"],
),
spectrum_id=spectrum_query["spectrumNativeID"]
if "spectrumNativeID" in spectrum_query
else spectrum_query["spectrum"],
spectrum_id=spectrum_query.get("spectrumNativeID", spectrum_query.get("spectrum")),
run=None,
collection=None,
spectrum=None,
is_decoy=None,
score=search_hit["search_score"][self.score_key],
score=search_hit["search_score"].get(self.score_key, None),
qvalue=None,
pep=None,
precursor_mz=mass_to_mz(
spectrum_query["precursor_neutral_mass"], spectrum_query["assumed_charge"]
),
retention_time=spectrum_query.get("retention_time_sec"),
ion_mobility=spectrum_query.get("ion_mobility"),
protein_list=[p["protein"] for p in search_hit["proteins"]],
rank=search_hit["hit_rank"],
protein_list=[p["protein"] for p in search_hit.get("proteins", [])],
rank=search_hit.get("hit_rank", None),
source=None,
provenance_data={
"pepxml_index": str(spectrum_query["index"]),
"start_scan": str(spectrum_query["start_scan"]),
"end_scan": str(spectrum_query["end_scan"]),
},
provenance_data=provenance_data,
metadata=metadata,
rescoring_features={},
)
6 changes: 4 additions & 2 deletions psm_utils/io/percolator.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,10 @@ def write_file(self, psm_list: PSMList) -> None:
f, fieldnames=self._columns, delimiter="\t", extrasaction="ignore"
)
writer.writeheader()
for psm in psm_list:
writer.writerow(self._psm_to_entry(psm))
for i, psm in enumerate(psm_list):
entry = self._psm_to_entry(psm)
entry["ScanNr"] = i
writer.writerow(entry)

def _psm_to_entry(self, psm: PSM) -> dict[str, Any]:
"""Parse PSM to Percolator Tab entry."""
Expand Down
Loading