4215df75c1b956b0554392282c815d614f6365ac
[jalview.git] / test / jalview / analysis / DnaAlignmentGenerator.java
1 package jalview.analysis;
2
3 import jalview.datamodel.Alignment;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.Sequence;
6 import jalview.datamodel.SequenceI;
7 import jalview.io.FastaFile;
8
9 import java.util.Random;
10
11 /**
12  * Generates, and outputs in Fasta format, a random DNA alignment for given
13  * sequence length and count. Will regenerate the same alignment each time if
14  * the same random seed is used (so may be used for reproducible unit tests).
15  * The alignment is not padded with gaps.
16  * 
17  * Arguments:
18  * <ul>
19  * <li>length (number of bases in each sequence)</li>
20  * <li>height (number of sequences)</li>
21  * <li>a whole number random seed</li>
22  * </ul>
23  * 
24  * @author gmcarstairs
25  *
26  */
27 public class DnaAlignmentGenerator
28 {
29   private static final char GAP = '-';
30   
31   private static final char[] BASES = new char[]
32     { 'G', 'T', 'C', 'A', GAP };
33
34   private Random random;
35   
36   /**
37    * Given args for sequence length and count, output a DNA 'alignment' where
38    * each position is a random choice from 'GTCA-'.
39    * 
40    * @param args
41    *          the width (base count) and height (sequence count) to generate
42    *          plus an integer random seed value
43    */
44   public static void main(String[] args)
45   {
46     int width = Integer.parseInt(args[0]);
47     int height = Integer.parseInt(args[1]);
48     long randomSeed = Long.valueOf(args[2]);
49     AlignmentI al = new DnaAlignmentGenerator().generate(width, height,
50             randomSeed);
51     System.out.println(new FastaFile().print(al.getSequencesArray()));
52   }
53
54   /**
55    * Default constructor
56    */
57   public DnaAlignmentGenerator()
58   {
59
60   }
61
62   /**
63    * Outputs a DNA 'alignment' of given width and height, where each position is
64    * a random choice from 'GTCA-'.
65    * 
66    * @param width
67    * @param height
68    * @param randomSeed
69    */
70   public AlignmentI generate(int width, int height, long randomSeed)
71   {
72     SequenceI[] seqs = new SequenceI[height];
73     random = new Random(randomSeed);
74     for (int seqno = 0; seqno < height; seqno++)
75     {
76       seqs[seqno] = generateSequence(seqno + 1, width);
77     }
78     AlignmentI al = new Alignment(seqs);
79     return al;
80   }
81
82   /**
83    * Outputs a DNA 'sequence' of given length, with some random gaps included.
84    * 
85    * @param seqno
86    * @param length
87    */
88   private SequenceI generateSequence(int seqno, int length)
89   {
90     StringBuilder seq = new StringBuilder(length);
91
92     /*
93      * Loop till we've added 'length' bases (excluding gaps)
94      */
95     for (int count = 0 ; count < length ; ) {
96       char c = BASES[random.nextInt(Integer.MAX_VALUE) % 5];
97       seq.append(c);
98       if (c != GAP)
99       {
100         count++;
101       }
102     }
103     final String seqName = ">SEQ" + seqno;
104     final String seqString = seq.toString();
105     SequenceI sq = new Sequence(seqName, seqString);
106     sq.createDatasetSequence();
107     return sq;
108   }
109 }