/**
   * KEGG get names by cpd id.
   *
   * @param identifier the identifier
   * @return the string
   */
  public static String[] KEGGgetNameByCpd(String identifier) {
    String ret = "";

    try {
      KEGGLocator locator = new KEGGLocator();
      KEGGPortType serv = locator.getKEGGPort();
      // get molecule by accession ID
      String str = "cpd:" + identifier;

      ret = serv.bget(str);
    } catch (Exception e) {
      System.err.println(e.toString());
    }

    // System.out.println(ret);
    String names = ret.substring(ret.indexOf("NAME") + 12, ret.indexOf("FORMULA") - 1);
    // System.out.println(names);

    // find white spaces
    Pattern p = Pattern.compile("\\s+");
    Matcher m = p.matcher(names);
    String cleanedNames = m.replaceAll(" ");

    // System.out.println(cleanedNames);

    String[] namesArr = cleanedNames.split(";");

    for (int i = 0; i < namesArr.length; i++) {
      Pattern p1 = Pattern.compile("^\\s+");
      Matcher m1 = p1.matcher(namesArr[i]);
      namesArr[i] = m1.replaceAll("");
    }

    return namesArr;
  }
  /**
   * Gets the mol file from a specified KEGG ID.
   *
   * @param accessionID the accession id (e.g. C00509)
   * @return the string
   * @throws CDKException
   */
  public static IAtomContainer getMol(String accessionID, String keggPath, boolean getAll)
      throws CDKException {
    String ret = "";

    try {
      KEGGLocator locator = new KEGGLocator();
      KEGGPortType serv = locator.getKEGGPort();
      // get molecule by accession ID
      String str = "-f m cpd:" + accessionID;

      boolean webservice = false;
      String mol = "";

      try {
        // Open the file that is the first
        // command line parameter
        FileInputStream fstream = new FileInputStream(keggPath + accessionID + ".mol");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        // Read File Line By Line
        String line;
        while ((line = br.readLine()) != null) {
          // Print the content on the console
          mol += line + "\n";
        }
        // Close the input stream
        in.close();

      } catch (FileNotFoundException e) {
        webservice = true;
      }

      // if this file is not found use webservice...otherwise use local molfile
      if (webservice) ret = serv.bget(str);
      else ret = mol;

    } catch (Exception e) {
      System.err.println(e.toString());
    }

    MDLReader reader;
    List<IAtomContainer> containersList;

    reader = new MDLReader(new StringReader(ret));
    ChemFile chemFile = (ChemFile) reader.read((ChemObject) new ChemFile());
    containersList = ChemFileManipulator.getAllAtomContainers(chemFile);
    IAtomContainer molecule = containersList.get(0);

    if (getAll) return molecule;
    if (!MolecularFormulaTools.isBiologicalCompound(molecule)) molecule = null;

    return molecule;
  }
  /**
   * Gets the mol file from a specified KEGG ID.
   *
   * @param accessionID the accession id (e.g. C00509)
   * @return the string
   */
  public static String KEGGgetMol(String accessionID, String keggPath) {
    String ret = "";

    try {
      KEGGLocator locator = new KEGGLocator();
      KEGGPortType serv = locator.getKEGGPort();
      // get molecule by accession ID
      String str = "-f m cpd:" + accessionID;

      boolean webservice = false;
      String mol = "";

      try {
        // Open the file that is the first
        // command line parameter
        FileInputStream fstream = new FileInputStream(keggPath + accessionID + ".mol");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        // Read File Line By Line
        String line;
        while ((line = br.readLine()) != null) {
          // Print the content on the console
          mol += line + "\n";
        }
        // Close the input stream
        in.close();

      } catch (FileNotFoundException e) {
        webservice = true;
      }

      // if this file is not found use webservice...otherwise use local molfile
      if (webservice) ret = serv.bget(str);
      else ret = mol;

    } catch (Exception e) {
      System.err.println(e.toString());
    }

    return ret;
  }
  /**
   * KEGG by sum formula.
   *
   * @param sumFormula the sum formula
   * @return the vector< string>
   */
  public static Vector<String> KEGGbySumFormula(String sumFormula) {
    Vector<String> KEGGCandidates = new Vector<String>();

    try {
      KEGGLocator locator = new KEGGLocator();
      KEGGPortType serv = locator.getKEGGPort();

      String[] results = serv.search_compounds_by_composition(sumFormula);

      for (int i = 0; i < results.length; i++) {
        System.out.println(results[i]);
        KEGGCandidates.add(results[i]);
      }
    } catch (Exception e) {
      System.err.println(e.toString());
    }

    return KEGGCandidates;
  }
  /**
   * KEGG search by mass. TODO: Get candidates through webservice...bget
   *
   * @param mass the mass
   * @param error the error
   * @return the vector< string>
   */
  public static Vector<String> KEGGbyMass(double mass, double error) {
    Vector<String> KEGGCandidates = new Vector<String>();

    try {
      KEGGLocator locator = new KEGGLocator();
      KEGGPortType serv = locator.getKEGGPort();

      String[] results = serv.search_compounds_by_mass((float) mass, (float) error);

      for (int i = 0; i < results.length; i++) {
        System.out.println(results[i]);
        KEGGCandidates.add(results[i]);
      }
    } catch (Exception e) {
      // System.err.println(e.toString());
      e.printStackTrace();
    }

    return KEGGCandidates;
  }