JAL-1270 constructor tests added, compiler warnings removed
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 30 Nov 2015 09:16:49 +0000 (09:16 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 30 Nov 2015 09:16:49 +0000 (09:16 +0000)
test/jalview/datamodel/SequenceTest.java

index 9c306a3..851caf0 100644 (file)
 package jalview.datamodel;
 
 import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertFalse;
+import static org.testng.AssertJUnit.assertNotNull;
 import static org.testng.AssertJUnit.assertNull;
 import static org.testng.AssertJUnit.assertSame;
 import static org.testng.AssertJUnit.assertTrue;
 
+import jalview.datamodel.PDBEntry.Type;
+
 import java.util.Arrays;
 import java.util.List;
+import java.util.Vector;
 
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
@@ -155,23 +160,22 @@ public class SequenceTest
     assertEquals(2, anns.length);
     assertSame(annotation, anns[0]);
     assertSame(annotation2, anns[1]);
-
   }
 
   @Test(groups = { "Functional" })
   public void testGetStartGetEnd()
   {
-    SequenceI seq = new Sequence("test", "ABCDEF");
-    assertEquals(1, seq.getStart());
-    assertEquals(6, seq.getEnd());
+    SequenceI sq = new Sequence("test", "ABCDEF");
+    assertEquals(1, sq.getStart());
+    assertEquals(6, sq.getEnd());
 
-    seq = new Sequence("test", "--AB-C-DEF--");
-    assertEquals(1, seq.getStart());
-    assertEquals(6, seq.getEnd());
+    sq = new Sequence("test", "--AB-C-DEF--");
+    assertEquals(1, sq.getStart());
+    assertEquals(6, sq.getEnd());
 
-    seq = new Sequence("test", "----");
-    assertEquals(1, seq.getStart());
-    assertEquals(0, seq.getEnd()); // ??
+    sq = new Sequence("test", "----");
+    assertEquals(1, sq.getStart());
+    assertEquals(0, sq.getEnd()); // ??
   }
 
   /**
@@ -181,24 +185,24 @@ public class SequenceTest
   @Test(groups = { "Functional" })
   public void testFindIndex()
   {
-    SequenceI seq = new Sequence("test", "ABCDEF");
-    assertEquals(0, seq.findIndex(0));
-    assertEquals(1, seq.findIndex(1));
-    assertEquals(5, seq.findIndex(5));
-    assertEquals(6, seq.findIndex(6));
-    assertEquals(6, seq.findIndex(9));
-
-    seq = new Sequence("test", "-A--B-C-D-E-F--");
-    assertEquals(2, seq.findIndex(1));
-    assertEquals(5, seq.findIndex(2));
-    assertEquals(7, seq.findIndex(3));
+    SequenceI sq = new Sequence("test", "ABCDEF");
+    assertEquals(0, sq.findIndex(0));
+    assertEquals(1, sq.findIndex(1));
+    assertEquals(5, sq.findIndex(5));
+    assertEquals(6, sq.findIndex(6));
+    assertEquals(6, sq.findIndex(9));
+
+    sq = new Sequence("test", "-A--B-C-D-E-F--");
+    assertEquals(2, sq.findIndex(1));
+    assertEquals(5, sq.findIndex(2));
+    assertEquals(7, sq.findIndex(3));
 
     // before start returns 0
-    assertEquals(0, seq.findIndex(0));
-    assertEquals(0, seq.findIndex(-1));
+    assertEquals(0, sq.findIndex(0));
+    assertEquals(0, sq.findIndex(-1));
 
     // beyond end returns last residue column
-    assertEquals(13, seq.findIndex(99));
+    assertEquals(13, sq.findIndex(99));
 
   }
 
@@ -209,65 +213,65 @@ public class SequenceTest
   @Test(groups = { "Functional" })
   public void testFindPosition()
   {
-    SequenceI seq = new Sequence("test", "ABCDEF");
-    assertEquals(1, seq.findPosition(0));
-    assertEquals(6, seq.findPosition(5));
+    SequenceI sq = new Sequence("test", "ABCDEF");
+    assertEquals(1, sq.findPosition(0));
+    assertEquals(6, sq.findPosition(5));
     // assertEquals(-1, seq.findPosition(6)); // fails
 
-    seq = new Sequence("test", "AB-C-D--");
-    assertEquals(1, seq.findPosition(0));
-    assertEquals(2, seq.findPosition(1));
+    sq = new Sequence("test", "AB-C-D--");
+    assertEquals(1, sq.findPosition(0));
+    assertEquals(2, sq.findPosition(1));
     // gap position 'finds' residue to the right (not the left as per javadoc)
-    assertEquals(3, seq.findPosition(2));
-    assertEquals(3, seq.findPosition(3));
-    assertEquals(4, seq.findPosition(4));
-    assertEquals(4, seq.findPosition(5));
+    assertEquals(3, sq.findPosition(2));
+    assertEquals(3, sq.findPosition(3));
+    assertEquals(4, sq.findPosition(4));
+    assertEquals(4, sq.findPosition(5));
     // returns 1 more than sequence length if off the end ?!?
-    assertEquals(5, seq.findPosition(6));
-    assertEquals(5, seq.findPosition(7));
-
-    seq = new Sequence("test", "--AB-C-DEF--");
-    assertEquals(1, seq.findPosition(0));
-    assertEquals(1, seq.findPosition(1));
-    assertEquals(1, seq.findPosition(2));
-    assertEquals(2, seq.findPosition(3));
-    assertEquals(3, seq.findPosition(4));
-    assertEquals(3, seq.findPosition(5));
-    assertEquals(4, seq.findPosition(6));
-    assertEquals(4, seq.findPosition(7));
-    assertEquals(5, seq.findPosition(8));
-    assertEquals(6, seq.findPosition(9));
-    assertEquals(7, seq.findPosition(10));
-    assertEquals(7, seq.findPosition(11));
+    assertEquals(5, sq.findPosition(6));
+    assertEquals(5, sq.findPosition(7));
+
+    sq = new Sequence("test", "--AB-C-DEF--");
+    assertEquals(1, sq.findPosition(0));
+    assertEquals(1, sq.findPosition(1));
+    assertEquals(1, sq.findPosition(2));
+    assertEquals(2, sq.findPosition(3));
+    assertEquals(3, sq.findPosition(4));
+    assertEquals(3, sq.findPosition(5));
+    assertEquals(4, sq.findPosition(6));
+    assertEquals(4, sq.findPosition(7));
+    assertEquals(5, sq.findPosition(8));
+    assertEquals(6, sq.findPosition(9));
+    assertEquals(7, sq.findPosition(10));
+    assertEquals(7, sq.findPosition(11));
   }
 
   @Test(groups = { "Functional" })
   public void testDeleteChars()
   {
-    SequenceI seq = new Sequence("test", "ABCDEF");
-    assertEquals(1, seq.getStart());
-    assertEquals(6, seq.getEnd());
-    seq.deleteChars(2, 3);
-    assertEquals("ABDEF", seq.getSequenceAsString());
-    assertEquals(1, seq.getStart());
-    assertEquals(5, seq.getEnd());
-
-    seq = new Sequence("test", "ABCDEF");
-    seq.deleteChars(0, 2);
-    assertEquals("CDEF", seq.getSequenceAsString());
-    assertEquals(3, seq.getStart());
-    assertEquals(6, seq.getEnd());
+    SequenceI sq = new Sequence("test", "ABCDEF");
+    assertEquals(1, sq.getStart());
+    assertEquals(6, sq.getEnd());
+    sq.deleteChars(2, 3);
+    assertEquals("ABDEF", sq.getSequenceAsString());
+    assertEquals(1, sq.getStart());
+    assertEquals(5, sq.getEnd());
+
+    sq = new Sequence("test", "ABCDEF");
+    sq.deleteChars(0, 2);
+    assertEquals("CDEF", sq.getSequenceAsString());
+    assertEquals(3, sq.getStart());
+    assertEquals(6, sq.getEnd());
   }
 
   @Test(groups = { "Functional" })
   public void testInsertCharAt()
   {
     // non-static methods:
-    SequenceI seq = new Sequence("test", "ABCDEF");
-    seq.insertCharAt(0, 'z');
-    assertEquals("zABCDEF", seq.getSequenceAsString());
-    seq.insertCharAt(2, 2, 'x');
-    assertEquals("zAxxBCDEF", seq.getSequenceAsString());
+    SequenceI sq = new Sequence("test", "ABCDEF");
+    sq.insertCharAt(0, 'z');
+    assertEquals("zABCDEF", sq.getSequenceAsString());
+    sq.insertCharAt(2, 2, 'x');
+    assertEquals("zAxxBCDEF", sq.getSequenceAsString());
 
     // for static method see StringUtilsTest
   }
@@ -279,9 +283,9 @@ public class SequenceTest
   @Test(groups = { "Functional" })
   public void testGapMap()
   {
-    SequenceI seq = new Sequence("test", "-A--B-CD-E--F-");
-    seq.createDatasetSequence();
-    assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(seq.gapMap()));
+    SequenceI sq = new Sequence("test", "-A--B-CD-E--F-");
+    sq.createDatasetSequence();
+    assertEquals("[1, 4, 6, 7, 9, 12]", Arrays.toString(sq.gapMap()));
   }
 
   /**
@@ -291,17 +295,17 @@ public class SequenceTest
   @Test(groups = { "Functional" })
   public void testGetSequenceFeatures()
   {
-    SequenceI seq = new Sequence("test", "GATCAT");
-    seq.createDatasetSequence();
+    SequenceI sq = new Sequence("test", "GATCAT");
+    sq.createDatasetSequence();
 
-    assertNull(seq.getSequenceFeatures());
+    assertNull(sq.getSequenceFeatures());
 
     /*
      * SequenceFeature on sequence
      */
     SequenceFeature sf = new SequenceFeature();
