1 package jalview.analysis;
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;
9 import java.util.Random;
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.
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>
27 public class DnaAlignmentGenerator
29 private static final char GAP = '-';
31 private static final char[] BASES = new char[]
32 { 'G', 'T', 'C', 'A', GAP };
34 private Random random;
37 * Given args for sequence length and count, output a DNA 'alignment' where
38 * each position is a random choice from 'GTCA-'.
41 * the width (base count) and height (sequence count) to generate
42 * plus an integer random seed value
44 public static void main(String[] args)
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,
51 System.out.println(new FastaFile().print(al.getSequencesArray()));
57 public DnaAlignmentGenerator()
63 * Outputs a DNA 'alignment' of given width and height, where each position is
64 * a random choice from 'GTCA-'.
70 public AlignmentI generate(int width, int height, long randomSeed)
72 SequenceI[] seqs = new SequenceI[height];
73 random = new Random(randomSeed);
74 for (int seqno = 0; seqno < height; seqno++)
76 seqs[seqno] = generateSequence(seqno + 1, width);
78 AlignmentI al = new Alignment(seqs);
83 * Outputs a DNA 'sequence' of given length, with some random gaps included.
88 private SequenceI generateSequence(int seqno, int length)
90 StringBuilder seq = new StringBuilder(length);
93 * Loop till we've added 'length' bases (excluding gaps)
95 for (int count = 0 ; count < length ; ) {
96 char c = BASES[random.nextInt(Integer.MAX_VALUE) % 5];
103 final String seqName = ">SEQ" + seqno;
104 final String seqString = seq.toString();
105 SequenceI sq = new Sequence(seqName, seqString);
106 sq.createDatasetSequence();