From: gmungoc Date: Thu, 6 Aug 2020 08:49:23 +0000 (+0100) Subject: JAL-3490 match count independent of contiguous matches count X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fbug%2FJAL-3490findSpanHiddenGaps;p=jalview.git JAL-3490 match count independent of contiguous matches count --- diff --git a/src/jalview/analysis/Finder.java b/src/jalview/analysis/Finder.java index 0d8665f..c545c7f 100644 --- a/src/jalview/analysis/Finder.java +++ b/src/jalview/analysis/Finder.java @@ -471,10 +471,7 @@ public class Finder implements FinderI */ int[] truePositions = searchedSequenceMap .locateInFrom(matchStartPosition, matchEndPosition); - for (int i = 0; i < truePositions.length - 1; i += 2) - { - searchResults.addResult(seq, truePositions[i], truePositions[i + 1]); - } + searchResults.addResult(seq, truePositions); } /** diff --git a/src/jalview/appletgui/AlignmentPanel.java b/src/jalview/appletgui/AlignmentPanel.java index e9081b0..365f886 100644 --- a/src/jalview/appletgui/AlignmentPanel.java +++ b/src/jalview/appletgui/AlignmentPanel.java @@ -344,7 +344,7 @@ public class AlignmentPanel extends Panel int verticalOffset, boolean redrawOverview, boolean centre) { // do we need to scroll the panel? - if (results != null && results.getSize() > 0) + if (results != null && results.getCount() > 0) { AlignmentI alignment = av.getAlignment(); int seqIndex = alignment.findIndex(results); diff --git a/src/jalview/appletgui/Finder.java b/src/jalview/appletgui/Finder.java index 4772580..a78e41c 100644 --- a/src/jalview/appletgui/Finder.java +++ b/src/jalview/appletgui/Finder.java @@ -211,13 +211,13 @@ public class Finder extends Panel implements ActionListener String message = (idMatches.size() > 0) ? "" + idMatches.size() + " IDs" : ""; if (idMatches.size() > 0 && searchResults != null - && searchResults.getSize() > 0) + && searchResults.getCount() > 0) { message += " and "; } if (searchResults != null) { - message += searchResults.getSize() + " subsequence matches."; + message += searchResults.getCount() + " subsequence matches."; } ap.alignFrame.statusBar.setText(MessageManager .formatMessage("label.search_results", new String[] diff --git a/src/jalview/datamodel/SearchResults.java b/src/jalview/datamodel/SearchResults.java index 31736e5..4867b5b 100755 --- a/src/jalview/datamodel/SearchResults.java +++ b/src/jalview/datamodel/SearchResults.java @@ -33,6 +33,7 @@ import java.util.List; */ public class SearchResults implements SearchResultsI { + private int count; private List matches = new ArrayList<>(); @@ -168,11 +169,30 @@ public class SearchResults implements SearchResultsI if (!matches.contains(m)) { matches.add(m); + count++; } return m; } @Override + public void addResult(SequenceI seq, int[] positions) + { + /* + * we only increment the match count by 1 - or not at all, + * if the matches are all duplicates of existing + */ + int beforeCount = count; + for (int i = 0; i < positions.length - 1; i += 2) + { + addResult(seq, positions[i], positions[i + 1]); + } + if (count > beforeCount) + { + count = beforeCount + 1; + } + } + + @Override public boolean involvesSequence(SequenceI sequence) { SequenceI ds = sequence.getDatasetSequence(); @@ -282,9 +302,9 @@ public class SearchResults implements SearchResultsI } @Override - public int getSize() + public int getCount() { - return matches.size(); + return count; } @Override diff --git a/src/jalview/datamodel/SearchResultsI.java b/src/jalview/datamodel/SearchResultsI.java index 3c3ad4e..c89f363 100644 --- a/src/jalview/datamodel/SearchResultsI.java +++ b/src/jalview/datamodel/SearchResultsI.java @@ -41,6 +41,17 @@ public interface SearchResultsI SearchResultMatchI addResult(SequenceI seq, int start, int end); /** + * Adds one ore more [start, end] ranges to the results (unless already added + * to avoid duplicates). This method only increments the match count by 1. + * This is for the case where a match spans ignored hidden residues - it is + * formally two or more contiguous matches, but only counted as one match. + * + * @param seq + * @param positions + */ + void addResult(SequenceI seq, int[] positions); + + /** * adds all match results in the argument to this set * * @param toAdd @@ -74,11 +85,13 @@ public interface SearchResultsI int[] getResults(SequenceI sequence, int start, int end); /** - * Returns the number of matches found + * Returns the number of matches found. Note that if a match straddles ignored + * hidden residues, it is counted as one match, although formally recorded as + * two (or more) contiguous matched sequence regions * * @return */ - int getSize(); + int getCount(); /** * Returns true if no search result matches are held. diff --git a/src/jalview/gui/Finder.java b/src/jalview/gui/Finder.java index 217a390..537c323 100755 --- a/src/jalview/gui/Finder.java +++ b/src/jalview/gui/Finder.java @@ -304,11 +304,11 @@ public class Finder extends GFinder : ""; if (searchResults != null) { - if (idMatch.size() > 0 && searchResults.getSize() > 0) + if (idMatch.size() > 0 && searchResults.getCount() > 0) { message += " and "; } - message += searchResults.getSize() + message += searchResults.getCount() + " subsequence matches found."; } JvOptionPane.showInternalMessageDialog(this, message, null, diff --git a/src/jalview/gui/SeqPanel.java b/src/jalview/gui/SeqPanel.java index 75bf0cc..78b0dd1 100644 --- a/src/jalview/gui/SeqPanel.java +++ b/src/jalview/gui/SeqPanel.java @@ -895,11 +895,12 @@ public class SeqPanel extends JPanel AlignFrame af = Desktop.getAlignFrameFor(complement); FeatureRendererModel fr2 = af.getFeatureRenderer(); - int j = results.getSize(); + List matches = results.getResults(); + int j = matches.size(); List infos = new ArrayList<>(); for (int i = 0; i < j; i++) { - SearchResultMatchI match = results.getResults().get(i); + SearchResultMatchI match = matches.get(i); int pos = match.getStart(); if (pos == match.getEnd()) { diff --git a/test/jalview/analysis/FinderTest.java b/test/jalview/analysis/FinderTest.java index 725be33..95e6c0d 100644 --- a/test/jalview/analysis/FinderTest.java +++ b/test/jalview/analysis/FinderTest.java @@ -103,7 +103,7 @@ public class FinderTest f.findNext("E.H", false, false, false); // 'E, any character, H' // should match seq2 efH only SearchResultsI sr = f.getSearchResults(); - assertEquals(sr.getSize(), 1); + assertEquals(sr.getCount(), 1); List matches = sr.getResults(); assertSame(matches.get(0).getSequence(), al.getSequenceAt(1)); assertEquals(matches.get(0).getStart(), 5); @@ -113,7 +113,7 @@ public class FinderTest f.findAll("E.H", false, false, false); // 'E, any character, H' // should match seq2 efH and seq3 EFH sr = f.getSearchResults(); - assertEquals(sr.getSize(), 2); + assertEquals(sr.getCount(), 2); matches = sr.getResults(); assertSame(matches.get(0).getSequence(), al.getSequenceAt(1)); assertSame(matches.get(1).getSequence(), al.getSequenceAt(2)); @@ -136,7 +136,7 @@ public class FinderTest */ f.findNext("9", false, false, false); SearchResultsI sr = f.getSearchResults(); - assertEquals(sr.getSize(), 1); + assertEquals(sr.getCount(), 1); List matches = sr.getResults(); assertSame(matches.get(0).getSequence(), al.getSequenceAt(0)); assertEquals(matches.get(0).getStart(), 9); @@ -151,7 +151,7 @@ public class FinderTest al.getSequenceAt(0).setName("Q9XA0"); f.findAll("9", false, false, false); sr = f.getSearchResults(); - assertEquals(sr.getSize(), 2); + assertEquals(sr.getCount(), 2); matches = sr.getResults(); assertSame(matches.get(0).getSequence(), al.getSequenceAt(0)); assertSame(matches.get(1).getSequence(), al.getSequenceAt(3)); @@ -196,7 +196,7 @@ public class FinderTest PA.setValue(f, "residueIndex", 0); f.findNext("e", false, false, false); // matches in sequence assertTrue(f.getIdMatches().isEmpty()); - assertEquals(f.getSearchResults().getSize(), 1); + assertEquals(f.getSearchResults().getCount(), 1); List matches = f.getSearchResults().getResults(); assertEquals(matches.get(0).getStart(), 5); assertEquals(matches.get(0).getEnd(), 5); @@ -272,7 +272,7 @@ public class FinderTest assertSame(f.getIdMatches().get(0), al2.getSequenceAt(0)); assertSame(f.getIdMatches().get(1), al2.getSequenceAt(1)); - assertEquals(f.getSearchResults().getSize(), 2); + assertEquals(f.getSearchResults().getCount(), 2); SearchResultMatchI match = f.getSearchResults().getResults().get(0); assertSame(match.getSequence(), al2.getSequenceAt(1)); assertEquals(match.getStart(), 5); @@ -320,7 +320,7 @@ public class FinderTest assertEquals(f.getIdMatches().size(), 1); assertSame(f.getIdMatches().get(0), al2.getSequenceAt(4)); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 2); + assertEquals(searchResults.getCount(), 2); SearchResultMatchI match = searchResults.getResults().get(0); assertSame(match.getSequence(), al2.getSequenceAt(4)); assertEquals(match.getStart(), 4); @@ -347,7 +347,7 @@ public class FinderTest FinderI f = new Finder(av); f.findNext("EfH", false, false, false); SearchResultsI searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); SearchResultMatchI match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(1)); assertEquals(match.getStart(), 5); @@ -359,7 +359,7 @@ public class FinderTest f = new Finder(av); f.findNext("I", false, false, false); // find next: seq1/16 searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(0)); assertEquals(match.getStart(), 16); @@ -367,7 +367,7 @@ public class FinderTest f.findNext("I", false, false, false); // find next: seq1/18 searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(0)); assertEquals(match.getStart(), 18); @@ -375,7 +375,7 @@ public class FinderTest f.findNext("I", false, false, false); // find next: seq2/8 searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(1)); assertEquals(match.getStart(), 8); @@ -389,7 +389,7 @@ public class FinderTest */ f.findNext("I", false, false, false); // find next: seq1/16 searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(0)); assertEquals(match.getStart(), 16); @@ -406,7 +406,7 @@ public class FinderTest Finder f = new Finder(av); f.findAll("M+", false, false, false); SearchResultsI searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); SearchResultMatchI match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(3)); assertEquals(match.getStart(), 4); // dataset sequence positions @@ -422,7 +422,7 @@ public class FinderTest Finder f = new Finder(av); f.findAll("EfH", false, false, false); SearchResultsI searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 2); + assertEquals(searchResults.getCount(), 2); SearchResultMatchI match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(1)); assertEquals(match.getStart(), 5); @@ -437,7 +437,7 @@ public class FinderTest */ f.findAll("I", false, false, false); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 3); + assertEquals(searchResults.getCount(), 3); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(0)); assertEquals(match.getStart(), 16); @@ -465,7 +465,7 @@ public class FinderTest */ f.findAll("BC", true, false, false); SearchResultsI searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 2); + assertEquals(searchResults.getCount(), 2); SearchResultMatchI match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(0)); assertEquals(match.getStart(), 9); @@ -481,7 +481,7 @@ public class FinderTest f = new Finder(av); f.findAll("bc", true, false, false); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(2)); assertEquals(match.getStart(), 1); @@ -513,7 +513,7 @@ public class FinderTest f.findNext("b", false, false, false); assertTrue(f.getIdMatches().isEmpty()); SearchResultsI searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); SearchResultMatchI match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(1)); assertEquals(match.getStart(), 2); @@ -530,7 +530,7 @@ public class FinderTest f.findNext("d", false, false, false); assertTrue(f.getIdMatches().isEmpty()); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(1)); assertEquals(match.getStart(), 4); @@ -538,7 +538,7 @@ public class FinderTest f.findNext("d", false, false, false); assertTrue(f.getIdMatches().isEmpty()); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(2)); assertEquals(match.getStart(), 3); @@ -572,7 +572,7 @@ public class FinderTest assertSame(f.getIdMatches().get(0), al.getSequenceAt(1)); assertSame(f.getIdMatches().get(1), al.getSequenceAt(2)); SearchResultsI searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); SearchResultMatchI match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(2)); assertEquals(match.getStart(), 4); @@ -616,7 +616,7 @@ public class FinderTest f.findAll("I", false, false, false); assertTrue(f.getIdMatches().isEmpty()); SearchResultsI searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 2); + assertEquals(searchResults.getCount(), 2); SearchResultMatchI match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(0)); assertEquals(match.getStart(), 16); @@ -652,7 +652,7 @@ public class FinderTest Finder f = new Finder(av); f.findAll("aaa", false, false, false); SearchResultsI searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 2); + assertEquals(searchResults.getCount(), 2); SearchResultMatchI match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(3)); assertEquals(match.getStart(), 1); @@ -674,7 +674,7 @@ public class FinderTest f = new Finder(av); f.findAll("D", false, false, false); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(1)); assertEquals(match.getStart(), 4); @@ -696,7 +696,7 @@ public class FinderTest f = new Finder(av); f.findAll("aaa", false, false, false); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 2); + assertEquals(searchResults.getCount(), 2); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(3)); assertEquals(match.getStart(), 1); @@ -715,7 +715,7 @@ public class FinderTest f = new Finder(av); f.findAll("aaa", false, false, false); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 2); + assertEquals(searchResults.getCount(), 2); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(3)); assertEquals(match.getStart(), 1); @@ -740,7 +740,7 @@ public class FinderTest f = new Finder(av); f.findNext("H", false, false, false); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 1); + assertEquals(searchResults.getCount(), 1); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(2)); assertEquals(match.getStart(), 6); @@ -783,7 +783,7 @@ public class FinderTest Finder f = new Finder(av); f.findAll("[AH]", false, false, false); SearchResultsI searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 2); + assertEquals(searchResults.getCount(), 2); SearchResultMatchI match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(1)); assertEquals(match.getStart(), 1); @@ -823,8 +823,10 @@ public class FinderTest /* * match of seq1 ABE made up of AB and E + * note only one match is counted */ - assertEquals(searchResults.getSize(), 2); + assertEquals(searchResults.getCount(), 1); + assertEquals(searchResults.getResults().size(), 2); SearchResultMatchI match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(0)); assertEquals(match.getStart(), 8); // A @@ -837,7 +839,8 @@ public class FinderTest f = new Finder(av); f.findNext("a.E", false, false, true); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 2); + assertEquals(searchResults.getCount(), 1); + assertEquals(searchResults.getResults().size(), 2); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(0)); assertEquals(match.getStart(), 8); // A @@ -849,7 +852,8 @@ public class FinderTest f.findNext("a.E", false, false, true); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 2); + assertEquals(searchResults.getCount(), 1); + assertEquals(searchResults.getResults().size(), 2); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(1)); assertEquals(match.getStart(), 1); // a @@ -861,11 +865,12 @@ public class FinderTest /* * find all matching across two hidden column regions - * note one 'match' is reported as three contiguous matches + * note one 'match' is returned as three contiguous matches */ f.findAll("BEG", false, false, true); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 3); + assertEquals(searchResults.getCount(), 1); + assertEquals(searchResults.getResults().size(), 3); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(0)); assertEquals(match.getStart(), 9); // B @@ -891,7 +896,8 @@ public class FinderTest av.setSelectionGroup(selection); f.findAll("A.*H", false, false, true); searchResults = f.getSearchResults(); - assertEquals(searchResults.getSize(), 3); + assertEquals(searchResults.getCount(), 1); + assertEquals(searchResults.getResults().size(), 3); // match made of contiguous matches A, DE, H match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(1)); diff --git a/test/jalview/datamodel/SearchResultsTest.java b/test/jalview/datamodel/SearchResultsTest.java index 349b5d1..836c419 100644 --- a/test/jalview/datamodel/SearchResultsTest.java +++ b/test/jalview/datamodel/SearchResultsTest.java @@ -297,10 +297,10 @@ public class SearchResultsTest SequenceI seq1 = new Sequence("", "abcdefghijklm"); SearchResultsI sr = new SearchResults(); sr.addResult(seq1, 3, 5); - assertEquals(1, sr.getSize()); + assertEquals(1, sr.getCount()); sr.addResult(seq1, 3, 5); - assertEquals(1, sr.getSize()); + assertEquals(1, sr.getCount()); sr.addResult(seq1, 3, 6); - assertEquals(2, sr.getSize()); + assertEquals(2, sr.getCount()); } }