package jalview.analysis; import java.util.Random; /** * Generates a random Fasta format DNA alignment for given sequence length and * count. * * @author gmcarstairs * */ public class DnaAlignmentGenerator { private static final char GAP = '-'; private static final char[] BASES = new char[] { 'G', 'T', 'C', 'A', GAP }; private Random random; /** * Given args for sequence length and count, output a DNA 'alignment' where * each position is a random choice from 'GTCA-'. * * @param args * the width (base count) and height (sequence count) to generate * plus an integer random seed value */ public static void main(String[] args) { int width = Integer.parseInt(args[0]); int height = Integer.parseInt(args[1]); long randomSeed = Long.valueOf(args[2]); new DnaAlignmentGenerator().generate(width, height, randomSeed); } /** * Outputs a DNA 'alignment' of given width and height, where each position is * a random choice from 'GTCA-'. * * @param width * @param height * @param randomSeed */ private void generate(int width, int height, long randomSeed) { random = new Random(randomSeed); for (int seqno = 0; seqno < height; seqno++) { generateSequence(seqno + 1, width); } } /** * Outputs a DNA 'sequence' of given length, with some random gaps included. * * @param seqno * @param length */ private void generateSequence(int seqno, int length) { System.out.println(">SEQ" + seqno); StringBuilder seq = new StringBuilder(length); /* * Loop till we've output 'length' real bases (excluding gaps) */ for (int count = 0 ; count < length ; ) { char c = BASES[random.nextInt(Integer.MAX_VALUE) % 5]; seq.append(c); if (c != GAP) { count++; } } System.out.println(seq.toString()); } }