Merge branch 'develop' into Release_2_9_0b1_Branch
[jalview.git] / test / jalview / datamodel / AlignmentTest.java
index f5f271d..24406e6 100644 (file)
@@ -57,21 +57,21 @@ public class AlignmentTest
           "//";
 
   private static final String AA_SEQS_1 = 
-          ">Seq1Name\n" +
+          ">Seq1Name/5-8\n" +
           "K-QY--L\n" +
-          ">Seq2Name\n" +
+          ">Seq2Name/12-15\n" +
           "-R-FP-W-\n";
 
   private static final String CDNA_SEQS_1 = 
-          ">Seq1Name\n" +
+          ">Seq1Name/100-111\n" +
           "AC-GG--CUC-CAA-CT\n" +
-          ">Seq2Name\n" +
+          ">Seq2Name/200-211\n" +
           "-CG-TTA--ACG---AAGT\n";
 
   private static final String CDNA_SEQS_2 = 
-          ">Seq1Name\n" +
+          ">Seq1Name/50-61\n" +
           "GCTCGUCGTACT\n" +
-          ">Seq2Name\n" +
+          ">Seq2Name/60-71\n" +
           "GGGTCAGGCAGT\n";
   // @formatter:on
 
@@ -176,11 +176,7 @@ public class AlignmentTest
      * Make mappings between sequences. The 'aligned cDNA' is playing the role
      * of what would normally be protein here.
      */
-    AlignedCodonFrame acf = new AlignedCodonFrame();
-    MapList ml = new MapList(new int[] { 1, 12 }, new int[] { 1, 12 }, 1, 1);
-    acf.addMap(al2.getSequenceAt(0), al1.getSequenceAt(0), ml);
-    acf.addMap(al2.getSequenceAt(1), al1.getSequenceAt(1), ml);
-    al1.addCodonFrame(acf);
+    makeMappings(al2, al1);
 
     ((Alignment) al2).alignAs(al1, false, true);
     assertEquals("GC-TC--GUC-GTA-CT", al2.getSequenceAt(0)
@@ -200,11 +196,7 @@ public class AlignmentTest
     // see also AlignmentUtilsTests
     AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
     AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
-    AlignedCodonFrame acf = new AlignedCodonFrame();
-    MapList ml = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 }, 3, 1);
-    acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml);
-    acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml);
-    al2.addCodonFrame(acf);
+    makeMappings(al1, al2);
 
     ((Alignment) al2).alignAs(al1, false, true);
     assertEquals("K-Q-Y-L-", al2.getSequenceAt(0).getSequenceAsString());
@@ -212,6 +204,27 @@ public class AlignmentTest
   }
 
   /**
+   * Aligning protein from cDNA for a single sequence. This is the 'simple' case
+   * (as there is no need to compute codon 'alignments') but worth testing
+   * before tackling the multiple sequence case.
+   * 
+   * @throws IOException
+   */
+  @Test(groups = { "Functional" })
+  public void testAlignAs_proteinAsCdna_singleSequence() throws IOException
+  {
+    /*
+     * simplest case remove all gaps
+     */
+    verifyAlignAs(">protein\n-Q-K-\n", ">dna\nCAAaaa\n", "QK");
+
+    /*
+     * with sequence offsets
+     */
+    verifyAlignAs(">protein/12-13\n-Q-K-\n", ">dna/20-25\nCAAaaa\n", "QK");
+  }
+
+  /**
    * Test aligning cdna as per protein alignment.
    * 
    * @throws IOException
@@ -224,11 +237,7 @@ public class AlignmentTest
      */
     AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
     AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
