JAL-1693 make exon alignment for get-xref splitframe (with CDS xref)
[jalview.git] / test / jalview / analysis / AlignmentUtilsTests.java
index 71b1bcb..98d77d4 100644 (file)
@@ -29,8 +29,10 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.junit.Test;
 
@@ -41,11 +43,14 @@ import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.Mapping;
+import jalview.datamodel.SearchResults;
+import jalview.datamodel.SearchResults.Match;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
 import jalview.io.AppletFormatAdapter;
 import jalview.io.FormatAdapter;
 import jalview.util.MapList;
+import jalview.util.MappingUtils;
 
 public class AlignmentUtilsTests 
 {
@@ -869,4 +874,137 @@ public class AlignmentUtilsTests
     assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
     assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
   }
+
+  /**
+   * Test the method that extracts the exon-only part of a dna alignment.
+   */
+  @Test
+  public void testMakeExonAlignment()
+  {
+    SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
+    SequenceI dna2 = new Sequence("dna2", "GGGcccTTTaaaCCC");
+    SequenceI pep1 = new Sequence("pep1", "GF");
+    SequenceI pep2 = new Sequence("pep2", "GFP");
+    dna1.createDatasetSequence();
+    dna2.createDatasetSequence();
+    pep1.createDatasetSequence();
+    pep2.createDatasetSequence();
+
+    Set<AlignedCodonFrame> mappings = new HashSet<AlignedCodonFrame>();
+    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 = new MapList(new int[]
+    { 1, 3, 7, 9, 13, 15 }, new int[]
+    { 1, 3 }, 3, 1);
+    acf = new AlignedCodonFrame();
+    acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(), map);
+    mappings.add(acf);
+    
+    AlignmentI exons = AlignmentUtils.makeExonAlignment(new SequenceI[]
+    { dna1, dna2 }, mappings);
+    assertEquals(2, exons.getSequences().size());
+    assertEquals("GGGTTT", exons.getSequenceAt(0).getSequenceAsString());
+    assertEquals("GGGTTTCCC", exons.getSequenceAt(1).getSequenceAsString());
+
+    /*
+     * Verify updated mappings
+     */
+    assertEquals(2, mappings.size());
+
+    /*
+     * Mapping from pep1 to GGGTTT in first new exon sequence
+     */
+    List<AlignedCodonFrame> pep1Mapping = MappingUtils
+            .findMappingsForSequence(pep1, mappings);
+    assertEquals(1, pep1Mapping.size());
+    // map G to GGG
+    SearchResults sr = MappingUtils.buildSearchResults(pep1, 1, mappings);
+    assertEquals(1, sr.getResults().size());
+    Match m = sr.getResults().get(0);
+    assertEquals(exons.getSequenceAt(0).getDatasetSequence(),
+            m.getSequence());
+    assertEquals(1, m.getStart());
+    assertEquals(3, m.getEnd());
+    // map F to TTT
+    sr = MappingUtils.buildSearchResults(pep1, 2, mappings);
+    m = sr.getResults().get(0);
+    assertEquals(exons.getSequenceAt(0).getDatasetSequence(),
+            m.getSequence());
+    assertEquals(4, m.getStart());
+    assertEquals(6, m.getEnd());
+
+    /*
+     * Mapping from pep2 to GGGTTTCCC in second new exon sequence
+     */
+    List<AlignedCodonFrame> pep2Mapping = MappingUtils
+            .findMappingsForSequence(pep2, mappings);
+    assertEquals(1, pep2Mapping.size());
+    // map G to GGG
+    sr = MappingUtils.buildSearchResults(pep2, 1, mappings);
+    assertEquals(1, sr.getResults().size());
+    m = sr.getResults().get(0);
+    assertEquals(exons.getSequenceAt(1).getDatasetSequence(),
+            m.getSequence());
+    assertEquals(1, m.getStart());
+    assertEquals(3, m.getEnd());
+    // map F to TTT
+    sr = MappingUtils.buildSearchResults(pep2, 2, mappings);
+    m = sr.getResults().get(0);
+    assertEquals(exons.getSequenceAt(1).getDatasetSequence(),
+            m.getSequence());
+    assertEquals(4, m.getStart());
+    assertEquals(6, m.getEnd());
+    // map P to CCC
+    sr = MappingUtils.buildSearchResults(pep2, 3, mappings);
+    m = sr.getResults().get(0);
+    assertEquals(exons.getSequenceAt(1).getDatasetSequence(),
+            m.getSequence());
+    assertEquals(7, m.getStart());
+    assertEquals(9, m.getEnd());
+  }
+
+  /**
+   * Test the method that makes an exon-only sequence from a DNA sequence and
+   * its product mapping. Test includes the expected case that the DNA sequence
+   * already has a protein product (Uniprot translation) which in turn has an
+   * x-ref to the EMBLCDS record.
+   */
+  @Test
+  public void testMakeExonSequence()
+  {
+    SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
+    SequenceI pep1 = new Sequence("pep1", "GF");
+    dna1.createDatasetSequence();
+    pep1.createDatasetSequence();
+    pep1.getDatasetSequence().addDBRef(
+            new DBRefEntry("EMBLCDS", "2", "A12345"));
+
+    /*
+     * Make the mapping from dna to protein. The protein sequence has a DBRef to
+     * EMBLCDS|A12345.
+     */
+    Set<AlignedCodonFrame> mappings = new HashSet<AlignedCodonFrame>();
+    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);
+
+    AlignedCodonFrame newMapping = new AlignedCodonFrame();
+    SequenceI exon = AlignmentUtils.makeExonSequence(dna1, acf, newMapping);
+
+    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());
+
+  }
 }