JAL-3746 apply copyright to tests
[jalview.git] / test / jalview / datamodel / features / FeatureStoreTest.java
index db21c2f..b0a1ac8 100644 (file)
@@ -1,7 +1,28 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.datamodel.features;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertSame;
 import static org.testng.Assert.assertTrue;
 
 import jalview.datamodel.SequenceFeature;
@@ -19,8 +40,7 @@ public class FeatureStoreTest
   public void testFindFeatures_nonNested()
   {
     FeatureStore fs = new FeatureStore();
-    fs.addFeature(new SequenceFeature("", "", 10, 20, Float.NaN,
-            null));
+    fs.addFeature(new SequenceFeature("", "", 10, 20, Float.NaN, null));
     // same range different description
     fs.addFeature(new SequenceFeature("", "desc", 10, 20, Float.NaN, null));
     fs.addFeature(new SequenceFeature("", "", 15, 25, Float.NaN, null));
@@ -184,58 +204,6 @@ public class FeatureStoreTest
     assertTrue(overlaps.contains(sf));
   }
 
-  /**
-   * Tests for the method that returns false for an attempt to add a feature
-   * that would enclose, or be enclosed by, another feature
-   */
-  @Test(groups = "Functional")
-  public void testAddNonNestedFeature()
-  {
-    FeatureStore fs = new FeatureStore();
-
-    String type = "Domain";
-    SequenceFeature sf1 = new SequenceFeature(type, type, 10, 20,
-            Float.NaN, null);
-    assertTrue(fs.addNonNestedFeature(sf1));
-
-    // co-located feature is ok
-    SequenceFeature sf2 = new SequenceFeature(type, type, 10, 20,
-            Float.NaN, null);
-    assertTrue(fs.addNonNestedFeature(sf2));
-
-    // overlap left is ok
-    SequenceFeature sf3 = new SequenceFeature(type, type, 5, 15, Float.NaN,
-            null);
-    assertTrue(fs.addNonNestedFeature(sf3));
-
-    // overlap right is ok
-    SequenceFeature sf4 = new SequenceFeature(type, type, 15, 25,
-            Float.NaN, null);
-    assertTrue(fs.addNonNestedFeature(sf4));
-
-    // add enclosing feature is not ok
-    SequenceFeature sf5 = new SequenceFeature(type, type, 10, 21,
-            Float.NaN, null);
-    assertFalse(fs.addNonNestedFeature(sf5));
-    SequenceFeature sf6 = new SequenceFeature(type, type, 4, 15, Float.NaN,
-            null);
-    assertFalse(fs.addNonNestedFeature(sf6));
-    SequenceFeature sf7 = new SequenceFeature(type, type, 1, 50, Float.NaN,
-            null);
-    assertFalse(fs.addNonNestedFeature(sf7));
-
-    // add enclosed feature is not ok
-    SequenceFeature sf8 = new SequenceFeature(type, type, 10, 19,
-            Float.NaN, null);
-    assertFalse(fs.addNonNestedFeature(sf8));
-    SequenceFeature sf9 = new SequenceFeature(type, type, 16, 25,
-            Float.NaN, null);
-    assertFalse(fs.addNonNestedFeature(sf9));
-    SequenceFeature sf10 = new SequenceFeature(type, type, 7, 7, Float.NaN,
-            null);
-    assertFalse(fs.addNonNestedFeature(sf10));
-  }
-
   @Test(groups = "Functional")
   public void testGetPositionalFeatures()
   {
@@ -264,8 +232,8 @@ public class FeatureStoreTest
             Float.NaN, null);
     store.addFeature(sf6);
     // contact feature
-    SequenceFeature sf7 = new SequenceFeature("Disulphide bond", "desc",
-            18, 45, Float.NaN, null);
+    SequenceFeature sf7 = new SequenceFeature("Disulphide bond", "desc", 18,
+            45, Float.NaN, null);
     store.addFeature(sf7);
 
     List<SequenceFeature> features = store.getPositionalFeatures();
