コード例 #1
0
  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();
    }
  }
コード例 #2
0
  /**
   * 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();
    }
  }