From 48e6a09db5cad4143d1b9fbebf1b6996fc95ed07 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Fri, 17 Feb 2017 15:41:39 +0000 Subject: [PATCH] JAL-2413 extracted new method SetUtils.countDisjunction() --- .../analysis/scoremodels/FeatureScoreModel.java | 30 +------------ src/jalview/util/SetUtils.java | 43 ++++++++++++++++++ test/jalview/util/SetUtilsTest.java | 46 ++++++++++++++++++++ 3 files changed, 91 insertions(+), 28 deletions(-) create mode 100644 src/jalview/util/SetUtils.java create mode 100644 test/jalview/util/SetUtilsTest.java diff --git a/src/jalview/analysis/scoremodels/FeatureScoreModel.java b/src/jalview/analysis/scoremodels/FeatureScoreModel.java index 6da55c3..6fffcab 100644 --- a/src/jalview/analysis/scoremodels/FeatureScoreModel.java +++ b/src/jalview/analysis/scoremodels/FeatureScoreModel.java @@ -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 set1, Set set2) - { - int size1 = set1.size(); - int size2 = set2.size(); - Set smallerSet = size1 < size2 ? set1 : set2; - Set 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 index 0000000..94248cb --- /dev/null +++ b/src/jalview/util/SetUtils.java @@ -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 set1, + Set 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 smallerSet = size1 < size2 ? set1 : set2; + Set 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 index 0000000..ad17d4f --- /dev/null +++ b/test/jalview/util/SetUtilsTest.java @@ -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 s1 = new HashSet(); + 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 s2 = new HashSet(); + 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); + + } +} -- 1.7.10.2