File: MolOps.cpp

package info (click to toggle)
rdkit 201203-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 37,840 kB
  • sloc: cpp: 93,902; python: 51,897; java: 5,192; ansic: 3,497; xml: 2,499; sql: 1,641; yacc: 1,518; lex: 1,076; makefile: 325; fortran: 183; sh: 153; cs: 51
file content (591 lines) | stat: -rw-r--r-- 15,780 bytes parent folder | download
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//  $Id: MolOps.cpp 1528 2010-09-26 17:04:37Z glandrum $
//
//  Copyright (C) 2005-2006 Rational Discovery LLC
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#include "RDLib.h"
#include <string>

#include <GraphMol/RDKitBase.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <GraphMol/Substruct/SubstructMatch.h>
#include <GraphMol/Fingerprints/Fingerprints.h>
#include <GraphMol/Descriptors/MolDescriptors.h>
#include <DataStructs/BitVects.h>
#include <DataStructs/BitOps.h>

extern "C" {
  PG_FUNCTION_INFO_V1(rd_canonsmiles);
   Datum rd_canonsmiles(PG_FUNCTION_ARGS);
  PG_FUNCTION_INFO_V1(rd_molpickle);
   Datum rd_molpickle(PG_FUNCTION_ARGS);

  PG_FUNCTION_INFO_V1(rd_hassubstruct_smi);
   Datum rd_hassubstruct_smi(PG_FUNCTION_ARGS);
  PG_FUNCTION_INFO_V1(rd_hassubstruct_pkl);
   Datum rd_hassubstruct_pkl(PG_FUNCTION_ARGS);
  PG_FUNCTION_INFO_V1(rd_substructcount_smi);
   Datum rd_substructcount_smi(PG_FUNCTION_ARGS);
  PG_FUNCTION_INFO_V1(rd_substructcount_pkl);
   Datum rd_substructcount_pkl(PG_FUNCTION_ARGS);

  PG_FUNCTION_INFO_V1(rd_substructfp);
   Datum rd_substructfp(PG_FUNCTION_ARGS);
  PG_FUNCTION_INFO_V1(rd_substructfp_bits);
   Datum rd_substructfp_bits(PG_FUNCTION_ARGS);

  PG_FUNCTION_INFO_V1(rd_similarityfp);
   Datum rd_similarityfp(PG_FUNCTION_ARGS);
  PG_FUNCTION_INFO_V1(rd_similarityfp_bits);
   Datum rd_similarityfp_bits(PG_FUNCTION_ARGS);

  PG_FUNCTION_INFO_V1(rd_mollogp_smi);
   Datum rd_mollogp_smi(PG_FUNCTION_ARGS);
  PG_FUNCTION_INFO_V1(rd_mollogp_pkl);
   Datum rd_mollogp_pkl(PG_FUNCTION_ARGS);
  PG_FUNCTION_INFO_V1(rd_amw_smi);
   Datum rd_amw_smi(PG_FUNCTION_ARGS);
  PG_FUNCTION_INFO_V1(rd_amw_pkl);
   Datum rd_amw_pkl(PG_FUNCTION_ARGS);


}

Datum rd_canonsmiles(PG_FUNCTION_ARGS)
{
  std::string res="";
  if(!PG_ARGISNULL(0)){
    text *smi = PG_GETARG_TEXT_P(0);
    RDKit::RWMol *mol=0;
    std::string smiS(VARDATA(smi),VARSIZE(smi)-VARHDRSZ);
    try {
      mol=RDKit::SmilesToMol(smiS);
    } catch (...) {
      mol=0;
    }
    if(mol) {
      try {
	res=RDKit::MolToSmiles(*mol,true);
      } catch (...) {
	ereport(ERROR,(errcode(ERRCODE_INTERNAL_ERROR),
		       errmsg("molecule constructed from SMILES '%s' could not be converted back to SMILES",
			      smiS.c_str())));
      }
      delete mol;
    } else {
      ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		     errmsg("SMILES '%s' could not be converted into a molecule",
			    smiS.c_str())));
    }
  } else {
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty SMILES in rd_canonsmiles function call")));
  }
  text *textRes;
  int totalSz=res.size()+VARHDRSZ;
  textRes = (text *)palloc(totalSz);
  VARATT_SIZEP(textRes)=totalSz;
  memcpy(VARDATA(textRes),res.c_str(),res.size());
  PG_RETURN_TEXT_P(textRes);
}


