JAL-653 AlignedCodonFrame collections changed from Set to List
[jalview.git] / test / jalview / analysis / AlignmentUtilsTests.java
index 2beacfe..dd99bc2 100644 (file)
@@ -45,10 +45,8 @@ import jalview.util.MappingUtils;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
-import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -281,7 +279,7 @@ public class AlignmentUtilsTests
    * @throws IOException
    */
   @Test(groups = { "Functional" })
-  public void testMapProteinToCdna_noXrefs() throws IOException
+  public void testMapProteinAlignmentToCdna_noXrefs() throws IOException
   {
     List<SequenceI> protseqs = new ArrayList<SequenceI>();
     protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
@@ -298,7 +296,7 @@ public class AlignmentUtilsTests
     AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[4]));
     cdna.setDataset(null);
 
-    assertTrue(AlignmentUtils.mapProteinToCdna(protein, cdna));
+    assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
 
     // 3 mappings made, each from 1 to 1 sequence
     assertEquals(3, protein.getCodonFrames().size());
@@ -439,7 +437,6 @@ public class AlignmentUtilsTests
   @Test(groups = { "Functional" })
   public void testAlignSequenceAs_withMapping_withUnmappedProtein()
   {
-
     /*
      * Exons at codon 2 (AAA) and 4 (TTT) mapped to A and P
      */
@@ -450,35 +447,37 @@ public class AlignmentUtilsTests
      * Expect alignment does nothing (aborts realignment). Change this test
      * first if different behaviour wanted.
      */
-    checkAlignSequenceAs("GGGAAACCCTTTGGG", "-A-L-P-", false, false, map,
-            "GGGAAACCCTTTGGG");
+    checkAlignSequenceAs("gggAAAcccTTTggg", "-A-L-P-", false, false, map,
+            "gggAAAccc---TTTggg");
   }
 
   /**
    * Helper method that performs and verifies the method under test.
    * 
-   * @param dnaSeq
-   * @param proteinSeq
+   * @param alignee
+   *          the sequence to be realigned
+   * @param alignModel
+   *          the sequence whose alignment is to be copied
    * @param preserveMappedGaps
    * @param preserveUnmappedGaps
    * @param map
    * @param expected
    */
-  protected void checkAlignSequenceAs(final String dnaSeq,
-          final String proteinSeq, final boolean preserveMappedGaps,
+  protected void checkAlignSequenceAs(final String alignee,
+          final String alignModel, final boolean preserveMappedGaps,
           final boolean preserveUnmappedGaps, MapList map,
           final String expected)
   {
-    SequenceI dna = new Sequence("Seq1", dnaSeq);
-    dna.createDatasetSequence();
-    SequenceI protein = new Sequence("Seq1", proteinSeq);
-    protein.createDatasetSequence();
+    SequenceI alignMe = new Sequence("Seq1", alignee);
+    alignMe.createDatasetSequence();
+    SequenceI alignFrom = new Sequence("Seq2", alignModel);
+    alignFrom.createDatasetSequence();
     AlignedCodonFrame acf = new AlignedCodonFrame();
-    acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
+    acf.addMap(alignMe.getDatasetSequence(), alignFrom.getDatasetSequence(), map);
 
-    AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-',
+    AlignmentUtils.alignSequenceAs(alignMe, alignFrom, acf, "---", '-',
             preserveMappedGaps, preserveUnmappedGaps);
-    assertEquals(expected, dna.getSequenceAsString());
+    assertEquals(expected, alignMe.getSequenceAsString());
   }
 
   /**
@@ -550,7 +549,7 @@ public class AlignmentUtilsTests
     acf.addMap(dna1.getDatasetSequence(), prot1.getDatasetSequence(), map);
     acf.addMap(dna2.getDatasetSequence(), prot2.getDatasetSequence(), map);
     acf.addMap(dna3.getDatasetSequence(), prot3.getDatasetSequence(), map);
-    protein.setCodonFrames(Collections.singleton(acf));
+    protein.setCodonFrames(new ArrayList<AlignedCodonFrame>());
 
     /*
      * Translated codon order is [1,2,3] [1,3,4] [4,5,6] [4,5,7] [5,6,7] [7,8,9]
@@ -617,7 +616,7 @@ public class AlignmentUtilsTests
    * @throws IOException
    */
   @Test(groups = { "Functional" })
-  public void testMapProteinToCdna_withStartAndStopCodons()
+  public void testMapProteinAlignmentToCdna_withStartAndStopCodons()
           throws IOException
   {
     List<SequenceI> protseqs = new ArrayList<SequenceI>();
@@ -638,7 +637,7 @@ public class AlignmentUtilsTests
     AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[4]));
     cdna.setDataset(null);
 
-    assertTrue(AlignmentUtils.mapProteinToCdna(protein, cdna));
+    assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
 
     // 3 mappings made, each from 1 to 1 sequence
     assertEquals(3, protein.getCodonFrames().size());
@@ -710,7 +709,7 @@ public class AlignmentUtilsTests
    * @throws IOException
    */
   @Test(groups = { "Functional" })
