JAL-845 linked protein/dna 'slave' further PoC functionality
[jalview.git] / test / jalview / analysis / AlignmentUtilsTests.java
index c436818..66f99f3 100644 (file)
@@ -324,62 +324,44 @@ public class AlignmentUtilsTests
   @Test
   public void testAlignSequenceAs_withMapping_noIntrons()
   {
-    /*
-     * Simple case: no gaps in dna
-     */
-    SequenceI dna = new Sequence("Seq1", "GGGAAA");
-    dna.createDatasetSequence();
-    SequenceI protein = new Sequence("Seq1", "-A-L-");
-    protein.createDatasetSequence();
-    AlignedCodonFrame acf = new AlignedCodonFrame();
     MapList map = new MapList(new int[]
     { 1, 6 }, new int[]
     { 1, 2 }, 3, 1);
-    acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
 
     /*
      * No existing gaps in dna:
      */
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
-            false);
-    assertEquals("---GGG---AAA", dna.getSequenceAsString());
+    checkAlignSequenceAs("GGGAAA", "-A-L-", false, false, map,
+            "---GGG---AAA");
 
     /*
      * Now introduce gaps in dna but ignore them when realigning.
      */
-    dna.setSequence("-G-G-G-A-A-A-");
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
-            false);
-    assertEquals("---GGG---AAA", dna.getSequenceAsString());
+    checkAlignSequenceAs("-G-G-G-A-A-A-", "-A-L-", false, false, map,
+            "---GGG---AAA");
 
     /*
      * Now include gaps in dna when realigning. First retaining 'mapped' gaps
      * only, i.e. those within the exon region.
      */
-    dna.setSequence("-G-G--G-A--A-A-");
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', true,
-            false);
-    assertEquals("---G-G--G---A--A-A", dna.getSequenceAsString());
+    checkAlignSequenceAs("-G-G--G-A--A-A-", "-A-L-", true, false, map,
+            "---G-G--G---A--A-A");
 
     /*
      * Include all gaps in dna when realigning (within and without the exon
      * region). The leading gap, and the gaps between codons, are subsumed by
      * the protein alignment gap.
      */
-    dna.setSequence("-G-GG--AA-A-");
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', true,
-            true);
-    assertEquals("---G-GG---AA-A-", dna.getSequenceAsString());
+    checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", true, true, map,
+            "---G-GG---AA-A-");
 
     /*
      * Include only unmapped gaps in dna when realigning (outside the exon
      * region). The leading gap, and the gaps between codons, are subsumed by
      * the protein alignment gap.
      */
-    dna.setSequence("-G-GG--AA-A-");
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
-            true);
-    assertEquals("---GGG---AAA-", dna.getSequenceAsString());
+    checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", false, true, map,
+            "---GGG---AAA-");
   }
 
   /**
@@ -389,62 +371,47 @@ public class AlignmentUtilsTests
   public void testAlignSequenceAs_withMapping_withIntrons()
   {
     /*
-     * Simple case: no gaps in dna
-     */
-    SequenceI dna = new Sequence("Seq1", "GGGAAACCCTTTGGG");
-    dna.createDatasetSequence();
-    SequenceI protein = new Sequence("Seq1", "-A-L-");
-    protein.createDatasetSequence();
-    AlignedCodonFrame acf = new AlignedCodonFrame();
-
-    /*
      * Exons at codon 2 (AAA) and 4 (TTT)
      */
     MapList map = new MapList(new int[]
     { 4, 6, 10, 12 }, new int[]
     { 1, 2 }, 3, 1);
-    acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
 
     /*
-     * Align dna as "-A-L-". The protein 'gaps' follow the introns, i.e are
-     * placed immediately before the mapped codons.
+     * Simple case: no gaps in dna
      */
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
-            false);
-    assertEquals("GGG---AAACCC---TTTGGG", dna.getSequenceAsString());
+    checkAlignSequenceAs("GGGAAACCCTTTGGG", "--A-L-", false, false, map,
+            "GGG---AAACCCTTTGGG");
 
     /*
      * Add gaps to dna - but ignore when realigning.
      */