@@ -308,8 +276,8 @@ public class FeatureStoreTest
     /*
      * contact feature deletion
      */
-    SequenceFeature sf3 = new SequenceFeature("", "Disulphide Bond", 11,
-            23, Float.NaN, null);
+    SequenceFeature sf3 = new SequenceFeature("", "Disulphide Bond", 11, 23,
+            Float.NaN, null);
     store.addFeature(sf3);
     assertEquals(store.getPositionalFeatures().size(), 1);
     assertTrue(store.getPositionalFeatures().contains(sf3));
@@ -360,10 +328,10 @@ public class FeatureStoreTest
   {
     FeatureStore fs = new FeatureStore();
 
-    SequenceFeature sf1 = new SequenceFeature("Cath", "", 10, 20,
-            Float.NaN, null);
-    SequenceFeature sf2 = new SequenceFeature("Cath", "", 10, 20,
-            Float.NaN, null);
+    SequenceFeature sf1 = new SequenceFeature("Cath", "", 10, 20, Float.NaN,
+            null);
+    SequenceFeature sf2 = new SequenceFeature("Cath", "", 10, 20, Float.NaN,
+            null);
 
     assertTrue(fs.addFeature(sf1));
     assertEquals(fs.getFeatureCount(true), 1); // positional
@@ -416,8 +384,8 @@ public class FeatureStoreTest
     /*
      * non-nested feature
      */
-    SequenceFeature sf1 = new SequenceFeature("Cath", "", 10, 20,
-            Float.NaN, null);
+    SequenceFeature sf1 = new SequenceFeature("Cath", "", 10, 20, Float.NaN,
+            null);
     fs.addFeature(sf1);
     assertFalse(fs.isEmpty());
     assertEquals(fs.getFeatureCount(true), 1);
@@ -440,7 +408,8 @@ public class FeatureStoreTest
     /*
      * contact feature
      */
-    sf1 = new SequenceFeature("Disulfide bond", "", 19, 49, Float.NaN, null);
+    sf1 = new SequenceFeature("Disulfide bond", "", 19, 49, Float.NaN,
+            null);
     fs.addFeature(sf1);
     assertFalse(fs.isEmpty());
     assertEquals(fs.getFeatureCount(true), 1);
@@ -452,19 +421,17 @@ public class FeatureStoreTest
      * sf2, sf3 added as nested features
      */
     sf1 = new SequenceFeature("Cath", "", 19, 49, Float.NaN, null);
-    SequenceFeature sf2 = new SequenceFeature("Cath", "", 20, 40,
-            Float.NaN, null);
-    SequenceFeature sf3 = new SequenceFeature("Cath", "", 25, 35,
-            Float.NaN, null);
+    SequenceFeature sf2 = new SequenceFeature("Cath", "", 20, 40, Float.NaN,
+            null);
+    SequenceFeature sf3 = new SequenceFeature("Cath", "", 25, 35, Float.NaN,
+            null);
     fs.addFeature(sf1);
     fs.addFeature(sf2);
     fs.addFeature(sf3);
     assertEquals(fs.getFeatureCount(true), 3);
     assertTrue(fs.delete(sf1));
     assertEquals(fs.getFeatureCount(true), 2);
-    // FeatureStore should now only contain features in the NCList
-    assertTrue(fs.nonNestedFeatures.isEmpty());
-    assertEquals(fs.nestedFeatures.size(), 2);
+    assertEquals(fs.features.size(), 2);
     assertFalse(fs.isEmpty());
     assertTrue(fs.delete(sf2));
     assertEquals(fs.getFeatureCount(true), 1);
@@ -481,7 +448,8 @@ public class FeatureStoreTest
     assertTrue(fs.getFeatureGroups(true).isEmpty());
     assertTrue(fs.getFeatureGroups(false).isEmpty());
 
-    SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 10, 20, 1f, "group1");
+    SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 10, 20, 1f,
+            "group1");
     fs.addFeature(sf1);
     Set<String> groups = fs.getFeatureGroups(true);
     assertEquals(groups.size(), 1);
