JAL-1619 refactored cDNA translation; modified codon comparison; tests
[jalview.git] / test / jalview / analysis / DnaAlignmentGenerator.java
diff --git a/test/jalview/analysis/DnaAlignmentGenerator.java b/test/jalview/analysis/DnaAlignmentGenerator.java
new file mode 100644 (file)
index 0000000..59a08be
--- /dev/null
@@ -0,0 +1,78 @@
+package jalview.analysis;
+
+import java.util.Random;
+
+/**
+ * Generates a random Fasta format DNA alignment for given sequence length and
+ * count.
+ * 
+ * @author gmcarstairs
+ *
+ */
+public class DnaAlignmentGenerator
+{
+  private static final char GAP = '-';
+  
+  private static final char[] BASES = new char[]
+    { 'G', 'T', 'C', 'A', GAP };
+
+  private Random random;
+  
+  /**
+   * Given args for sequence length and count, output a DNA 'alignment' where
+   * each position is a random choice from 'GTCA-'.
+   * 
+   * @param args
+   *          the width (base count) and height (sequence count) to generate
+   *          plus an integer random seed value
+   */
+  public static void main(String[] args)
+  {
+    int width = Integer.parseInt(args[0]);
+    int height = Integer.parseInt(args[1]);
+    long randomSeed = Long.valueOf(args[2]);
+    new DnaAlignmentGenerator().generate(width, height, randomSeed);
+  }
+
+  /**
+   * Outputs a DNA 'alignment' of given width and height, where each position is
+   * a random choice from 'GTCA-'.
+   * 
+   * @param width
+   * @param height
+   * @param randomSeed
+   */
+  private void generate(int width, int height, long randomSeed)
+  {
+    random = new Random(randomSeed);
+    for (int seqno = 0; seqno < height; seqno++)
+    {
+      generateSequence(seqno + 1, width);
+    }
+  }
+
+  /**
+   * Outputs a DNA 'sequence' of given length, with some random gaps included.
+   * 
+   * @param seqno
+   * @param length
+   */
+  private void generateSequence(int seqno, int length)
+  {
+    System.out.println(">SEQ" + seqno);
+    StringBuilder seq = new StringBuilder(length);
+
+    /*
+     * Loop till we've output 'length' real bases (excluding gaps)
+     */
+    for (int count = 0 ; count < length ; ) {
+      char c = BASES[random.nextInt(Integer.MAX_VALUE) % 5];
+      seq.append(c);
+      if (c != GAP)
+      {
+        count++;
+      }
+    }
+    System.out.println(seq.toString());
+  }
+}