import jalview.datamodel.AlignmentView;
import jalview.datamodel.SeqCigar;
import jalview.datamodel.SequenceFeature;
+import jalview.util.SetUtils;
import java.util.HashMap;
import java.util.HashSet;
{
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];
}
}
}
}
/**
- * 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
*
--- /dev/null
+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;
+ }
+}
--- /dev/null
+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);
+
+ }
+}