molalchemy 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
molalchemy/__init__.py ADDED
File without changes
molalchemy/base.py ADDED
File without changes
@@ -0,0 +1,23 @@
1
+ from .index import (
2
+ BingoBinaryMolIndex,
3
+ BingoBinaryRxnIndex,
4
+ BingoMolIndex,
5
+ BingoRxnIndex,
6
+ )
7
+ from .proxy import BingoMolProxy, BingoRxnProxy
8
+ from .types import BingoBinaryMol, BingoBinaryReaction, BingoMol, BingoReaction
9
+
10
+ __all__ = [
11
+ "BingoBinaryMol",
12
+ "BingoBinaryMolIndex",
13
+ "BingoBinaryReaction",
14
+ "BingoBinaryRxnIndex",
15
+ "BingoMol",
16
+ "BingoMolIndex",
17
+ "BingoMolProxy",
18
+ "BingoReaction",
19
+ "BingoRxnIndex",
20
+ "BingoRxnProxy",
21
+ "bingo_func",
22
+ "bingo_rxn_func",
23
+ ]
@@ -0,0 +1,157 @@
1
+ """Bingo SQLAlchemy comparators for chemical structure searching."""
2
+
3
+ from sqlalchemy import text
4
+ from sqlalchemy.types import UserDefinedType
5
+
6
+
7
+ class BingoMolComparator(UserDefinedType.Comparator):
8
+ """
9
+ Comparator class for molecular structure operations using Bingo database.
10
+
11
+ This class provides methods for chemical structure searching including
12
+ substructure matching, SMARTS pattern matching, and exact structure matching.
13
+ """
14
+
15
+ def __eq__(self, other: str):
16
+ return self.equals(other)
17
+
18
+ def has_substructure(self, query: str, parameters: str = ""):
19
+ """
20
+ Check if the molecular structure contains a given substructure.
21
+
22
+ Parameters
23
+ ----------
24
+ query : str
25
+ The substructure query as a SMILES or MOL string.
26
+ parameters : str, optional
27
+ Additional parameters for the substructure search, by default "".
28
+
29
+ Returns
30
+ -------
31
+ sqlalchemy expression
32
+ A SQLAlchemy expression for the substructure match operation.
33
+
34
+ Examples
35
+ --------
36
+ >>> mol_column.has_substructure('c1ccccc1') # benzene ring
37
+ """
38
+ return self.expr.op("@")(text(f"('{query}', '{parameters}')::bingo.sub"))
39
+
40
+ def has_smarts(self, query: str, parameters: str = ""):
41
+ """
42
+ Check if the molecular structure matches a SMARTS pattern.
43
+
44
+ Parameters
45
+ ----------
46
+ query : str
47
+ The SMARTS pattern string for pattern matching.
48
+ parameters : str, optional
49
+ Additional parameters for the SMARTS search, by default "".
50
+
51
+ Returns
52
+ -------
53
+ sqlalchemy expression
54
+ A SQLAlchemy expression for the SMARTS pattern match operation.
55
+
56
+ Examples
57
+ --------
58
+ >>> mol_column.has_smarts('[#6]1:[#6]:[#6]:[#6]:[#6]:[#6]:1') # aromatic ring
59
+ """
60
+ return self.expr.op("@")(text(f"('{query}', '{parameters}')::bingo.smarts"))
61
+
62
+ def equals(self, query: str, parameters: str = ""):
63
+ """
64
+ Check if the molecular structure exactly matches the given structure.
65
+
66
+ Parameters
67
+ ----------
68
+ query : str
69
+ The molecular structure query as a SMILES or MOL string.
70
+ parameters : str, optional
71
+ Additional parameters for the exact match search, by default "".
72
+
73
+ Returns
74
+ -------
75
+ sqlalchemy expression
76
+ A SQLAlchemy expression for the exact structure match operation.
77
+
78
+ Examples
79
+ --------
80
+ >>> mol_column.equals('CCO') # ethanol exact match
81
+ """
82
+ return self.expr.op("@")(text(f"('{query}', '{parameters}')::bingo.exact"))
83
+
84
+
85
+ class BingoRxnComparator(UserDefinedType.Comparator):
86
+ """
87
+ Comparator class for chemical reaction operations using Bingo database.
88
+
89
+ This class provides methods for chemical reaction searching including
90
+ reaction substructure matching, SMARTS pattern matching, and exact reaction matching.
91
+ """
92
+
93
+ def has_substructure(self, query: str, parameters: str = ""):
94
+ """
95
+ Check if the reaction contains a given substructure pattern.
96
+
97
+ Parameters
98
+ ----------
99
+ query : str
100
+ The reaction substructure query as a reaction SMILES or RXN string.
101
+ parameters : str, optional
102
+ Additional parameters for the reaction substructure search, by default "".
103
+
104
+ Returns
105
+ -------
106
+ sqlalchemy expression
107
+ A SQLAlchemy expression for the reaction substructure match operation.
108
+
109
+ Examples
110
+ --------
111
+ >>> rxn_column.has_substructure('c1ccccc1>>c1ccc(O)cc1') # phenol formation
112
+ """
113
+ return self.expr.op("@")(text(f"('{query}', '{parameters}')::bingo.rsub"))
114
+
115
+ def has_smarts(self, query: str, parameters: str = ""):
116
+ """
117
+ Check if the reaction matches a SMARTS pattern.
118
+
119
+ Parameters
120
+ ----------
121
+ query : str
122
+ The reaction SMARTS pattern string for pattern matching.
123
+ parameters : str, optional
124
+ Additional parameters for the reaction SMARTS search, by default "".
125
+
126
+ Returns
127
+ -------
128
+ sqlalchemy expression
129
+ A SQLAlchemy expression for the reaction SMARTS pattern match operation.
130
+
131
+ Examples
132
+ --------
133
+ >>> rxn_column.has_smarts('[C:1]>>[C:1][O]') # C-O bond formation
134
+ """
135
+ return self.expr.op("@")(text(f"('{query}', '{parameters}')::bingo.rsmarts"))
136
+
137
+ def equals(self, query: str, parameters: str = ""):
138
+ """
139
+ Check if the reaction exactly matches the given reaction.
140
+
141
+ Parameters
142
+ ----------
143
+ query : str
144
+ The reaction query as a reaction SMILES or RXN string.
145
+ parameters : str, optional
146
+ Additional parameters for the exact reaction match search, by default "".
147
+
148
+ Returns
149
+ -------
150
+ sqlalchemy expression
151
+ A SQLAlchemy expression for the exact reaction match operation.
152
+
153
+ Examples
154
+ --------
155
+ >>> rxn_column.has_equals('CCO>>CC=O') # ethanol to acetaldehyde exact match
156
+ """
157
+ return self.expr.op("@")(text(f"('{query}', '{parameters}')::bingo.rexact"))
@@ -0,0 +1,3 @@
1
+ from . import mol, rxn
2
+
3
+ __all__ = ["mol", "rxn"]
@@ -0,0 +1,360 @@
1
+ """
2
+ Collection of Bingo PostgreSQL functions for molecular structure search and analysis.
3
+
4
+ This class provides static methods that wrap Bingo PostgreSQL functions for performing
5
+ various chemical structure operations including substructure search, exact matching,
6
+ similarity search, and format conversions.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from typing import TYPE_CHECKING, Literal
12
+
13
+ from sqlalchemy import text
14
+ from sqlalchemy.sql import ColumnElement, func
15
+ from sqlalchemy.sql.elements import BinaryExpression
16
+ from sqlalchemy.sql.functions import Function
17
+
18
+ if TYPE_CHECKING:
19
+ from molalchemy.bingo.types import BingoBinaryMol
20
+
21
+ _WEIGHT_TYPES = Literal["molecular-weight", "most-abundant-mass", "monoisotopic"]
22
+
23
+
24
+ def has_substructure(mol_column: ColumnElement, query: str, parameters: str = ""):
25
+ """
26
+ Perform substructure search on a molecule column.
27
+
28
+ Parameters
29
+ ----------
30
+ mol_column : ColumnElement
31
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
32
+ query : str
33
+ Query molecule as SMILES, SMARTS, or Molfile string.
34
+ parameters : str, optional
35
+ Search parameters for customizing the matching behavior (default is "").
36
+ Examples: "TAU" for tautomer search, "RES" for resonance search.
37
+
38
+ Returns
39
+ -------
40
+ BinaryExpression
41
+ SQLAlchemy expression for substructure matching that can be used in WHERE clauses.
42
+
43
+ """
44
+ return mol_column.op("@")(text(f"('{query}', '{parameters}')::bingo.sub"))
45
+
46
+
47
+ def matches_smarts(mol_column: ColumnElement, query: str, parameters: str = ""):
48
+ """
49
+ Perform SMARTS pattern matching on a molecule column.
50
+
51
+ Parameters
52
+ ----------
53
+ mol_column : ColumnElement
54
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
55
+ query : str
56
+ SMARTS pattern string for matching.
57
+ parameters : str, optional
58
+ Search parameters for customizing the matching behavior (default is "").
59
+
60
+ Returns
61
+ -------
62
+ BinaryExpression
63
+ SQLAlchemy expression for SMARTS matching that can be used in WHERE clauses.
64
+
65
+ """
66
+ return mol_column.op("@")(text(f"('{query}', '{parameters}')::bingo.smarts"))
67
+
68
+
69
+ def equals(mol_column: ColumnElement, query: str, parameters: str = ""):
70
+ """
71
+ Perform exact structure matching on a molecule column.
72
+
73
+ Parameters
74
+ ----------
75
+ mol_column : ColumnElement
76
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
77
+ query : str
78
+ Query molecule as SMILES or Molfile string for exact matching.
79
+ parameters : str, optional
80
+ Search parameters for customizing the matching behavior (default is "").
81
+ Examples: "TAU" for tautomer matching, "STE" for stereochemistry.
82
+
83
+ Returns
84
+ -------
85
+ BinaryExpression
86
+ SQLAlchemy expression for exact matching that can be used in WHERE clauses.
87
+
88
+ """
89
+ return mol_column.op("@")(text(f"('{query}', '{parameters}')::bingo.exact"))
90
+
91
+
92
+ def similarity(
93
+ mol_column: ColumnElement,
94
+ query: str,
95
+ bottom: float = 0.0,
96
+ top: float = 1.0,
97
+ metric: str = "Tanimoto",
98
+ ) -> BinaryExpression:
99
+ """
100
+ Perform similarity search on a molecule column. This should be used in WHERE clauses, as it
101
+ returns a boolean expression indicating whether the similarity criteria are met.
102
+
103
+ Parameters
104
+ ----------
105
+ mol_column : ColumnElement
106
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
107
+ query : str
108
+ Query molecule as SMILES or Molfile string for similarity comparison.
109
+ bottom : float, optional
110
+ Minimum similarity threshold (default is 0.0).
111
+ top : float, optional
112
+ Maximum similarity threshold (default is 1.0).
113
+ metric : str, optional
114
+ Similarity metric to use (default is "Tanimoto").
115
+ Other options include "Dice", "Cosine", etc.
116
+
117
+ Returns
118
+ -------
119
+ BinaryExpression
120
+ SQLAlchemy expression for similarity matching that can be used in WHERE clauses.
121
+
122
+ """
123
+ return mol_column.op("%")(
124
+ text(f"('{query}', {bottom}, {top}, '{metric}')::bingo.sim")
125
+ )
126
+
127
+
128
+ def get_similarity(
129
+ mol_column: ColumnElement,
130
+ query: str,
131
+ metric: str = "Tanimoto",
132
+ ) -> Function[float]:
133
+ """
134
+ Calculate the similarity score between a molecule column and a query molecule.
135
+ This function returns a float value representing the similarity score.
136
+
137
+ Parameters
138
+ ----------
139
+ mol_column : ColumnElement
140
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
141
+ query : str
142
+ Query molecule as SMILES or Molfile string for similarity comparison.
143
+ metric : str, optional
144
+ Similarity metric to use (default is "Tanimoto").
145
+ Other options include "Dice", "Cosine", etc.
146
+
147
+ Returns
148
+ -------
149
+ Function[float]
150
+ SQLAlchemy function expression returning the similarity score as a float.
151
+
152
+ """
153
+ return func.bingo.getsimilarity(mol_column, query, metric)
154
+
155
+
156
+ def has_gross_formula(mol_column: ColumnElement, formula: str):
157
+ """
158
+ Search for molecules with a specific gross formula.
159
+
160
+ Parameters
161
+ ----------
162
+ mol_column : ColumnElement
163
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
164
+ formula : str
165
+ Gross formula string (e.g., "C6H6" for benzene).
166
+
167
+ Returns
168
+ -------
169
+ BinaryExpression
170
+ SQLAlchemy expression for gross formula matching that can be used in WHERE clauses.
171
+
172
+ """
173
+ return mol_column.op("@")(text(f"('{formula}')::bingo.gross"))
174
+
175
+
176
+ def get_weight(
177
+ mol_column: ColumnElement, weight_type: _WEIGHT_TYPES = "molecular-weight"
178
+ ):
179
+ """
180
+ Calculate molecular weight of molecules.
181
+
182
+ Parameters
183
+ ----------
184
+ mol_column : ColumnElement
185
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
186
+ weight_type : {"molecular-weight", "most-abundant-mass", "monoisotopic"}, optional
187
+ Type of molecular weight to calculate (default is "molecular-weight").
188
+
189
+ Returns
190
+ -------
191
+ Function[float]
192
+ SQLAlchemy function expression returning the molecular weight.
193
+
194
+ """
195
+ return func.Bingo.getWeight(mol_column, weight_type)
196
+
197
+
198
+ def gross_formula(mol_column: ColumnElement) -> Function[str]:
199
+ """
200
+ Calculate the gross formula of molecules.
201
+
202
+ Parameters
203
+ ----------
204
+ mol_column : ColumnElement
205
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
206
+
207
+ Returns
208
+ -------
209
+ Function[str]
210
+ SQLAlchemy function expression returning the gross formula as a string.
211
+
212
+ """
213
+ return func.Bingo.Gross(mol_column)
214
+
215
+
216
+ def check_molecule(mol_column: ColumnElement) -> Function[str | None]:
217
+ """
218
+ Check if molecules are valid and return error messages for invalid ones.
219
+
220
+ Parameters
221
+ ----------
222
+ mol_column : ColumnElement
223
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
224
+
225
+ Returns
226
+ -------
227
+ Function[str | None]
228
+ SQLAlchemy function expression returning None for valid molecules,
229
+ or an error message string for invalid molecules.
230
+
231
+ """
232
+ return func.Bingo.CheckMolecule(mol_column)
233
+
234
+
235
+ def to_canonical(mol_column: ColumnElement) -> Function[str]:
236
+ """
237
+ Convert molecules to canonical SMILES format.
238
+
239
+ Parameters
240
+ ----------
241
+ mol_column : ColumnElement
242
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
243
+
244
+ Returns
245
+ -------
246
+ Function[str]
247
+ SQLAlchemy function expression returning canonical SMILES strings.
248
+
249
+ """
250
+ return func.Bingo.CanSMILES(mol_column)
251
+
252
+
253
+ def to_inchi(mol_column: ColumnElement) -> Function[str]:
254
+ """
255
+ Convert molecules to InChI format.
256
+
257
+ Parameters
258
+ ----------
259
+ mol_column : ColumnElement
260
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
261
+
262
+ Returns
263
+ -------
264
+ Function[str]
265
+ SQLAlchemy function expression returning InChI strings.
266
+ """
267
+ return func.bingo.InChI(mol_column)
268
+
269
+
270
+ def to_inchikey(mol_column: ColumnElement) -> Function[str]:
271
+ """
272
+ Convert molecules to InChIKey format.
273
+
274
+ Parameters
275
+ ----------
276
+ mol_column : ColumnElement
277
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
278
+
279
+ Returns
280
+ -------
281
+ Function[str]
282
+ SQLAlchemy function expression returning InChIKey strings.
283
+ """
284
+ return func.Bingo.InChIKey(mol_column)
285
+
286
+
287
+ def to_binary(
288
+ mol_column: ColumnElement, preserve_pos: bool = True
289
+ ) -> Function[BingoBinaryMol]:
290
+ """
291
+ Convert molecules to Bingo's internal binary format.
292
+
293
+ Parameters
294
+ ----------
295
+ mol_column : ColumnElement
296
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
297
+ preserve_pos : bool, optional
298
+ Whether to preserve atom positions in the binary format (default is True).
299
+ If False, only connectivity information is stored.
300
+
301
+ Returns
302
+ -------
303
+ Function[bytes]
304
+ SQLAlchemy function expression returning binary data.
305
+ """
306
+ return func.Bingo.CompactMolecule(mol_column, preserve_pos)
307
+
308
+
309
+ def to_smiles(mol_column: ColumnElement) -> Function[str]:
310
+ """
311
+ Convert molecules to SMILES format.
312
+
313
+ Parameters
314
+ ----------
315
+ mol_column : ColumnElement
316
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
317
+
318
+ Returns
319
+ -------
320
+ Function[str]
321
+ SQLAlchemy function expression returning SMILES strings.
322
+
323
+ """
324
+ return func.Bingo.SMILES(mol_column)
325
+
326
+
327
+ def to_molfile(mol_column: ColumnElement) -> Function[str]:
328
+ """
329
+ Convert molecules to Molfile format.
330
+
331
+ Parameters
332
+ ----------
333
+ mol_column : ColumnElement
334
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
335
+
336
+ Returns
337
+ -------
338
+ Function[str]
339
+ SQLAlchemy function expression returning Molfile strings with 2D coordinates.
340
+
341
+ """
342
+ return func.Bingo.Molfile(mol_column)
343
+
344
+
345
+ def to_cml(mol_column: ColumnElement) -> Function[str]:
346
+ """
347
+ Convert molecules to CML (Chemical Markup Language) format.
348
+
349
+ Parameters
350
+ ----------
351
+ mol_column : ColumnElement
352
+ SQLAlchemy column containing molecule data (SMILES, Molfile, or binary).
353
+
354
+ Returns
355
+ -------
356
+ Function[str]
357
+ SQLAlchemy function expression returning CML strings.
358
+
359
+ """
360
+ return func.Bingo.CML(mol_column)