From: gmungoc Date: Wed, 14 Mar 2018 12:38:23 +0000 (+0000) Subject: JAL-2933 gui and applet Finder hold a FinderI per viewport X-Git-Tag: Release_2_11_0~17^2~67^2~1 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=24de2d6a6f3db1b9d55f367e2bf8ba112e202a8a;p=jalview.git JAL-2933 gui and applet Finder hold a FinderI per viewport --- diff --git a/src/jalview/analysis/Finder.java b/src/jalview/analysis/Finder.java index adf7ff6..0996830 100644 --- a/src/jalview/analysis/Finder.java +++ b/src/jalview/analysis/Finder.java @@ -20,6 +20,7 @@ */ package jalview.analysis; +import jalview.api.FinderI; import jalview.datamodel.AlignmentI; import jalview.datamodel.Range; import jalview.datamodel.SearchResultMatchI; @@ -38,7 +39,7 @@ import com.stevesoft.pat.Regex; /** * Implements the search algorithm for the Find dialog */ -public class Finder +public class Finder implements FinderI { /* * matched residue locations @@ -48,7 +49,7 @@ public class Finder /* * sequences matched by id or description */ - private Vector idMatch; + private Vector idMatches; /* * the alignment to search over @@ -61,21 +62,6 @@ public class Finder private SequenceGroup selection; /* - * set true for case-sensitive search (default is false) - */ - private boolean caseSensitive; - - /* - * set true to search sequence description (default is false) - */ - private boolean includeDescription; - - /* - * set true to return all matches (default is next match only) - */ - private boolean findAll; - - /* * sequence index in alignment to search from */ private int sequenceIndex; @@ -83,63 +69,78 @@ public class Finder /* * column position in sequence to search from, base 0 * - absolute column number including any hidden columns - * (position of last match for a repeat search) + * (position after start of last match for a repeat search) */ private int columnIndex; /** - * Constructor to start searching an alignment, optionally restricting results - * to a selection + * Constructor for searching an alignment * * @param al - * @param sel */ - public Finder(AlignmentI al, SequenceGroup sel) + public Finder(AlignmentI al) { - this(al, sel, 0, -1); + this.alignment = al; + this.sequenceIndex = 0; + this.columnIndex = -1; } - /** - * Constructor to resume search at given sequence and residue on alignment and - * (optionally) restricted to a selection - * - * @param al - * @param sel - * @param seqindex - * @param colindex - */ - public Finder(AlignmentI al, SequenceGroup sel, int seqindex, - int colindex) + @Override + public void findAll(String theSearchString, SequenceGroup sg, + boolean matchCase, boolean searchDescription) { - this.alignment = al; - this.selection = sel; - this.sequenceIndex = seqindex; - this.columnIndex = colindex; + /* + * search from the start + */ + sequenceIndex = 0; + columnIndex = -1; + + doFind(theSearchString, sg, matchCase, searchDescription, true); + + /* + * reset to start for next search + */ + sequenceIndex = 0; + columnIndex = -1; } - /** - * Performs a find for the given search string. By default the next match is - * found, but if setFindAll(true) has been called, then all matches are found. - * Sequences matched by id or description can be retrieved by getIdMatch(), - * and matched residue patterns by getSearchResults(). - * - * @param theSearchString - * @return - */ - public void find(String theSearchString) + @Override + public void findNext(String theSearchString, SequenceGroup sg, + boolean matchCase, boolean searchDescription) { - if (findAll) + doFind(theSearchString, sg, matchCase, searchDescription, false); + + if (searchResults.isEmpty() && idMatches.isEmpty()) { + /* + * search failed - reset to start for next search + */ sequenceIndex = 0; columnIndex = -1; } + } - String searchString = caseSensitive ? theSearchString + /** + * Performs a 'find next' or 'find all', optionally restricted to the + * specified selection region + * + * @param theSearchString + * @param selectionRegion + * @param matchCase + * @param searchDescription + * @param findAll + */ + protected void doFind(String theSearchString, SequenceGroup selectionRegion, + boolean matchCase, boolean searchDescription, boolean findAll) + { + this.selection = selectionRegion; + String searchString = matchCase ? theSearchString : theSearchString.toUpperCase(); Regex searchPattern = new Regex(searchString); - searchPattern.setIgnoreCase(!caseSensitive); + searchPattern.setIgnoreCase(!matchCase); + searchResults = new SearchResults(); - idMatch = new Vector<>(); + idMatches = new Vector<>(); if (selection != null && selection.getSize() < 1) { @@ -151,7 +152,8 @@ public class Finder while (sequenceIndex < end) { SequenceI seq = alignment.getSequenceAt(sequenceIndex); - boolean found = findNext(seq, searchString, searchPattern); + boolean found = findNextMatch(seq, searchString, searchPattern, + searchDescription); if (found && !findAll) { return; @@ -225,10 +227,11 @@ public class Finder * @param seq * @param searchString * @param searchPattern + * @param matchDescription * @return */ - protected boolean findNext(SequenceI seq, String searchString, - Regex searchPattern) + protected boolean findNextMatch(SequenceI seq, String searchString, + Regex searchPattern, boolean matchDescription) { if (selection != null && !selection.contains(seq)) { @@ -244,7 +247,8 @@ public class Finder * at start of sequence; try find by residue number, in sequence id, * or (optionally) in sequence description */ - if (doNonMotifSearches(seq, searchString, searchPattern)) + if (doNonMotifSearches(seq, searchString, searchPattern, + matchDescription)) { return true; } @@ -360,10 +364,11 @@ public class Finder * @param seq * @param searchString * @param searchPattern + * @param includeDescription * @return */ protected boolean doNonMotifSearches(SequenceI seq, String searchString, - Regex searchPattern) + Regex searchPattern, boolean includeDescription) { /* * position sequence search to start of sequence @@ -397,9 +402,9 @@ public class Finder protected boolean searchSequenceDescription(SequenceI seq, Regex searchPattern) { String desc = seq.getDescription(); - if (desc != null && searchPattern.search(desc) && !idMatch.contains(seq)) + if (desc != null && searchPattern.search(desc) && !idMatches.contains(seq)) { - idMatch.addElement(seq); + idMatches.addElement(seq); return true; } return false; @@ -416,9 +421,9 @@ public class Finder */ protected boolean searchSequenceName(SequenceI seq, Regex searchPattern) { - if (searchPattern.search(seq.getName()) && !idMatch.contains(seq)) + if (searchPattern.search(seq.getName()) && !idMatches.contains(seq)) { - idMatch.addElement(seq); + idMatches.addElement(seq); return true; } return false; @@ -445,79 +450,21 @@ public class Finder return false; } - /** - * Sets whether the search is case sensitive (default is no) - * - * @param value + /* (non-Javadoc) + * @see jalview.analysis.FinderI#getIdMatch() */ - public void setCaseSensitive(boolean value) + @Override + public Vector getIdMatches() { - this.caseSensitive = value; + return idMatches; } - /** - * Sets whether search returns all matches. Default is to return the next - * match only. - * - * @param value - */ - public void setFindAll(boolean value) - { - this.findAll = value; - } - - /** - * Returns the (possibly empty) list of sequences matched on sequence name or - * description - * - * @return - */ - public Vector getIdMatch() - { - return idMatch; - } - - /** - * Answers the search results (possibly empty) from the last search - * - * @return + /* (non-Javadoc) + * @see jalview.analysis.FinderI#getSearchResults() */ + @Override public SearchResultsI getSearchResults() { return searchResults; } - - /** - * Answers the absolute column position (base 0, including any hidden columns) - * of the start of the last sequence motif (residue pattern) match found. A - * 'Find next' will search from the next position. - * - * @return - */ - public int getColumnIndex() - { - return columnIndex; - } - - /** - * Answers the offset in the alignment (0..) of the sequence in which the last - * match was found (if any) - * - * @return - */ - public int getSequenceIndex() - { - return sequenceIndex; - } - - /** - * Sets whether search also searches in sequence description text (default is - * no) - * - * @param value - */ - public void setIncludeDescription(boolean value) - { - this.includeDescription = value; - } } diff --git a/src/jalview/api/FinderI.java b/src/jalview/api/FinderI.java new file mode 100644 index 0000000..a1a3efd --- /dev/null +++ b/src/jalview/api/FinderI.java @@ -0,0 +1,64 @@ +package jalview.api; + +import jalview.datamodel.SearchResultsI; +import jalview.datamodel.SequenceGroup; +import jalview.datamodel.SequenceI; + +import java.util.List; + +/** + * An interface for searching for a pattern in an aligment + */ +public interface FinderI +{ + + /** + * Performs a find for the given search string (interpreted as a regular + * expression). Search may optionally be case-sensitive, and may optionally + * including match in sequence description (sequence id is always searched). + * If selection is not null, then the find is restricted to the + * selection region. Sequences matched by id or description can be retrieved + * by getIdMatches(), and matched residue patterns by getSearchResults(). + * + * @param theSearchString + * @param caseSensitive + * @param searchDescription + * @return + */ + void findAll(String theSearchString, SequenceGroup selection, + boolean caseSensitive, boolean searchDescription); + + /** + * Finds the next match for the given search string (interpreted as a regular + * expression), starting from the position after the last match found. Search + * may optionally be case-sensitive, and may optionally including match in + * sequence description (sequence id is always searched). If + * selection is not null, then the find is restricted to the + * selection region. Sequences matched by id or description can be retrieved + * by getIdMatches(), and matched residue patterns by getSearchResults(). + * + * @param theSearchString + * @param selection + * @param caseSensitive + * @param searchDescription + * @return + */ + void findNext(String theSearchString, SequenceGroup selection, + boolean caseSensitive, boolean searchDescription); + + /** + * Returns the (possibly empty) list of sequences matched on sequence name or + * description + * + * @return + */ + List getIdMatches(); + + /** + * Answers the search results (possibly empty) from the last search + * + * @return + */ + SearchResultsI getSearchResults(); + +} \ No newline at end of file diff --git a/src/jalview/appletgui/Finder.java b/src/jalview/appletgui/Finder.java index 286cd5f..4b55574 100644 --- a/src/jalview/appletgui/Finder.java +++ b/src/jalview/appletgui/Finder.java @@ -21,9 +21,11 @@ package jalview.appletgui; import jalview.api.AlignViewportI; +import jalview.api.FinderI; import jalview.datamodel.SearchResultMatchI; import jalview.datamodel.SearchResultsI; import jalview.datamodel.SequenceFeature; +import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.util.MessageManager; @@ -45,7 +47,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Vector; public class Finder extends Panel implements ActionListener { @@ -68,21 +69,13 @@ public class Finder extends Panel implements ActionListener private SearchResultsI searchResults; /* - * sequence index and column position of last match, - * for current search and per viewport + * Finder agent per viewport searched */ - private int seqIndex = 0; - - private int colIndex = -1; - - Map seqIndices; - - Map colIndices; + Map finders; public Finder(final AlignmentPanel ap) { - seqIndices = new HashMap<>(); - colIndices = new HashMap<>(); + finders = new HashMap<>(); try { @@ -126,8 +119,6 @@ public class Finder extends Panel implements ActionListener else if (evt.getSource() == findAll) { - colIndex = -1; - seqIndex = 0; doSearch(true); } else if (evt.getSource() == createFeatures) @@ -164,36 +155,37 @@ public class Finder extends Panel implements ActionListener { ap = ap.av.applet.currentAlignFrame.alignPanel; av = ap.av; - seqIndex = 0; - colIndex = -1; - if (seqIndices.containsKey(av)) - { - seqIndex = seqIndices.get(av).intValue(); - } - if (colIndices.containsKey(av)) - { - colIndex = colIndices.get(av).intValue(); - } } createFeatures.setEnabled(false); - jalview.analysis.Finder finder = new jalview.analysis.Finder( - av.getAlignment(), av.getSelectionGroup(), seqIndex, colIndex); - finder.setCaseSensitive(caseSensitive.getState()); - finder.setIncludeDescription(searchDescription.getState()); - finder.setFindAll(doFindAll); + FinderI finder = finders.get(av); + if (finder == null) + { + /* + * first time we searched this viewport + */ + finder = new jalview.analysis.Finder(av.getAlignment()); + finders.put(av, finder); + } String searchString = textfield.getText(); + SequenceGroup selectionGroup = av.getSelectionGroup(); + boolean isCaseSensitive = caseSensitive.getState(); + boolean doSearchDescription = searchDescription.getState(); + if (doFindAll) + { + finder.findAll(searchString, selectionGroup, isCaseSensitive, + doSearchDescription); + } + else + { + finder.findNext(searchString, selectionGroup, isCaseSensitive, + doSearchDescription); + } - finder.find(searchString); - - seqIndex = finder.getSequenceIndex(); - colIndex = finder.getColumnIndex(); - seqIndices.put(av, seqIndex); - colIndices.put(av, colIndex); searchResults = finder.getSearchResults(); - Vector idMatch = finder.getIdMatch(); - ap.idPanel.highlightSearchResults(idMatch); + List idMatches = finder.getIdMatches(); + ap.idPanel.highlightSearchResults(idMatches); if (searchResults.isEmpty()) { @@ -209,20 +201,18 @@ public class Finder extends Panel implements ActionListener ap.highlightSearchResults(searchResults); // TODO: add enablers for 'SelectSequences' or 'SelectColumns' or // 'SelectRegion' selection - if (idMatch.isEmpty() && searchResults == null) + if (idMatches.isEmpty() && searchResults == null) { ap.alignFrame.statusBar.setText( MessageManager.getString("label.finished_searching")); - colIndex = -1; - seqIndex = 0; } else { if (doFindAll) { - String message = (idMatch.size() > 0) ? "" + idMatch.size() + " IDs" + String message = (idMatches.size() > 0) ? "" + idMatches.size() + " IDs" : ""; - if (idMatch.size() > 0 && searchResults != null + if (idMatches.size() > 0 && searchResults != null && searchResults.getSize() > 0) { message += " and "; diff --git a/src/jalview/gui/Finder.java b/src/jalview/gui/Finder.java index c5b25bf..dcc3518 100755 --- a/src/jalview/gui/Finder.java +++ b/src/jalview/gui/Finder.java @@ -21,9 +21,11 @@ package jalview.gui; import jalview.api.AlignViewportI; +import jalview.api.FinderI; import jalview.datamodel.SearchResultMatchI; import jalview.datamodel.SearchResultsI; import jalview.datamodel.SequenceFeature; +import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.jbgui.GFinder; import jalview.util.MessageManager; @@ -36,7 +38,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Vector; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; @@ -76,16 +77,9 @@ public class Finder extends GFinder private JInternalFrame frame; /* - * sequence and column position of the last match, - * as currently used, and saved for each viewport + * Finder agent per viewport searched */ - private int seqIndex = 0; - - private int colIndex = -1; - - private Map seqIndices; - - private Map colIndices; + private Map finders; private SearchResultsI searchResults; @@ -114,8 +108,7 @@ public class Finder extends GFinder { av = viewport; ap = alignPanel; - seqIndices = new HashMap<>(); - colIndices = new HashMap<>(); + finders = new HashMap<>(); focusfixed = viewport != null; frame = new JInternalFrame(); frame.setContentPane(this); @@ -173,8 +166,6 @@ public class Finder extends GFinder { if (getFocusedViewport()) { - colIndex = -1; - seqIndex = 0; doSearch(true); } } @@ -208,21 +199,6 @@ public class Finder extends GFinder { av = ((AlignFrame) alignFrame).viewport; ap = ((AlignFrame) alignFrame).alignPanel; - - /* - * restore search position if switching to a - * panel where we have previously searched - */ - seqIndex = 0; - colIndex = -1; - if (seqIndices.containsKey(av)) - { - seqIndex = seqIndices.get(av).intValue(); - } - if (colIndices.containsKey(av)) - { - colIndex = colIndices.get(av).intValue(); - } return true; } } @@ -287,22 +263,32 @@ public class Finder extends GFinder // other stuff // TODO: add switches to control what is searched - sequences, IDS, // descriptions, features - jalview.analysis.Finder finder = new jalview.analysis.Finder( - av.getAlignment(), av.getSelectionGroup(), seqIndex, colIndex); - finder.setCaseSensitive(caseSensitive.isSelected()); - finder.setIncludeDescription(searchDescription.isSelected()); - - finder.setFindAll(doFindAll); - - finder.find(searchString); + FinderI finder = finders.get(av); + if (finder == null) + { + /* + * first time we've searched this viewport + */ + finder = new jalview.analysis.Finder(av.getAlignment()); + finders.put(av, finder); + } - seqIndex = finder.getSequenceIndex(); - colIndex = finder.getColumnIndex(); - seqIndices.put(av, seqIndex); - colIndices.put(av, colIndex); + SequenceGroup selectionGroup = av.getSelectionGroup(); + boolean isCaseSensitive = caseSensitive.isSelected(); + boolean doSearchDescription = searchDescription.isSelected(); + if (doFindAll) + { + finder.findAll(searchString, selectionGroup, isCaseSensitive, + doSearchDescription); + } + else + { + finder.findNext(searchString, selectionGroup, isCaseSensitive, + doSearchDescription); + } searchResults = finder.getSearchResults(); - Vector idMatch = finder.getIdMatch(); + List idMatch = finder.getIdMatches(); ap.getIdPanel().highlightSearchResults(idMatch); if (searchResults.isEmpty()) @@ -322,8 +308,6 @@ public class Finder extends GFinder JvOptionPane.showInternalMessageDialog(this, MessageManager.getString("label.finished_searching"), null, JvOptionPane.INFORMATION_MESSAGE); - colIndex = -1; - seqIndex = 0; } else { @@ -343,8 +327,6 @@ public class Finder extends GFinder } JvOptionPane.showInternalMessageDialog(this, message, null, JvOptionPane.INFORMATION_MESSAGE); - colIndex = -1; - seqIndex = 0; } } searchBox.updateCache(); diff --git a/test/jalview/analysis/FinderTest.java b/test/jalview/analysis/FinderTest.java index f05bfb3..e453dd3 100644 --- a/test/jalview/analysis/FinderTest.java +++ b/test/jalview/analysis/FinderTest.java @@ -24,6 +24,7 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertSame; import static org.testng.Assert.assertTrue; +import jalview.api.FinderI; import jalview.bin.Cache; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentI; @@ -43,6 +44,8 @@ import java.util.List; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import junit.extensions.PA; + public class FinderTest { @BeforeClass(alwaysRun = true) @@ -79,8 +82,8 @@ public class FinderTest /* * find next match only */ - Finder f = new Finder(al, null); - f.find("E.H"); // 'E, any character, H' + Finder f = new Finder(al); + f.findNext("E.H", null, false, false); // 'E, any character, H' // should match seq2 efH only SearchResultsI sr = f.getSearchResults(); assertEquals(sr.getSize(), 1); @@ -89,9 +92,8 @@ public class FinderTest assertEquals(matches.get(0).getStart(), 5); assertEquals(matches.get(0).getEnd(), 7); - f = new Finder(al, null); - f.setFindAll(true); - f.find("E.H"); // 'E, any character, H' + f = new Finder(al); + f.findAll("E.H", null, false, false); // 'E, any character, H' // should match seq2 efH and seq3 EFH sr = f.getSearchResults(); assertEquals(sr.getSize(), 2); @@ -110,12 +112,12 @@ public class FinderTest @Test(groups = "Functional") public void testFind_residueNumber() { - Finder f = new Finder(al, null); + Finder f = new Finder(al); /* * find first match should return seq1 residue 9 */ - f.find("9"); + f.findNext("9", null, false, false); SearchResultsI sr = f.getSearchResults(); assertEquals(sr.getSize(), 1); List matches = sr.getResults(); @@ -126,9 +128,8 @@ public class FinderTest /* * find all matches should return seq1 and seq4 (others are too short) */ - f = new Finder(al, null); - f.setFindAll(true); - f.find("9"); + f = new Finder(al); + f.findAll("9", null, false, false); sr = f.getSearchResults(); assertEquals(sr.getSize(), 2); matches = sr.getResults(); @@ -142,8 +143,8 @@ public class FinderTest /* * parsing of search string as integer is strict */ - f = new Finder(al, null); - f.find(" 9"); + f = new Finder(al); + f.findNext(" 9", null, false, false); assertTrue(f.getSearchResults().isEmpty()); } @@ -157,34 +158,40 @@ public class FinderTest * start at second sequence; colIndex of -1 * means sequence id / description is searched */ - Finder f = new Finder(al, null, 1, -1); - f.find("e"); // matches id + Finder f = new Finder(al); + PA.setValue(f, "sequenceIndex", 1); + PA.setValue(f, "columnIndex", -1); + f.findNext("e", null, false, false); // matches id assertTrue(f.getSearchResults().isEmpty()); - assertEquals(f.getIdMatch().size(), 1); - assertSame(f.getIdMatch().get(0), al.getSequenceAt(1)); + assertEquals(f.getIdMatches().size(), 1); + assertSame(f.getIdMatches().get(0), al.getSequenceAt(1)); // colIndex is now 0 - for use in next find next // searching A--BCDefHI - assertEquals(f.getColumnIndex(), 0); - f = new Finder(al, null, 1, 0); - f.find("e"); // matches in sequence - assertTrue(f.getIdMatch().isEmpty()); + assertEquals(PA.getValue(f, "columnIndex"), 0); + f = new Finder(al); + PA.setValue(f, "sequenceIndex", 1); + PA.setValue(f, "columnIndex", 0); + f.findNext("e", null, false, false); // matches in sequence + assertTrue(f.getIdMatches().isEmpty()); assertEquals(f.getSearchResults().getSize(), 1); List matches = f.getSearchResults().getResults(); assertEquals(matches.get(0).getStart(), 5); assertEquals(matches.get(0).getEnd(), 5); assertSame(matches.get(0).getSequence(), al.getSequenceAt(1)); // still in the second sequence - assertEquals(f.getSequenceIndex(), 1); + assertEquals(PA.getValue(f, "sequenceIndex"), 1); // next column position to search from is 7 - assertEquals(f.getColumnIndex(), 7); + assertEquals(PA.getValue(f, "columnIndex"), 7); // find next from end of sequence - finds next sequence id - f = new Finder(al, null, 1, 7); - f.find("e"); - assertEquals(f.getIdMatch().size(), 1); - assertSame(f.getIdMatch().get(0), al.getSequenceAt(2)); + f = new Finder(al); + PA.setValue(f, "sequenceIndex", 1); + PA.setValue(f, "columnIndex", 7); + f.findNext("e", null, false, false); + assertEquals(f.getIdMatches().size(), 1); + assertSame(f.getIdMatches().get(0), al.getSequenceAt(2)); assertTrue(f.getSearchResults().isEmpty()); } @@ -201,36 +208,29 @@ public class FinderTest /* * find first match only */ - Finder f = new Finder(al2, null); - f.setIncludeDescription(true); - f.find("rAF"); - assertEquals(f.getIdMatch().size(), 1); - assertSame(f.getIdMatch().get(0), al2.getSequenceAt(0)); + Finder f = new Finder(al2); + f.findNext("rAF", null, false, true); + assertEquals(f.getIdMatches().size(), 1); + assertSame(f.getIdMatches().get(0), al2.getSequenceAt(0)); assertTrue(f.getSearchResults().isEmpty()); /* * find all matches */ - f = new Finder(al2, null); - f.setFindAll(true); - f.setIncludeDescription(true); - f.find("rAF"); - assertEquals(f.getIdMatch().size(), 2); - assertSame(f.getIdMatch().get(0), al2.getSequenceAt(0)); - assertSame(f.getIdMatch().get(1), al2.getSequenceAt(1)); + f = new Finder(al2); + f.findAll("rAF", null, false, true); + assertEquals(f.getIdMatches().size(), 2); + assertSame(f.getIdMatches().get(0), al2.getSequenceAt(0)); + assertSame(f.getIdMatches().get(1), al2.getSequenceAt(1)); assertTrue(f.getSearchResults().isEmpty()); /* * case sensitive */ - f = new Finder(al2, null); - f.setFindAll(true); - f.setCaseSensitive(true); - f.setIncludeDescription(true); - - f.find("RAF"); - assertEquals(f.getIdMatch().size(), 1); - assertSame(f.getIdMatch().get(0), al2.getSequenceAt(0)); + f = new Finder(al2); + f.findAll("RAF", null, true, true); + assertEquals(f.getIdMatches().size(), 1); + assertSame(f.getIdMatches().get(0), al2.getSequenceAt(0)); assertTrue(f.getSearchResults().isEmpty()); /* @@ -239,17 +239,15 @@ public class FinderTest al2.getSequenceAt(0).setDescription("the efh sequence"); al2.getSequenceAt(0).setName("mouseEFHkinase"); al2.getSequenceAt(1).setName("humanEFHkinase"); - f = new Finder(al2, null); - f.setFindAll(true); - f.setIncludeDescription(true); + f = new Finder(al2); /* * sequence matches should have no duplicates */ - f.find("EFH"); - assertEquals(f.getIdMatch().size(), 2); - assertSame(f.getIdMatch().get(0), al2.getSequenceAt(0)); - assertSame(f.getIdMatch().get(1), al2.getSequenceAt(1)); + f.findAll("EFH", null, false, true); + assertEquals(f.getIdMatches().size(), 2); + assertSame(f.getIdMatches().get(0), al2.getSequenceAt(0)); + assertSame(f.getIdMatches().get(1), al2.getSequenceAt(1)); assertEquals(f.getSearchResults().getSize(), 2); SearchResultMatchI match = f.getSearchResults().getResults().get(0); @@ -268,26 +266,23 @@ public class FinderTest @Test(groups = "Functional") public void testFindAll_sequenceIds() { - Finder f = new Finder(al, null); - f.setFindAll(true); + Finder f = new Finder(al); /* * case insensitive; seq1 occurs twice in sequence id but * only one match should be returned */ - f.find("SEQ1"); - assertEquals(f.getIdMatch().size(), 1); - assertSame(f.getIdMatch().get(0), al.getSequenceAt(0)); + f.findAll("SEQ1", null, false, false); + assertEquals(f.getIdMatches().size(), 1); + assertSame(f.getIdMatches().get(0), al.getSequenceAt(0)); SearchResultsI searchResults = f.getSearchResults(); assertTrue(searchResults.isEmpty()); /* * case sensitive */ - f = new Finder(al, null); - f.setFindAll(true); - f.setCaseSensitive(true); - f.find("SEQ1"); + f = new Finder(al); + f.findAll("SEQ1", null, true, false); searchResults = f.getSearchResults(); assertTrue(searchResults.isEmpty()); @@ -296,11 +291,10 @@ public class FinderTest */ AlignmentI al2 = new Alignment(al); al2.addSequence(new Sequence("aBz", "xyzabZpqrAbZ")); - f = new Finder(al2, null); - f.setFindAll(true); - f.find("ABZ"); - assertEquals(f.getIdMatch().size(), 1); - assertSame(f.getIdMatch().get(0), al2.getSequenceAt(4)); + f = new Finder(al2); + f.findAll("ABZ", null, false, false); + assertEquals(f.getIdMatches().size(), 1); + assertSame(f.getIdMatches().get(0), al2.getSequenceAt(4)); searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 2); SearchResultMatchI match = searchResults.getResults().get(0); @@ -322,8 +316,8 @@ public class FinderTest /* * efh should be matched in seq2 only */ - Finder f = new Finder(al, null); - f.find("EfH"); + FinderI f = new Finder(al); + f.findNext("EfH", null, false, false); SearchResultsI searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); SearchResultMatchI match = searchResults.getResults().get(0); @@ -334,8 +328,8 @@ public class FinderTest /* * I should be found in seq1 (twice) and seq2 (once) */ - f = new Finder(al, null); - f.find("I"); // find next: seq1/16 + f = new Finder(al); + f.findNext("I", null, false, false); // find next: seq1/16 searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); match = searchResults.getResults().get(0); @@ -343,7 +337,7 @@ public class FinderTest assertEquals(match.getStart(), 16); assertEquals(match.getEnd(), 16); - f.find("I"); // find next: seq1/18 + f.findNext("I", null, false, false); // find next: seq1/18 searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); match = searchResults.getResults().get(0); @@ -351,7 +345,7 @@ public class FinderTest assertEquals(match.getStart(), 18); assertEquals(match.getEnd(), 18); - f.find("I"); // find next: seq2/8 + f.findNext("I", null, false, false); // find next: seq2/8 searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); match = searchResults.getResults().get(0); @@ -359,8 +353,19 @@ public class FinderTest assertEquals(match.getStart(), 8); assertEquals(match.getEnd(), 8); - f.find("I"); + f.findNext("I", null, false, false); assertTrue(f.getSearchResults().isEmpty()); + + /* + * find should reset to start of alignment after a failed search + */ + f.findNext("I", null, false, false); // find next: seq1/16 + searchResults = f.getSearchResults(); + assertEquals(searchResults.getSize(), 1); + match = searchResults.getResults().get(0); + assertSame(match.getSequence(), al.getSequenceAt(0)); + assertEquals(match.getStart(), 16); + assertEquals(match.getEnd(), 16); } /** @@ -370,9 +375,8 @@ public class FinderTest @Test(groups = "Functional") public void testFind_maximalResultOnly() { - Finder f = new Finder(al, null); - f.setFindAll(true); - f.find("M+"); + Finder f = new Finder(al); + f.findAll("M+", null, false, false); SearchResultsI searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); SearchResultMatchI match = searchResults.getResults().get(0); @@ -387,9 +391,8 @@ public class FinderTest @Test(groups = "Functional") public void testFind_findAll() { - Finder f = new Finder(al, null); - f.setFindAll(true); - f.find("EfH"); + Finder f = new Finder(al); + f.findAll("EfH", null, false, false); SearchResultsI searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 2); SearchResultMatchI match = searchResults.getResults().get(0); @@ -404,7 +407,7 @@ public class FinderTest /* * find all I should find 2 positions in seq1, 1 in seq2 */ - f.find("I"); + f.findAll("I", null, false, false); searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 3); match = searchResults.getResults().get(0); @@ -427,14 +430,12 @@ public class FinderTest @Test(groups = "Functional") public void testFind_findAllCaseSensitive() { - Finder f = new Finder(al, null); - f.setCaseSensitive(true); - f.setFindAll(true); + Finder f = new Finder(al); /* * BC should match seq1/9-10 and seq2/2-3 */ - f.find("BC"); + f.findAll("BC", null, true, false); SearchResultsI searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 2); SearchResultMatchI match = searchResults.getResults().get(0); @@ -449,10 +450,8 @@ public class FinderTest /* * bc should match seq3/1-2 */ - f = new Finder(al, null); - f.setCaseSensitive(true); - f.setFindAll(true); - f.find("bc"); + f = new Finder(al); + f.findAll("bc", null, true, false); searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); match = searchResults.getResults().get(0); @@ -460,7 +459,7 @@ public class FinderTest assertEquals(match.getStart(), 1); assertEquals(match.getEnd(), 2); - f.find("bC"); + f.findAll("bC", null, true, false); assertTrue(f.getSearchResults().isEmpty()); } @@ -481,9 +480,9 @@ public class FinderTest sg.addSequence(al.getSequenceAt(1), false); sg.addSequence(al.getSequenceAt(2), false); - Finder f = new Finder(al, sg); - f.find("b"); - assertTrue(f.getIdMatch().isEmpty()); + FinderI f = new Finder(al); + f.findNext("b", sg, false, false); + assertTrue(f.getIdMatches().isEmpty()); SearchResultsI searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); SearchResultMatchI match = searchResults.getResults().get(0); @@ -494,21 +493,21 @@ public class FinderTest /* * a second Find should not return the 'b' in seq3 as outside the selection */ - f.find("b"); + f.findNext("b", sg, false, false); assertTrue(f.getSearchResults().isEmpty()); - assertTrue(f.getIdMatch().isEmpty()); + assertTrue(f.getIdMatches().isEmpty()); - f = new Finder(al, sg); - f.find("d"); - assertTrue(f.getIdMatch().isEmpty()); + f = new Finder(al); + f.findNext("d", sg, false, false); + assertTrue(f.getIdMatches().isEmpty()); searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); match = searchResults.getResults().get(0); assertSame(match.getSequence(), al.getSequenceAt(1)); assertEquals(match.getStart(), 4); assertEquals(match.getEnd(), 4); - f.find("d"); - assertTrue(f.getIdMatch().isEmpty()); + f.findNext("d", sg, false, false); + assertTrue(f.getIdMatches().isEmpty()); searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); match = searchResults.getResults().get(0); @@ -537,12 +536,11 @@ public class FinderTest /* * search for 'e' should match two sequence ids and one residue */ - Finder f = new Finder(al, sg); - f.setFindAll(true); - f.find("e"); - assertEquals(f.getIdMatch().size(), 2); - assertSame(f.getIdMatch().get(0), al.getSequenceAt(1)); - assertSame(f.getIdMatch().get(1), al.getSequenceAt(2)); + Finder f = new Finder(al); + f.findAll("e", sg, false, false); + assertEquals(f.getIdMatches().size(), 2); + assertSame(f.getIdMatches().get(0), al.getSequenceAt(1)); + assertSame(f.getIdMatches().get(1), al.getSequenceAt(2)); SearchResultsI searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); SearchResultMatchI match = searchResults.getResults().get(0); @@ -553,12 +551,11 @@ public class FinderTest /* * search for 'Q' should match two sequence ids only */ - f = new Finder(al, sg); - f.setFindAll(true); - f.find("Q"); - assertEquals(f.getIdMatch().size(), 2); - assertSame(f.getIdMatch().get(0), al.getSequenceAt(1)); - assertSame(f.getIdMatch().get(1), al.getSequenceAt(2)); + f = new Finder(al); + f.findAll("Q", sg, false, false); + assertEquals(f.getIdMatches().size(), 2); + assertSame(f.getIdMatches().get(0), al.getSequenceAt(1)); + assertSame(f.getIdMatches().get(1), al.getSequenceAt(2)); assertTrue(f.getSearchResults().isEmpty()); } @@ -584,10 +581,9 @@ public class FinderTest /* * search for 'I' should match two sequence positions */ - Finder f = new Finder(al, sg); - f.setFindAll(true); - f.find("I"); - assertTrue(f.getIdMatch().isEmpty()); + Finder f = new Finder(al); + f.findAll("I", sg, false, false); + assertTrue(f.getIdMatches().isEmpty()); SearchResultsI searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 2); SearchResultMatchI match = searchResults.getResults().get(0); @@ -625,9 +621,8 @@ public class FinderTest * find all search for D should ignore hidden positions in seq1 and seq3, * find the visible D in seq2 */ - Finder f = new Finder(al, null); - f.setFindAll(true); - f.find("D"); + Finder f = new Finder(al); + f.findAll("D", null, false, false); SearchResultsI searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); SearchResultMatchI match = searchResults.getResults().get(0); @@ -640,8 +635,8 @@ public class FinderTest * find all 'aaa' should find end of seq4 only */ hc.hideColumns(2, 5); - f = new Finder(al, null); - f.find("aaa"); + f = new Finder(al); + f.findAll("aaa", null, false, false); searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); match = searchResults.getResults().get(0); @@ -652,7 +647,7 @@ public class FinderTest /* * find all 'BE' should not match across hidden columns in seq1 */ - f.find("BE"); + f.findAll("BE", null, false, false); assertTrue(f.getSearchResults().isEmpty()); /* @@ -661,8 +656,8 @@ public class FinderTest */ hc.revealAllHiddenColumns(new ColumnSelection()); hc.hideColumns(8, 13); - f = new Finder(al, null); - f.find("H"); + f = new Finder(al); + f.findNext("H", null, false, false); searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 1); match = searchResults.getResults().get(0); @@ -703,9 +698,8 @@ public class FinderTest * find all search for A or H * should match seq2/1, seq2/7, not seq3/6 */ - Finder f = new Finder(al, sg); - f.setFindAll(true); - f.find("[AH]"); + Finder f = new Finder(al); + f.findAll("[AH]", sg, false, false); SearchResultsI searchResults = f.getSearchResults(); assertEquals(searchResults.getSize(), 2); SearchResultMatchI match = searchResults.getResults().get(0); diff --git a/test/jalview/controller/AlignViewControllerTest.java b/test/jalview/controller/AlignViewControllerTest.java index efee93b..ad86f32 100644 --- a/test/jalview/controller/AlignViewControllerTest.java +++ b/test/jalview/controller/AlignViewControllerTest.java @@ -26,6 +26,7 @@ import static org.testng.AssertJUnit.assertTrue; import jalview.analysis.Finder; import jalview.api.AlignViewControllerI; import jalview.api.FeatureColourI; +import jalview.api.FinderI; import jalview.datamodel.Alignment; import jalview.datamodel.SearchResults; import jalview.datamodel.SearchResultsI; @@ -222,10 +223,8 @@ public class AlignViewControllerTest /* * test Match/Find works first */ - Finder f = new Finder(af.getViewport().getAlignment(), null); - f.setFindAll(true); - f.setCaseSensitive(true); - f.find("M+"); + FinderI f = new Finder(af.getViewport().getAlignment()); + f.findAll("M+", null, true, false); assertEquals( "Finder found different set of results to manually created SearchResults", sr, f.getSearchResults());