JAL-1693 generate multiple exon sequences for 1-many dna-protein
[jalview.git] / test / jalview / analysis / AlignmentUtilsTests.java
index 98d77d4..c19884e 100644 (file)
@@ -30,6 +30,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -974,7 +975,7 @@ public class AlignmentUtilsTests
    * x-ref to the EMBLCDS record.
    */
   @Test
-  public void testMakeExonSequence()
+  public void testMakeExonSequences()
   {
     SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
     SequenceI pep1 = new Sequence("pep1", "GF");
@@ -996,7 +997,10 @@ public class AlignmentUtilsTests
     mappings.add(acf);
 
     AlignedCodonFrame newMapping = new AlignedCodonFrame();
-    SequenceI exon = AlignmentUtils.makeExonSequence(dna1, acf, newMapping);
+    List<SequenceI> exons = AlignmentUtils.makeExonSequences(dna1, acf,
+            newMapping);
+    assertEquals(1, exons.size());
+    SequenceI exon = exons.get(0);
 
     assertEquals("GGGTTT", exon.getSequenceAsString());
     assertEquals("dna1|A12345", exon.getName());
@@ -1005,6 +1009,95 @@ public class AlignmentUtilsTests
     assertEquals("EMBLCDS", cdsRef.getSource());
     assertEquals("2", cdsRef.getVersion());
     assertEquals("A12345", cdsRef.getAccessionId());
+  }
+
+  /**
+   * Test the method that makes an exon-only alignment from a DNA sequence and
+   * its product mappings, for the case where there are multiple exon mappings
+   * to different protein products.
+   */
+  @Test
+  public void testMakeExonAlignment_multipleProteins()
+  {
+    SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
+    SequenceI pep1 = new Sequence("pep1", "GF"); // GGGTTT
+    SequenceI pep2 = new Sequence("pep2", "KP"); // aaaccc
+    SequenceI pep3 = new Sequence("pep3", "KF"); // aaaTTT
+    dna1.createDatasetSequence();
+    pep1.createDatasetSequence();
+    pep2.createDatasetSequence();
+    pep3.createDatasetSequence();
+    pep1.getDatasetSequence().addDBRef(
+            new DBRefEntry("EMBLCDS", "2", "A12345"));
+    pep2.getDatasetSequence().addDBRef(
+            new DBRefEntry("EMBLCDS", "3", "A12346"));
+    pep3.getDatasetSequence().addDBRef(
+            new DBRefEntry("EMBLCDS", "4", "A12347"));
+
+    /*
+     * Make the mappings from dna to protein. Using LinkedHashset is a
+     * convenience so results are in the input order. There is no assertion that
+     * the generated exon sequences are in any particular order.
+     */
+    Set<AlignedCodonFrame> mappings = new LinkedHashSet<AlignedCodonFrame>();
+    // map ...GGG...TTT to GF
+    MapList map = new MapList(new int[]
+    { 4, 6, 10, 12 }, new int[]
+    { 1, 2 }, 3, 1);
+    AlignedCodonFrame acf = new AlignedCodonFrame();
+    acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
+    mappings.add(acf);
+
+    // map aaa...ccc to KP
+    map = new MapList(new int[]
+    { 1, 3, 7, 9 }, new int[]
+    { 1, 2 }, 3, 1);
+    acf = new AlignedCodonFrame();
+    acf.addMap(dna1.getDatasetSequence(), pep2.getDatasetSequence(), map);
+    mappings.add(acf);
+
+    // map aaa......TTT to KF
+    map = new MapList(new int[]
+    { 1, 3, 10, 12 }, new int[]
+    { 1, 2 }, 3, 1);
+    acf = new AlignedCodonFrame();
+    acf.addMap(dna1.getDatasetSequence(), pep3.getDatasetSequence(), map);
+    mappings.add(acf);
+
+    AlignmentI exal = AlignmentUtils.makeExonAlignment(new SequenceI[]
+    { dna1 }, mappings);
+
+    /*
+     * Verify we have 3 exon sequences, mapped to pep1/2/3 respectively
+     */
+    List<SequenceI> exons = exal.getSequences();
+    assertEquals(3, exons.size());
+
+    SequenceI exon = exons.get(0);
+    assertEquals("GGGTTT", exon.getSequenceAsString());
+    assertEquals("dna1|A12345", exon.getName());
+    assertEquals(1, exon.getDBRef().length);
+    DBRefEntry cdsRef = exon.getDBRef()[0];
+    assertEquals("EMBLCDS", cdsRef.getSource());
+    assertEquals("2", cdsRef.getVersion());
+    assertEquals("A12345", cdsRef.getAccessionId());
+
+    exon = exons.get(1);
+    assertEquals("aaaccc", exon.getSequenceAsString());
+    assertEquals("dna1|A12346", exon.getName());
+    assertEquals(1, exon.getDBRef().length);
+    cdsRef = exon.getDBRef()[0];
+    assertEquals("EMBLCDS", cdsRef.getSource());
+    assertEquals("3", cdsRef.getVersion());
+    assertEquals("A12346", cdsRef.getAccessionId());
 
+    exon = exons.get(2);
+    assertEquals("aaaTTT", exon.getSequenceAsString());
+    assertEquals("dna1|A12347", exon.getName());
+    assertEquals(1, exon.getDBRef().length);
+    cdsRef = exon.getDBRef()[0];
+    assertEquals("EMBLCDS", cdsRef.getSource());
+    assertEquals("4", cdsRef.getVersion());
+    assertEquals("A12347", cdsRef.getAccessionId());
   }
 }