JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / test / jalview / datamodel / AlignmentTest.java
index 58e024f..7958e9b 100644 (file)
@@ -134,19 +134,23 @@ public class AlignmentTest
    *          - the alignmentI object to verify (either alignment or dataset)
    * @param raiseAssert
    *          - when set, testng assertions are raised.
-   *          @param message
-   *          - null or a string message to prepend to the assert failed messages.
+   * @param message
+   *          - null or a string message to prepend to the assert failed
+   *          messages.
    * @return true if alignment references were in order, otherwise false.
    */
   public static boolean verifyAlignmentDatasetRefs(AlignmentI alignment,
           boolean raiseAssert, String message)
   {
-    if (message==null) { message = ""; }
+    if (message == null)
+    {
+      message = "";
+    }
     if (alignment == null)
     {
       if (raiseAssert)
       {
-        Assert.fail(message+"Alignment for verification was null.");
+        Assert.fail(message + "Alignment for verification was null.");
       }
       return false;
     }
@@ -161,7 +165,8 @@ public class AlignmentTest
         {
           if (raiseAssert)
           {
-            Assert.fail(message+" Alignment contained a sequence who's dataset sequence has a second dataset reference.");
+            Assert.fail(message
+                    + " Alignment contained a sequence who's dataset sequence has a second dataset reference.");
           }
           return false;
         }
@@ -169,12 +174,14 @@ public class AlignmentTest
         {
           if (raiseAssert)
           {
-            Assert.fail(message+" Alignment contained a sequence who's dataset sequence was not in the dataset.");
+            Assert.fail(message
+                    + " Alignment contained a sequence who's dataset sequence was not in the dataset.");
           }
           return false;
         }
       }
-      return verifyAlignmentDatasetRefs(alignment.getDataset(), raiseAssert, message);
+      return verifyAlignmentDatasetRefs(alignment.getDataset(),
+              raiseAssert, message);
     }
     else
     {
@@ -187,7 +194,8 @@ public class AlignmentTest
         {
           if (raiseAssert)
           {
-            Assert.fail(message+" Dataset contained a sequence with non-null dataset reference (ie not a dataset sequence!)");
+            Assert.fail(message
+                    + " Dataset contained a sequence with non-null dataset reference (ie not a dataset sequence!)");
           }
           return false;
         }
@@ -216,7 +224,8 @@ public class AlignmentTest
                 {
                   if (raiseAssert)
                   {
-                    Assert.fail(message+" DBRefEntry for sequence in alignment had map to sequence which was not a dataset sequence");
+                    Assert.fail(message
+                            + " DBRefEntry for sequence in alignment had map to sequence which was not a dataset sequence");
                   }
                   return false;
 
@@ -225,7 +234,8 @@ public class AlignmentTest
                 {
                   if (raiseAssert)
                   {
-                    Assert.fail(message+" DBRefEntry for sequence in alignment had map to sequence not in dataset");
+                    Assert.fail(message
+                            + " DBRefEntry for sequence in alignment had map to sequence not in dataset");
                   }
                   return false;
                 }
@@ -245,7 +255,8 @@ public class AlignmentTest
             {
               if (raiseAssert)
               {
-                Assert.fail(message+" CodonFrame-SSM-FromSeq is not a dataset sequence");
+                Assert.fail(message
+                        + " CodonFrame-SSM-FromSeq is not a dataset sequence");
               }
               return false;
             }
@@ -254,7 +265,8 @@ public class AlignmentTest
 
               if (raiseAssert)
               {
-                Assert.fail(message+" CodonFrame-SSM-FromSeq is not contained in dataset");
+                Assert.fail(message
+                        + " CodonFrame-SSM-FromSeq is not contained in dataset");
               }
               return false;
             }
@@ -262,7 +274,8 @@ public class AlignmentTest
             {
               if (raiseAssert)
               {
-                Assert.fail(message+" CodonFrame-SSM-Mapping-ToSeq is not a dataset sequence");
+                Assert.fail(message
+                        + " CodonFrame-SSM-Mapping-ToSeq is not a dataset sequence");
               }
               return false;
             }
@@ -271,7 +284,8 @@ public class AlignmentTest
 
               if (raiseAssert)
               {
-                Assert.fail(message+" CodonFrame-SSM-Mapping-ToSeq is not contained in dataset");
+                Assert.fail(message
+                        + " CodonFrame-SSM-Mapping-ToSeq is not contained in dataset");
               }
               return false;
             }
@@ -335,6 +349,7 @@ public class AlignmentTest
                       + msg);
     }
   }
+
   @Test(groups = { "Functional" })
   public void testVerifyAlignmentDatasetRefs()
   {
@@ -342,16 +357,13 @@ public class AlignmentTest
             "TTTTTT");
 
     // construct simple valid alignment dataset
-    Alignment al = new Alignment(new SequenceI[] {
-        sq1, sq2 });
+    Alignment al = new Alignment(new SequenceI[] { sq1, sq2 });
     // expect this to pass
     assertVerifyAlignment(al, true, "Simple valid alignment didn't verify");
 
     // check test for sequence->datasetSequence validity
     sq1.setDatasetSequence(sq2);