Datum rd_molpickle(PG_FUNCTION_ARGS)
{
  StringInfoData buf;
  pq_begintypsend(&buf);
  std::string pkl="";
  if(!PG_ARGISNULL(0)){
    text *smi = PG_GETARG_TEXT_P(0);
    RDKit::RWMol *mol=0;
    std::string smiS(VARDATA(smi),VARSIZE(smi)-VARHDRSZ);
    try {
      mol=RDKit::SmilesToMol(smiS);
    } catch (...) {
      mol=0;
    }
    if(mol) {
      RDKit::MolPickler::pickleMol(*mol,pkl);
      delete mol;
    } else {
      ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		     errmsg("SMILES '%s' could not be converted into a molecule",
			    smiS.c_str())));
    }
  } else {
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty SMILES in rd_molpickle function call")));
  }
  pq_sendtext(&buf,pkl.c_str(),pkl.size());
  PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}


Datum rd_hassubstruct_smi(PG_FUNCTION_ARGS)
{
  if(PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty argument in rd_hassubstruct_smi function call")));
  }
  text *sma = PG_GETARG_TEXT_P(0);
  text *smi = PG_GETARG_TEXT_P(1);

  RDKit::RWMol *probe=0;

  std::string smiS(VARDATA(smi),VARSIZE(smi)-VARHDRSZ);
  std::string smaS(VARDATA(sma),VARSIZE(sma)-VARHDRSZ);
  bool res = false;
  RDKit::RWMol *mol;
  try {
    mol=RDKit::SmilesToMol(smiS);
  } catch (...) {
    mol=0;
  }
  if(mol) {
    try {
      probe=RDKit::SmartsToMol(smaS);
    } catch (...) {
      probe=0;
    }
    if(probe){
      RDKit::MatchVectType matchVect;
      res = RDKit::SubstructMatch(*mol,*probe,matchVect);
    } else {
      ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		     errmsg("SMILES/SMARTS '%s' could not be converted into a query",
			    smaS.c_str())));
    }
    delete mol;
  } else {
    ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		   errmsg("SMILES '%s' could not be converted into a molecule",
			  smiS.c_str())));
  }
  PG_RETURN_BOOL(res);
}



Datum rd_hassubstruct_pkl(PG_FUNCTION_ARGS)
{
  bool res = false;
  if(!PG_ARGISNULL(0) && !PG_ARGISNULL(1)) {
    bytea *probePkl=PG_GETARG_BYTEA_P(0);
    bytea *refPkl=PG_GETARG_BYTEA_P(1);
    RDKit::ROMol *probe=0,*ref=0;
    try {
      probe = new RDKit::ROMol(std::string(VARDATA(probePkl),VARSIZE(probePkl)-VARHDRSZ));
    } catch (...) {
      probe=0;
    }
    try {
      ref = new RDKit::ROMol(std::string(VARDATA(refPkl),VARSIZE(refPkl)-VARHDRSZ));
    } catch (...) {
      ref=0;
    }
    if(probe && ref){
      RDKit::MatchVectType matchVect;
      res = RDKit::SubstructMatch(*ref,*probe,matchVect);
    } else {
      if(!probe){
	ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		       errmsg("problems converting argument 1 to a molecule")));
      }
      if(!ref){
	ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		       errmsg("problems converting argument 2 to a molecule")));
      }
    }
    if(probe) delete probe;
    if(ref) delete ref;
  } else {
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty argument in rd_hassubstruct_pkl function call")));
  }
  //elog(INFO,"%d %s",res,smiS.c_str());
  PG_RETURN_BOOL(res);
}


