Merge branch 'develop' into bug/JAL-2255_seq-fetcher-broken-on-linux
[jalview.git] / test / jalview / analysis / AlignmentGenerator.java
@@ -24,11 +24,14 @@ import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
+import jalview.gui.JvOptionPane;
 import jalview.io.FastaFile;
 
 import java.util.Arrays;
 import java.util.Random;
 
+import org.testng.annotations.BeforeClass;
+
 /**
  * Generates, and outputs in Fasta format, a random DNA alignment for given
  * sequence length and count. Will regenerate the same alignment each time if
@@ -48,16 +51,28 @@ import java.util.Random;
  * @author gmcarstairs
  *
  */
-public class DnaAlignmentGenerator
+public class AlignmentGenerator
 {
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
   private static final char GAP = '-';
 
   private static final char ZERO = '0';
 
-  private static final char[] BASES = new char[] { 'G', 'T', 'C', 'A' };
+  private static final char[] NUCS = "GTCA".toCharArray();
+
+  private static final char[] PEPS = "MILVFYWHKRDEQNTCGASNP".toCharArray();
+
+  private static char[] BASES;
 
   private Random random;
 
+
   /**
    * Outputs a DNA 'alignment' where each position is a random choice from
    * 'GTCA-'.
@@ -66,24 +81,26 @@ public class DnaAlignmentGenerator
    */
   public static void main(String[] args)
   {
-    if (args.length != 5)
+    if (args.length != 6)
     {
       usage();
       return;
     }
-    int width = Integer.parseInt(args[0]);
-    int height = Integer.parseInt(args[1]);
-    long randomSeed = Long.valueOf(args[2]);
-    int gapPercentage = Integer.valueOf(args[3]);
-    int changePercentage = Integer.valueOf(args[4]);
-    AlignmentI al = new DnaAlignmentGenerator().generate(width, height,
+    boolean nucleotide = args[0].toLowerCase().startsWith("n");
+    int width = Integer.parseInt(args[1]);
+    int height = Integer.parseInt(args[2]);
+    long randomSeed = Long.valueOf(args[3]);
+    int gapPercentage = Integer.valueOf(args[4]);
+    int changePercentage = Integer.valueOf(args[5]);
+    AlignmentI al = new AlignmentGenerator(nucleotide).generate(width,
+            height,
             randomSeed, gapPercentage, changePercentage);
 
     System.out.println("; " + height + " sequences of " + width
             + " bases with " + gapPercentage + "% gaps and "
             + changePercentage + "% mutations (random seed = " + randomSeed
             + ")");
-    System.out.println(new FastaFile().print(al.getSequencesArray()));
+    System.out.println(new FastaFile().print(al.getSequencesArray(), true));
   }
 
   /**
@@ -92,25 +109,26 @@ public class DnaAlignmentGenerator
   private static void usage()
   {
     System.out.println("Usage:");
-    System.out.println("arg0: number of (non-gap) bases per sequence");
-    System.out.println("arg1: number sequences");
+    System.out.println("arg0: n (for nucleotide) or p (for peptide)");
+    System.out.println("arg1: number of (non-gap) bases per sequence");
+    System.out.println("arg2: number sequences");
     System.out
-            .println("arg2: an integer as random seed (same seed = same results)");
-    System.out.println("arg3: percentage of gaps to (randomly) generate");
+            .println("arg3: an integer as random seed (same seed = same results)");
+    System.out.println("arg4: percentage of gaps to (randomly) generate");
     System.out
-            .println("arg4: percentage of 'mutations' to (randomly) generate");
-    System.out.println("Example: DnaAlignmentGenerator 12 15 387 10 5");
+            .println("arg5: percentage of 'mutations' to (randomly) generate");
+    System.out.println("Example: AlignmentGenerator n 12 15 387 10 5");
     System.out
-            .println("- 15 sequences of 12 bases each, approx 10% gaps and 5% mutations, random seed = 387");
+            .println("- 15 nucleotide sequences of 12 bases each, approx 10% gaps and 5% mutations, random seed = 387");
 
   }
 
   /**
-   * Default constructor
+   * Constructor that sets nucleotide or peptide symbol set
    */
-  public DnaAlignmentGenerator()
+  public AlignmentGenerator(boolean nuc)
   {
-
+    BASES = nuc ? NUCS : PEPS;
   }
 
   /**
@@ -156,7 +174,8 @@ public class DnaAlignmentGenerator
     for (int count = 0; count < length;)
     {
       boolean addGap = random.nextInt(100) < gapPercentage;
-      char c = addGap ? GAP : BASES[random.nextInt(Integer.MAX_VALUE) % 4];
+      char c = addGap ? GAP : BASES[random.nextInt(Integer.MAX_VALUE)
+              % BASES.length];
       seq.append(c);
       if (!addGap)
       {
@@ -265,7 +284,7 @@ public class DnaAlignmentGenerator
     char newchar = c;
     while (newchar == c)
     {
-      newchar = BASES[random.nextInt(Integer.MAX_VALUE) % 4];
+      newchar = BASES[random.nextInt(Integer.MAX_VALUE) % BASES.length];
     }
     return newchar;
   }