From f92f257fedde23c96bef9efc48c9df85f3b9f9d9 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 20 Jan 2015 09:11:43 +0000 Subject: [PATCH] JAL-1619 tweaked for better reusability --- test/jalview/analysis/DnaAlignmentGenerator.java | 49 ++++++++++++++++++---- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/test/jalview/analysis/DnaAlignmentGenerator.java b/test/jalview/analysis/DnaAlignmentGenerator.java index 59a08be..4215df7 100644 --- a/test/jalview/analysis/DnaAlignmentGenerator.java +++ b/test/jalview/analysis/DnaAlignmentGenerator.java @@ -1,10 +1,25 @@ package jalview.analysis; +import jalview.datamodel.Alignment; +import jalview.datamodel.AlignmentI; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceI; +import jalview.io.FastaFile; + import java.util.Random; /** - * Generates a random Fasta format DNA alignment for given sequence length and - * count. + * Generates, and outputs in Fasta format, a random DNA alignment for given + * sequence length and count. Will regenerate the same alignment each time if + * the same random seed is used (so may be used for reproducible unit tests). + * The alignment is not padded with gaps. + * + * Arguments: + * * * @author gmcarstairs * @@ -31,7 +46,17 @@ public class DnaAlignmentGenerator int width = Integer.parseInt(args[0]); int height = Integer.parseInt(args[1]); long randomSeed = Long.valueOf(args[2]); - new DnaAlignmentGenerator().generate(width, height, randomSeed); + AlignmentI al = new DnaAlignmentGenerator().generate(width, height, + randomSeed); + System.out.println(new FastaFile().print(al.getSequencesArray())); + } + + /** + * Default constructor + */ + public DnaAlignmentGenerator() + { + } /** @@ -42,13 +67,16 @@ public class DnaAlignmentGenerator * @param height * @param randomSeed */ - private void generate(int width, int height, long randomSeed) + public AlignmentI generate(int width, int height, long randomSeed) { + SequenceI[] seqs = new SequenceI[height]; random = new Random(randomSeed); for (int seqno = 0; seqno < height; seqno++) { - generateSequence(seqno + 1, width); + seqs[seqno] = generateSequence(seqno + 1, width); } + AlignmentI al = new Alignment(seqs); + return al; } /** @@ -57,13 +85,12 @@ public class DnaAlignmentGenerator * @param seqno * @param length */ - private void generateSequence(int seqno, int length) + private SequenceI 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) + * Loop till we've added 'length' bases (excluding gaps) */ for (int count = 0 ; count < length ; ) { char c = BASES[random.nextInt(Integer.MAX_VALUE) % 5]; @@ -73,6 +100,10 @@ public class DnaAlignmentGenerator count++; } } - System.out.println(seq.toString()); + final String seqName = ">SEQ" + seqno; + final String seqString = seq.toString(); + SequenceI sq = new Sequence(seqName, seqString); + sq.createDatasetSequence(); + return sq; } } -- 1.7.10.2