2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceI;
27 import jalview.gui.JvOptionPane;
28 import jalview.io.FastaFile;
30 import java.util.Arrays;
31 import java.util.Random;
33 import org.testng.annotations.BeforeClass;
36 * Generates, and outputs in Fasta format, a random DNA alignment for given
37 * sequence length and count. Will regenerate the same alignment each time if
38 * the same random seed is used (so may be used for reproducible unit tests).
39 * Not guaranteed to reproduce the same results between versions, as the rules
40 * may get tweaked to produce more 'realistic' results.
44 * <li>length (number of bases in each sequence)</li>
45 * <li>height (number of sequences)</li>
46 * <li>a whole number random seed</li>
47 * <li>percentage of gaps to include (0-100)</li>
48 * <li>percentage chance of variation of each position (0-100)</li>
54 public class DnaAlignmentGenerator
57 @BeforeClass(alwaysRun = true)
58 public void setUpJvOptionPane()
60 JvOptionPane.setInteractiveMode(false);
61 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
64 private static final char GAP = '-';
66 private static final char ZERO = '0';
68 private static final char[] BASES = new char[] { 'G', 'T', 'C', 'A' };
70 private Random random;
74 * Outputs a DNA 'alignment' where each position is a random choice from
79 public static void main(String[] args)
86 int width = Integer.parseInt(args[0]);
87 int height = Integer.parseInt(args[1]);
88 long randomSeed = Long.valueOf(args[2]);
89 int gapPercentage = Integer.valueOf(args[3]);
90 int changePercentage = Integer.valueOf(args[4]);
91 AlignmentI al = new DnaAlignmentGenerator().generate(width, height,
92 randomSeed, gapPercentage, changePercentage);
94 System.out.println("; " + height + " sequences of " + width
95 + " bases with " + gapPercentage + "% gaps and "
96 + changePercentage + "% mutations (random seed = " + randomSeed
98 System.out.println(new FastaFile().print(al.getSequencesArray(), true));
102 * Print parameter help.
104 private static void usage()
106 System.out.println("Usage:");
107 System.out.println("arg0: number of (non-gap) bases per sequence");
108 System.out.println("arg1: number sequences");
110 .println("arg2: an integer as random seed (same seed = same results)");
111 System.out.println("arg3: percentage of gaps to (randomly) generate");
113 .println("arg4: percentage of 'mutations' to (randomly) generate");
114 System.out.println("Example: DnaAlignmentGenerator 12 15 387 10 5");
116 .println("- 15 sequences of 12 bases each, approx 10% gaps and 5% mutations, random seed = 387");
121 * Default constructor
123 public DnaAlignmentGenerator()
129 * Outputs a DNA 'alignment' of given width and height, where each position is
130 * a random choice from 'GTCA-'.
135 * @param changePercentage
136 * @param gapPercentage
138 public AlignmentI generate(int width, int height, long randomSeed,
139 int gapPercentage, int changePercentage)
141 SequenceI[] seqs = new SequenceI[height];
142 random = new Random(randomSeed);
143 seqs[0] = generateSequence(1, width, gapPercentage);
144 for (int seqno = 1; seqno < height; seqno++)
146 seqs[seqno] = generateAnotherSequence(seqs[0].getSequence(),
147 seqno + 1, width, changePercentage);
149 AlignmentI al = new Alignment(seqs);
154 * Outputs a DNA 'sequence' of given length, with some random gaps included.
158 * @param gapPercentage
160 private SequenceI generateSequence(int seqno, int length,
163 StringBuilder seq = new StringBuilder(length);
166 * Loop till we've added 'length' bases (excluding gaps)
168 for (int count = 0; count < length;)
170 boolean addGap = random.nextInt(100) < gapPercentage;
171 char c = addGap ? GAP : BASES[random.nextInt(Integer.MAX_VALUE) % 4];
178 final String seqName = "SEQ" + seqno;
179 final String seqString = seq.toString();
180 SequenceI sq = new Sequence(seqName, seqString);
181 sq.createDatasetSequence();
186 * Generate a sequence approximately aligned to the first one.
192 * @param changePercentage
195 private SequenceI generateAnotherSequence(char[] ds, int seqno,
196 int width, int changePercentage)
198 int length = ds.length;
199 char[] seq = new char[length];
200 Arrays.fill(seq, ZERO);
201 int gapsWanted = length - width;
205 * First 'randomly' mimic gaps in model sequence.
207 for (int pos = 0; pos < length; pos++)
212 * Add a gap at the same position with changePercentage likelihood
214 seq[pos] = randomCharacter(GAP, changePercentage);
223 * Next scatter any remaining gaps (if any) at random. This gives an even
226 while (gapsAdded < gapsWanted)
228 boolean added = false;
231 int pos = random.nextInt(length);
242 * Finally fill in the rest with randomly mutated bases.
244 for (int pos = 0; pos < length; pos++)
246 if (seq[pos] == ZERO)
248 char c = randomCharacter(ds[pos], changePercentage);
252 final String seqName = "SEQ" + seqno;
253 final String seqString = new String(seq);
254 SequenceI sq = new Sequence(seqName, seqString);
255 sq.createDatasetSequence();
260 * Returns a random character that is changePercentage% likely to match the
261 * given type (as base or gap).
263 * @param changePercentage
268 private char randomCharacter(char c, int changePercentage)
270 final boolean mutation = random.nextInt(100) < changePercentage;
280 newchar = BASES[random.nextInt(Integer.MAX_VALUE) % 4];