-    seq.addSequenceFeature(sf);
-    SequenceFeature[] sfs = seq.getSequenceFeatures();
+    sq.addSequenceFeature(sf);
+    SequenceFeature[] sfs = sq.getSequenceFeatures();
     assertEquals(1, sfs.length);
     assertSame(sf, sfs[0]);
 
@@ -310,16 +314,16 @@ public class SequenceTest
      * sequence
      */
     SequenceFeature sf2 = new SequenceFeature();
-    seq.getDatasetSequence().addSequenceFeature(sf2);
-    sfs = seq.getSequenceFeatures();
+    sq.getDatasetSequence().addSequenceFeature(sf2);
+    sfs = sq.getSequenceFeatures();
     assertEquals(1, sfs.length);
     assertSame(sf, sfs[0]);
 
     /*
      * SequenceFeature on dataset sequence only
      */
-    seq.setSequenceFeatures(null);
-    sfs = seq.getSequenceFeatures();
+    sq.setSequenceFeatures(null);
+    sfs = sq.getSequenceFeatures();
     assertEquals(1, sfs.length);
     assertSame(sf2, sfs[0]);
 
@@ -327,9 +331,9 @@ public class SequenceTest
      * Corrupt case - no SequenceFeature, dataset's dataset is the original
      * sequence. Test shows no infinite loop results.
      */
