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.Arrays;
10 import java.util.Random;
13 * Generates, and outputs in Fasta format, a random DNA alignment for given
14 * sequence length and count. Will regenerate the same alignment each time if
15 * the same random seed is used (so may be used for reproducible unit tests).
16 * Not guaranteed to reproduce the same results between versions, as the rules
17 * may get tweaked to produce more 'realistic' results.
21 * <li>length (number of bases in each sequence)</li>
22 * <li>height (number of sequences)</li>
23 * <li>a whole number random seed</li>
24 * <li>percentage of gaps to include (0-100)</li>
25 * <li>percentage chance of variation of each position (0-100)</li>
31 public class DnaAlignmentGenerator
33 private static final char GAP = '-';
35 private static final char ZERO = '0';
37 private static final char[] BASES = new char[] { 'G', 'T', 'C', 'A' };
39 private Random random;
42 * Outputs a DNA 'alignment' where each position is a random choice from
47 public static void main(String[] args)
54 int width = Integer.parseInt(args[0]);
55 int height = Integer.parseInt(args[1]);
56 long randomSeed = Long.valueOf(args[2]);
57 int gapPercentage = Integer.valueOf(args[3]);
58 int changePercentage = Integer.valueOf(args[4]);
59 AlignmentI al = new DnaAlignmentGenerator().generate(width, height,
60 randomSeed, gapPercentage, changePercentage);
62 System.out.println("; " + height + " sequences of " + width
63 + " bases with " + gapPercentage + "% gaps and "
64 + changePercentage + "% mutations (random seed = " + randomSeed
66 System.out.println(new FastaFile().print(al.getSequencesArray()));
70 * Print parameter help.
72 private static void usage()
74 System.out.println("Usage:");
75 System.out.println("arg0: number of (non-gap) bases per sequence");
76 System.out.println("arg1: number sequences");
78 .println("arg2: an integer as random seed (same seed = same results)");
79 System.out.println("arg3: percentage of gaps to (randomly) generate");
81 .println("arg4: percentage of 'mutations' to (randomly) generate");
82 System.out.println("Example: DnaAlignmentGenerator 12 15 387 10 5");
84 .println("- 15 sequences of 12 bases each, approx 10% gaps and 5% mutations, random seed = 387");
91 public DnaAlignmentGenerator()
97 * Outputs a DNA 'alignment' of given width and height, where each position is
98 * a random choice from 'GTCA-'.
103 * @param changePercentage
104 * @param gapPercentage
106 public AlignmentI generate(int width, int height, long randomSeed,
107 int gapPercentage, int changePercentage)
109 SequenceI[] seqs = new SequenceI[height];
110 random = new Random(randomSeed);
111 seqs[0] = generateSequence(1, width, gapPercentage);
112 for (int seqno = 1; seqno < height; seqno++)
114 seqs[seqno] = generateAnotherSequence(seqs[0].getSequence(),
115 seqno + 1, width, changePercentage);
117 AlignmentI al = new Alignment(seqs);
122 * Outputs a DNA 'sequence' of given length, with some random gaps included.
126 * @param gapPercentage
128 private SequenceI generateSequence(int seqno, int length,
131 StringBuilder seq = new StringBuilder(length);
134 * Loop till we've added 'length' bases (excluding gaps)
136 for (int count = 0; count < length;)
138 boolean addGap = random.nextInt(100) < gapPercentage;
139 char c = addGap ? GAP : BASES[random.nextInt(Integer.MAX_VALUE) % 4];
146 final String seqName = "SEQ" + seqno;
147 final String seqString = seq.toString();
148 SequenceI sq = new Sequence(seqName, seqString);
149 sq.createDatasetSequence();
154 * Generate a sequence approximately aligned to the first one.
160 * @param changePercentage
163 private SequenceI generateAnotherSequence(char[] ds, int seqno,
164 int width, int changePercentage)
166 int length = ds.length;
167 char[] seq = new char[length];
168 Arrays.fill(seq, ZERO);
169 int gapsWanted = length - width;
173 * First 'randomly' mimic gaps in model sequence.
175 for (int pos = 0; pos < length; pos++)
180 * Add a gap at the same position with changePercentage likelihood
182 seq[pos] = randomCharacter(GAP, changePercentage);
191 * Next scatter any remaining gaps (if any) at random. This gives an even
194 while (gapsAdded < gapsWanted)
196 boolean added = false;
199 int pos = random.nextInt(length);
210 * Finally fill in the rest with randomly mutated bases.
212 for (int pos = 0; pos < length; pos++)
214 if (seq[pos] == ZERO)
216 char c = randomCharacter(ds[pos], changePercentage);
220 final String seqName = "SEQ" + seqno;
221 final String seqString = new String(seq);
222 SequenceI sq = new Sequence(seqName, seqString);
223 sq.createDatasetSequence();
228 * Returns a random character that is changePercentage% likely to match the
229 * given type (as base or gap).
231 * @param changePercentage
236 private char randomCharacter(char c, int changePercentage)
238 final boolean mutation = random.nextInt(100) < changePercentage;
248 newchar = BASES[random.nextInt(Integer.MAX_VALUE) % 4];