1 //
2 //  Copyright (c) 2010-2015, Novartis Institutes for BioMedical Research Inc.
3 //  All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 //       notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 //       copyright notice, this list of conditions and the following
13 //       disclaimer in the documentation and/or other materials provided
14 //       with the distribution.
15 //     * Neither the name of Novartis Institutes for BioMedical Research Inc.
16 //       nor the names of its contributors may be used to endorse or promote
17 //       products derived from this software without specific prior written
18 //       permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 //
32 #include <RDGeneral/export.h>
33 #ifndef RDKIT_H_PSQL
34 #define RDKIT_H_PSQL
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #include <postgres.h>
41 
42 typedef bytea Mol;
43 
44 #define DatumGetMolP(x) ((Mol *)PG_DETOAST_DATUM(x))
45 #define DatumGetMolPCopy(x) ((Mol *)PG_DETOAST_DATUM_COPY(x))
46 #define MolPGetDatum(x) (PointerGetDatum(x))
47 
48 #define PG_GETARG_MOL_P(x) DatumGetMolP(PG_GETARG_DATUM(x))
49 #define PG_GETARG_MOL_P_COPY(x) DatumGetMolPCopy(PG_GETARG_DATUM(x))
50 #define PG_RETURN_MOL_P(x) PG_RETURN_DATUM(MolPGetDatum(x))
51 
52 typedef bytea Bfp;
53 
54 typedef struct {
55   char vl_len_[4];
56   uint16 weight;
57   uint8 fp[FLEXIBLE_ARRAY_MEMBER];
58 } BfpSignature;
59 
60 #define DatumGetBfpP(x) ((Bfp *)PG_DETOAST_DATUM(x))
61 #define DatumGetBfpPCopy(x) ((Bfp *)PG_DETOAST_DATUM_COPY(x))
62 #define BfpPGetDatum(x) (PointerGetDatum(x))
63 
64 #define PG_GETARG_BFP_P(x) DatumGetBfpP(PG_GETARG_DATUM(x))
65 #define PG_GETARG_BFP_P_COPY(x) DatumGetBfpPCopy(PG_GETARG_DATUM(x))
66 #define PG_RETURN_BFP_P(x) PG_RETURN_DATUM(BfpPGetDatum(x))
67 
68 #define BFP_SIGLEN(x) (VARSIZE(x) - VARHDRSZ)
69 
70 typedef bytea Sfp;
71 
72 #define DatumGetSfpP(x) ((Sfp *)PG_DETOAST_DATUM(x))
73 #define DatumGetSfpPCopy(x) ((Sfp *)PG_DETOAST_DATUM_COPY(x))
74 #define SfpPGetDatum(x) (PointerGetDatum(x))
75 
76 #define PG_GETARG_SFP_P(x) DatumGetSfpP(PG_GETARG_DATUM(x))
77 #define PG_GETARG_SFP_P_COPY(x) DatumGetSfpPCopy(PG_GETARG_DATUM(x))
78 #define PG_RETURN_SFP_P(x) PG_RETURN_DATUM(SfpPGetDatum(x))
79 
80 typedef bytea Reaction;
81 
82 #define DatumGetReactionP(x) ((Reaction *)PG_DETOAST_DATUM(x))
83 #define DatumGetReactionPCopy(x) ((Reaction *)PG_DETOAST_DATUM_COPY(x))
84 #define ReactionPGetDatum(x) (PointerGetDatum(x))
85 
86 #define PG_GETARG_REACTION_P(x) DatumGetReactionP(PG_GETARG_DATUM(x))
87 #define PG_GETARG_REACTION_P_COPY(x) DatumGetReactionPCopy(PG_GETARG_DATUM(x))
88 #define PG_RETURN_REACTION_P(x) PG_RETURN_DATUM(ReactionPGetDatum(x))
89 
90 /*
91  * From/to C/C++
92  */
93 
94 /* RDKit::ROMol */
95 typedef void *CROMol;
96 void freeCROMol(CROMol data);
97 
98 CROMol constructROMol(Mol *data);
99 Mol *deconstructROMol(CROMol data);
100 
101 CROMol parseMolBlob(char *data, int len);
102 char *makeMolBlob(CROMol data, int *len);
103 CROMol parseMolText(char *data, bool asSmarts, bool warnOnFail, bool asQuery);
104 CROMol parseMolCTAB(char *data, bool keepConformer, bool warnOnFail,
105                     bool asQuery);
106 char *makeMolText(CROMol data, int *len, bool asSmarts, bool cxSmiles);
107 char *makeCtabText(CROMol data, int *len, bool createDepictionIfMissing);
108 bool isValidSmiles(char *data);
109 bool isValidSmarts(char *data);
110 bool isValidCTAB(char *data);
111 bool isValidMolBlob(char *data, int len);
112 
113 int molcmp(CROMol i, CROMol a);
114 
115 int MolSubstruct(CROMol i, CROMol a);
116 int MolSubstructCount(CROMol i, CROMol a, bool uniquify);
117 
118 bytea *makeMolSignature(CROMol data);
119 
120 double MolAMW(CROMol i);
121 double MolLogP(CROMol i);
122 int MolHBA(CROMol i);
123 int MolHBD(CROMol i);
124 int MolNumAtoms(CROMol i);
125 int MolNumHeavyAtoms(CROMol i);
126 int MolNumRotatableBonds(CROMol i);
127 int MolNumHeteroatoms(CROMol i);
128 int MolNumRings(CROMol i);
129 int MolNumAromaticRings(CROMol i);
130 int MolNumAliphaticRings(CROMol i);
131 int MolNumSaturatedRings(CROMol i);
132 int MolNumAromaticHeterocycles(CROMol i);
133 int MolNumAliphaticHeterocycles(CROMol i);
134 int MolNumSaturatedHeterocycles(CROMol i);
135 int MolNumAromaticCarbocycles(CROMol i);
136 int MolNumAliphaticCarbocycles(CROMol i);
137 int MolNumSaturatedCarbocycles(CROMol i);
138 int MolNumHeterocycles(CROMol i);
139 
140 double MolFractionCSP3(CROMol i);
141 double MolTPSA(CROMol i);
142 double MolChi0v(CROMol i);
143 double MolChi1v(CROMol i);
144 double MolChi2v(CROMol i);
145 double MolChi3v(CROMol i);
146 double MolChi4v(CROMol i);
147 double MolChi0n(CROMol i);
148 double MolChi1n(CROMol i);
149 double MolChi2n(CROMol i);
150 double MolChi3n(CROMol i);
151 double MolChi4n(CROMol i);
152 double MolKappa1(CROMol i);
153 double MolKappa2(CROMol i);
154 double MolKappa3(CROMol i);
155 
156 int MolNumSpiroAtoms(CROMol i);
157 int MolNumBridgeheadAtoms(CROMol i);
158 
159 char *makeMolFormulaText(CROMol data, int *len, bool separateIsotopes,
160                          bool abbreviateHIsotopes);
161 
162 const char *MolInchi(CROMol i, const char *opts);
163 const char *MolInchiKey(CROMol i, const char *opts);
164 CROMol MolMurckoScaffold(CROMol i);
165 
166 CROMol MolAdjustQueryProperties(CROMol m, const char *params);
167 char *MolGetSVG(CROMol i, unsigned int w, unsigned int h, const char *legend,
168                 const char *params);
169 
170 /* ExplicitBitVect */
171 typedef void *CBfp;
172 void freeCBfp(CBfp data);
173 
174 CBfp constructCBfp(Bfp *data);
175 Bfp *deconstructCBfp(CBfp data);
176 BfpSignature *makeBfpSignature(CBfp data);
177 
178 int CBfpSize(CBfp a);
179 
180 double calcBitmapTanimotoSml(CBfp a, CBfp b);
181 double calcBitmapDiceSml(CBfp a, CBfp b);
182 double calcBitmapTverskySml(CBfp a, CBfp b, float ca, float cb);
183 
184 /* SparseIntVect<std::int32_t> */
185 typedef void *CSfp;
186 void freeCSfp(CSfp data);
187 
188 CSfp constructCSfp(Sfp *data);
189 Sfp *deconstructCSfp(CSfp data);
190 bytea *makeSfpSignature(CSfp data, int numBits);
191 bytea *makeLowSparseFingerPrint(CSfp data, int numInts);
192 
193 double calcSparseTanimotoSml(CSfp a, CSfp b);
194 double calcSparseDiceSml(CSfp a, CSfp b);
195 double calcSparseStringDiceSml(const char *a, unsigned int sza, const char *b,
196                                unsigned int szb);
197 bool calcSparseStringAllValsGT(const char *a, unsigned int sza, int tgt);
198 bool calcSparseStringAllValsLT(const char *a, unsigned int sza, int tgt);
199 CSfp addSFP(CSfp a, CSfp b);
200 CSfp subtractSFP(CSfp a, CSfp b);
201 
202 void countOverlapValues(bytea *sign, CSfp data, int numBits, int *sum,
203                         int *overlapSum, int *overlapN);
204 void countLowOverlapValues(bytea *sign, CSfp data, int numInts, int *querySum,
205                            int *keySum, int *overlapUp, int *overlapDown);
206 /*
207  * Various mol -> fp transformation
208  */
209 
210 CBfp makeLayeredBFP(CROMol data);
211 CBfp makeRDKitBFP(CROMol data);
212 CBfp makeMorganBFP(CROMol data, int radius);
213 CSfp makeMorganSFP(CROMol data, int radius);
214 CBfp makeFeatMorganBFP(CROMol data, int radius);
215 CSfp makeFeatMorganSFP(CROMol data, int radius);
216 CSfp makeAtomPairSFP(CROMol data);
217 CSfp makeTopologicalTorsionSFP(CROMol data);
218 CBfp makeAtomPairBFP(CROMol data);
219 CBfp makeTopologicalTorsionBFP(CROMol data);
220 CBfp makeMACCSBFP(CROMol data);
221 CBfp makeAvalonBFP(CROMol data, bool isQuery, unsigned int bitFlags);
222 
223 /*
224  * Indexes
225  */
226 
227 #define NUMBITS (2048)
228 #define NUMRANGE (120)
229 
230 #define INTRANGEMAX (0xff)
231 typedef struct IntRange {
232   uint8 low;
233   uint8 high;
234 } IntRange;
235 
236 #define RDKitTanimotoStrategy (1)
237 #define RDKitDiceStrategy (2)
238 #define RDKitOrderByTanimotoStrategy (3)
239 #define RDKitOrderByDiceStrategy (4)
240 #define RDKitContains (3)
241 #define RDKitContained (4)
242 #define RDKitEquals (6)
243 #define RDKitSmaller (7)
244 #define RDKitGreater (8)
245 
246 bool calcConsistency(bool isLeaf, uint16 strategy, double nCommonUp,
247                      double nCommonDown, double nKey, double nQuery);
248 
249 /* Chemical Reactions
250  * RDKit::ChemicalReaction */
251 typedef void *CChemicalReaction;
252 
253 void freeChemReaction(CChemicalReaction data);
254 
255 CChemicalReaction constructChemReact(Reaction *data);
256 Reaction *deconstructChemReact(CChemicalReaction data);
257 
258 CChemicalReaction parseChemReactBlob(char *data, int len);
259 CChemicalReaction parseChemReactText(char *data, bool asSmarts,
260                                      bool warnOnFail);
261 CChemicalReaction parseChemReactCTAB(char *data, bool warnOnFail);
262 char *makeChemReactBlob(CChemicalReaction data, int *len);
263 char *makeChemReactText(CChemicalReaction data, int *len, bool asSmarts);
264 char *makeCTABChemReact(CChemicalReaction data, int *len);
265 char *ReactionGetSVG(CChemicalReaction i, unsigned int w, unsigned int h,
266                      bool highlightByReactant, const char *params);
267 
268 int ChemReactNumReactants(CChemicalReaction rxn);
269 int ChemReactNumProducts(CChemicalReaction rxn);
270 int ChemReactNumAgents(CChemicalReaction rxn);
271 
272 /* Reaction substructure search */
273 bytea *makeReactionSign(CChemicalReaction data);
274 int ReactionSubstruct(CChemicalReaction rxn, CChemicalReaction rxn2);
275 int reactioncmp(CChemicalReaction rxn, CChemicalReaction rxn2);
276 CBfp makeReactionBFP(CChemicalReaction data, int size, int fpType);
277 
278 /* Reaction difference fingerprint */
279 CSfp makeReactionDifferenceSFP(CChemicalReaction data, int size, int fpType);
280 
281 char *computeMolHash(CROMol data, int *len);
282 char *computeNMMolHash(CROMol data, const char *which);
283 
284 char *findMCSsmiles(char *smiles, char *params);
285 void *addMol2list(void *lst, Mol *mol);
286 char *findMCS(void *lst, char *params);
287 
288 #ifdef __cplusplus
289 }
290 #endif
291 #endif
292