예제 #1
0
  /**
   * Converts the fasta sequences into phylip format: <number of sequences> <length of aligned
   * sequences> <id - exactly ten-space character padded><sequence for id> ...
   *
   * @param fasta_aligned_sequences
   * @return the phylip formatted result
   */
  protected String fasta2phylip(String fasta_aligned_sequences) throws IOException {
    StringBuffer seq = new StringBuffer();
    int count = 0;
    int length = 0;
    boolean prev = false;
    BufferedReader rdr = new BufferedReader(new StringReader(fasta_aligned_sequences));
    String line;
    String cur_id = null;
    StringBuffer tmp = new StringBuffer();

    while ((line = rdr.readLine()) != null) {
      // System.err.println(cur_id+" "+line);
      if (line.startsWith(">")) {
        count++;
        if (prev) {
          seq.append(fasta_accession2phylip(cur_id) + tmp + "\n");
          cur_id = line.trim().substring(1);
          length = tmp.length();
          tmp = new StringBuffer();
        } else {
          prev = true;
          cur_id = line.trim().substring(1);
        }
      } else {
        tmp.append(line.trim());
      }
    }
    // dont forget the last sequence
    if (tmp.length() > 0) {
      seq.append(fasta_accession2phylip(cur_id) + tmp + "\n");
    }
    return " " + count + " " + length + "\n" + seq.toString();
  }