-    seq.getDatasetSequence().setSequenceFeatures(null);
-    seq.getDatasetSequence().setDatasetSequence(seq); // loop!
-    assertNull(seq.getSequenceFeatures());
+    sq.getDatasetSequence().setSequenceFeatures(null);
+    sq.getDatasetSequence().setDatasetSequence(sq); // loop!
+    assertNull(sq.getSequenceFeatures());
   }
 
   /**
@@ -346,8 +350,8 @@ public class SequenceTest
      * right. Also it returns a non-existent residue position for a gap beyond
      * the sequence.
      */
-    Sequence seq = new Sequence("TestSeq", "AB.C-D E.");
-    int[] map = seq.findPositionMap();
+    Sequence sq = new Sequence("TestSeq", "AB.C-D E.");
+    int[] map = sq.findPositionMap();
     assertEquals(Arrays.toString(new int[] { 1, 2, 3, 3, 4, 4, 5, 5, 6 }),
             Arrays.toString(map));
   }
@@ -358,18 +362,18 @@ public class SequenceTest
   @Test(groups = { "Functional" })
   public void testGetSubsequence()
   {
-    SequenceI seq = new Sequence("TestSeq", "ABCDEFG");
-    seq.createDatasetSequence();
+    SequenceI sq = new Sequence("TestSeq", "ABCDEFG");
+    sq.createDatasetSequence();
 
     // positions are base 0, end position is exclusive
-    SequenceI subseq = seq.getSubSequence(2, 4);
+    SequenceI subseq = sq.getSubSequence(2, 4);
 
     assertEquals("CD", subseq.getSequenceAsString());
     // start/end are base 1 positions
     assertEquals(3, subseq.getStart());
     assertEquals(4, subseq.getEnd());
     // subsequence shares the full dataset sequence
-    assertSame(seq.getDatasetSequence(), subseq.getDatasetSequence());
+    assertSame(sq.getDatasetSequence(), subseq.getDatasetSequence());
   }
 
   /**
@@ -378,13 +382,13 @@ public class SequenceTest
   @Test(groups = { "Functional" })
   public void testDeriveSequence_existingDataset()
   {
-    SequenceI seq = new Sequence("Seq1", "CD");
-    seq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
-    seq.setStart(3);
-    seq.setEnd(4);
-    SequenceI derived = seq.deriveSequence();
+    SequenceI sq = new Sequence("Seq1", "CD");
+    sq.setDatasetSequence(new Sequence("Seq1", "ABCDEF"));
+    sq.setStart(3);
+    sq.setEnd(4);
+    SequenceI derived = sq.deriveSequence();
     assertEquals("CD", derived.getSequenceAsString());
-    assertSame(seq.getDatasetSequence(), derived.getDatasetSequence());
+    assertSame(sq.getDatasetSequence(), derived.getDatasetSequence());
   }
 
   /**
@@ -393,10 +397,10 @@ public class SequenceTest
   @Test(groups = { "Functional" })
   public void testDeriveSequence_noDatasetUngapped()
   {
-    SequenceI seq = new Sequence("Seq1", "ABCDEF");
-    assertEquals(1, seq.getStart());
-    assertEquals(6, seq.getEnd());
-    SequenceI derived = seq.deriveSequence();
+    SequenceI sq = new Sequence("Seq1", "ABCDEF");
+    assertEquals(1, sq.getStart());
+    assertEquals(6, sq.getEnd());
+    SequenceI derived = sq.deriveSequence();
     assertEquals("ABCDEF", derived.getSequenceAsString());
     assertEquals("ABCDEF", derived.getDatasetSequence()
             .getSequenceAsString());
@@ -408,13 +412,108 @@ public class SequenceTest
   @Test(groups = { "Functional" })
   public void testDeriveSequence_noDatasetGapped()
   {
-    SequenceI seq = new Sequence("Seq1", "AB-C.D EF");
-    assertEquals(1, seq.getStart());
-    assertEquals(6, seq.getEnd());
-    assertNull(seq.getDatasetSequence());
-    SequenceI derived = seq.deriveSequence();
+    SequenceI sq = new Sequence("Seq1", "AB-C.D EF");
+    assertEquals(1, sq.getStart());
+    assertEquals(6, sq.getEnd());
+    assertNull(sq.getDatasetSequence());
+    SequenceI derived = sq.deriveSequence();
     assertEquals("AB-C.D EF", derived.getSequenceAsString());
     assertEquals("ABCDEF", derived.getDatasetSequence()
             .getSequenceAsString());
   }
+
+  @Test(groups = { "Functional" })
+  public void testCopyConstructor_noDataset()
+  {
+    SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
+    seq1.setDescription("description");
+    seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
+            1.3d));
+    seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
+            12.4f, "group"));
+    seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
+    seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
+    
+    SequenceI copy = new Sequence(seq1);
+
+    assertNull(copy.getDatasetSequence());
+
+    verifyCopiedSequence(seq1, copy);
+
+    // copy has a copy of the DBRefEntry
+    // this is murky - DBrefs are only copied for dataset sequences
+    // where the test for 'dataset sequence' is 'dataset is null'
+    // but that doesn't distinguish it from an aligned sequence
+    // which has not yet generated a dataset sequence
+    // NB getDBRef looks inside dataset sequence if not null
+    DBRefEntry[] dbrefs = copy.getDBRef();
+    assertEquals(1, dbrefs.length);
+    assertFalse(dbrefs[0] == seq1.getDBRef()[0]);
+    assertTrue(dbrefs[0].equals(seq1.getDBRef()[0]));
+  }
+
+  @Test(groups = { "Functional" })
+  public void testCopyConstructor_withDataset()
+  {
+    SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
+    seq1.createDatasetSequence();
+    seq1.setDescription("description");
+    seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
+            1.3d));
+    seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
+            12.4f, "group"));
+    seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
+    // here we add DBRef to the dataset sequence:
+    seq1.getDatasetSequence().addDBRef(
+            new DBRefEntry("EMBL", "1.2", "AZ12345"));
+
+    SequenceI copy = new Sequence(seq1);
+
+    assertNotNull(copy.getDatasetSequence());
+    assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
+
+    verifyCopiedSequence(seq1, copy);
+
+    // getDBRef looks inside dataset sequence and this is shared,
+    // so holds the same dbref objects
+    DBRefEntry[] dbrefs = copy.getDBRef();
+    assertEquals(1, dbrefs.length);
+    assertSame(dbrefs[0], seq1.getDBRef()[0]);
+  }
+
+  /**
+   * Helper to make assertions about a copied sequence
+   * 
+   * @param seq1
+   * @param copy
+   */
+  protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
+  {
+    // verify basic properties:
+    assertEquals(copy.getName(), seq1.getName());
+    assertEquals(copy.getDescription(), seq1.getDescription());
+    assertEquals(copy.getStart(), seq1.getStart());
+    assertEquals(copy.getEnd(), seq1.getEnd());
+    assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
+
+    // copy has a copy of the annotation:
+    AlignmentAnnotation[] anns = copy.getAnnotation();
+    assertEquals(1, anns.length);
+    assertFalse(anns[0] == seq1.getAnnotation()[0]);
+    assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
+    assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
+    assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
+
+    // copy has a copy of the sequence feature:
+    SequenceFeature[] sfs = copy.getSequenceFeatures();
+    assertEquals(1, sfs.length);
+    assertFalse(sfs[0] == seq1.getSequenceFeatures()[0]);
+    assertTrue(sfs[0].equals(seq1.getSequenceFeatures()[0]));
+
+    // copy has a copy of the PDB entry
+    Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
+    assertEquals(1, pdbs.size());
+    assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
+    assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
+  }
 }