JAL-845 SplitFrame for "show product" and after aligning from SplitFrame
[jalview.git] / test / jalview / analysis / AlignmentUtilsTests.java
index 3ada6fa..c29658b 100644 (file)
@@ -21,6 +21,7 @@
 package jalview.analysis;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import jalview.analysis.AlignmentUtils.MappingResult;
 import jalview.datamodel.AlignedCodonFrame;
@@ -167,15 +168,15 @@ public class AlignmentUtilsTests
     /*
      * Check two mappings (one for Mouse, one for Human)
      */
-    assertEquals(2, protein.getCodonFrames().length);
-    assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).length);
-    assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).length);
+    assertEquals(2, protein.getCodonFrames().size());
+    assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
+    assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
 
     /*
      * Inspect mapping for Human protein
      */
-    AlignedCodonFrame humanMapping = protein.getCodonFrame(protein
-            .getSequenceAt(0))[0];
+    AlignedCodonFrame humanMapping = protein.getCodonFrame(
+            protein.getSequenceAt(0)).get(0);
     assertEquals(1, humanMapping.getdnaSeqs().length);
     assertEquals(cdna1.getSequenceAt(1).getDatasetSequence(),
             humanMapping.getdnaSeqs()[0]);
@@ -192,8 +193,8 @@ public class AlignmentUtilsTests
     /*
      * Inspect mappings for Mouse protein
      */
-    AlignedCodonFrame mouseMapping1 = protein.getCodonFrame(protein
-            .getSequenceAt(1))[0];
+    AlignedCodonFrame mouseMapping1 = protein.getCodonFrame(
+            protein.getSequenceAt(1)).get(0);
     assertEquals(2, mouseMapping1.getdnaSeqs().length);
     assertEquals(cdna1.getSequenceAt(0).getDatasetSequence(),
             mouseMapping1.getdnaSeqs()[0]);
@@ -241,15 +242,15 @@ public class AlignmentUtilsTests
     /*
      * Check two mappings (one for Mouse, one for Human)
      */
-    assertEquals(2, protein.getCodonFrames().length);
-    assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).length);
-    assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).length);
+    assertEquals(2, protein.getCodonFrames().size());
+    assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
+    assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
 
     /*
      * Inspect mapping for Human protein - should map to 2nd and 4th cDNA seqs
      */
-    AlignedCodonFrame humanMapping = protein.getCodonFrame(protein
-            .getSequenceAt(0))[0];
+    AlignedCodonFrame humanMapping = protein.getCodonFrame(
+            protein.getSequenceAt(0)).get(0);
     assertEquals(2, humanMapping.getdnaSeqs().length);
     assertEquals(cdna1.getSequenceAt(1).getDatasetSequence(),
             humanMapping.getdnaSeqs()[0]);
@@ -276,8 +277,8 @@ public class AlignmentUtilsTests
     /*
      * Inspect mapping for Mouse protein - should map to 1st/3rd/5th cDNA seqs
      */
-    AlignedCodonFrame mouseMapping = protein.getCodonFrame(protein
-            .getSequenceAt(1))[0];
+    AlignedCodonFrame mouseMapping = protein.getCodonFrame(
+            protein.getSequenceAt(1)).get(0);
     assertEquals(3, mouseMapping.getdnaSeqs().length);
     assertEquals(cdna1.getSequenceAt(0).getDatasetSequence(),
             mouseMapping.getdnaSeqs()[0]);
@@ -317,4 +318,193 @@ public class AlignmentUtilsTests
     assertTrue(Arrays.equals(new int[]
     { 1, 3 }, mapList.getToRanges()));
   }
+
+  /**
+   * Test for the alignSequenceAs method that takes two sequences and a mapping.
+   */
+  @Test
+  public void testAlignSequenceAs_withMapping_noIntrons()
+  {
+    MapList map = new MapList(new int[]
+    { 1, 6 }, new int[]
+    { 1, 2 }, 3, 1);
+
+    /*
+     * No existing gaps in dna:
+     */
+    checkAlignSequenceAs("GGGAAA", "-A-L-", false, false, map,
+            "---GGG---AAA");
+
+    /*
+     * Now introduce gaps in dna but ignore them when realigning.
+     */
+    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.
+     */
+    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.
+     */
+    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.
+     */
+    checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", false, true, map,
+            "---GGG---AAA-");
+  }
+
+  /**
+   * Test for the alignSequenceAs method that takes two sequences and a mapping.
+   */
+  @Test
+  public void testAlignSequenceAs_withMapping_withIntrons()
+  {
+    /*
+     * Exons at codon 2 (AAA) and 4 (TTT)
+     */
+    MapList map = new MapList(new int[]
+    { 4, 6, 10, 12 }, new int[]
+    { 1, 2 }, 3, 1);
+
+    /*
+     * Simple case: no gaps in dna
+     */
+    checkAlignSequenceAs("GGGAAACCCTTTGGG", "--A-L-", false, false, map,
+            "GGG---AAACCCTTTGGG");
+
+    /*
+     * Add gaps to dna - but ignore when realigning.
+     */
+    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.
+     */
+    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.
+     */
+    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.
+     */
+    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-");
+  }
+
+  /**
+   * Test for the case where not all of the protein sequence is mapped to cDNA.
+   */
+  @Test
+  public void testAlignSequenceAs_withMapping_withUnmappedProtein()
+  {
+    
+    /*
+     * Exons at codon 2 (AAA) and 4 (TTT) mapped to A and P
+     */
+    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()
+  {
+
+    /*
+     * Intron GGGAAA followed by exon CCCTTT
+     */
+    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");
+  }
+
+  /**
+   * Test for the method that generates an aligned translated sequence from one
+   * mapping.
+   */
+  @Test
+  public void testGetAlignedTranslation_dnaLikeProtein()
+  {
+    // dna alignment will be replaced
+    SequenceI dna = new Sequence("Seq1", "T-G-CC-A--T-TAC-CAG-");
+    dna.createDatasetSequence();
+    // protein alignment will be 'applied' to dna
+    SequenceI protein = new Sequence("Seq1", "-CH-Y--Q-");
+    protein.createDatasetSequence();
+    MapList map = new MapList(new int[]
+    { 1, 12 }, new int[]
+    { 1, 4 }, 3, 1);
+    AlignedCodonFrame acf = new AlignedCodonFrame();
+    acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
+
+    final SequenceI aligned = AlignmentUtils
+                .getAlignedTranslation(protein, '-', acf);
+    assertEquals("---TGCCAT---TAC------CAG---", aligned.getSequenceAsString());
+    assertSame(aligned.getDatasetSequence(), dna.getDatasetSequence());
+  }
 }