bool isOrganic(int structure) { int atom, atomIter; bool structureOrganic = true; atomIter = indigoIterateAtoms(structure); while(atom = indigoNext(atomIter)) { bool atomOrganic = indigoAtomicNumber(atom) == 1 || indigoAtomicNumber(atom) == 6 || indigoAtomicNumber(atom) == 7 || indigoAtomicNumber(atom) == 8 || indigoAtomicNumber(atom) == 15 || indigoAtomicNumber(atom) == 16 || indigoAtomicNumber(atom) == 9 || indigoAtomicNumber(atom) == 17 || indigoAtomicNumber(atom) == 35 || indigoAtomicNumber(atom) == 53; if(!atomOrganic) { structureOrganic = false; indigoFree(atom); break; } indigoFree(atom); } indigoFree(atomIter); return structureOrganic; }
// returns the largest component within the structure (you need to free this returned object later) // doesn't free structure int pickLargestSubstructure(int structure) { int componentIter, component, largestComponent; // Indigo objects that require freeing componentIter = indigoIterateComponents(structure); int maxAtoms = 0; if (componentIter != -1) { while (component = indigoNext(componentIter)) { // if only one component if (indigoIndex(component) == 0 && !indigoHasNext(componentIter)) { indigoFree(componentIter); return indigoClone(structure); } int currentComponent = indigoClone(component); int numAtoms = indigoCountAtoms(currentComponent); //Rprintf("Number of atoms: %d\n\n", numAtoms); if (numAtoms>maxAtoms) { maxAtoms = numAtoms; largestComponent = indigoClone(currentComponent); } indigoFree(currentComponent); indigoFree(component); } } indigoFree(componentIter); return largestComponent; }
void resetIsotopes(int structure) { int atom, atomIter; atomIter = indigoIterateAtoms(structure); while(atom = indigoNext(atomIter)) { indigoResetIsotope(atom); indigoFree(atom); } indigoFree(atomIter); }
void _handleInputFile (const char *path, int structures) { if ((strlen(path) > 4 && strcmp(path + strlen(path) - 4, ".sdf") == 0) || (strlen(path) > 7 && strcmp(path + strlen(path) - 7, ".sdf.gz") == 0)) { int item, iter = indigoIterateSDFile(path); while ((item = indigoNext(iter))) { indigoArrayAdd(structures, item); indigoFree(item); } indigoFree(iter); } else if ((strlen(path) > 4 && strcmp(path + strlen(path) - 4, ".rdf") == 0) || (strlen(path) > 7 && strcmp(path + strlen(path) - 7, ".rdf.gz") == 0)) { int item, iter = indigoIterateRDFile(path); while ((item = indigoNext(iter))) { indigoArrayAdd(structures, item); indigoFree(item); } indigoFree(iter); } else if ((strlen(path) > 4 && strcmp(path + strlen(path) - 4, ".smi") == 0) || (strlen(path) > 7 && strcmp(path + strlen(path) - 7, ".smi.gz") == 0)) { int item, iter = indigoIterateSmilesFile(path); while ((item = indigoNext(iter))) { indigoArrayAdd(structures, item); indigoFree(item); } indigoFree(iter); } else if ((strlen(path) > 4 && strcmp(path + strlen(path) - 4, ".cml") == 0)) { int item, iter = indigoIterateCMLFile(path); while ((item = indigoNext(iter))) { indigoArrayAdd(structures, item); indigoFree(item); } indigoFree(iter); } else { int item = indigoLoadMoleculeFromFile(path); indigoArrayAdd(structures, item); indigoFree(item); } }
bool containsMoreThanX(int structure, int X, int atomicNumber) { int count = 0; int atom, atomIter; atomIter = indigoIterateAtoms(structure); while(atom = indigoNext(atomIter)) { if(indigoAtomicNumber(atom)==atomicNumber) { count++; } indigoFree(atom); } indigoFree(atomIter); return count > X; }
std::string expandSuperatoms(const Settings& vars, const Molecule &molecule ) { logEnterFunction(); std::string molString; ArrayOutput so(molString); MolfileSaver ma(so); ma.saveMolecule(vars, molecule); if (!vars.general.ExpandAbbreviations) return molString; indigoSetOption("treat-x-as-pseudoatom", "true"); indigoSetOption("ignore-stereochemistry-errors", "true"); int mol = indigoLoadMoleculeFromString(molString.c_str()); if (mol == -1) { fprintf(stderr, "%s\n", indigoGetLastError()); return molString; } int expCount = indigoExpandAbbreviations(mol); if (expCount == -1) { fprintf(stderr, "%s\n", indigoGetLastError()); return molString; } std::string newMolfile = indigoMolfile(mol); indigoFree(mol); return newMolfile; }
bool isAromatic(int structure) { int bond, bondIter; bool structureAromatic = false; bondIter = indigoIterateBonds(structure); while(bond = indigoNext(bondIter)) { bool bondAromatic = (indigoBondOrder(bond) == 4); if(bondAromatic) { structureAromatic = true; indigoFree(bond); break; } indigoFree(bond); } indigoFree(bondIter); return structureAromatic; }
// looks for CO- SO- and PO- instances, neutralises the O // returns the number of hits int neutraliseCSPnegO(int structure) { int query, matcher, matchIter, match, queryAtomIter, queryAtom; // Indigo objects that require freeing query = indigoLoadSmartsFromString("[O-$(*[#6,#14,#15])]"); matcher = indigoSubstructureMatcher(structure, 0); matchIter = indigoIterateMatches(matcher, query); int numMatches = 0; if (matchIter != -1) { while (match = indigoNext(matchIter)) { queryAtomIter = indigoIterateAtoms(query); while (queryAtom = indigoNext(queryAtomIter)) { int structureAtom = indigoMapAtom(match, queryAtom); indigoSetCharge(structureAtom, 0); indigoFree(queryAtom); } numMatches++; indigoFree(queryAtomIter); indigoFree(match); } } indigoFree(matchIter); indigoFree(matcher); indigoFree(query); return numMatches; }
// looks for NH+ instances, neutralises the N // returns the number of hits int neutraliseNposH(int structure) { int query, matcher, matchIter, match, queryAtomIter, queryAtom; // Indigo objects that require freeing query = indigoLoadSmartsFromString("[#7+$(*[H])]"); // #7 means any nitrogen, aliphatic or aromatic matcher = indigoSubstructureMatcher(structure, 0); matchIter = indigoIterateMatches(matcher, query); int numMatches = 0; if (matchIter != -1) { while (match = indigoNext(matchIter)) { queryAtomIter = indigoIterateAtoms(query); while (queryAtom = indigoNext(queryAtomIter)) { int structureAtom = indigoMapAtom(match, queryAtom); indigoSetCharge(structureAtom, 0); indigoFree(queryAtom); } numMatches++; indigoFree(queryAtomIter); indigoFree(match); } } indigoFree(matchIter); indigoFree(matcher); indigoFree(query); return numMatches; }
CEXPORT int indigoSaveRxnfileToFile (int reaction, const char *filename) { int f = indigoWriteFile(filename); int res; if (f == -1) return -1; res = indigoSaveRxnfile(reaction, f); indigoFree(f); return res; }
CEXPORT int indigoSaveCmlToFile (int molecule, const char *filename) { int f = indigoWriteFile(filename); int res; if (f == -1) return -1; res = indigoSaveCml(molecule, f); indigoFree(f); return res; }
int _isMultipleCML (const char *filename, int *reaction) { int iter = indigoIterateCMLFile(filename); *reaction = 0; if (indigoHasNext(iter)) { int item = indigoNext(iter); if (strstr(indigoRawData(item), "<reaction") != NULL) *reaction = 1; if (indigoHasNext(iter)) { indigoFree(item); indigoFree(iter); return 1; } indigoFree(item); } indigoFree(iter); return 0; }
CEXPORT const char * indigoRxnfile (int molecule) { int b = indigoWriteBuffer(); const char *res; if (b == -1) return 0; if (indigoSaveRxnfile(molecule, b) == -1) return 0; res = indigoToString(b); indigoFree(b); return res; }
void renderPair(int s1, int s2, const char* folder, const char* file, int number, const char* title) { int collection = indigoCreateArray(); indigoArrayAdd(collection, s1); indigoArrayAdd(collection, s2); indigoSetOption("render-output-format", "png"); indigoSetOption("render-comment", title); indigoSetOption("render-comment-position", "top"); indigoSetOptionXY("render-image-size", 800, 1000); indigoSetOptionColor("render-background-color", 1.0, 1.0, 1.0); char str[256]; if(number != -1) { sprintf(str, "./%s/%d%s%s%s", folder, number, "_", file, ".png"); } else { sprintf(str, "%s%s", file, ".png"); } indigoRenderGridToFile(collection, NULL, 2, str); indigoFree(collection); }
int main (int argc, const char** argv) { int i; int done_with_options = 0; int approximate = 0; int scaffold = 0; int aromatic = 1; const char *outfile_hl = 0; const char *outfile_rg = 0; const char *outfile_maxscaf = 0; const char *outfile_allscafs = 0; const char *outfile_scaf_r = 0; int deco = 0; int structures = 0; indigoSetErrorHandler(onError, 0); printf("R-Group deconvolution utility, powered by Indigo API version %s\n", indigoVersion()); structures = indigoCreateArray(); indigoSetOptionBool("treat-x-as-pseudoatom", 1); indigoSetOptionBool("ignore-stereochemistry-errors", 1); for (i = 1; i < argc; i++) { if (!done_with_options && argv[i][0] == '-') { if (strcmp(argv[i], "--") == 0) done_with_options = 1; else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0 || strcmp(argv[i], "/?") == 0 || strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0) { _printHelpMessage(); return 0; } else if (strcmp(argv[i], "-a") == 0) approximate = 1; else if (strcmp(argv[i], "-l") == 0) { if (++i == argc) { fprintf(stderr, "expecting filename after -l\n"); return -1; } scaffold = indigoLoadMoleculeFromFile(argv[i]); } else if (strcmp(argv[i], "-o") == 0) { if (++i == argc) { fprintf(stderr, "expecting filename after -o\n"); return -1; } outfile_hl = argv[i]; } else if (strcmp(argv[i], "-r") == 0) { if (++i == argc) { fprintf(stderr, "expecting filename after -r\n"); return -1; } outfile_rg = argv[i]; } else if (strcmp(argv[i], "-s") == 0) { if (++i == argc) { fprintf(stderr, "expecting filename after -s\n"); return -1; } outfile_maxscaf = argv[i]; } else if (strcmp(argv[i], "-sr") == 0) { if (++i == argc) { fprintf(stderr, "expecting filename after -sr\n"); return -1; } outfile_scaf_r = argv[i]; } else if (strcmp(argv[i], "-S") == 0) { if (++i == argc) { fprintf(stderr, "expecting filename after -S\n"); return -1; } outfile_allscafs = argv[i]; } else if (strcmp(argv[i], "-na") == 0) aromatic = 0; else { fprintf(stderr, "Unknown option: %s", argv[i]); _printHelpMessage(); return -1; } } else { char dirname[1024]; char errbuf[1024]; const char *filename = 0; int k; for (k = (int)strlen(argv[i]) - 1; k >= 0; k--) if (argv[i][k] == '/' || argv[i][k] == '\\') break; if (k == -1) strncpy(dirname, ".", sizeof(dirname)); else if (k == 0) { dirname[0] = argv[i][0]; dirname[1] = 0; } else if (k == strlen(argv[i]) - 1) { fprintf(stderr, "can not handle filenames ending with a slash\n"); return -1; } else if (k > sizeof(dirname) - 1) { fprintf(stderr, "filename too long\n"); return -1; } else { memcpy(dirname, argv[i], k); dirname[k] = 0; } _replaceSlashes(dirname); filename = argv[i] + k + 1; { OsDirIter dir_iter; int rc = osDirSearch(dirname, filename, &dir_iter); if (rc == OS_DIR_OK) { int count = 0; while ((rc = osDirNext(&dir_iter)) == OS_DIR_OK) { _replaceSlashes(dir_iter.path); _handleInputFile(dir_iter.path, structures); count++; } if (rc != OS_DIR_END) { fprintf(stderr, "%s\n", osDirLastError(errbuf, sizeof(errbuf))); return -1; } if (count == 0) { fprintf(stderr, "can not find %s in directory %s\n", filename, dirname); return -1; } } else { fprintf(stderr, "%s\n", osDirLastError(errbuf, sizeof(errbuf))); return -1; } } } } if (indigoCount(structures) < 1) { fprintf(stderr, "no input structures\n"); _printHelpMessage(); return -1; } printf("got %d input structures\n", indigoCount(structures)); indigoSetOptionBool("deconvolution-aromatization", aromatic); if (scaffold == 0) { printf("calculating scaffold... "); fflush(stdout); if (approximate) scaffold = indigoExtractCommonScaffold(structures, "approximate"); else scaffold = indigoExtractCommonScaffold(structures, "exact"); printf("done\n"); fflush(stdout); } if (outfile_maxscaf != 0) { printf("saving the scaffold to %s\n", outfile_maxscaf); indigoSaveMolfileToFile(scaffold, outfile_maxscaf); } if (outfile_allscafs != 0) { int output = indigoWriteFile(outfile_allscafs); int allscafs = indigoAllScaffolds(scaffold); int item, iter = indigoIterateArray(allscafs); printf("saving all obtained scaffolds (%d total) to %s\n", indigoCount(allscafs), outfile_allscafs); while ((item = indigoNext(iter))) { indigoSdfAppend(output, item); indigoFree(item); } indigoFree(iter); indigoFree(output); } if (outfile_hl == 0 && outfile_rg == 0 && outfile_scaf_r == 0) { printf("none of -o, -r, -sr specified, nothing left to do\n"); return 0; } printf("decomposing the structures... "); fflush(stdout); deco = indigoDecomposeMolecules(scaffold, structures); printf("done\n"); fflush(stdout); if (outfile_scaf_r != 0) { int sr = indigoDecomposedMoleculeScaffold(deco); indigoLayout(sr); printf("saving the scaffold with R-sites to %s\n", outfile_scaf_r); indigoSaveMolfileToFile(sr, outfile_scaf_r); } if (outfile_hl != 0) { int output = indigoWriteFile(outfile_hl); int item, iter = indigoIterateDecomposedMolecules(deco); printf("saving the highlighted structures to %s\n", outfile_hl); while ((item = indigoNext(iter))) { indigoSdfAppend(output, indigoDecomposedMoleculeHighlighted(item)); indigoFree(item); } indigoFree(iter); indigoFree(output); } if (outfile_rg != 0) { int output = indigoWriteFile(outfile_rg); int item, iter = indigoIterateDecomposedMolecules(deco); printf("saving the structures with R-groups to %s\n", outfile_rg); while ((item = indigoNext(iter))) { indigoSdfAppend(output, indigoDecomposedMoleculeWithRGroups(item)); indigoFree(item); } indigoFree(iter); indigoFree(output); } return 0; };
int performFileAction(bool verbose, imago::Settings& vars, const std::string& imageName, const std::string& configName, const std::string& outputName) { logEnterFunction(); int result = 0; // ok mark imago::VirtualFS vfs; vars.general.StartTime = 0; // reset timelimit if (vars.general.ExtractCharactersOnly) { if (verbose) printf("Characters extraction from image '%s'\n", imageName.c_str()); } else { if (verbose) printf("Recognition of image '%s'\n", imageName.c_str()); } try { imago::Image image; if (vars.general.LogVFSEnabled) { imago::getLogExt().SetVirtualFS(vfs); } imago::ImageUtils::loadImageFromFile(image, imageName.c_str()); if (vars.general.ExtractCharactersOnly) { imago::Image out; imago::prefilterEntrypoint(vars, out, image); applyConfig(verbose, vars, configName); imago::ChemicalStructureRecognizer _csr; _csr.extractCharacters(vars, out); } else { RecognitionResult result = recognizeImage(verbose, vars, image, configName); imago::FileOutput fout(outputName.c_str()); fout.writeString(result.molecule.c_str()); if (imago::getLogExt().loggingEnabled()) { int molObj = indigoLoadMoleculeFromString(result.molecule.c_str()); if (molObj != -1) { indigoSetOption("render-output-format", "png"); indigoSetOption("render-background-color", "255, 255, 255"); std::string outputImg = imago::getLogExt().generateImageName(); indigoRenderToFile(molObj, outputImg.c_str()); imago::getLogExt().appendImageFile("Result image:", outputImg); indigoFree(molObj); } } } } catch (std::exception &e) { result = 2; // error mark puts(e.what()); } dumpVFS(vfs, "log_vfs.txt"); return result; }
int main (int argc, char *argv[]) { Params p; int obj = -1, reader = -1, writer = -1; int i = 0; char number[100]; char outfilename[4096]; const char *id; p.width = p.height = p.bond = p.mode = -1; p.id = p.string_to_load = p.file_to_load = NULL; p.hydro_set = p.query_set = p.smarts_set = 0; p.aromatization = NONE; p.comment_field = NULL; p.comment = NULL; p.comment_name = 0; if (argc <= 2) USAGE(); indigoSetErrorHandler(onError, 0); indigoSetOption("ignore-stereochemistry-errors", "on"); if (parseParams(&p, argc, argv) < 0) return -1; p.out_ext = OEXT_OTHER; if (strcmp(p.outfile_ext, "mol") == 0) p.out_ext = OEXT_MOL; else if (strcmp(p.outfile_ext, "sdf") == 0) p.out_ext = OEXT_SDF; else if (strcmp(p.outfile_ext, "rxn") == 0) p.out_ext = OEXT_RXN; else if (strcmp(p.outfile_ext, "rdf") == 0) p.out_ext = OEXT_RDF; else if (strcmp(p.outfile_ext, "cml") == 0) p.out_ext = OEXT_CML; // guess whether to layout or render by extension p.action = ACTION_LAYOUT; if (p.out_ext == OEXT_OTHER) { indigoSetOption("render-output-format", p.outfile_ext); p.action = ACTION_RENDER; } // read in the input reader = (p.file_to_load != NULL) ? indigoReadFile(p.file_to_load) : indigoReadString(p.string_to_load); if (p.mode == MODE_SINGLE_MOLECULE) { if (p.id != NULL) ERROR("on single input, setting '-id' is not allowed\n"); if (p.out_ext == OEXT_RXN) ERROR("reaction output specified for molecule input\n"); if (p.smarts_set) obj = indigoLoadSmarts(reader); else if (p.query_set) obj = indigoLoadQueryMolecule(reader); else obj = indigoLoadMolecule(reader); _prepare(obj, p.aromatization); if (p.action == ACTION_LAYOUT) { indigoLayout(obj); if (p.out_ext == OEXT_MOL) indigoSaveMolfileToFile(obj, p.outfile); else indigoSaveCmlToFile(obj, p.outfile); } else { _setComment(obj, &p); renderToFile(obj, p.outfile); } } else if (p.mode == MODE_SINGLE_REACTION) { if (p.id != NULL) ERROR("on single input, setting '-id' is not allowed\n"); if (p.out_ext == OEXT_MOL) ERROR("molecule output specified for reaction input\n"); if (p.smarts_set) obj = indigoLoadReactionSmarts(reader); else if (p.query_set) obj = indigoLoadQueryReaction(reader); else obj = indigoLoadReaction(reader); _prepare(obj, p.aromatization); if (p.action == ACTION_LAYOUT) { indigoLayout(obj); if (p.out_ext == OEXT_CML) indigoSaveCmlToFile(obj, p.outfile); else indigoSaveRxnfileToFile(obj, p.outfile); } else { _setComment(obj, &p); renderToFile(obj, p.outfile); } } else { int item; int have_percent_s = (strstr(p.outfile, "%s") != NULL); if (p.mode == MODE_MULTILINE_SMILES) obj = indigoIterateSmiles(reader); else if (p.mode == MODE_SDF) obj = indigoIterateSDF(reader); else if (p.mode == MODE_MULTIPLE_CML) obj = indigoIterateCML(reader); else if (p.mode == MODE_RDF) obj = indigoIterateRDF(reader); else { fprintf(stderr, "internal error: wrong branch\n"); return -1; } if ((p.out_ext == OEXT_MOL || p.out_ext == OEXT_RXN || p.out_ext == OEXT_OTHER) && !have_percent_s) ERROR("on multiple output, output file name must have '%%s'\n"); if (p.out_ext == OEXT_SDF || p.out_ext == OEXT_RDF || (p.out_ext == OEXT_CML && !have_percent_s)) { writer = indigoWriteFile(p.outfile); if (p.out_ext == OEXT_RDF) indigoRdfHeader(writer); if (p.out_ext == OEXT_CML) indigoCmlHeader(writer); } i = -1; while ((item = indigoNext(obj))) { int rc; ++i; if (writer > 0) printf("saving item #%d... ", i); else { if (p.id) { if (!indigoHasProperty(item, p.id)) { fprintf(stderr, "item #%d does not have %s, skipping\n", i, p.id); continue; } id = indigoGetProperty(item, p.id); snprintf(outfilename, sizeof(outfilename), p.outfile, id); } else { snprintf(number, sizeof(number), "%d", i); snprintf(outfilename, sizeof(outfilename), p.outfile, number); } printf("saving %s... ", outfilename); } indigoSetErrorHandler(0, 0); if (_prepare(item, p.aromatization) < 0) { printf("%s\n", indigoGetLastError()); indigoSetErrorHandler(onError, 0); continue; } if (p.action == ACTION_LAYOUT) { if (indigoLayout(item) < 0) { printf("%s\n", indigoGetLastError()); indigoSetErrorHandler(onError, 0); continue; } } if (writer > 0) { if (p.out_ext == OEXT_SDF) rc = indigoSdfAppend(writer, item); else if (p.out_ext == OEXT_RDF) rc = indigoRdfAppend(writer, item); else rc = indigoCmlAppend(writer, item); } else { if (p.action == ACTION_LAYOUT) { if (p.out_ext == OEXT_MOL) rc = indigoSaveMolfileToFile(item, outfilename); else if (p.out_ext == OEXT_RXN) rc = indigoSaveRxnfileToFile(item, outfilename); else ERROR("extension unexpected"); } else { _setComment(item, &p); rc = indigoRenderToFile(item, outfilename); } } if (rc < 0) { printf("%s\n", indigoGetLastError()); indigoSetErrorHandler(onError, 0); continue; } indigoFree(item); indigoSetErrorHandler(onError, 0); printf("\n"); } if (writer > 0) { if (p.out_ext == OEXT_CML) indigoCmlFooter(writer); indigoFree(writer); } } indigoFree(reader); indigoFree(obj); return 0; }
void renderToFile (int obj, const char* outfile) { int out = indigoWriteFile(outfile); indigoRender(obj, out); indigoFree(out); }