-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
145 lines (105 loc) · 3.89 KB
/
models.py
File metadata and controls
145 lines (105 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from pydantic import ConfigDict
from rdkit import DataStructs
from rdkit.Chem import AllChem as Chem
from rdkit.DataStructs import ExplicitBitVect
from sqlalchemy import Index
from sqlalchemy import types as sqltypes
from sqlalchemy.dialects import postgresql
from sqlalchemy.sql import operators
from sqlalchemy.sql.functions import GenericFunction
from sqlalchemy.types import UserDefinedType
from sqlmodel import Field, SQLModel
class Mol(UserDefinedType):
cache_ok = True
class comparator_factory(UserDefinedType.Comparator):
def hassubstruct(self, other):
return self.operate(
operators.custom_op("@>"), other, result_type=sqltypes.Boolean
)
def issubstruct(self, other):
return self.operate(
operators.custom_op("<@"), other, result_type=sqltypes.Boolean
)
def __eq__(self, other):
return self.operate(
operators.custom_op("@="), other, result_type=sqltypes.Boolean
)
def get_col_spec(self, **kw):
return "mol"
def bind_processor(self, dialect):
def process(value):
if isinstance(value, Chem.Mol):
value = memoryview(value.ToBinary())
elif isinstance(value, str):
value = memoryview(Chem.MolFromSmiles(value).ToBinary())
return value
return process
def bind_expression(self, bindvalue):
return mol_from_pkl(bindvalue)
def column_expression(self, colexpr):
return mol_to_pkl(colexpr, type_=self)
def result_processor(self, dialect, coltype):
def process(value):
if value is None:
return value
return Chem.Mol(bytes(value))
return process
class mol_from_pkl(GenericFunction):
name = "mol_from_pkl"
type = Mol()
class mol_to_pkl(GenericFunction):
name = "mol_to_pkl"
type = postgresql.BYTEA()
class QMol(UserDefinedType):
cache_ok = True
def get_col_spec(self, **kw):
return "qmol"
class Bfp(UserDefinedType):
cache_ok = True
class comparator_factory(UserDefinedType.Comparator):
def tanimoto_sml(self, other):
return self.operate(
operators.custom_op("%"), other, result_type=sqltypes.Boolean
)
def get_col_spec(self, **kw):
return "bfp"
def bind_processor(self, dialect):
def process(value):
if isinstance(value, ExplicitBitVect):
value = memoryview(DataStructs.BitVectToBinaryText(value))
return value
return process
def bind_expression(self, bindvalue):
return bfp_from_binary_text(bindvalue)
def column_expression(self, colexpr):
return bfp_to_binary_text(colexpr, type_=self)
def result_processor(self, dialect, coltype):
def process(value):
if value is None:
return value
elif isinstance(value, bytes | bytearray | memoryview):
return DataStructs.CreateFromBinaryText(bytes(value))
else:
raise RuntimeError("Unexpected row value type for a Bfp instance")
return process
class bfp_from_binary_text(GenericFunction):
name = "bfp_from_binary_text"
type = Bfp()
class bfp_to_binary_text(GenericFunction):
name = "bfp_to_binary_text"
type = postgresql.BYTEA()
class morganbv_fp(GenericFunction):
inherit_cache = True
name = "morganbv_fp"
type = Bfp()
class Compound(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
molecule: Mol = Field(sa_type=Mol)
mfp2: Bfp = Field(sa_type=Bfp, nullable=True)
__table_args__ = (
Index("compound_molecule", "molecule", postgresql_using="gist"),
)
model_config = ConfigDict(arbitrary_types_allowed=True)
def __repr__(self):
return f"({self.name}) < {self.molecule} >"