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 AlignmentGenerator
56 @BeforeClass(alwaysRun = true)
57 public void setUpJvOptionPane()
59 JvOptionPane.setInteractiveMode(false);
60 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
63 private static final char GAP = '-';
65 private static final char ZERO = '0';
67 private static final char[] NUCS = "GTCA".toCharArray();
69 private static final char[] PEPS = "MILVFYWHKRDEQNTCGASNP".toCharArray();
71 private static char[] BASES;
73 private Random random;
77 * Outputs a DNA 'alignment' where each position is a random choice from
82 public static void main(String[] args)
89 boolean nucleotide = args[0].toLowerCase().startsWith("n");
90 int width = Integer.parseInt(args[1]);
91 int height = Integer.parseInt(args[2]);
92 long randomSeed = Long.valueOf(args[3]);
93 int gapPercentage = Integer.valueOf(args[4]);
94 int changePercentage = Integer.valueOf(args[5]);
95 AlignmentI al = new AlignmentGenerator(nucleotide).generate(width,
97 randomSeed, gapPercentage, changePercentage);
99 System.out.println("; " + height + " sequences of " + width
100 + " bases with " + gapPercentage + "% gaps and "
101 + changePercentage + "% mutations (random seed = " + randomSeed
103 System.out.println(new FastaFile().print(al.getSequencesArray(), true));
107 * Print parameter help.
109 private static void usage()
111 System.out.println("Usage:");
112 System.out.println("arg0: n (for nucleotide) or p (for peptide)");
113 System.out.println("arg1: number of (non-gap) bases per sequence");
114 System.out.println("arg2: number sequences");
116 .println("arg3: an integer as random seed (same seed = same results)");
117 System.out.println("arg4: percentage of gaps to (randomly) generate");
119 .println("arg5: percentage of 'mutations' to (randomly) generate");
120 System.out.println("Example: AlignmentGenerator n 12 15 387 10 5");
122 .println("- 15 nucleotide sequences of 12 bases each, approx 10% gaps and 5% mutations, random seed = 387");
127 * Constructor that sets nucleotide or peptide symbol set
129 public AlignmentGenerator(boolean nuc)
131 BASES = nuc ? NUCS : PEPS;
135 * Outputs a DNA 'alignment' of given width and height, where each position is
136 * a random choice from 'GTCA-'.
141 * @param changePercentage
142 * @param gapPercentage
144 public AlignmentI generate(int width, int height, long randomSeed,
145 int gapPercentage, int changePercentage)
147 SequenceI[] seqs = new SequenceI[height];
148 random = new Random(randomSeed);
149 seqs[0] = generateSequence(1, width, gapPercentage);
150 for (int seqno = 1; seqno < height; seqno++)
152 seqs[seqno] = generateAnotherSequence(seqs[0].getSequence(),
153 seqno + 1, width, changePercentage);
155 AlignmentI al = new Alignment(seqs);
160 * Outputs a DNA 'sequence' of given length, with some random gaps included.
164 * @param gapPercentage
166 private SequenceI generateSequence(int seqno, int length,
169 StringBuilder seq = new StringBuilder(length);
172 * Loop till we've added 'length' bases (excluding gaps)
174 for (int count = 0; count < length;)
176 boolean addGap = random.nextInt(100) < gapPercentage;
177 char c = addGap ? GAP : BASES[random.nextInt(Integer.MAX_VALUE)
185 final String seqName = "SEQ" + seqno;
186 final String seqString = seq.toString();
187 SequenceI sq = new Sequence(seqName, seqString);
188 sq.createDatasetSequence();
193 * Generate a sequence approximately aligned to the first one.
199 * @param changePercentage
202 private SequenceI generateAnotherSequence(char[] ds, int seqno,
203 int width, int changePercentage)
205 int length = ds.length;
206 char[] seq = new char[length];
207 Arrays.fill(seq, ZERO);
208 int gapsWanted = length - width;
212 * First 'randomly' mimic gaps in model sequence.
214 for (int pos = 0; pos < length; pos++)
219 * Add a gap at the same position with changePercentage likelihood
221 seq[pos] = randomCharacter(GAP, changePercentage);
230 * Next scatter any remaining gaps (if any) at random. This gives an even
233 while (gapsAdded < gapsWanted)
235 boolean added = false;
238 int pos = random.nextInt(length);
249 * Finally fill in the rest with randomly mutated bases.
251 for (int pos = 0; pos < length; pos++)
253 if (seq[pos] == ZERO)
255 char c = randomCharacter(ds[pos], changePercentage);
259 final String seqName = "SEQ" + seqno;
260 final String seqString = new String(seq);
261 SequenceI sq = new Sequence(seqName, seqString);
262 sq.createDatasetSequence();
267 * Returns a random character that is changePercentage% likely to match the
268 * given type (as base or gap).
270 * @param changePercentage
275 private char randomCharacter(char c, int changePercentage)
277 final boolean mutation = random.nextInt(100) < changePercentage;
287 newchar = BASES[random.nextInt(Integer.MAX_VALUE) % BASES.length];