Datum rd_substructcount_smi(PG_FUNCTION_ARGS)
{
  if(PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty argument in rd_substructcount_smi function call")));
  }
  text *sma = PG_GETARG_TEXT_P(0);
  text *smi = PG_GETARG_TEXT_P(1);

  RDKit::RWMol *probe=0;

  std::string smiS(VARDATA(smi),VARSIZE(smi)-VARHDRSZ);
  std::string smaS(VARDATA(sma),VARSIZE(sma)-VARHDRSZ);
  int res = 0;
  RDKit::RWMol *mol;
  try {
    mol=RDKit::SmilesToMol(smiS);
  } catch (...) {
    mol=0;
  }
  if(mol) {
    try {
      probe=RDKit::SmartsToMol(smaS);
    } catch (...) {
      probe=0;
    }
    if(probe){
      std::vector<RDKit::MatchVectType> matchVect;
      res = RDKit::SubstructMatch(*mol,*probe,matchVect,true);
    } else {
      ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		     errmsg("SMILES/SMARTS '%s' could not be converted into a query",
			    smaS.c_str())));
    }
    delete mol;
  } else {
    ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		   errmsg("SMILES '%s' could not be converted into a molecule",
			  smiS.c_str())));
  }
  PG_RETURN_INT32(res);
}



Datum rd_substructcount_pkl(PG_FUNCTION_ARGS)
{
  int res = 0;
  if(!PG_ARGISNULL(0) && !PG_ARGISNULL(1)) {
    bytea *probePkl=PG_GETARG_BYTEA_P(0);
    bytea *refPkl=PG_GETARG_BYTEA_P(1);
    RDKit::ROMol *probe=0,*ref=0;
    try {
      probe = new RDKit::ROMol(std::string(VARDATA(probePkl),VARSIZE(probePkl)-VARHDRSZ));
    } catch (...) {
      probe=0;
    }
    try {
      ref = new RDKit::ROMol(std::string(VARDATA(refPkl),VARSIZE(refPkl)-VARHDRSZ));
    } catch (...) {
      ref=0;
    }
    if(probe && ref){
      std::vector<RDKit::MatchVectType> matchVect;
      res = RDKit::SubstructMatch(*ref,*probe,matchVect,true);
    } else {
      if(!probe){
	ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		       errmsg("problems converting argument 1 to a molecule")));
      }
      if(!ref){
	ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		       errmsg("problems converting argument 2 to a molecule")));
      }
    }
    if(probe) delete probe;
    if(ref) delete ref;
  } else {
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty argument in rd_substructcount_pkl function call")));
  }
  //elog(INFO,"%d %s",res,smiS.c_str());
  PG_RETURN_INT32(res);
}


Datum rd_substructfp(PG_FUNCTION_ARGS)
{
  StringInfoData buf;
  pq_begintypsend(&buf);
  ExplicitBitVect *bv=0;
  int fpSize=2048;
  if(!PG_ARGISNULL(0)){
    text *smi = PG_GETARG_TEXT_P(0);
    std::string smiS(VARDATA(smi),VARSIZE(smi)-VARHDRSZ);

    RDKit::RWMol *mol;
    try {
      mol=RDKit::SmilesToMol(smiS);
    } catch (...) {
      mol=0;
    }
    if(mol) {
      try{
	bv = RDKFingerprintMol(*mol,1,7,fpSize);
      } catch (...) {
	bv = new ExplicitBitVect(fpSize);
      }
      delete mol;
    } else {
      ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		     errmsg("SMILES '%s' could not be converted into a molecule",
			    smiS.c_str())));
    }
  } else {
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty SMILES in rd_substructfp function call")));
  }
  std::string pkl=bv->toString();
  delete bv;
  pq_sendtext(&buf,pkl.c_str(),pkl.size());
  PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}


