JAL-845 cDNA mapping: select on sequence translation instead of name
[jalview.git] / test / jalview / analysis / AlignmentUtilsTests.java
index c436818..f56dd5d 100644 (file)
@@ -21,6 +21,8 @@
 package jalview.analysis;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import jalview.analysis.AlignmentUtils.MappingResult;
 import jalview.datamodel.AlignedCodonFrame;
@@ -35,6 +37,7 @@ import jalview.util.MapList;
 
 import java.io.IOException;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
@@ -167,15 +170,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]);
@@ -185,15 +188,17 @@ public class AlignmentUtilsTests
     assertEquals(3, mapList.getFromRatio());
     assertEquals(1, mapList.getToRatio());
     assertTrue(Arrays.equals(new int[]
-    { 1, 9 }, mapList.getFromRanges()));
+    { 1, 9 }, mapList.getFromRanges().get(0)));
+    assertEquals(1, mapList.getFromRanges().size());
     assertTrue(Arrays.equals(new int[]
-    { 1, 3 }, mapList.getToRanges()));
+    { 1, 3 }, mapList.getToRanges().get(0)));
+    assertEquals(1, mapList.getToRanges().size());
 
     /*
      * 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]);
@@ -207,9 +212,11 @@ public class AlignmentUtilsTests
       assertEquals(3, mapList.getFromRatio());
       assertEquals(1, mapList.getToRatio());
       assertTrue(Arrays.equals(new int[]
-      { 1, 9 }, mapList.getFromRanges()));
+      { 1, 9 }, mapList.getFromRanges().get(0)));
+      assertEquals(1, mapList.getFromRanges().size());
       assertTrue(Arrays.equals(new int[]
-      { 1, 3 }, mapList.getToRanges()));
+      { 1, 3 }, mapList.getToRanges().get(0)));
+      assertEquals(1, mapList.getToRanges().size());
     }
   }
 
@@ -241,15 +248,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]);
@@ -258,26 +265,32 @@ public class AlignmentUtilsTests
     Mapping[] protMappings = humanMapping.getProtMappings();
     // two mappings, both to cDNA with stop codon
     assertEquals(2, protMappings.length);
+
     MapList mapList = protMappings[0].getMap();
     assertEquals(3, mapList.getFromRatio());
     assertEquals(1, mapList.getToRatio());
     assertTrue(Arrays.equals(new int[]
-    { 1, 9 }, mapList.getFromRanges()));
+    { 1, 9 }, mapList.getFromRanges().get(0)));
+    assertEquals(1, mapList.getFromRanges().size());
     assertTrue(Arrays.equals(new int[]
-    { 1, 3 }, mapList.getToRanges()));
+    { 1, 3 }, mapList.getToRanges().get(0)));
+    assertEquals(1, mapList.getToRanges().size());
+
     mapList = protMappings[1].getMap();
     assertEquals(3, mapList.getFromRatio());
     assertEquals(1, mapList.getToRatio());
     assertTrue(Arrays.equals(new int[]
-    { 1, 9 }, mapList.getFromRanges()));
+    { 1, 9 }, mapList.getFromRanges().get(0)));
+    assertEquals(1, mapList.getFromRanges().size());
     assertTrue(Arrays.equals(new int[]
-    { 1, 3 }, mapList.getToRanges()));
+    { 1, 3 }, mapList.getToRanges().get(0)));
+    assertEquals(1, mapList.getToRanges().size());
 
     /*
      * 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]);
@@ -295,27 +308,33 @@ public class AlignmentUtilsTests
     assertEquals(3, mapList.getFromRatio());
     assertEquals(1, mapList.getToRatio());
     assertTrue(Arrays.equals(new int[]
-    { 4, 12 }, mapList.getFromRanges()));
+    { 4, 12 }, mapList.getFromRanges().get(0)));
+    assertEquals(1, mapList.getFromRanges().size());
     assertTrue(Arrays.equals(new int[]
-    { 1, 3 }, mapList.getToRanges()));
+    { 1, 3 }, mapList.getToRanges().get(0)));
+    assertEquals(1, mapList.getToRanges().size());
 
     // second mapping to cDNA with stop codon
     mapList = protMappings[1].getMap();
     assertEquals(3, mapList.getFromRatio());
     assertEquals(1, mapList.getToRatio());
     assertTrue(Arrays.equals(new int[]
-    { 1, 9 }, mapList.getFromRanges()));
+    { 1, 9 }, mapList.getFromRanges().get(0)));
+    assertEquals(1, mapList.getFromRanges().size());
     assertTrue(Arrays.equals(new int[]
-    { 1, 3 }, mapList.getToRanges()));
+    { 1, 3 }, mapList.getToRanges().get(0)));
+    assertEquals(1, mapList.getToRanges().size());
 
     // third mapping to cDNA with start and stop codon
     mapList = protMappings[2].getMap();
     assertEquals(3, mapList.getFromRatio());
     assertEquals(1, mapList.getToRatio());
     assertTrue(Arrays.equals(new int[]
-    { 4, 12 }, mapList.getFromRanges()));
+    { 4, 12 }, mapList.getFromRanges().get(0)));
+    assertEquals(1, mapList.getFromRanges().size());
     assertTrue(Arrays.equals(new int[]
-    { 1, 3 }, mapList.getToRanges()));
+    { 1, 3 }, mapList.getToRanges().get(0)));
+    assertEquals(1, mapList.getToRanges().size());
   }
 
   /**
@@ -324,62 +343,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 +390,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 +439,178 @@ public class AlignmentUtilsTests
   @Test
   public void testAlignSequenceAs_withMapping_withUnmappedProtein()
   {
-    SequenceI dna = new Sequence("Seq1", "GGGAAACCCTTTGGG");
+    
+    /*
+     * 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", "-A-L-P-");
+    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()
+  {
 
     /*
-     * Exons at codon 2 (AAA) and 4 (TTT) mapped to A and P
+     * Intron GGGAAA followed by exon CCCTTT
      */
     MapList map = new MapList(new int[]
-    { 4, 6, 10, 12 }, new int[]
-    { 1, 1, 3, 3 }, 3, 1);
+    { 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());
+  }
+
+  /**
+   * Test the method that realigns protein to match mapped codon alignment.
+   */
+  @Test
+  public void testAlignProteinAsDna()
+  {
+    // seq1 codons are [1,2,3] [4,5,6] [7,8,9] [10,11,12]
+    SequenceI dna1 = new Sequence("Seq1", "TGCCATTACCAG-");
+    // seq2 codons are [1,3,4] [5,6,7] [8,9,10] [11,12,13]
+    SequenceI dna2 = new Sequence("Seq2", "T-GCCATTACCAG");
+    // seq3 codons are [1,2,3] [4,5,7] [8,9,10] [11,12,13]
+    SequenceI dna3 = new Sequence("Seq3", "TGCCA-TTACCAG");
+    AlignmentI dna = new Alignment(new SequenceI[]
+    { dna1, dna2, dna3 });
+    dna.setDataset(null);
+
+    // protein alignment will be realigned like dna
+    SequenceI prot1 = new Sequence("Seq1", "CHYQ");
+    SequenceI prot2 = new Sequence("Seq2", "CHYQ");
+    SequenceI prot3 = new Sequence("Seq3", "CHYQ");
+    AlignmentI protein = new Alignment(new SequenceI[]
+    { prot1, prot2, prot3 });
+    protein.setDataset(null);
+
+    MapList map = new MapList(new int[]
+    { 1, 12 }, new int[]
+    { 1, 4 }, 3, 1);
+    AlignedCodonFrame acf = new AlignedCodonFrame();
+    acf.addMap(dna1.getDatasetSequence(), prot1.getDatasetSequence(), map);
+    acf.addMap(dna2.getDatasetSequence(), prot2.getDatasetSequence(), map);
+    acf.addMap(dna3.getDatasetSequence(), prot3.getDatasetSequence(), map);
+    protein.setCodonFrames(Collections.singleton(acf));
+
     /*
-     * Align dna as "-A-L-P-". Currently, does nothing (aborts realignment).
-     * Change this test first if different behaviour wanted.
+     * Translated codon order is [1,2,3] [1,3,4] [4,5,6] [4,5,7] [5,6,7] [7,8,9]
+     * [8,9,10] [10,11,12] [11,12,13]
      */
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-', false,
-            false);
-    assertEquals("GGGAAACCCTTTGGG", dna.getSequenceAsString());
+    AlignmentUtils.alignProteinAsDna(protein, dna);
+    assertEquals("C-H--Y-Q-", prot1.getSequenceAsString());
+    assertEquals("-C--H-Y-Q", prot2.getSequenceAsString());
+    assertEquals("C--H--Y-Q", prot3.getSequenceAsString());
+  }
+
+  /**
+   * Test the method that tests whether a CDNA sequence translates to a protein
+   * sequence
+   */
+  @Test
+  public void testTranslatesAs()
+  {
+    assertTrue(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(), 0,
+            "FPKG".toCharArray()));
+    // with start codon
+    assertTrue(AlignmentUtils.translatesAs("atgtttcccaaaggg".toCharArray(),
+            3, "FPKG".toCharArray()));
+    // with stop codon1
+    assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtaa".toCharArray(),
+            0, "FPKG".toCharArray()));
+    // with stop codon2
+    assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtag".toCharArray(),
+            0, "FPKG".toCharArray()));
+    // with stop codon3
+    assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtga".toCharArray(),
+            0, "FPKG".toCharArray()));
+    // with start and stop codon1
+    assertTrue(AlignmentUtils.translatesAs(
+            "atgtttcccaaaggtaa".toCharArray(), 3, "FPKG".toCharArray()));
+    // with start and stop codon2
+    assertTrue(AlignmentUtils.translatesAs(
+            "atgtttcccaaaggtag".toCharArray(), 3, "FPKG".toCharArray()));
+    // with start and stop codon3
+    assertTrue(AlignmentUtils.translatesAs(
+            "atgtttcccaaaggtga".toCharArray(), 3, "FPKG".toCharArray()));
+
+    // wrong protein
+    assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(),
+            0,
+            "FPMG".toCharArray()));
+  }
+
+  @Test
+  public void testTranslatesAs_withAmbiguityCodes()
+  {
+    // R means A or G
+    assertTrue(AlignmentUtils.translatesAs("car".toCharArray(), 0,
+            "Q".toCharArray()));
   }
 }