Skip to content

Commit a9181fe

Browse files
committed
Fix support for sqlalchemy v2, keeping backwards compatibility with v1.4
1 parent bac0012 commit a9181fe

File tree

4 files changed

+281
-46
lines changed

4 files changed

+281
-46
lines changed

ms2pip/_utils/dlib.py

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@
1818
TypeDecorator,
1919
)
2020
from sqlalchemy.dialects.sqlite import BLOB
21+
from sqlalchemy.engine import Connection
2122

2223
DLIB_VERSION = "0.1.14"
2324

2425

2526
class CompressedArray(TypeDecorator):
26-
""" Sqlite-like does not support arrays.
27-
Let's use a custom type decorator.
27+
"""Sqlite-like does not support arrays.
28+
Let's use a custom type decorator.
2829
29-
See http://docs.sqlalchemy.org/en/latest/core/types.html#sqlalchemy.types.TypeDecorator
30+
See http://docs.sqlalchemy.org/en/latest/core/types.html#sqlalchemy.types.TypeDecorator
3031
"""
32+
3133
impl = BLOB
3234

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

5052
metadata = MetaData()
5153

52-
big_float = numpy.dtype('>f4')
53-
big_double = numpy.dtype('>f8')
54+
big_float = numpy.dtype(">f4")
55+
big_double = numpy.dtype(">f8")
5456

5557
Entry = Table(
56-
'entries',
58+
"entries",
5759
metadata,
58-
Column('PrecursorMz', Float, nullable=False, index=True),
59-
Column('PrecursorCharge', Integer, nullable=False),
60-
Column('PeptideModSeq', String, nullable=False),
61-
Column('PeptideSeq', String, nullable=False, index=True),
62-
Column('Copies', Integer, nullable=False),
63-
Column('RTInSeconds', Float, nullable=False),
64-
Column('Score', Float, nullable=False),
65-
Column('MassEncodedLength', Integer, nullable=False),
66-
Column('MassArray', CompressedArray(big_double), nullable=False),
67-
Column('IntensityEncodedLength', Integer, nullable=False),
68-
Column('IntensityArray', CompressedArray(big_float), nullable=False),
69-
Column('CorrelationEncodedLength', Integer, nullable=True),
70-
Column('CorrelationArray', CompressedArray(big_float), nullable=True),
71-
Column('RTInSecondsStart', Float, nullable=True),
72-
Column('RTInSecondsStop', Float, nullable=True),
73-
Column('MedianChromatogramEncodedLength', Integer, nullable=True),
74-
Column('MedianChromatogramArray', CompressedArray(big_float), nullable=True),
75-
Column('SourceFile', String, nullable=False),
60+
Column("PrecursorMz", Float, nullable=False, index=True),
61+
Column("PrecursorCharge", Integer, nullable=False),
62+
Column("PeptideModSeq", String, nullable=False),
63+
Column("PeptideSeq", String, nullable=False, index=True),
64+
Column("Copies", Integer, nullable=False),
65+
Column("RTInSeconds", Float, nullable=False),
66+
Column("Score", Float, nullable=False),
67+
Column("MassEncodedLength", Integer, nullable=False),
68+
Column("MassArray", CompressedArray(big_double), nullable=False),
69+
Column("IntensityEncodedLength", Integer, nullable=False),
70+
Column("IntensityArray", CompressedArray(big_float), nullable=False),
71+
Column("CorrelationEncodedLength", Integer, nullable=True),
72+
Column("CorrelationArray", CompressedArray(big_float), nullable=True),
73+
Column("RTInSecondsStart", Float, nullable=True),
74+
Column("RTInSecondsStop", Float, nullable=True),
75+
Column("MedianChromatogramEncodedLength", Integer, nullable=True),
76+
Column("MedianChromatogramArray", CompressedArray(big_float), nullable=True),
77+
Column("SourceFile", String, nullable=False),
7678
)
7779

78-
Index('ix_entries_PeptideModSeq_PrecursorCharge_SourceFile', Entry.c.PeptideModSeq, Entry.c.PrecursorCharge, Entry.c.SourceFile)
80+
Index(
81+
"ix_entries_PeptideModSeq_PrecursorCharge_SourceFile",
82+
Entry.c.PeptideModSeq,
83+
Entry.c.PrecursorCharge,
84+
Entry.c.SourceFile,
85+
)
7986

8087
PeptideToProtein = Table(
81-
'peptidetoprotein',
88+
"peptidetoprotein",
8289
metadata,
83-
Column('PeptideSeq', String, nullable=False, index=True),
84-
Column('isDecoy', Boolean, nullable=True),
85-
Column('ProteinAccession', String, nullable=False, index=True),
90+
Column("PeptideSeq", String, nullable=False, index=True),
91+
Column("isDecoy", Boolean, nullable=True),
92+
Column("ProteinAccession", String, nullable=False, index=True),
8693
)
8794

8895
Metadata = Table(
89-
'metadata',
96+
"metadata",
9097
metadata,
91-
Column('Key', String, nullable=False, index=True),
92-
Column('Value', String, nullable=False),
98+
Column("Key", String, nullable=False, index=True),
99+
Column("Value", String, nullable=False),
93100
)
94101

95102

96-
def open_sqlite(filename: Union[str, Path]) -> sqlalchemy.engine.Connection:
103+
def open_sqlite(filename: Union[str, Path]) -> Connection:
97104
engine = sqlalchemy.create_engine(f"sqlite:///{filename}")
98-
metadata.bind = engine
99105
return engine.connect()

ms2pip/spectrum_output.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
import numpy as np
5454
from psm_utils import PSM, Peptidoform
5555
from pyteomics import proforma
56-
from sqlalchemy import engine, select
56+
from sqlalchemy import select
57+
from sqlalchemy.engine import Connection
5758

5859
from ms2pip._utils import dlib
5960
from ms2pip.result import ProcessingResult
@@ -653,9 +654,9 @@ def open(self):
653654
def write(self, processing_results: List[ProcessingResult]):
654655
"""Write MS2PIP predictions to a DLIB SQLite file."""
655656
connection = self._file_object
656-
dlib.metadata.create_all()
657+
dlib.metadata.create_all(connection.engine)
657658
self._write_metadata(connection)
658-
self._write_entries(processing_results, connection, self.filename)
659+
self._write_entries(processing_results, connection, str(self.filename))
659660
self._write_peptide_to_protein(processing_results, connection)
660661

661662
def _write_result(self, result: ProcessingResult): ...
@@ -682,11 +683,11 @@ def _format_modified_sequence(peptidoform: Peptidoform) -> str:
682683
)
683684

684685
@staticmethod
685-
def _write_metadata(connection: engine.Connection):
686+
def _write_metadata(connection: Connection):
686687
"""Write metadata to DLIB SQLite file."""
687688
with connection.begin():
688689
version = connection.execute(
689-
select([dlib.Metadata.c.Value]).where(dlib.Metadata.c.Key == "version")
690+
select(dlib.Metadata.c.Value).where(dlib.Metadata.c.Key == "version")
690691
).scalar()
691692
if version is None:
692693
connection.execute(
@@ -699,7 +700,7 @@ def _write_metadata(connection: engine.Connection):
699700
@staticmethod
700701
def _write_entries(
701702
processing_results: List[ProcessingResult],
702-
connection: engine.Connection,
703+
connection: Connection,
703704
output_filename: str,
704705
):
705706
"""Write spectra to DLIB SQLite file."""
@@ -730,7 +731,7 @@ def _write_entries(
730731
)
731732

732733
@staticmethod
733-
def _write_peptide_to_protein(results: List[ProcessingResult], connection: engine.Connection):
734+
def _write_peptide_to_protein(results: List[ProcessingResult], connection: Connection):
734735
"""Write peptide-to-protein mappings to DLIB SQLite file."""
735736
peptide_to_proteins = {
736737
(result.psm.peptidoform.sequence, protein)
@@ -743,7 +744,7 @@ def _write_peptide_to_protein(results: List[ProcessingResult], connection: engin
743744
sql_peptide_to_proteins = set()
744745
proteins = {protein for _, protein in peptide_to_proteins}
745746
for peptide_to_protein in connection.execute(
746-
dlib.PeptideToProtein.select().where(
747+
select(dlib.PeptideToProtein).where(
747748
dlib.PeptideToProtein.c.ProteinAccession.in_(proteins)
748749
)
749750
):

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ dependencies = [
3737
"pandas>=1,<3",
3838
"pyarrow",
3939
"pyteomics>=3.5,<5",
40-
"tomlkit>=0.5,<1",
41-
"sqlalchemy>=1.3,<2",
40+
"sqlalchemy>=1.4,<3",
4241
"click>=7,<9",
4342
"xgboost>=1.3,<3",
4443
"lxml>=4",

0 commit comments

Comments
 (0)