Datum mol_rsubstruct(PG_FUNCTION_ARGS) { CROMol i, a; fcinfo->flinfo->fn_extra = searchMolCache(fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt, PG_GETARG_DATUM(0), NULL, &i, NULL); fcinfo->flinfo->fn_extra = searchMolCache(fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt, PG_GETARG_DATUM(1), NULL, &a, NULL); PG_RETURN_BOOL(MolSubstruct(a, i)); }
Datum mol_substruct_count(PG_FUNCTION_ARGS) { CROMol i, a; bool uniquify = PG_GETARG_BOOL(2); fcinfo->flinfo->fn_extra = searchMolCache(fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt, PG_GETARG_DATUM(0), NULL, &i, NULL); fcinfo->flinfo->fn_extra = searchMolCache(fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt, PG_GETARG_DATUM(1), NULL, &a, NULL); PG_RETURN_INT32(MolSubstructCount(i, a, uniquify)); }
Datum mol_hash(PG_FUNCTION_ARGS) { CROMol mol; char *str; int len; fcinfo->flinfo->fn_extra = searchMolCache(fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt, PG_GETARG_DATUM(0), NULL, &mol, NULL); Assert(mol != 0); str = computeMolHash(mol, &len); Assert(str != 0 && strlen(str) != 0); PG_RETURN_CSTRING(pnstrdup(str, len)); }
Datum mol_murckoscaffold(PG_FUNCTION_ARGS) { CROMol mol; Mol *res; fcinfo->flinfo->fn_extra = searchMolCache(fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt, PG_GETARG_DATUM(0), NULL, &mol, NULL); CROMol scaffold = MolMurckoScaffold(mol); if (!scaffold) PG_RETURN_NULL(); res = deconstructROMol(scaffold); freeCROMol(scaffold); PG_RETURN_MOL_P(res); }
Datum mol_inchikey(PG_FUNCTION_ARGS) { CROMol mol; const char *str; char *res, *opts = PG_GETARG_CSTRING(1); fcinfo->flinfo->fn_extra = searchMolCache(fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt, PG_GETARG_DATUM(0), NULL, &mol, NULL); str = MolInchiKey(mol, opts); res = pnstrdup(str, strlen(str)); free((void *)str); PG_RETURN_CSTRING(res); }
Datum mol_adjust_query_properties(PG_FUNCTION_ARGS) { CROMol mol; fcinfo->flinfo->fn_extra = searchMolCache(fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt, PG_GETARG_DATUM(0), NULL, &mol, NULL); Assert(mol != 0); char *data = PG_GETARG_CSTRING(1); CROMol adj = MolAdjustQueryProperties(mol, data); if (!adj) PG_RETURN_NULL(); Mol *res = deconstructROMol(adj); freeCROMol(adj); PG_RETURN_MOL_P(res); }
Datum mol_formula(PG_FUNCTION_ARGS) { CROMol mol; char *str; int len; bool separateIsotopes = PG_GETARG_BOOL(1); bool abbreviateHIsotopes = PG_GETARG_BOOL(2); fcinfo->flinfo->fn_extra = searchMolCache(fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt, PG_GETARG_DATUM(0), NULL, &mol, NULL); str = makeMolFormulaText(mol, &len, separateIsotopes, abbreviateHIsotopes); PG_RETURN_CSTRING(pnstrdup(str, len)); }
Datum gmol_consistent(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); bool *recheck = (bool *) PG_GETARG_POINTER(4); bytea *key = (bytea*)DatumGetPointer(entry->key); bytea *query; bool res = true; int siglen = SIGLEN(key); fcinfo->flinfo->fn_extra = searchMolCache( fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt, PG_GETARG_DATUM(1), NULL, NULL,&query); /* ** recheck is required for all strategies */ *recheck = true; switch (strategy) { case RDKitContains: if (!ISALLTRUE(key)) { if (siglen != SIGLEN(query)) { elog(ERROR, "All fingerprints should be the same length"); } uint8 *k = (uint8 *)VARDATA(key); uint8 *q = (uint8 *)VARDATA(query); res = bitstringContains(siglen, k, q); } break; case RDKitContained: if (!ISALLTRUE(key)) { if (siglen != SIGLEN(query)) { elog(ERROR, "All fingerprints should be the same length"); } uint8 *k = (uint8 *)VARDATA(key); uint8 *q = (uint8 *)VARDATA(query); if (GIST_LEAF(entry)) { res = bitstringContains(siglen, q, k); } else { /* * Due to superimposed key on inner page we could only check * overlapping */ res = bitstringIntersects(siglen, q, k); } } else if (GIST_LEAF(entry)) { /* * key is all true, it may be contained in query, iff query is also * all true */ res = bitstringAllTrue(siglen, (uint8 *)VARDATA(query)); } break; case RDKitEquals: if (!ISALLTRUE(key)) { /* ** verify the necessary condition that key should contain the query ** (on leaf nodes, couldn't it also verify that query contains key?) */ if (siglen != SIGLEN(query)) { elog(ERROR, "All fingerprints should be the same length"); } uint8 *k = (uint8 *)VARDATA(key); uint8 *q = (uint8 *)VARDATA(query); res = bitstringContains(siglen, k, q); } break; default: elog(ERROR,"Unknown strategy: %d", strategy); } PG_RETURN_BOOL(res); }
Datum fmcs_mol2s_transition(PG_FUNCTION_ARGS) { // elog(WARNING, (PG_ARGISNULL(0)) ? "arg 0 is NULL" : "arg 0 is NOT NULL"); // elog(WARNING, (PG_ARGISNULL(1)) ? "arg 1 is NULL" : "arg 1 is NOT NULL"); if (!AggCheckCallContext(fcinfo, NULL)) { // elog(WARNING, "fmcs_mol2s_transition() called in out of aggregate // context"); ereport( ERROR, (errmsg("fmcs_mol2s_transition() called in out of aggregate context"))); } if (PG_ARGISNULL(0) && !PG_ARGISNULL(1)) { // first call /// elog(WARNING, "fmcs_mol2s_transition() called first time"); CROMol mol = PG_GETARG_DATUM(1); int len, ts_size; char *smiles; elog(WARNING, "mol=%p, fcinfo: %p, %p", mol, fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt); fcinfo->flinfo->fn_extra = searchMolCache(fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt, PG_GETARG_DATUM(1), NULL, &mol, NULL); smiles = makeMolText(mol, &len, false); // char *smiles= Mol2Smiles(mol); // int len = strlen(smiles); /// elog(WARNING, smiles); ts_size = len + VARHDRSZ; text *ts = (text *)palloc(ts_size); // new return value SET_VARSIZE(ts, ts_size); memcpy(VARDATA(ts), smiles, len); PG_RETURN_TEXT_P(ts); } else if (!PG_ARGISNULL(0) && !PG_ARGISNULL(1)) { // Called in aggregate context... text *t0 = PG_GETARG_TEXT_P(0); /// elog(WARNING, "fmcs_mol2s_transition(): next iteration in the same /// run"); // elog(WARNING, VARDATA(t0)); // mol_to_smiles(): CROMol mol = PG_GETARG_DATUM(1); int len; elog(WARNING, "mol=%p, fcinfo: %p, %p", mol, fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt); fcinfo->flinfo->fn_extra = searchMolCache(fcinfo->flinfo->fn_extra, fcinfo->flinfo->fn_mcxt, PG_GETARG_DATUM(1), NULL, &mol, NULL); char *smiles = makeMolText(mol, &len, false); // char *smiles= Mol2Smiles(mol); // int len = strlen(smiles); /// elog(WARNING, smiles); int32 ts_size = VARSIZE(t0) + 1 + len; text *ts = (text *)palloc(ts_size); // new return value SET_VARSIZE(ts, ts_size); memcpy(VARDATA(ts), VARDATA(t0), VARSIZE(t0) - VARHDRSZ); *(char *)(VARDATA(ts) + VARSIZE(t0) - VARHDRSZ) = ' '; memcpy(VARDATA(ts) + VARSIZE(t0) - VARHDRSZ + 1, smiles, len); // elog(WARNING, VARDATA(ts)); PG_RETURN_TEXT_P(ts); } //------ /// elog(WARNING, "fmcs_mol2s_transition(): return empty text block"); { int32 ts_size = VARHDRSZ; text *ts = (text *)palloc(ts_size); // return empty text block SET_VARSIZE(ts, ts_size); PG_RETURN_TEXT_P(ts); } }