Datum rd_substructfp_bits(PG_FUNCTION_ARGS)
{
  int fpSize=2048;

  ExplicitBitVect *bv=0;
  if(!PG_ARGISNULL(0)){
    text *smi = PG_GETARG_TEXT_P(0);
    std::string smiS(VARDATA(smi),VARSIZE(smi)-VARHDRSZ);

    RDKit::RWMol *mol;
    try {
      mol=RDKit::SmilesToMol(smiS);
    } catch (...) {
      mol=0;
    }
    if(mol) {
      try{
	bv = RDKFingerprintMol(*mol,1,7,fpSize);
      } catch (...) {
	bv = 0;
      }
      delete mol;
    } else {
      bv = 0;
    }
  } else {
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty SMILES in rd_substructfp_bits function call")));
  }
  // BVToVarBits handles null pointers w/o problems so we can go ahead and
  // call it directly:
  VarBit *result = BVToVarBits(bv,fpSize);
  if(bv){
    delete bv;
  }
  PG_RETURN_VARBIT_P(result);
}

Datum rd_similarityfp(PG_FUNCTION_ARGS)
{
  StringInfoData buf;
  pq_begintypsend(&buf);
  ExplicitBitVect *bv=0;
  int fpSize=2048;
  if(!PG_ARGISNULL(0)){
    text *smi = PG_GETARG_TEXT_P(0);
    std::string smiS(VARDATA(smi),VARSIZE(smi)-VARHDRSZ);

    double tgtDensity=0.3;
    RDKit::RWMol *mol;
    try {
      mol=RDKit::SmilesToMol(smiS);
    } catch (...) {
      mol=0;
    }
    if(mol) {
      try{
	bv = RDKFingerprintMol(*mol,1,7,fpSize);
	double density=static_cast<double>(bv->getNumOnBits())/bv->getNumBits();
	while(density<tgtDensity && bv->getNumBits()>8){
	  ExplicitBitVect *tmp=FoldFingerprint(*bv,2);
	  delete bv;
	  bv = tmp;
	  density=static_cast<double>(bv->getNumOnBits())/bv->getNumBits();
	}
      } catch (...) {
	bv = new ExplicitBitVect(fpSize);
      }
      delete mol;
    } else {
      bv = new ExplicitBitVect(fpSize);
    }
  } else {
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty SMILES in rd_similarityfp function call")));
  }
  std::string pkl=bv->toString();
  delete bv;
  pq_sendtext(&buf,pkl.c_str(),pkl.size());
  PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}


Datum rd_similarityfp_bits(PG_FUNCTION_ARGS)
{
  int fpSize=2048;
  ExplicitBitVect *bv=0;

  if(!PG_ARGISNULL(0)){
    text *smi = PG_GETARG_TEXT_P(0);
    std::string smiS(VARDATA(smi),VARSIZE(smi)-VARHDRSZ);

    double tgtDensity=0.3;
    
    RDKit::RWMol *mol;
    try {
      mol=RDKit::SmilesToMol(smiS);
    } catch (...) {
      mol=0;
    }
    if(mol) {
      try{
	bv = RDKFingerprintMol(*mol,1,7,fpSize);
	double density=static_cast<double>(bv->getNumOnBits())/bv->getNumBits();
	while(density<tgtDensity && bv->getNumBits()>8){
	  ExplicitBitVect *tmp=FoldFingerprint(*bv,2);
	  delete bv;
	  bv = tmp;
	  density=static_cast<double>(bv->getNumOnBits())/bv->getNumBits();
	}
      } catch (...) {
	bv = new ExplicitBitVect(fpSize);
      }
      delete mol;
    } else {
      bv = new ExplicitBitVect(fpSize);
    }
  } else {
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty SMILES in rd_similarityfp_bits function call")));
  }
  // BVToVarBits handles null pointers w/o problems so we can go ahead and
  // call it directly:
  if(bv) fpSize=bv->getNumBits();
  VarBit *result = BVToVarBits(bv,fpSize);
  if(bv){
    delete bv;
  }
  PG_RETURN_VARBIT_P(result);
}


