Exemplo n.º 1
0
Arquivo: P3.java Projeto: nkibler7/P3
  /**
   * Main method to control data flow. This function takes the command line parameter as input and
   * uses it to read from the input file, executing and outputting commands along the way.
   *
   * @param args - the command line arguments
   */
  public static void main(String[] args) {

    // Check for proper usage
    if (args.length != 1) {
      System.out.println("Usage:");
      System.out.println("P3 COMMAND_FILE");
      System.exit(0);
    }

    String fileName = args[0];

    tree = new DNATree();
    dbm = new DatabaseManager();

    // Main command line reading
    try {

      // Attempt to open the input file into a buffered reader
      BufferedReader in = new BufferedReader(new FileReader(fileName));

      // Keep reading in commands until we reach the EOF
      String line;
      while ((line = in.readLine()) != null) {
        if (line.matches(INSERT_PATTERN)) {

          // Parse out the sequence id and length from the command line
          int begin = Math.max(line.indexOf("r"), line.indexOf("R")) + 2;
          int end =
              Math.max(
                      Math.max(
                          Math.max(line.lastIndexOf('A'), line.lastIndexOf('C')),
                          line.lastIndexOf('G')),
                      line.lastIndexOf('T'))
                  + 1;
          String sequence = line.substring(begin, end);
          sequence = sequence.trim();

          int length = Integer.parseInt(line.substring(end).trim());

          if (length <= 0) {
            System.out.println("Length less than zero.");
            continue;
          }

          // Get the next line for the sequence
          String entry = in.readLine();

          // Add to the dbm
          Handle handle = dbm.insert(entry, length);

          // Add to tree
          int result = tree.insert(sequence, handle);

          if (result < 0) {
            dbm.remove(handle);
            System.out.println("Sequence " + sequence + " already in tree.");
          } else {
            System.out.println("Sequence " + sequence + " inserted at level " + result + ".");
          }
          System.out.println();
        } else if (line.matches(REMOVE_PATTERN)) {

          // Parse out the sequence id from the command line
          int index = Math.max(line.indexOf("v"), line.indexOf("V")) + 2;
          String sequence = line.substring(index);
          sequence = sequence.trim();

          // Remove sequence id from tree
          Handle handle = tree.remove(sequence);

          // Remove sequence from dbm
          if (handle == null) {
            System.out.println("Sequence " + sequence + " not found in tree.");
            System.out.println();
          } else {
            dbm.remove(handle);
          }
        } else if (line.matches(PRINT_PATTERN)) {

          // Output the tree
          System.out.println(tree);

          // Output free blocks
          System.out.println(dbm);
          System.out.println();
        } else if (line.matches(SEARCH_PATTERN)) {

          // Parse out the sequence id from the command line
          int index = Math.max(line.indexOf("h"), line.indexOf("H")) + 1;
          String sequence = line.substring(index);
          sequence = sequence.trim();

          // Search the tree for handles
          String results = tree.search(sequence);
          Scanner scan = new Scanner(results);
          String output = scan.nextLine() + "\n";

          // Augment output with results from dbm
          String entry;
          int offset, length;
          Handle handle;

          while (scan.hasNextLine()) {
            entry = scan.nextLine();

            if (entry.matches(KEY_PATTERN)) {
              output += entry + "\n";
            } else if (entry.matches(HANDLE_PATTERN)) {

              // Parse out offset and length
              offset =
                  Integer.parseInt(entry.substring(entry.indexOf('[') + 1, entry.indexOf(',')));
              length =
                  Integer.parseInt(entry.substring(entry.indexOf(',') + 2, entry.indexOf(']')));

              // Create handle and use it to query database
              handle = new Handle(offset, length);
              output += "Sequence:\n" + dbm.getEntry(handle) + "\n";
            } else {
              continue;
            }
          }

          scan.close();

          System.out.println(output.substring(0, output.length() - 1));
          System.out.println();
        } else {
          continue;
        }
      }

      in.close();
    } catch (FileNotFoundException e) {
      System.out.println("The input file could not be found.");
      System.exit(0);
    } catch (IOException e) {
      System.out.println("Error reading from file.");
      System.exit(0);
    } catch (Exception e) {
      System.out.println("Incorrect file formatting.");
      e.printStackTrace();
      System.exit(0);
    }
  }