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:
+ * <ul>
+ * <li>length (number of bases in each sequence)</li>
+ * <li>height (number of sequences)</li>
+ * <li>a whole number random seed</li>
+ * </ul>
*
* @author gmcarstairs
*
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()
+ {
+
}
/**
* @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;
}
/**
* @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];
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;
}
}