JAL-1270 main method converted to JUnit test 'as seen'
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 15 May 2015 13:54:23 +0000 (14:54 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 15 May 2015 13:54:23 +0000 (14:54 +0100)
src/jalview/datamodel/SeqCigar.java
test/jalview/datamodel/SeqCigarTest.java [new file with mode: 0644]

index 090d216..09b2e89 100644 (file)
@@ -23,8 +23,10 @@ package jalview.datamodel;
 import java.util.Enumeration;
 import java.util.Hashtable;
 
-import jalview.analysis.*;
-import jalview.util.*;
+import jalview.analysis.AlignSeq;
+import jalview.analysis.SeqsetUtils;
+import jalview.util.MessageManager;
+import jalview.util.ShiftList;
 
 public class SeqCigar extends CigarSimple
 {
@@ -538,157 +540,6 @@ public class SeqCigar extends CigarSimple
   }
 
   /**
-   * non rigorous testing
-   */
-  /**
-   * 
-   * @param seq
-   *          Sequence
-   * @param ex_cs_gapped
-   *          String
-   * @return String
-   */
-  public static String testCigar_string(Sequence seq, String ex_cs_gapped)
-  {
-    SeqCigar c_sgapped = new SeqCigar(seq);
-    String cs_gapped = c_sgapped.getCigarstring();
-    if (!cs_gapped.equals(ex_cs_gapped))
-    {
-      System.err.println("Failed getCigarstring: incorect string '"
-              + cs_gapped + "' != " + ex_cs_gapped);
-    }
-    return cs_gapped;
-  }
-
-  public static boolean testSeqRecovery(SeqCigar gen_sgapped,
-          SequenceI s_gapped)
-  {
-    // this is non-rigorous - start and end recovery is not tested.
-    SequenceI gen_sgapped_s = gen_sgapped.getSeq('-');
-    if (!gen_sgapped_s.getSequence().equals(s_gapped.getSequence()))
-    {
-      System.err.println("Couldn't reconstruct sequence.\n"
-              + gen_sgapped_s.getSequenceAsString() + "\n"
-              + s_gapped.getSequenceAsString());
-      return false;
-    }
-    return true;
-  }
-
-  public static void main(String argv[]) throws Exception
-  {
-    String o_seq;
-    Sequence s = new Sequence("MySeq",
-            o_seq = "asdfktryasdtqwrtsaslldddptyipqqwaslchvhttt", 39, 80);
-    String orig_gapped;
-    Sequence s_gapped = new Sequence(
-            "MySeq",
-            orig_gapped = "----asdf------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhttt",
-            39, 80);
-    String ex_cs_gapped = "4I4M6I6M3I11M4I12M4I9M";
-    s_gapped.setDatasetSequence(s);
-    String sub_gapped_s;
-    Sequence s_subsequence_gapped = new Sequence(
-            "MySeq",
-            sub_gapped_s = "------ktryas---dtqwrtsasll----dddptyipqqwa----slchvh",
-            43, 77);
-
-    s_subsequence_gapped.setDatasetSequence(s);
-    SeqCigar c_null = new SeqCigar(s);
-    String cs_null = c_null.getCigarstring();
-    if (!cs_null.equals("42M"))
-    {
-      System.err
-              .println("Failed to recover ungapped sequence cigar operations:"
-                      + ((cs_null == "") ? "empty string" : cs_null));
-    }
-    testCigar_string(s_gapped, ex_cs_gapped);
-    SeqCigar gen_sgapped = SeqCigar.parseCigar(s, ex_cs_gapped);
-    if (!gen_sgapped.getCigarstring().equals(ex_cs_gapped))
-    {
-      System.err.println("Failed parseCigar(" + ex_cs_gapped
-              + ")->getCigarString()->'" + gen_sgapped.getCigarstring()
-              + "'");
-    }
-    testSeqRecovery(gen_sgapped, s_gapped);
-    // Test dataset resolution
-    SeqCigar sub_gapped = new SeqCigar(s_subsequence_gapped);
-    if (!testSeqRecovery(sub_gapped, s_subsequence_gapped))
-    {
-      System.err
-              .println("Failed recovery for subsequence of dataset sequence");
-    }
-    // width functions
-    if (sub_gapped.getWidth() != sub_gapped_s.length())
-    {
-      System.err.println("Failed getWidth()");
-    }
-
-    sub_gapped.getFullWidth();
-    if (sub_gapped.hasDeletedRegions())
-    {
-      System.err.println("hasDeletedRegions is incorrect.");
-    }
-    // Test start-end region SeqCigar
-    SeqCigar sub_se_gp = new SeqCigar(s_subsequence_gapped, 8, 48);
-    if (sub_se_gp.getWidth() != 41)
-    {
-      System.err
-              .println("SeqCigar(seq, start, end) not properly clipped alignsequence.");
-    }
-    System.out.println("Original sequence align:\n" + sub_gapped_s
-            + "\nReconstructed window from 8 to 48\n" + "XXXXXXXX"
-            + sub_se_gp.getSequenceString('-') + "..." + "\nCigar String:"
-            + sub_se_gp.getCigarstring() + "\n");
-    SequenceI ssgp = sub_se_gp.getSeq('-');
-    System.out.println("\t " + ssgp.getSequenceAsString());
-    for (int r = 0; r < 10; r++)
-    {
-      sub_se_gp = new SeqCigar(s_subsequence_gapped, 8, 48);
-      int sl = sub_se_gp.getWidth();
-      int st = sl - 1 - r;
-      for (int rs = 0; rs < 10; rs++)
-      {
-        int e = st + rs;
-        sub_se_gp.deleteRange(st, e);
-        String ssgapedseq = sub_se_gp.getSeq('-').getSequenceAsString();
-        System.out.println(st + "," + e + "\t:" + ssgapedseq);
-        st -= 3;
-      }
-    }
-    {
-      SeqCigar[] set = new SeqCigar[]
-      { new SeqCigar(s), new SeqCigar(s_subsequence_gapped, 8, 48),
-          new SeqCigar(s_gapped) };
-      Alignment al = new Alignment(set);
-      for (int i = 0; i < al.getHeight(); i++)
-      {
-        System.out.println("" + al.getSequenceAt(i).getName() + "\t"
-                + al.getSequenceAt(i).getStart() + "\t"
-                + al.getSequenceAt(i).getEnd() + "\t"
-                + al.getSequenceAt(i).getSequenceAsString());
-      }
-    }
-    {
-      System.out.println("Gapped.");
-      SeqCigar[] set = new SeqCigar[]
-      { new SeqCigar(s), new SeqCigar(s_subsequence_gapped, 8, 48),
-          new SeqCigar(s_gapped) };
-      set[0].deleteRange(20, 25);
-      Alignment al = new Alignment(set);
-      for (int i = 0; i < al.getHeight(); i++)
-      {
-        System.out.println("" + al.getSequenceAt(i).getName() + "\t"
-                + al.getSequenceAt(i).getStart() + "\t"
-                + al.getSequenceAt(i).getEnd() + "\t"
-                + al.getSequenceAt(i).getSequenceAsString());
-      }
-    }
-    // if (!ssgapedseq.equals("ryas---dtqqwa----slchvh"))
-    // System.err.println("Subseqgaped\n------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhryas---dtqwrtsasll--qwa----slchvh\n"+ssgapedseq+"\n"+sub_se_gp.getCigarstring());
-  }
-
-  /**
    * references to entities that this sequence cigar is associated with.
    */
   private Hashtable selGroups = null;
diff --git a/test/jalview/datamodel/SeqCigarTest.java b/test/jalview/datamodel/SeqCigarTest.java
new file mode 100644 (file)
index 0000000..fc0cb0e
--- /dev/null
@@ -0,0 +1,153 @@
+package jalview.datamodel;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import org.junit.Test;
+
+/**
+ * Unit tests for SeqCigar
+ */
+public class SeqCigarTest
+{
+  /*
+   * refactored 'as is' from main method
+   * 
+   * TODO: split into separate tests
+   */
+  @Test
+  public void testSomething() throws Exception
+  {
+    String o_seq = "asdfktryasdtqwrtsaslldddptyipqqwaslchvhttt";
+    Sequence s = new Sequence("MySeq", o_seq, 39, 80);
+    String orig_gapped = "----asdf------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhttt";
+    Sequence s_gapped = new Sequence("MySeq", orig_gapped, 39, 80);
+    String ex_cs_gapped = "4I4M6I6M3I11M4I12M4I9M";
+    s_gapped.setDatasetSequence(s);
+    String sub_gapped_s = "------ktryas---dtqwrtsasll----dddptyipqqwa----slchvh";
+    Sequence s_subsequence_gapped = new Sequence("MySeq", sub_gapped_s, 43,
+            77);
+    s_subsequence_gapped.setDatasetSequence(s);
+
+    SeqCigar c_null = new SeqCigar(s);
+    String cs_null = c_null.getCigarstring();
+    assertEquals("Failed to recover ungapped sequence cigar operations",
+            "42M", cs_null);
+    testCigar_string(s_gapped, ex_cs_gapped);
+    SeqCigar gen_sgapped = SeqCigar.parseCigar(s, ex_cs_gapped);
+    assertEquals("Failed parseCigar", ex_cs_gapped,
+            gen_sgapped.getCigarstring());
+
+    testSeqRecovery(gen_sgapped, s_gapped);
+
+    /*
+     * Test dataset resolution
+     */
+    SeqCigar sub_gapped = new SeqCigar(s_subsequence_gapped);
+    testSeqRecovery(sub_gapped, s_subsequence_gapped);
+
+    /*
+     * Test width functions
+     */
+    assertEquals("Failed getWidth", sub_gapped_s.length(),
+            sub_gapped.getWidth());
+  
+    sub_gapped.getFullWidth();
+    assertFalse("hasDeletedRegions is incorrect",
+            sub_gapped.hasDeletedRegions());
+
+    // Test start-end region SeqCigar
+    SeqCigar sub_se_gp = new SeqCigar(s_subsequence_gapped, 8, 48);
+    assertEquals(
+            "SeqCigar(seq, start, end) not properly clipped alignsequence",
+            41, sub_se_gp.getWidth());
+
+    /*
+     * TODO: can we add assertions to the sysouts that follow?
+     */
+    System.out.println("Original sequence align:\n" + sub_gapped_s
+            + "\nReconstructed window from 8 to 48\n" + "XXXXXXXX"
+            + sub_se_gp.getSequenceString('-') + "..." + "\nCigar String:"
+            + sub_se_gp.getCigarstring() + "\n");
+    SequenceI ssgp = sub_se_gp.getSeq('-');
+    System.out.println("\t " + ssgp.getSequenceAsString());
+    for (int r = 0; r < 10; r++)
+    {
+      sub_se_gp = new SeqCigar(s_subsequence_gapped, 8, 48);
+      int sl = sub_se_gp.getWidth();
+      int st = sl - 1 - r;
+      for (int rs = 0; rs < 10; rs++)
+      {
+        int e = st + rs;
+        sub_se_gp.deleteRange(st, e);
+        String ssgapedseq = sub_se_gp.getSeq('-').getSequenceAsString();
+        System.out.println(st + "," + e + "\t:" + ssgapedseq);
+        st -= 3;
+      }
+    }
+
+    SeqCigar[] set = new SeqCigar[]
+    { new SeqCigar(s), new SeqCigar(s_subsequence_gapped, 8, 48),
+        new SeqCigar(s_gapped) };
+    Alignment al = new Alignment(set);
+    for (int i = 0; i < al.getHeight(); i++)
+    {
+      System.out.println("" + al.getSequenceAt(i).getName() + "\t"
+              + al.getSequenceAt(i).getStart() + "\t"
+              + al.getSequenceAt(i).getEnd() + "\t"
+              + al.getSequenceAt(i).getSequenceAsString());
+    }
+
+    System.out.println("Gapped.");
+    set = new SeqCigar[]
+    { new SeqCigar(s), new SeqCigar(s_subsequence_gapped, 8, 48),
+        new SeqCigar(s_gapped) };
+    set[0].deleteRange(20, 25);
+    al = new Alignment(set);
+    for (int i = 0; i < al.getHeight(); i++)
+    {
+      System.out.println("" + al.getSequenceAt(i).getName() + "\t"
+              + al.getSequenceAt(i).getStart() + "\t"
+              + al.getSequenceAt(i).getEnd() + "\t"
+              + al.getSequenceAt(i).getSequenceAsString());
+    }
+
+    // if (!ssgapedseq.equals("ryas---dtqqwa----slchvh"))
+    // System.err.println("Subseqgaped\n------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhryas---dtqwrtsasll--qwa----slchvh\n"+ssgapedseq+"\n"+sub_se_gp.getCigarstring());
+  }
+
+  /**
+   * non rigorous testing
+   * 
+   * @param seq
+   *          Sequence
+   * @param ex_cs_gapped
+   *          String
+   * @return String
+   */
+  protected void testCigar_string(Sequence seq, String ex_cs_gapped)
+  {
+    SeqCigar c_sgapped = new SeqCigar(seq);
+    String cs_gapped = c_sgapped.getCigarstring();
+    assertEquals("Failed getCigarstring", ex_cs_gapped,
+            cs_gapped);
+  }
+
+  protected void testSeqRecovery(SeqCigar gen_sgapped,
+          SequenceI s_gapped)
+  {
+    // this is non-rigorous - start and end recovery is not tested.
+    SequenceI gen_sgapped_s = gen_sgapped.getSeq('-');
+    // assertEquals("Couldn't reconstruct sequence", s_gapped.getSequence(),
+    // gen_sgapped_s);
+    if (!gen_sgapped_s.getSequence().equals(s_gapped.getSequence()))
+    {
+      // TODO: investigate errors reported here, to allow full conversion to
+      // passing JUnit assertion form
+      System.err.println("Couldn't reconstruct sequence.\n"
+              + gen_sgapped_s.getSequenceAsString() + "\n"
+              + s_gapped.getSequenceAsString());
+    }
+  }
+
+}