001package algs55; // section 5.5 002import stdlib.*; 003import algs51.Alphabet; 004/* *********************************************************************** 005 * Compilation: javac Genome.java 006 * Execution: java Genome - < input.txt (compress) 007 * Execution: java Genome + < input.txt (expand) 008 * Dependencies: BinaryIn.java BinaryOut.java 009 * 010 * Compress or expand a genomic sequence using a 2-bit code. 011 * 012 * % more genomeTiny.txt 013 * ATAGATGCATAGCGCATAGCTAGATGTGCTAGC 014 * 015 * % java Genome - < genomeTiny.txt | java Genome + 016 * ATAGATGCATAGCGCATAGCTAGATGTGCTAGC 017 * 018 *************************************************************************/ 019 020public class Genome { 021 private static BinaryIn binaryIn; 022 private static BinaryOut binaryOut; 023 024 public static void compress() { 025 Alphabet DNA = new Alphabet("ACTG"); 026 String s = binaryIn.readString(); 027 int N = s.length(); 028 binaryOut.write(N); 029 030 // Write two-bit code for char. 031 for (int i = 0; i < N; i++) { 032 int d = DNA.toIndex(s.charAt(i)); 033 binaryOut.write(d, 2); 034 } 035 binaryOut.close(); 036 } 037 038 public static void expand() { 039 Alphabet DNA = new Alphabet("ACTG"); 040 int N = binaryIn.readInt(); 041 // Read two bits; write char. 042 for (int i = 0; i < N; i++) { 043 char c = binaryIn.readChar(2); 044 binaryOut.write(DNA.toChar(c), 8); 045 } 046 binaryOut.close(); 047 } 048 049 050 public static void main(String[] args) { 051 String txtFile = "data/genomeTiny.txt"; 052 String binFile = "/tmp/genomeTiny.bin"; 053 //args = new String[] { "+" }; binaryIn = new BinaryIn(binFile); binaryOut = new BinaryOut(); 054 args = new String[] { "-" }; binaryIn = new BinaryIn(txtFile); binaryOut = new BinaryOut(binFile); 055 if (args[0].equals("-")) compress(); 056 else if (args[0].equals("+")) expand(); 057 else throw new Error("Illegal command line argument"); 058 } 059 060}