JAL-1619 first draft of 'linked protein and cDNA'
[jalview.git] / test / jalview / datamodel / AlignmentTest.java
index 5920189..1713bc6 100644 (file)
@@ -3,7 +3,9 @@ package jalview.datamodel;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import jalview.io.AppletFormatAdapter;
+import jalview.io.FormatAdapter;
 
 import java.io.IOException;
 import java.util.Iterator;
@@ -32,8 +34,25 @@ public class AlignmentTest
           "D.melanogaster.3          G.UGGCGCU..UAUGACGCA\n" +
           "#=GR D.melanogaster.3 SS  (.(((...(....(((((((\n" +
           "//";
-  // @formatter:on
 
+  private static final String TEST_DATA2 = 
+          ">TEST21 test21\n" +
+          "AC-GG--CUC-CAA-CT\n" +
+          ">TEST22 test22\n" +
+          "-CG-TTA--ACG---AAGT\n";
+
+  private static final String TEST_DATA3 = 
+          ">TEST31 test31\n" +
+          "K-QY--L\n" +
+          ">TEST32 test32\n" +
+          "-R-FP-W-\n";
+
+  private static final String TEST_DATA4 = 
+          ">TEST41 test41\n" +
+          "GCTCGUCGTACT\n" +
+          ">TEST42 test42\n" +
+          "GGGTCAGGCAGT\n";
+  // @formatter:on
 
   private Alignment al;
 
@@ -44,15 +63,14 @@ public class AlignmentTest
   @Before
   public void setUp() throws IOException
   {
-    al = new jalview.io.FormatAdapter().readFile(TEST_DATA,
+    al = new FormatAdapter().readFile(TEST_DATA,
             AppletFormatAdapter.PASTE, "STH");
-    for (int i = 0; i < al.getSequencesArray().length; ++i)
+    int i = 0;
+    for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
     {
-      al.getSequenceAt(i).setDatasetSequence(
-              al.getSequenceAt(i).createDatasetSequence());
-      al.addAnnotation(al.getSequenceAt(i).getAnnotation()[0]);
-      al.getSequenceAt(i).getAnnotation()[0].setCalcId("CalcIdFor"
+      ann.setCalcId("CalcIdFor"
               + al.getSequenceAt(i).getName());
+      i++;
     }
   }
 
@@ -70,4 +88,114 @@ public class AlignmentTest
     assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
     assertFalse(iter.hasNext());
   }
+
+  /**
+   * Tests for method that checks for alignment 'mappability'.
+   * 
+   * @throws IOException
+   */
+  @Test
+  public void testIsMappableTo() throws IOException
+  {
+    al = new FormatAdapter().readFile(TEST_DATA2,
+            AppletFormatAdapter.PASTE, "FASTA");
+    al.setDataset(null);
+
+    // not mappable to self
+    assertFalse(al.isMappableTo(al));
+
+    // dna mappable to protein and vice versa
+    AlignmentI alp = new FormatAdapter().readFile(TEST_DATA3,
+            AppletFormatAdapter.PASTE, "FASTA");
+    alp.setDataset(null);
+    assertTrue(al.isMappableTo(alp));
+    assertTrue(alp.isMappableTo(al));
+    assertFalse(alp.isMappableTo(alp));
+
+    // not mappable if any sequence length mismatch
+    alp.getSequenceAt(1).setSequence("-R--FP-");
+    alp.getSequenceAt(1).setDatasetSequence(new Sequence("", "RFP"));
+    assertFalse(alp.isMappableTo(al));
+    assertFalse(al.isMappableTo(alp));
+
+    // not mappable if number of sequences differs
+    alp.deleteSequence(1);
+    assertFalse(alp.isMappableTo(al));
+    assertFalse(al.isMappableTo(alp));
+  }
+
+  /**
+   * Tests for realigning as per a supplied alignment.
+   * 
+   * @throws IOException
+   */
+  @Test
+  public void testAlignAs_dnaAsDna() throws IOException
+  {
+    // aligned cDNA:
+    Alignment al1 = new FormatAdapter().readFile(TEST_DATA2,
+            AppletFormatAdapter.PASTE, "FASTA");
+    al1.setDataset(null);
+    // unaligned cDNA:
+    Alignment al2 = new FormatAdapter().readFile(TEST_DATA4,
+            AppletFormatAdapter.PASTE, "FASTA");
+    al2.setDataset(null);
+
+    al2.alignAs(al1);
+    assertEquals("GC-TC--GUC-GTA-CT", al2.getSequenceAt(0)
+            .getSequenceAsString());
+    assertEquals("-GG-GTC--AGG---CAGT", al2.getSequenceAt(1)
+            .getSequenceAsString());
+  }
+
+  /**
+   * Aligning protein from cDNA yet to be implemented.
+   * 
+   * @throws IOException
+   */
+  @Test
+  public void testAlignAs_proteinAsCdna() throws IOException
+  {
+    // aligned cDNA:
+    Alignment al1 = new FormatAdapter().readFile(TEST_DATA2,
+            AppletFormatAdapter.PASTE, "FASTA");
+    al1.setDataset(null);
+    // unaligned cDNA:
+    Alignment al2 = new FormatAdapter().readFile(TEST_DATA3,
+            AppletFormatAdapter.PASTE, "FASTA");
+    al2.setDataset(null);
+
+    try
+    {
+      al2.alignAs(al1);
+      fail("No exception thrown");
+    } catch (UnsupportedOperationException e)
+    {
+      // expected;
+    }
+  }
+
+  /**
+   * Test aligning cdna as per protein alignment.
+   * 
+   * @throws IOException
+   */
+  @Test
+  public void testAlignAs_cdnaAsProtein() throws IOException
+  {
+    // aligned cDNA:
+    Alignment al1 = new FormatAdapter().readFile(TEST_DATA2,
+            AppletFormatAdapter.PASTE, "FASTA");
+    al1.setDataset(null);
+    // unaligned cDNA:
+    Alignment al2 = new FormatAdapter().readFile(TEST_DATA3,
+            AppletFormatAdapter.PASTE, "FASTA");
+    al2.setDataset(null);
+
+    al1.alignAs(al2);
+    assertEquals("ACG---GCUCCA------ACT", al1.getSequenceAt(0)
+            .getSequenceAsString());
+    assertEquals("---CGT---TAACGA---AGT---", al1.getSequenceAt(1)
+            .getSequenceAsString());
+  }
 }