@@ -490,7 +458,8 @@ public class FeatureStoreTest
     /*
      * add another feature of the same group, delete one, delete both
      */
-    SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 20, 30, 1f, "group1");
+    SequenceFeature sf2 = new SequenceFeature("Cath", "desc", 20, 30, 1f,
+            "group1");
     fs.addFeature(sf2);
     groups = fs.getFeatureGroups(true);
     assertEquals(groups.size(), 1);
@@ -503,11 +472,14 @@ public class FeatureStoreTest
     groups = fs.getFeatureGroups(true);
     assertTrue(fs.getFeatureGroups(true).isEmpty());
 
-    SequenceFeature sf3 = new SequenceFeature("Cath", "desc", 20, 30, 1f, "group2");
+    SequenceFeature sf3 = new SequenceFeature("Cath", "desc", 20, 30, 1f,
+            "group2");
     fs.addFeature(sf3);
-    SequenceFeature sf4 = new SequenceFeature("Cath", "desc", 20, 30, 1f, "Group2");
+    SequenceFeature sf4 = new SequenceFeature("Cath", "desc", 20, 30, 1f,
+            "Group2");
     fs.addFeature(sf4);
-    SequenceFeature sf5 = new SequenceFeature("Cath", "desc", 20, 30, 1f, null);
+    SequenceFeature sf5 = new SequenceFeature("Cath", "desc", 20, 30, 1f,
+            null);
     fs.addFeature(sf5);
     groups = fs.getFeatureGroups(true);
     assertEquals(groups.size(), 3);
@@ -560,8 +532,8 @@ public class FeatureStoreTest
     assertEquals(fs.getTotalFeatureLength(), 93);
 
     // contact features count 1
-    SequenceFeature sf3 = new SequenceFeature("disulphide bond", "desc",
-            15, 35, 1f, "group1");
+    SequenceFeature sf3 = new SequenceFeature("disulphide bond", "desc", 15,
+            35, 1f, "group1");
     fs.addFeature(sf3);
     assertEquals(fs.getTotalFeatureLength(), 94);
 
@@ -581,9 +553,10 @@ public class FeatureStoreTest
     /*
      * positional feature
      */
-    SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 10, 20, 1f, "group1");
+    SequenceFeature sf1 = new SequenceFeature("Cath", "desc", 10, 20, 1f,
+            "group1");
     assertEquals(FeatureStore.getFeatureLength(sf1), 11);
-  
+
     /*
      * non-positional feature
      */
@@ -594,8 +567,8 @@ public class FeatureStoreTest
     /*
      * contact feature counts 1
      */
-    SequenceFeature sf3 = new SequenceFeature("Disulphide Bond", "desc",
-            14, 28, 1f, "AGroup");
+    SequenceFeature sf3 = new SequenceFeature("Disulphide Bond", "desc", 14,
+            28, 1f, "AGroup");
     assertEquals(FeatureStore.getFeatureLength(sf3), 1);
   }
 