Datum rd_mollogp_smi(PG_FUNCTION_ARGS)
{
  //elog(NOTICE,"rd_mollogp_smi");
  if(PG_ARGISNULL(0)){
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty argument in rd_mollogp_smi function call")));
  }

  text *smi = PG_GETARG_TEXT_P(0);
  std::string smiS(VARDATA(smi),VARSIZE(smi)-VARHDRSZ);

  
  double res = 0.0;
  RDKit::RWMol *mol;
  try {
    mol=RDKit::SmilesToMol(smiS);
    
  } catch (...) {
    mol=0;
  }
  if(mol) {
    double tmp;
    RDKit::Descriptors::CalcCrippenDescriptors(*mol,res,tmp);
    delete mol;
  } else {
    ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		   errmsg("SMILES '%s' could not be converted into a molecule",
			  smiS.c_str())));
  }
  PG_RETURN_FLOAT8(res);
}



Datum rd_mollogp_pkl(PG_FUNCTION_ARGS)
{
  //elog(NOTICE,"rd_mollogp_pkl");
  double res=0;
  if(!PG_ARGISNULL(0)){
    bytea *molPkl=PG_GETARG_BYTEA_P(0);
    RDKit::ROMol *mol=0;
    try {
      mol = new RDKit::ROMol(std::string(VARDATA(molPkl),VARSIZE(molPkl)-VARHDRSZ));
    } catch (...) {
      mol=0;
    }
    if(mol){
      //elog(NOTICE,"   numatoms: %d",mol->getNumAtoms());
      double tmp;
      RDKit::Descriptors::CalcCrippenDescriptors(*mol,res,tmp);
      delete mol;
    } else {
      ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		     errmsg("problems converting argument to a molecule in rd_mollogp_pkl")));
    }
  } else {
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty argument in rd_mollogp_pkl function call")));
  }
  PG_RETURN_FLOAT8(res);
}

Datum rd_amw_smi(PG_FUNCTION_ARGS)
{
  if(PG_ARGISNULL(0)){
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty argument in rd_amw_smi function call")));
  }

  text *smi = PG_GETARG_TEXT_P(0);
  std::string smiS(VARDATA(smi),VARSIZE(smi)-VARHDRSZ);

  
  double res = 0.0;
  RDKit::RWMol *mol;
  try {
    mol=RDKit::SmilesToMol(smiS);
    
  } catch (...) {
    mol=0;
  }
  if(mol) {
    res = RDKit::Descriptors::CalcAMW(*mol);
    delete mol;
  } else {
    ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		   errmsg("SMILES '%s' could not be converted into a molecule",
			  smiS.c_str())));
  }
  PG_RETURN_FLOAT8(res);
}



Datum rd_amw_pkl(PG_FUNCTION_ARGS)
{
  double res=0;
  if(!PG_ARGISNULL(0)){
    bytea *molPkl=PG_GETARG_BYTEA_P(0);
    RDKit::ROMol *mol=0;
    try {
      mol = new RDKit::ROMol(std::string(VARDATA(molPkl),VARSIZE(molPkl)-VARHDRSZ));
    } catch (...) {
      mol=0;
    }
    if(mol){
      res = RDKit::Descriptors::CalcAMW(*mol);
      delete mol;
    } else {
      ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
		     errmsg("problems converting argument to a molecule in rd_amw_pkl")));
    }
  } else {
    ereport(ERROR,(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
		   errmsg("missing or empty argument in rd_amw_pkl function call")));
  }
  PG_RETURN_FLOAT8(res);
}