Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
74 changes: 40 additions & 34 deletions ms2pip/_utils/dlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@
TypeDecorator,
)
from sqlalchemy.dialects.sqlite import BLOB
from sqlalchemy.engine import Connection

DLIB_VERSION = "0.1.14"


class CompressedArray(TypeDecorator):
""" Sqlite-like does not support arrays.
Let's use a custom type decorator.
"""Sqlite-like does not support arrays.
Let's use a custom type decorator.

See http://docs.sqlalchemy.org/en/latest/core/types.html#sqlalchemy.types.TypeDecorator
See http://docs.sqlalchemy.org/en/latest/core/types.html#sqlalchemy.types.TypeDecorator
"""

impl = BLOB

def __init__(self, dtype, *args, **kwargs):
Expand All @@ -49,51 +51,55 @@ def copy(self):

metadata = MetaData()

big_float = numpy.dtype('>f4')
big_double = numpy.dtype('>f8')
big_float = numpy.dtype(">f4")
big_double = numpy.dtype(">f8")

Entry = Table(
'entries',
"entries",
metadata,
Column('PrecursorMz', Float, nullable=False, index=True),
Column('PrecursorCharge', Integer, nullable=False),
Column('PeptideModSeq', String, nullable=False),
Column('PeptideSeq', String, nullable=False, index=True),
Column('Copies', Integer, nullable=False),
Column('RTInSeconds', Float, nullable=False),
Column('Score', Float, nullable=False),
Column('MassEncodedLength', Integer, nullable=False),
Column('MassArray', CompressedArray(big_double), nullable=False),
Column('IntensityEncodedLength', Integer, nullable=False),
Column('IntensityArray', CompressedArray(big_float), nullable=False),
Column('CorrelationEncodedLength', Integer, nullable=True),
Column('CorrelationArray', CompressedArray(big_float), nullable=True),
Column('RTInSecondsStart', Float, nullable=True),
Column('RTInSecondsStop', Float, nullable=True),
Column('MedianChromatogramEncodedLength', Integer, nullable=True),
Column('MedianChromatogramArray', CompressedArray(big_float), nullable=True),
Column('SourceFile', String, nullable=False),
Column("PrecursorMz", Float, nullable=False, index=True),
Column("PrecursorCharge", Integer, nullable=False),
Column("PeptideModSeq", String, nullable=False),
Column("PeptideSeq", String, nullable=False, index=True),
Column("Copies", Integer, nullable=False),
Column("RTInSeconds", Float, nullable=False),
Column("Score", Float, nullable=False),
Column("MassEncodedLength", Integer, nullable=False),
Column("MassArray", CompressedArray(big_double), nullable=False),
Column("IntensityEncodedLength", Integer, nullable=False),
Column("IntensityArray", CompressedArray(big_float), nullable=False),
Column("CorrelationEncodedLength", Integer, nullable=True),
Column("CorrelationArray", CompressedArray(big_float), nullable=True),
Column("RTInSecondsStart", Float, nullable=True),
Column("RTInSecondsStop", Float, nullable=True),
Column("MedianChromatogramEncodedLength", Integer, nullable=True),
Column("MedianChromatogramArray", CompressedArray(big_float), nullable=True),
Column("SourceFile", String, nullable=False),
)

Index('ix_entries_PeptideModSeq_PrecursorCharge_SourceFile', Entry.c.PeptideModSeq, Entry.c.PrecursorCharge, Entry.c.SourceFile)
Index(
"ix_entries_PeptideModSeq_PrecursorCharge_SourceFile",
Entry.c.PeptideModSeq,
Entry.c.PrecursorCharge,
Entry.c.SourceFile,
)

PeptideToProtein = Table(
'peptidetoprotein',
"peptidetoprotein",
metadata,
Column('PeptideSeq', String, nullable=False, index=True),
Column('isDecoy', Boolean, nullable=True),
Column('ProteinAccession', String, nullable=False, index=True),
Column("PeptideSeq", String, nullable=False, index=True),
Column("isDecoy", Boolean, nullable=True),
Column("ProteinAccession", String, nullable=False, index=True),
)

Metadata = Table(
'metadata',
"metadata",
metadata,
Column('Key', String, nullable=False, index=True),
Column('Value', String, nullable=False),
Column("Key", String, nullable=False, index=True),
Column("Value", String, nullable=False),
)


def open_sqlite(filename: Union[str, Path]) -> sqlalchemy.engine.Connection:
def open_sqlite(filename: Union[str, Path]) -> Connection:
engine = sqlalchemy.create_engine(f"sqlite:///{filename}")
metadata.bind = engine
return engine.connect()
26 changes: 14 additions & 12 deletions ms2pip/spectrum_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@
from collections import defaultdict
from io import StringIO
from pathlib import Path
from os import PathLike
from time import localtime, strftime
from typing import Any, Dict, Generator, List, Optional, Union

import numpy as np
from psm_utils import PSM, Peptidoform
from pyteomics import proforma
from sqlalchemy import engine, select
from sqlalchemy import select
from sqlalchemy.engine import Connection

from ms2pip._utils import dlib
from ms2pip.result import ProcessingResult
Expand All @@ -62,7 +64,7 @@


def write_spectra(
filename: Union[str, Path],
filename: Union[str, PathLike],
processing_results: List[ProcessingResult],
file_format: str = "tsv",
write_mode: str = "w",
Expand Down Expand Up @@ -92,7 +94,7 @@ class _Writer(ABC):

suffix = ""

def __init__(self, filename: Union[str, Path], write_mode: str = "w"):
def __init__(self, filename: Union[str, PathLike], write_mode: str = "w"):
self.filename = Path(filename).with_suffix(self.suffix)
self.write_mode = write_mode

Expand Down Expand Up @@ -466,7 +468,7 @@ class Bibliospec(_Writer):
"ion-mobility",
]

def __init__(self, filename: Union[str, Path], write_mode: str = "w"):
def __init__(self, filename: Union[str, PathLike], write_mode: str = "w"):
super().__init__(filename, write_mode)
self.ssl_file = self.filename.with_suffix(self.ssl_suffix)
self.ms2_file = self.filename.with_suffix(self.ms2_suffix)
Expand Down Expand Up @@ -618,7 +620,7 @@ def _format_modified_sequence(peptidoform: Peptidoform) -> str:
)

@staticmethod
def _get_last_ssl_scan_number(ssl_file: Union[str, Path, StringIO]):
def _get_last_ssl_scan_number(ssl_file: Union[str, PathLike, StringIO]):
"""Read scan number of last line in a Bibliospec SSL file."""
if isinstance(ssl_file, StringIO):
ssl_file.seek(0)
Expand Down Expand Up @@ -653,7 +655,7 @@ def open(self):
def write(self, processing_results: List[ProcessingResult]):
"""Write MS2PIP predictions to a DLIB SQLite file."""
connection = self._file_object
dlib.metadata.create_all()
dlib.metadata.create_all(connection.engine)
self._write_metadata(connection)
self._write_entries(processing_results, connection, self.filename)
self._write_peptide_to_protein(processing_results, connection)
Expand Down Expand Up @@ -682,11 +684,11 @@ def _format_modified_sequence(peptidoform: Peptidoform) -> str:
)

@staticmethod
def _write_metadata(connection: engine.Connection):
def _write_metadata(connection: Connection):
"""Write metadata to DLIB SQLite file."""
with connection.begin():
version = connection.execute(
select([dlib.Metadata.c.Value]).where(dlib.Metadata.c.Key == "version")
select(dlib.Metadata.c.Value).where(dlib.Metadata.c.Key == "version")
).scalar()
if version is None:
connection.execute(
Expand All @@ -699,8 +701,8 @@ def _write_metadata(connection: engine.Connection):
@staticmethod
def _write_entries(
processing_results: List[ProcessingResult],
connection: engine.Connection,
output_filename: str,
connection: Connection,
output_filename: Union[str, PathLike],
):
"""Write spectra to DLIB SQLite file."""
with connection.begin():
Expand Down Expand Up @@ -730,7 +732,7 @@ def _write_entries(
)

@staticmethod
def _write_peptide_to_protein(results: List[ProcessingResult], connection: engine.Connection):
def _write_peptide_to_protein(results: List[ProcessingResult], connection: Connection):
"""Write peptide-to-protein mappings to DLIB SQLite file."""
peptide_to_proteins = {
(result.psm.peptidoform.sequence, protein)
Expand All @@ -743,7 +745,7 @@ def _write_peptide_to_protein(results: List[ProcessingResult], connection: engin
sql_peptide_to_proteins = set()
proteins = {protein for _, protein in peptide_to_proteins}
for peptide_to_protein in connection.execute(
dlib.PeptideToProtein.select().where(
select(dlib.PeptideToProtein).where(
dlib.PeptideToProtein.c.ProteinAccession.in_(proteins)
)
):
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ dependencies = [
"pandas>=1,<3",
"pyarrow",
"pyteomics>=3.5,<5",
"tomlkit>=0.5,<1",
"sqlalchemy>=1.3,<2",
"sqlalchemy>=1.4,<3",
"click>=7,<9",
"xgboost>=1.3,<3",
"lxml>=4",
Expand Down
Loading