-    assertVerifyAlignment(
-            al,
-            false,
+    assertVerifyAlignment(al, false,
             "didn't detect dataset sequence with a dataset sequence reference.");
 
     sq1.setDatasetSequence(null);
@@ -428,28 +440,43 @@ public class AlignmentTest
    * checks that the sequence data for an alignment's dataset is non-redundant.
    * Fails if there are sequences with same id, sequence, start, and.
    */
+
   public static void assertDatasetIsNormalised(AlignmentI al)
   {
-    if (al.getDataset()!=null)
+    assertDatasetIsNormalised(al, null);
+  }
+
+  /**
+   * checks that the sequence data for an alignment's dataset is non-redundant.
+   * Fails if there are sequences with same id, sequence, start, and.
+   * 
+   * @param al
+   *          - alignment to verify
+   * @param message
+   *          - null or message prepended to exception message.
+   */
+  public static void assertDatasetIsNormalised(AlignmentI al, String message)
+  {
+    if (al.getDataset() != null)
     {
-      assertDatasetIsNormalised(al.getDataset());
+      assertDatasetIsNormalised(al.getDataset(), message);
       return;
     }
     /*
      * look for pairs of sequences with same ID, start, end, and sequence
      */
     List<SequenceI> seqSet = al.getSequences();
-    for (int p=0;p<seqSet.size(); p++)
+    for (int p = 0; p < seqSet.size(); p++)
     {
       SequenceI pSeq = seqSet.get(p);
-      for (int q=p+1; q<seqSet.size(); q++)
+      for (int q = p + 1; q < seqSet.size(); q++)
       {
         SequenceI qSeq = seqSet.get(q);
-        if (pSeq.getStart()!=qSeq.getStart())
+        if (pSeq.getStart() != qSeq.getStart())
         {
           continue;
         }
-        if (pSeq.getEnd()!=qSeq.getEnd())
+        if (pSeq.getEnd() != qSeq.getEnd())
         {
           continue;
         }
@@ -461,11 +488,13 @@ public class AlignmentTest
         {
           continue;
         }
-        Assert.fail("Found similar sequences at position "+p+" and "+q+"\n"+pSeq.toString());
+        Assert.fail((message == null ? "" : message + " :")
+                + "Found similar sequences at position " + p + " and " + q
+                + "\n" + pSeq.toString());
       }
     }
   }
-  
+
   @Test(groups = { "Functional", "Asserts" })
   public void testAssertDatasetIsNormalised()
   {
@@ -540,6 +569,7 @@ public class AlignmentTest
       Assert.fail("Expected identical sequence to raise exception.");
     }
   }
+
   /*
    * Read in Stockholm format test data including secondary structure
    * annotations.
@@ -996,6 +1026,7 @@ public class AlignmentTest
     assertAlignmentDatasetRefs(align,
             "addSequence broke dataset reference integrity");
   }
+
   @Test(groups = "Functional")
   public void getVisibleStartAndEndIndexTest()
   {
@@ -1024,4 +1055,68 @@ public class AlignmentTest
     assertEquals(1, startEnd[0]);
     assertEquals(23, startEnd[1]);
   }
+
+  /**
+   * Tests that dbrefs with mappings to sequence get updated if the sequence
+   * acquires a dataset sequence
+   */
+  @Test(groups = "Functional")
+  public void testCreateDataset_updateDbrefMappings()
+  {
+    SequenceI pep = new Sequence("pep", "ASD");
+    SequenceI dna = new Sequence("dna", "aaaGCCTCGGATggg");
+    SequenceI cds = new Sequence("cds", "GCCTCGGAT");
+
+    // add dbref from dna to peptide
+    DBRefEntry dbr = new DBRefEntry("UNIPROT", "", "pep");
+    dbr.setMap(new Mapping(pep, new MapList(new int[] { 4, 15 }, new int[] {
+        1, 4 }, 3, 1)));
+    dna.addDBRef(dbr);
+
+    // add dbref from dna to peptide
+    DBRefEntry dbr2 = new DBRefEntry("UNIPROT", "", "pep");
+    dbr2.setMap(new Mapping(pep, new MapList(new int[] { 1, 12 }, new int[]
+    { 1, 4 }, 3, 1)));
+    cds.addDBRef(dbr2);
+
+    // add dbref from peptide to dna
+    DBRefEntry dbr3 = new DBRefEntry("EMBL", "", "dna");
+    dbr3.setMap(new Mapping(dna, new MapList(new int[] { 1, 4 }, new int[] {
+        4, 15 }, 1, 3)));
+    pep.addDBRef(dbr3);
+
+    // add dbref from peptide to cds
+    DBRefEntry dbr4 = new DBRefEntry("EMBLCDS", "", "cds");
+    dbr4.setMap(new Mapping(cds, new MapList(new int[] { 1, 4 }, new int[] {
+        1, 12 }, 1, 3)));
+    pep.addDBRef(dbr4);
+
+    AlignmentI protein = new Alignment(new SequenceI[] { pep });
+
+    /*
+     * create the alignment dataset
+     */
+    ((Alignment) protein).createDatasetAlignment();
+
+    AlignmentI ds = protein.getDataset();
+
+    // should be 3 sequences in dataset
+    assertEquals(3, ds.getHeight());
+    assertTrue(ds.getSequences().contains(pep.getDatasetSequence()));
+    assertTrue(ds.getSequences().contains(dna));
+    assertTrue(ds.getSequences().contains(cds));
+
+    /*
+     * verify peptide.cdsdbref.peptidedbref is now mapped to peptide dataset
+     */
+    DBRefEntry[] dbRefs = pep.getDBRefs();
+    assertEquals(2, dbRefs.length);
+    assertSame(dna, dbRefs[0].map.to);
+    assertSame(cds, dbRefs[1].map.to);
+    assertEquals(1, dna.getDBRefs().length);
+    assertSame(pep.getDatasetSequence(), dna.getDBRefs()[0].map.to);
+    assertEquals(1, cds.getDBRefs().length);
+    assertSame(pep.getDatasetSequence(), cds.getDBRefs()[0].map.to);
+  }
+
 }