Exemplo n.º 1
0
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;
};
Exemplo n.º 2
0
Arquivo: main.c Projeto: Rillke/indigo
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;
}