JAL-3621 corrected sequence feature sort and test
[jalview.git] / test / jalview / datamodel / features / SequenceFeaturesTest.java
index 3f0eb9a..56512cd 100644 (file)
@@ -5,18 +5,17 @@ import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertSame;
 import static org.testng.Assert.assertTrue;
 
-import jalview.datamodel.SequenceFeature;
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import junit.extensions.PA;
-
 import org.testng.annotations.Test;
 
+import jalview.datamodel.SequenceFeature;
+import junit.extensions.PA;
+
 public class SequenceFeaturesTest
 {
   @Test(groups = "Functional")
@@ -940,14 +939,14 @@ public class SequenceFeaturesTest
     assertFalse(iterator.hasNext());
 
     /*
-     * two types specified - get sorted alphabetically
+     * two types specified - order is preserved
      */
     types = sf.varargToTypes("Metal", "Cath");
     iterator = types.iterator();
     assertTrue(iterator.hasNext());
-    assertSame(iterator.next(), featureStores.get("Cath"));
-    assertTrue(iterator.hasNext());
     assertSame(iterator.next(), featureStores.get("Metal"));
+    assertTrue(iterator.hasNext());
+    assertSame(iterator.next(), featureStores.get("Cath"));
     assertFalse(iterator.hasNext());
 
     /*
@@ -1005,40 +1004,55 @@ public class SequenceFeaturesTest
     assertTrue(store.getFeaturesByOntology(new String[] {}).isEmpty());
     assertTrue(store.getFeaturesByOntology((String[]) null).isEmpty());
   
-    SequenceFeature sf1 = new SequenceFeature("transcript", "desc", 10, 20,
+    SequenceFeature transcriptFeature = new SequenceFeature("transcript", "desc", 10, 20,
             Float.NaN, null);
-    store.add(sf1);
+    store.add(transcriptFeature);
 
-    // mRNA isA transcript; added here 'as if' non-positional
-    // just to show that non-positional features are included in results
-    SequenceFeature sf2 = new SequenceFeature("mRNA", "desc", 0, 0,
+    /*
+     * mRNA is a sub-type of transcript; added here 'as if' non-positional
+     * just to show that non-positional features are included in results
+     */
+    SequenceFeature mrnaFeature = new SequenceFeature("mRNA", "desc", 0, 0,
             Float.NaN, null);
-    store.add(sf2);
+    store.add(mrnaFeature);
 
-    SequenceFeature sf3 = new SequenceFeature("Pfam", "desc", 30, 40,
+    SequenceFeature pfamFeature = new SequenceFeature("Pfam", "desc", 30, 40,
             Float.NaN, null);
-    store.add(sf3);
+    store.add(pfamFeature);
 
+    /*
+     * "transcript" matches both itself and the sub-term "mRNA"
+     */
     features = store.getFeaturesByOntology("transcript");
     assertEquals(features.size(), 2);
-    assertTrue(features.contains(sf1));
-    assertTrue(features.contains(sf2));
+    assertTrue(features.contains(transcriptFeature));
+    assertTrue(features.contains(mrnaFeature));
 
+    /*
+     * "mRNA" matches itself but not parent term "transcript"
+     */
     features = store.getFeaturesByOntology("mRNA");
     assertEquals(features.size(), 1);
-    assertTrue(features.contains(sf2));
+    assertTrue(features.contains(mrnaFeature));
 
+    /*
+     * "pfam" is not an SO term but is included as an exact match
+     */
     features = store.getFeaturesByOntology("mRNA", "Pfam");
     assertEquals(features.size(), 2);
-    assertTrue(features.contains(sf2));
-    assertTrue(features.contains(sf3));
+    assertTrue(features.contains(mrnaFeature));
+    assertTrue(features.contains(pfamFeature));
+
+    features = store.getFeaturesByOntology("sequence_variant");
+    assertTrue(features.isEmpty());
   }
 
   @Test(groups = "Functional")
   public void testSortFeatures()
   {
-    List<SequenceFeature> sfs = new ArrayList<SequenceFeature>();
-    SequenceFeature sf1 = new SequenceFeature("Pfam", "desc", 30, 80,
+    List<SequenceFeature> sfs = new ArrayList<>();
+    SequenceFeature sf1 = new SequenceFeature("Pfam", "desc", 30,
+            60,
             Float.NaN, null);
     sfs.add(sf1);
     SequenceFeature sf2 = new SequenceFeature("Rfam", "desc", 40, 50,
@@ -1047,18 +1061,34 @@ public class SequenceFeaturesTest
     SequenceFeature sf3 = new SequenceFeature("Rfam", "desc", 50, 60,
             Float.NaN, null);
     sfs.add(sf3);
+    SequenceFeature sf4 = new SequenceFeature("Xfam", "desc", 30,
+            80,
+            Float.NaN, null);
+    sfs.add(sf4);
+    SequenceFeature sf5 = new SequenceFeature("Xfam", "desc", 30,
+            90,
+            Float.NaN, null);
+    sfs.add(sf5);
 
-    // sort by end position descending
+    /*
+     * sort by end position descending, order unchanged if matched
+     */
     SequenceFeatures.sortFeatures(sfs, false);
-    assertSame(sfs.get(0), sf1);
-    assertSame(sfs.get(1), sf3);
-    assertSame(sfs.get(2), sf2);
+    assertSame(sfs.get(0), sf5); // end 90
+    assertSame(sfs.get(1), sf4); // end 80
+    assertSame(sfs.get(2), sf1); // end 60, start 50
+    assertSame(sfs.get(3), sf3); // end 60, start 30
+    assertSame(sfs.get(4), sf2); // end 50
 
-    // sort by start position ascending
+    /*
+     * resort {5, 4, 1, 3, 2} by start position ascending, end descending
+     */
     SequenceFeatures.sortFeatures(sfs, true);
-    assertSame(sfs.get(0), sf1);
-    assertSame(sfs.get(1), sf2);
-    assertSame(sfs.get(2), sf3);
+    assertSame(sfs.get(0), sf5); // start 30, end 90
+    assertSame(sfs.get(1), sf4); // start 30, end 80
+    assertSame(sfs.get(2), sf1); // start 30, end 60
+    assertSame(sfs.get(3), sf2); // start 40
+    assertSame(sfs.get(4), sf3); // start 50
   }
 
   @Test(groups = "Functional")
@@ -1247,4 +1277,18 @@ public class SequenceFeaturesTest
     assertTrue(store.isOntologyTerm("junk", new String[] {}));
     assertTrue(store.isOntologyTerm("junk", (String[]) null));
   }
+
+  @Test(groups = "Functional")
+  public void testDeleteAll()
+  {
+    SequenceFeaturesI store = new SequenceFeatures();
+    assertFalse(store.hasFeatures());
+    store.deleteAll();
+    assertFalse(store.hasFeatures());
+    store.add(new SequenceFeature("Cath", "Desc", 12, 20, 0f, "Group"));
+    store.add(new SequenceFeature("Pfam", "Desc", 6, 12, 2f, "Group2"));
+    assertTrue(store.hasFeatures());
+    store.deleteAll();
+    assertFalse(store.hasFeatures());
+  }
 }