@@ -693,7 +666,7 @@ public class FeatureStoreTest
   public void testListContains()
   {
     assertFalse(FeatureStore.listContains(null, null));
-    List<SequenceFeature> features = new ArrayList<SequenceFeature>();
+    List<SequenceFeature> features = new ArrayList<>();
     assertFalse(FeatureStore.listContains(features, null));
 
     SequenceFeature sf1 = new SequenceFeature("type1", "desc1", 20, 30, 3f,
@@ -774,7 +747,7 @@ public class FeatureStoreTest
   public void testShiftFeatures()
   {
     FeatureStore fs = new FeatureStore();
-    assertFalse(fs.shiftFeatures(1));
+    assertFalse(fs.shiftFeatures(0, 1)); // nothing to do
 
     SequenceFeature sf1 = new SequenceFeature("Cath", "", 2, 5, 0f, null);
     fs.addFeature(sf1);
@@ -790,9 +763,9 @@ public class FeatureStoreTest
     fs.addFeature(sf4);
 
     /*
-     * shift features right by 5
+     * shift all features right by 5
      */
-    assertTrue(fs.shiftFeatures(5));
+    assertTrue(fs.shiftFeatures(0, 5));
 
     // non-positional features untouched:
     List<SequenceFeature> nonPos = fs.getNonPositionalFeatures();
@@ -818,7 +791,7 @@ public class FeatureStoreTest
      * feature at [7-10] should be removed
      * feature at [13-19] should become [1-4] 
      */
-    assertTrue(fs.shiftFeatures(-15));
+    assertTrue(fs.shiftFeatures(0, -15));
     pos = fs.getPositionalFeatures();
     assertEquals(pos.size(), 2);
     SequenceFeatures.sortFeatures(pos, true);
@@ -826,6 +799,32 @@ public class FeatureStoreTest
     assertEquals(pos.get(0).getEnd(), 4);
     assertEquals(pos.get(1).getBegin(), 13);
     assertEquals(pos.get(1).getEnd(), 22);
+
+    /*
+     * shift right by 4 from position 2 onwards
+     * feature at [1-4] unchanged, feature at [13-22] shifts
+     */
+    assertTrue(fs.shiftFeatures(2, 4));
+    pos = fs.getPositionalFeatures();
+    assertEquals(pos.size(), 2);
+    SequenceFeatures.sortFeatures(pos, true);
+    assertEquals(pos.get(0).getBegin(), 1);
+    assertEquals(pos.get(0).getEnd(), 4);
+    assertEquals(pos.get(1).getBegin(), 17);
+    assertEquals(pos.get(1).getEnd(), 26);
+
+    /*
+     * shift right by 4 from position 18 onwards
+     * should be no change
+     */
+    SequenceFeature f1 = pos.get(0);
+    SequenceFeature f2 = pos.get(1);
+    assertFalse(fs.shiftFeatures(18, 4)); // no update
+    pos = fs.getPositionalFeatures();
+    assertEquals(pos.size(), 2);
+    SequenceFeatures.sortFeatures(pos, true);
+    assertSame(pos.get(0), f1);
+    assertSame(pos.get(1), f2);
   }
 
   @Test(groups = "Functional")
@@ -842,9 +841,9 @@ public class FeatureStoreTest
     assertEquals(features.size(), 2);
     assertTrue(features.contains(sf1));
     assertTrue(features.contains(sf2));
-    assertTrue(store.nonNestedFeatures.contains(sf1));
-    assertTrue(store.nestedFeatures.contains(sf2));
-  
+    assertTrue(store.features.contains(sf1));
+    assertTrue(store.features.contains(sf2));
+
     /*
      * delete the first feature
      */
@@ -866,10 +865,10 @@ public class FeatureStoreTest
   public void testContains()
   {
     FeatureStore fs = new FeatureStore();
-    SequenceFeature sf1 = new SequenceFeature("Cath", "", 10, 20,
-            Float.NaN, "group1");
-    SequenceFeature sf2 = new SequenceFeature("Cath", "", 10, 20,
-            Float.NaN, "group2");
+    SequenceFeature sf1 = new SequenceFeature("Cath", "", 10, 20, Float.NaN,
+            "group1");
+    SequenceFeature sf2 = new SequenceFeature("Cath", "", 10, 20, Float.NaN,
+            "group2");
     SequenceFeature sf3 = new SequenceFeature("Cath", "", 0, 0, Float.NaN,
             "group1");
     SequenceFeature sf4 = new SequenceFeature("Cath", "", 0, 0, 0f,
@@ -895,8 +894,8 @@ public class FeatureStoreTest
     /*
      * add a nested feature
      */
-    SequenceFeature sf7 = new SequenceFeature("Cath", "", 12, 16,
-            Float.NaN, "group1");
+    SequenceFeature sf7 = new SequenceFeature("Cath", "", 12, 16, Float.NaN,
+            "group1");
     fs.addFeature(sf7);
     assertTrue(fs.contains(sf7));
     assertTrue(fs.contains(new SequenceFeature(sf7)));