-    AlignedCodonFrame acf = new AlignedCodonFrame();
-    MapList ml = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 }, 3, 1);
-    acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml);
-    acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml);
-    al2.addCodonFrame(acf);
+    makeMappings(al1, al2);
 
     /*
      * Realign DNA; currently keeping existing gaps in introns only
@@ -241,6 +250,88 @@ public class AlignmentTest
   }
 
   /**
+   * Test aligning cdna as per protein - single sequences
+   * 
+   * @throws IOException
+   */
+  @Test(groups = { "Functional" })
+  public void testAlignAs_cdnaAsProtein_singleSequence() throws IOException
+  {
+    /*
+     * simple case insert one gap
+     */
+    verifyAlignAs(">dna\nCAAaaa\n", ">protein\nQ-K\n", "CAA---aaa");
+
+    /*
+     * simple case but with sequence offsets
+     */
+    verifyAlignAs(">dna/5-10\nCAAaaa\n", ">protein/20-21\nQ-K\n",
+            "CAA---aaa");
+
+    /*
+     * insert gaps as per protein, drop gaps within codons
+     */
+    verifyAlignAs(">dna/10-18\nCA-Aa-aa--AGA\n", ">aa/6-8\n-Q-K--R\n",
+            "---CAA---aaa------AGA");
+  }
+
+  /**
+   * Helper method that makes mappings and then aligns the first alignment as
+   * the second
+   * 
+   * @param fromSeqs
+   * @param toSeqs
+   * @param expected
+   * @throws IOException
+   */
+  public void verifyAlignAs(String fromSeqs, String toSeqs, String expected)
+          throws IOException
+  {
+    /*
+     * Load alignments and add mappings from nucleotide to protein (or from
+     * first to second if both the same type)
+     */
+    AlignmentI al1 = loadAlignment(fromSeqs, "FASTA");
+    AlignmentI al2 = loadAlignment(toSeqs, "FASTA");
+    makeMappings(al1, al2);
+
+    /*
+     * Realign DNA; currently keeping existing gaps in introns only
+     */
+    ((Alignment) al1).alignAs(al2, false, true);
+    assertEquals(expected, al1.getSequenceAt(0).getSequenceAsString());
+  }
+
+  /**
+   * Helper method to make mappings from protein to dna sequences, and add the
+   * mappings to the protein alignment
+   * 
+   * @param alFrom
+   * @param alTo
+   */
+  public void makeMappings(AlignmentI alFrom, AlignmentI alTo)
+  {
+    AlignmentI prot = !alFrom.isNucleotide() ? alFrom : alTo;
+    AlignmentI nuc = alFrom == prot ? alTo : alFrom;
+
+    int ratio = (alFrom.isNucleotide() == alTo.isNucleotide() ? 1 : 3);
+
+    AlignedCodonFrame acf = new AlignedCodonFrame();
+
+    for (int i = 0; i < nuc.getHeight(); i++)
+    {
+      SequenceI seqFrom = nuc.getSequenceAt(i);
+      SequenceI seqTo = prot.getSequenceAt(i);
+      MapList ml = new MapList(new int[] { seqFrom.getStart(),
+          seqFrom.getEnd() },
+              new int[] { seqTo.getStart(), seqTo.getEnd() }, ratio, 1);
+      acf.addMap(seqFrom, seqTo, ml);
+    }
+
+    prot.addCodonFrame(acf);
+  }
+
+  /**
    * Test aligning dna as per protein alignment, for the case where there are
    * introns (i.e. some dna sites have no mapping from a peptide).
    * 
@@ -254,18 +345,19 @@ public class AlignmentTest
      */
     String dna1 = "A-Aa-gG-GCC-cT-TT";
     String dna2 = "c--CCGgg-TT--T-AA-A";
-    AlignmentI al1 = loadAlignment(">Seq1\n" + dna1 + "\n>Seq2\n" + dna2
-            + "\n", "FASTA");
-    AlignmentI al2 = loadAlignment(">Seq1\n-P--YK\n>Seq2\nG-T--F\n",
-            "FASTA");
+    AlignmentI al1 = loadAlignment(">Seq1/6-17\n" + dna1
+            + "\n>Seq2/20-31\n" + dna2 + "\n", "FASTA");
+    AlignmentI al2 = loadAlignment(
+            ">Seq1/7-9\n-P--YK\n>Seq2/11-13\nG-T--F\n", "FASTA");
     AlignedCodonFrame acf = new AlignedCodonFrame();
     // Seq1 has intron at dna positions 3,4,9 so splice is AAG GCC TTT
     // Seq2 has intron at dna positions 1,5,6 so splice is CCG TTT AAA
-    MapList ml1 = new MapList(new int[] { 1, 2, 5, 8, 10, 12 }, new int[] {
-        1, 3 }, 3, 1);
+    // TODO sequence offsets
+    MapList ml1 = new MapList(new int[] { 6, 7, 10, 13, 15, 17 }, new int[]
+    { 7, 9 }, 3, 1);
     acf.addMap(al1.getSequenceAt(0), al2.getSequenceAt(0), ml1);
-    MapList ml2 = new MapList(new int[] { 2, 4, 7, 12 },
-            new int[] { 1, 3 }, 3, 1);
+    MapList ml2 = new MapList(new int[] { 21, 23, 26, 31 }, new int[] { 11,
+        13 }, 3, 1);
     acf.addMap(al1.getSequenceAt(1), al2.getSequenceAt(1), ml2);
     al2.addCodonFrame(acf);