JAL-1681 show cDNA consensus on protein alignment - first version
[jalview.git] / test / jalview / datamodel / AlignedCodonFrameTest.java
index 9f1d1e0..0e24bf6 100644 (file)
 package jalview.datamodel;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertNull;
 import jalview.util.MapList;
 
+import java.util.Arrays;
+
 import org.junit.Test;
 
 public class AlignedCodonFrameTest
 {
 
   /**
-   * Test the constructor which copies all except the aligned protein sequences.
+   * Test the method that locates the first aligned sequence that has a mapping.
    */
   @Test
-  public void testConstructor_copyWithSequence()
+  public void testFindAlignedSequence()
+  {
+    AlignmentI cdna = new Alignment(new SequenceI[]
+    {});
+    final Sequence seq1 = new Sequence("Seq1", "C-G-TA-GC");
+    seq1.createDatasetSequence();
+    cdna.addSequence(seq1);
+    final Sequence seq2 = new Sequence("Seq2", "-TA-GG-GG");
+    seq2.createDatasetSequence();
+    cdna.addSequence(seq2);
+
+    AlignmentI aa = new Alignment(new SequenceI[]
+    {});
+    final Sequence aseq1 = new Sequence("Seq1", "-P-R");
+    aseq1.createDatasetSequence();
+    aa.addSequence(aseq1);
+    final Sequence aseq2 = new Sequence("Seq2", "-LY-");
+    aseq2.createDatasetSequence();
+    aa.addSequence(aseq2);
+
+    /*
+     * Mapping from first DNA sequence to second AA sequence.
+     */
+    AlignedCodonFrame acf = new AlignedCodonFrame();
+
+    assertNull(acf.findAlignedSequence(seq1, aa));
+
+    MapList map = new MapList(new int[]
+    { 1, 6 }, new int[]
+    { 1, 2 }, 3, 1);
+    acf.addMap(seq1.getDatasetSequence(), aseq2.getDatasetSequence(), map);
+
+    /*
+     * DNA seq1 maps to AA seq2
+     */
+    assertEquals(aa.getSequenceAt(1),
+ acf.findAlignedSequence(cdna
+            .getSequenceAt(0).getDatasetSequence(), aa));
+
+    assertEquals(cdna.getSequenceAt(0),
+ acf.findAlignedSequence(aa
+            .getSequenceAt(1).getDatasetSequence(), cdna));
+  }
+
+  /**
+   * Test the method that locates the mapped codon for a protein position.
+   */
+    @Test
+  public void testGetMappedRegion()
   {
-    AlignedCodonFrame acf = new AlignedCodonFrame(0);
-    acf.codons = new int[][]
-    { new int[]
-    { 1, 3 }, new int[]
-    { 4, 6 } };
+    // introns lower case, exons upper case
+    final Sequence seq1 = new Sequence("Seq1", "c-G-TA-gC-gT-T");
+    seq1.createDatasetSequence();
+    final Sequence seq2 = new Sequence("Seq2", "-TA-gG-Gg-CG-a");
+    seq2.createDatasetSequence();
+
+    final Sequence aseq1 = new Sequence("Seq1", "-P-R");
+    aseq1.createDatasetSequence();
+    final Sequence aseq2 = new Sequence("Seq2", "-LY-");
+    aseq2.createDatasetSequence();
+
+    /*
+     * First with no mappings
+     */
+    AlignedCodonFrame acf = new AlignedCodonFrame();
+
+    assertNull(acf.getMappedRegion(seq1, aseq1, 1));
+
+    /*
+     * Set up the mappings for the exons (upper-case bases)
+     */
+    MapList map = new MapList(new int[]
+    { 2, 4, 6, 6, 8, 9 }, new int[]
+    { 1, 2 }, 3, 1);
+    acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
+    map = new MapList(new int[]
+    { 1, 2, 4, 5, 7, 8 }, new int[]
+    { 1, 2 }, 3, 1);
+    acf.addMap(seq2.getDatasetSequence(), aseq2.getDatasetSequence(), map);
+
+    assertEquals("[2, 4]",
+            Arrays.toString(acf.getMappedRegion(seq1, aseq1, 1)));
+    assertEquals("[6, 6, 8, 9]",
+            Arrays.toString(acf.getMappedRegion(seq1, aseq1, 2)));
+    assertEquals("[1, 2, 4, 4]",
+            Arrays.toString(acf.getMappedRegion(seq2, aseq2, 1)));
+    assertEquals("[5, 5, 7, 8]",
+            Arrays.toString(acf.getMappedRegion(seq2, aseq2, 2)));
+
+    /*
+     * No mapping from sequence 1 to sequence 2
+     */
+    assertNull(acf.getMappedRegion(seq1, aseq2, 1));
+  }
+
+  @Test
+  public void testGetMappedCodon()
+  {
+    final Sequence seq1 = new Sequence("Seq1", "c-G-TA-gC-gT-T");
+    seq1.createDatasetSequence();
+    final Sequence aseq1 = new Sequence("Seq1", "-P-R");
+    aseq1.createDatasetSequence();
+
+    /*
+     * First with no mappings
+     */
+    AlignedCodonFrame acf = new AlignedCodonFrame();
+
+    assertNull(acf.getMappedCodon(seq1.getDatasetSequence(), 0));
+
+    /*
+     * Set up the mappings for the exons (upper-case bases)
+     */
     MapList map = new MapList(new int[]
-    { 1, 3 }, new int[]
-    { 1, 1 }, 3, 1);
-    SequenceI aaseq = new Sequence("", "FKQ");
-    SequenceI dnaseq = new Sequence("", "ATTCGTACGGAC");
-    acf.addMap(dnaseq, aaseq, map);
-    SequenceI[] newaligned = new SequenceI[1];
-    newaligned[0] = new Sequence("", "-F-K-Q");
-    newaligned[0].setDatasetSequence(aaseq.getDatasetSequence());
-    AlignedCodonFrame copy = new AlignedCodonFrame(acf, newaligned);
-    assertSame(copy.codons, acf.codons);
-    assertEquals(copy.aaWidth, acf.aaWidth);
-    assertSame(copy.getdnaSeqs(), acf.getdnaSeqs());
-    assertSame(newaligned[0], copy.getAaForDnaSeq(dnaseq, false));
+    { 2, 4, 6, 6, 8, 9 }, new int[]
+    { 1, 2 }, 3, 1);
+    acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
+    
+    assertEquals("[G, T, A]", Arrays.toString(acf.getMappedCodon(
+            aseq1.getDatasetSequence(), 1)));
+    assertEquals("[C, T, T]", Arrays.toString(acf.getMappedCodon(
+            aseq1.getDatasetSequence(), 2)));
   }
 }