-  public void testMapProteinToCdna_withXrefs() throws IOException
+  public void testMapProteinAlignmentToCdna_withXrefs() throws IOException
   {
     List<SequenceI> protseqs = new ArrayList<SequenceI>();
     protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
@@ -739,7 +738,7 @@ public class AlignmentUtilsTests
     // A11111 should be mapped to V12347
     // A55555 is spare and has no xref so is not mapped
 
-    assertTrue(AlignmentUtils.mapProteinToCdna(protein, cdna));
+    assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
 
     // 4 protein mappings made for 3 proteins, 2 to V12345, 1 each to V12346/7
     assertEquals(3, protein.getCodonFrames().size());
@@ -786,7 +785,8 @@ public class AlignmentUtilsTests
    * @throws IOException
    */
   @Test(groups = { "Functional" })
-  public void testMapProteinToCdna_prioritiseXrefs() throws IOException
+  public void testMapProteinAlignmentToCdna_prioritiseXrefs()
+          throws IOException
   {
     List<SequenceI> protseqs = new ArrayList<SequenceI>();
     protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
@@ -806,7 +806,7 @@ public class AlignmentUtilsTests
     // A11111 should then be mapped to the unmapped V12346
     dnaseqs.get(1).addDBRef(new DBRefEntry("UNIPROT", "1", "V12345"));
 
-    assertTrue(AlignmentUtils.mapProteinToCdna(protein, cdna));
+    assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
 
     // 2 protein mappings made
     assertEquals(2, protein.getCodonFrames().size());
@@ -1013,7 +1013,7 @@ public class AlignmentUtilsTests
     pep1.createDatasetSequence();
     pep2.createDatasetSequence();
 
-    Set<AlignedCodonFrame> mappings = new HashSet<AlignedCodonFrame>();
+    List<AlignedCodonFrame> mappings = new ArrayList<AlignedCodonFrame>();
     MapList map = new MapList(new int[] { 4, 6, 10, 12 },
             new int[] { 1, 2 }, 3, 1);
     AlignedCodonFrame acf = new AlignedCodonFrame();
@@ -1158,7 +1158,7 @@ public class AlignmentUtilsTests
      * 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>();
+    List<AlignedCodonFrame> mappings = new ArrayList<AlignedCodonFrame>();
     // map ...GGG...TTT to GF
     MapList map = new MapList(new int[] { 4, 6, 10, 12 },
             new int[] { 1, 2 }, 3, 1);
@@ -1279,4 +1279,64 @@ public class AlignmentUtilsTests
     assertTrue(AlignmentUtils.isMappable(al1, al2));
     assertTrue(AlignmentUtils.isMappable(al2, al1));
   }
+
+  /**
+   * Test creating a mapping when the sequences involved do not start at residue
+   * 1
+   * 
+   * @throws IOException
+   */
+  @Test(groups = { "Functional" })
+  public void testMapProteinSequenceToCdna_forSubsequence()
+          throws IOException
+  {
+    SequenceI prot = new Sequence("UNIPROT|V12345", "E-I--Q", 10, 12);
+    prot.createDatasetSequence();
+
+    SequenceI dna = new Sequence("EMBL|A33333", "GAA--AT-C-CAG", 40, 48);
+    dna.createDatasetSequence();
+
+    MapList map = AlignmentUtils.mapProteinSequenceToCdna(prot, dna);
+    assertEquals(10, map.getToLowest());
+    assertEquals(12, map.getToHighest());
+    assertEquals(40, map.getFromLowest());
+    assertEquals(48, map.getFromHighest());
+  }
+
+  /**
+   * Test for the alignSequenceAs method where we have protein mapped to protein
+   */
+  @Test(groups = { "Functional" })
+  public void testAlignSequenceAs_mappedProteinProtein()
+  {
+  
+    SequenceI alignMe = new Sequence("Match", "MGAASEV");
+    alignMe.createDatasetSequence();
+    SequenceI alignFrom = new Sequence("Query", "LQTGYMGAASEVMFSPTRR");
+    alignFrom.createDatasetSequence();
+
+    AlignedCodonFrame acf = new AlignedCodonFrame();
+    // this is like a domain or motif match of part of a peptide sequence
+    MapList map = new MapList(new int[] { 6, 12 }, new int[] { 1, 7 }, 1, 1);
+    acf.addMap(alignFrom.getDatasetSequence(),
+            alignMe.getDatasetSequence(), map);
+    
+    AlignmentUtils.alignSequenceAs(alignMe, alignFrom, acf, "-", '-', true,
+            true);
+    assertEquals("-----MGAASEV-------", alignMe.getSequenceAsString());
+  }
+
+  /**
+   * Test for the alignSequenceAs method where there are trailing unmapped
+   * residues in the model sequence
+   */
+  @Test(groups = { "Functional" })
+  public void testAlignSequenceAs_withTrailingPeptide()
+  {
+    // map first 3 codons to KPF; G is a trailing unmapped residue
+    MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
+  
+    checkAlignSequenceAs("AAACCCTTT", "K-PFG", true, true, map,
+            "AAA---CCCTTT---");
+  }
 }