-    dna.setSequence("-G-G-G--A--A---AC-CC-T-TT-GG-G-");
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
-            false);
-    assertEquals("GGG---AAACCC---TTTGGG", dna.getSequenceAsString());
+    checkAlignSequenceAs("-G-G-G--A--A---AC-CC-T-TT-GG-G-", "--A-L-",
+            false, false, map, "GGG---AAACCCTTTGGG");
 
     /*
      * Add gaps to dna - include within exons only when realigning.
      */
-    dna.setSequence("-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', true,
-            false);
-    assertEquals("GGG---A--A---ACCC---T-TTGGG", dna.getSequenceAsString());
+    checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
+            true, false, map, "GGG---A--A---ACCCT-TTGGG");
 
     /*
      * Include gaps outside exons only when realigning.
      */
-    dna.setSequence("-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
-            true);
-    assertEquals("-G-G-G---AAA-C-CC---TTT-GG-G-", dna.getSequenceAsString());
+    checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
+            false, true, map, "-G-G-GAAAC-CCTTT-GG-G-");
+
+    /*
+     * Include gaps following first intron if we are 'preserving mapped gaps'
+     */
+    checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
+            true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
 
     /*
      * Include all gaps in dna when realigning.
      */
-    dna.setSequence("-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', true,
-            true);
-    assertEquals("-G-G-G---A--A---A-C-CC---T-TT-GG-G-",
-            dna.getSequenceAsString());
+    checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
+            true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
   }
 
   /**
@@ -453,26 +420,65 @@ public class AlignmentUtilsTests
   @Test
   public void testAlignSequenceAs_withMapping_withUnmappedProtein()
   {
-    SequenceI dna = new Sequence("Seq1", "GGGAAACCCTTTGGG");
-    dna.createDatasetSequence();
-    SequenceI protein = new Sequence("Seq1", "-A-L-P-");
-    protein.createDatasetSequence();
-    AlignedCodonFrame acf = new AlignedCodonFrame();
-
+    
     /*
      * Exons at codon 2 (AAA) and 4 (TTT) mapped to A and P
      */
-    MapList map = new MapList(new int[]
+    final MapList map = new MapList(new int[]
     { 4, 6, 10, 12 }, new int[]
     { 1, 1, 3, 3 }, 3, 1);
+    
+
+    /*
+     * Expect alignment does nothing (aborts realignment). Change this test
+     * first if different behaviour wanted.
+     */
+    checkAlignSequenceAs("GGGAAACCCTTTGGG", "-A-L-P-", false,
+            false, map, "GGGAAACCCTTTGGG");
+  }
+
+  /**
+   * Helper method that performs and verifies the method under test.
+   * 
+   * @param dnaSeq
+   * @param proteinSeq
+   * @param preserveMappedGaps
+   * @param preserveUnmappedGaps
+   * @param map
+   * @param expected
+   */
+  protected void checkAlignSequenceAs(final String dnaSeq,
+          final String proteinSeq, final boolean preserveMappedGaps,
+          final boolean preserveUnmappedGaps, MapList map,
+          final String expected)
+  {
+    SequenceI dna = new Sequence("Seq1", dnaSeq);
+    dna.createDatasetSequence();
+    SequenceI protein = new Sequence("Seq1", proteinSeq);
+    protein.createDatasetSequence();
+    AlignedCodonFrame acf = new AlignedCodonFrame();
     acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
 
+    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-',
+            preserveMappedGaps, preserveUnmappedGaps);
+    assertEquals(expected, dna.getSequenceAsString());
+  }
+
+  /**
+   * Test for the alignSequenceAs method where we preserve gaps in introns only.
+   */
+  @Test
+  public void testAlignSequenceAs_keepIntronGapsOnly()
+  {
+
     /*
-     * Align dna as "-A-L-P-". Currently, does nothing (aborts realignment).
-     * Change this test first if different behaviour wanted.
+     * Intron GGGAAA followed by exon CCCTTT
      */
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
-            false);
-    assertEquals("GGGAAACCCTTTGGG", dna.getSequenceAsString());
+    MapList map = new MapList(new int[]
+    { 7, 12 }, new int[]
+    { 1, 2 }, 3, 1);
+    
+    checkAlignSequenceAs("GG-G-AA-A-C-CC-T-TT", "AL",
+            false, true, map, "GG-G-AA-ACCCTTT");
   }
 }