Skip to content

Commit

Permalink
Reimplement most of spectrum_output for v4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfG committed May 7, 2024
1 parent 2ee2340 commit 59ad47d
Show file tree
Hide file tree
Showing 5 changed files with 661 additions and 703 deletions.
4 changes: 3 additions & 1 deletion ms2pip/_utils/dlib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Database configuration for EncyclopeDIA DLIB SQLite format."""

import zlib
from pathlib import Path
from typing import Union

import numpy
import sqlalchemy
Expand Down Expand Up @@ -91,7 +93,7 @@ def copy(self):
)


def open_sqlite(filename):
def open_sqlite(filename: Union[str, Path]) -> sqlalchemy.engine.Connection:
engine = sqlalchemy.create_engine(f"sqlite:///{filename}")
metadata.bind = engine
return engine.connect()
23 changes: 23 additions & 0 deletions ms2pip/plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pathlib import Path
from typing import Union

from ms2pip.spectrum import Spectrum

try:
import matplotlib.pyplot as plt
import spectrum_utils.plot as sup

_can_plot = True
except ImportError:
_can_plot = False


def spectrum_to_png(spectrum: Spectrum, filepath: Union[str, Path]):
"""Plot a single spectrum and write to a PNG file."""
if not _can_plot:
raise ImportError("Matplotlib and spectrum_utils are required to plot spectra.")
ax = plt.gca()
ax.set_title("MS²PIP prediction for " + str(spectrum.peptidoform))
sup.spectrum(spectrum.to_spectrum_utils(), ax=ax)
plt.savefig(Path(filepath).with_suffix(".png"))
plt.close()
6 changes: 3 additions & 3 deletions ms2pip/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ def __repr__(self) -> str:
@model_validator(mode="after")
@classmethod
def check_array_lengths(cls, data: dict):
if len(data["mz"]) != len(data["intensity"]):
if len(data.mz) != len(data.intensity):
raise ValueError("Array lengths do not match.")
if data["annotations"] is not None:
if len(data["annotations"]) != len(data["intensity"]):
if data.annotations is not None:
if len(data.annotations) != len(data.intensity):
raise ValueError("Array lengths do not match.")
return data

Expand Down
Loading

0 comments on commit 59ad47d

Please sign in to comment.