JAL-1619 refactored cDNA translation; modified codon comparison; tests
[jalview.git] / test / jalview / analysis / DnaAlignmentGenerator.java
1 package jalview.analysis;
2
3 import java.util.Random;
4
5 /**
6  * Generates a random Fasta format DNA alignment for given sequence length and
7  * count.
8  * 
9  * @author gmcarstairs
10  *
11  */
12 public class DnaAlignmentGenerator
13 {
14   private static final char GAP = '-';
15   
16   private static final char[] BASES = new char[]
17     { 'G', 'T', 'C', 'A', GAP };
18
19   private Random random;
20   
21   /**
22    * Given args for sequence length and count, output a DNA 'alignment' where
23    * each position is a random choice from 'GTCA-'.
24    * 
25    * @param args
26    *          the width (base count) and height (sequence count) to generate
27    *          plus an integer random seed value
28    */
29   public static void main(String[] args)
30   {
31     int width = Integer.parseInt(args[0]);
32     int height = Integer.parseInt(args[1]);
33     long randomSeed = Long.valueOf(args[2]);
34     new DnaAlignmentGenerator().generate(width, height, randomSeed);
35   }
36
37   /**
38    * Outputs a DNA 'alignment' of given width and height, where each position is
39    * a random choice from 'GTCA-'.
40    * 
41    * @param width
42    * @param height
43    * @param randomSeed
44    */
45   private void generate(int width, int height, long randomSeed)
46   {
47     random = new Random(randomSeed);
48     for (int seqno = 0; seqno < height; seqno++)
49     {
50       generateSequence(seqno + 1, width);
51     }
52   }
53
54   /**
55    * Outputs a DNA 'sequence' of given length, with some random gaps included.
56    * 
57    * @param seqno
58    * @param length
59    */
60   private void generateSequence(int seqno, int length)
61   {
62     System.out.println(">SEQ" + seqno);
63     StringBuilder seq = new StringBuilder(length);
64
65     /*
66      * Loop till we've output 'length' real bases (excluding gaps)
67      */
68     for (int count = 0 ; count < length ; ) {
69       char c = BASES[random.nextInt(Integer.MAX_VALUE) % 5];
70       seq.append(c);
71       if (c != GAP)
72       {
73         count++;
74       }
75     }
76     System.out.println(seq.toString());
77   }
78 }