JAL-2413 extracted new method SetUtils.countDisjunction()
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 17 Feb 2017 15:41:39 +0000 (15:41 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 17 Feb 2017 15:41:39 +0000 (15:41 +0000)
src/jalview/analysis/scoremodels/FeatureScoreModel.java
src/jalview/util/SetUtils.java [new file with mode: 0644]
test/jalview/util/SetUtilsTest.java [new file with mode: 0644]

index 6da55c3..6fffcab 100644 (file)
@@ -25,6 +25,7 @@ import jalview.api.analysis.ViewBasedAnalysisI;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.SeqCigar;
 import jalview.datamodel.SequenceFeature;
+import jalview.util.SetUtils;
 
 import java.util.HashMap;
 import java.util.HashSet;
@@ -91,10 +92,9 @@ public class FeatureScoreModel implements ScoreModelI, ViewBasedAnalysisI
         {
           for (int j = i + 1; j < noseqs; j++)
           {
-            int seqDistance = countUnsharedFeatureTypes(sfap.get(seqs[i]),
+            int seqDistance = SetUtils.countDisjunction(sfap.get(seqs[i]),
                     sfap.get(seqs[j]));
             distance[i][j] += seqDistance;
-            // distance[j][i] += distance[i][j];
           }
         }
       }
@@ -116,32 +116,6 @@ public class FeatureScoreModel implements ScoreModelI, ViewBasedAnalysisI
   }
 
   /**
-   * Returns the count of values that are set1 or set2 but not in both
-   * 
-   * @param set1
-   * @param set2
-   * @return
-   */
-  protected int countUnsharedFeatureTypes(Set<String> set1, Set<String> set2)
-  {
-    int size1 = set1.size();
-    int size2 = set2.size();
-    Set<String> smallerSet = size1 < size2 ? set1 : set2;
-    Set<String> largerSet = (smallerSet == set1 ? set2 : set1);
-    int inCommon = 0;
-    for (String k : smallerSet)
-    {
-      if (largerSet.contains(k))
-      {
-        inCommon++;
-      }
-    }
-
-    int notInCommon = (size1 - inCommon) + (size2 - inCommon);
-    return notInCommon;
-  }
-
-  /**
    * Builds and returns a list (one per SeqCigar) of visible feature types at
    * the given column position
    * 
diff --git a/src/jalview/util/SetUtils.java b/src/jalview/util/SetUtils.java
new file mode 100644 (file)
index 0000000..94248cb
--- /dev/null
@@ -0,0 +1,43 @@
+package jalview.util;
+
+import java.util.Set;
+
+public class SetUtils
+{
+  /**
+   * Returns the count of things that are one or other of two sets but not in
+   * both. The sets are not modified.
+   * 
+   * @param set1
+   * @param set2
+   * @return
+   */
+  public static int countDisjunction(Set<? extends Object> set1,
+          Set<? extends Object> set2)
+  {
+    if (set1 == null)
+    {
+      return set2 == null ? 0 : set2.size();
+    }
+    if (set2 == null)
+    {
+      return set1.size();
+    }
+
+    int size1 = set1.size();
+    int size2 = set2.size();
+    Set<? extends Object> smallerSet = size1 < size2 ? set1 : set2;
+    Set<? extends Object> largerSet = (smallerSet == set1 ? set2 : set1);
+    int inCommon = 0;
+    for (Object k : smallerSet)
+    {
+      if (largerSet.contains(k))
+      {
+        inCommon++;
+      }
+    }
+
+    int notInCommon = (size1 - inCommon) + (size2 - inCommon);
+    return notInCommon;
+  }
+}
diff --git a/test/jalview/util/SetUtilsTest.java b/test/jalview/util/SetUtilsTest.java
new file mode 100644 (file)
index 0000000..ad17d4f
--- /dev/null
@@ -0,0 +1,46 @@
+package jalview.util;
+
+import static org.testng.Assert.assertEquals;
+
+import java.awt.Color;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.testng.annotations.Test;
+
+public class SetUtilsTest
+{
+  @Test(groups = "Functional")
+  public void testCountDisjunction()
+  {
+    Set<Color> s1 = new HashSet<Color>();
+    assertEquals(SetUtils.countDisjunction(null, null), 0);
+    assertEquals(SetUtils.countDisjunction(s1, null), 0);
+    assertEquals(SetUtils.countDisjunction(null, s1), 0);
+    s1.add(Color.white);
+    assertEquals(SetUtils.countDisjunction(s1, null), 1);
+    assertEquals(SetUtils.countDisjunction(null, s1), 1);
+    assertEquals(SetUtils.countDisjunction(s1, null), 1);
+    assertEquals(SetUtils.countDisjunction(s1, s1), 0);
+
+    Set<Object> s2 = new HashSet<Object>();
+    assertEquals(SetUtils.countDisjunction(s2, s2), 0);
+    assertEquals(SetUtils.countDisjunction(s1, s2), 1);
+    assertEquals(SetUtils.countDisjunction(s2, s1), 1);
+
+    s1.add(Color.yellow);
+    s1.add(Color.blue);
+    s2.add(new Color(Color.yellow.getRGB()));
+
+    /*
+     * now s1 is {white, yellow, blue}
+     *     s2 is {yellow'}
+     */
+    assertEquals(SetUtils.countDisjunction(s1, s2), 2);
+    s2.add(Color.blue);
+    assertEquals(SetUtils.countDisjunction(s1, s2), 1);
+    s2.add(Color.pink);
+    assertEquals(SetUtils.countDisjunction(s1, s2), 2);
+
+  }
+}