/**
   * @param graphDBNameOld
   * @param graphDBName
   * @throws IOException
   * @throws ParseException
   */
  public static void pruningUnconnected(String graphDBNameOld, String graphDBName)
      throws IOException, ParseException {
    System.out.println("In Pruning Unconnected");
    SmilesParser sParser = new SmilesParser();
    // InputStream file = new FileInputStream(graphDBNameOld);
    // Graph[] graphs = sParser.parse(file, MyFactory.getGraphFactory());
    BufferedReader bin = new BufferedReader(new FileReader(graphDBNameOld));

    LinkedList<Graph> graphlists = new LinkedList<Graph>();
    String line;
    while ((line = bin.readLine()) != null) {
      int pos = line.indexOf(" => ");
      try {
        graphlists.add(
            sParser.parse(
                line.substring(pos + " => ".length()),
                line.substring(0, pos),
                MyFactory.getGraphFactory()));
        System.out.println("GraphID: " + line.substring(0, pos));
      } catch (ParseException e) {
        System.out.println(line);
        e.printStackTrace();
      }
    }

    Graph[] graphs = new Graph[graphlists.size()];
    graphlists.toArray(graphs);
    System.out.println("After Parsing all Graphs");
    int connect = 0;
    BufferedWriter indexWriter = new BufferedWriter(new FileWriter(graphDBName));
    for (int i = 0; i < graphs.length; i++) {
      if (GraphConnectivityTester.isConnected(graphs[i])) {
        StringBuffer buf = new StringBuffer();
        buf.append(connect);
        buf.append(" => ");
        buf.append(sParser.serialize(graphs[i]));
        buf.append("\n");
        indexWriter.write(buf.toString());
        connect++;
      }
    }
    indexWriter.close();
    // Write the meta information of the smile data file:
    BufferedWriter metaWriter = new BufferedWriter(new FileWriter(graphDBName + "_Meta"));
    // 1. Processing Date
    SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
    Date date = new Date();
    metaWriter.write(bartDateFormat.format(date));
    metaWriter.newLine();
    // 2. Number of graphs in this file
    metaWriter.write("Number of Graphs:" + connect);
    // Close meta data file
    try {
      metaWriter.close();
      bin.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("TOTAL Graph: " + graphs.length + "Connected : " + connect);
  }
  public static void changeFormatSimple(
      String SDFFileName, String SmilesFileName, GraphFactory graphFactory) throws IOException {
    // Open InputFile
    BufferedReader fileBufReader = new BufferedReader(new FileReader(SDFFileName));
    // Open OutputFile
    BufferedWriter outputWriter = new BufferedWriter(new FileWriter(SmilesFileName));

    SDFParserModified SDFParser = MyFactory.getSDFParserM();
    SmilesParser smilesParser = MyFactory.getSmilesParser();
    Graph oneGraph = null;

    double aveEdgeCount = 0;
    double aveNodeCount = 0;

    int index = 0;
    String spliter = " => ";
    while (fileBufReader.ready()) {
      try {
        oneGraph = SDFParser.parse(fileBufReader, graphFactory);
      } catch (ParseException e) {
        System.out.println("skip one graph");
        while (fileBufReader.ready()) {
          String aLine = fileBufReader.readLine();
          if (aLine.equals("$$$$")) break;
        }
        continue;
      }

      if (GraphConnectivityTester.isConnected(oneGraph)) {
        aveEdgeCount =
            (aveEdgeCount) * (index / ((double) index + 1))
                + oneGraph.getEdgeCount() / ((double) index + 1);
        aveNodeCount =
            (aveNodeCount) * (index / ((double) index + 1))
                + oneGraph.getNodeCount() / ((double) index + 1);
        if (index > 0) outputWriter.newLine();
        outputWriter.write(index + spliter + smilesParser.serialize(oneGraph));
        index++;
      }
    }
    // Close input File
    try {
      fileBufReader.close();
      outputWriter.flush();
      outputWriter.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println(
        "In processor: changeFormat, "
            + index
            + " number of graphs"
            + "has been formated into Smiles Format");
    // Intrigue java garbage collector
    Runtime r = Runtime.getRuntime();
    r.gc();
    // Write the meta information of the smile data file:
    BufferedWriter metaWriter = new BufferedWriter(new FileWriter(SmilesFileName + "_Meta"));
    // 1. Processing Date
    SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
    Date date = new Date();
    metaWriter.write(bartDateFormat.format(date));
    metaWriter.newLine();
    // 2. Number of graphs in this file
    metaWriter.write("Number of Graphs:" + index);
    metaWriter.write("Ave EdgeCount: " + aveEdgeCount + " Ave NodeCount: " + aveNodeCount);
    // Close meta data file
    try {
      metaWriter.flush();
      metaWriter.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  /**
   * Given a file of graphs in SDF format Populate graphs in this file into A new graph file [with
   * ID = > in Smiles format] Pay attention: all graphs have to be connected
   *
   * @param SDFFileName
   * @param graphFactory
   * @throws IOException
   * @throws DataAccessException
   * @throws IOException
   */
  public static void changeFormat(
      String SDFFileName, String SmilesFileName, GraphFactory graphFactory) throws IOException {
    // Open InputFile
    BufferedReader fileBufReader = new BufferedReader(new FileReader(SDFFileName));
    // Open OutputFile
    BufferedWriter outputWriter = new BufferedWriter(new FileWriter(SmilesFileName));

    SDFParserModified SDFParser = MyFactory.getSDFParserM();
    SmilesParser smilesParser = MyFactory.getSmilesParser();
    String lineString;
    StringBuffer graphBuffer = new StringBuffer(1024);
    Graph oneGraph = null;

    int index = 0;
    String spliter = " => ";
    boolean attStatus = false;
    while ((lineString = fileBufReader.readLine()) != null) {
      if (lineString.startsWith(">")) {
        // enter the attribute status
        attStatus = true;
      }
      if (attStatus) {
        // One graph Has been read
        if (attStatus && lineString.equals("$$$$")) {
          // Transform this graphString to a realGraph and save into
          // database
          try {
            oneGraph = SDFParser.parse(graphBuffer.toString(), graphFactory);
          } catch (ParseException e) {
            e.printStackTrace();
          }

          if (GraphConnectivityTester.isConnected(oneGraph)) {
            if (index > 0) outputWriter.newLine();
            outputWriter.write(index + spliter + smilesParser.serialize(oneGraph));
            index++;
            // System.out.println(index + ":" + oneGraph.getName());
          }

          graphBuffer.delete(0, graphBuffer.length());
          attStatus = false;
        } else continue;
      } else graphBuffer = graphBuffer.append(lineString + '\n');
    }
    // Close input File
    try {
      fileBufReader.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    // Close out File
    try {
      outputWriter.newLine();
      outputWriter.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println(
        "In processor: changeFormat, "
            + index
            + " number of graphs"
            + "has been formated into Smiles Format");
    // Intrigue java garbage collector
    Runtime r = Runtime.getRuntime();
    r.gc();
    // Write the meta information of the smile data file:
    BufferedWriter metaWriter = new BufferedWriter(new FileWriter(SmilesFileName + "_Meta"));
    // 1. Processing Date
    SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
    Date date = new Date();
    metaWriter.write(bartDateFormat.format(date));
    metaWriter.newLine();
    // 2. Number of graphs in this file
    metaWriter.write("Number of Graphs:" + index);
    // Close meta data file
    try {
      metaWriter.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }