Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:56:54 GMT
  2. Package jalview.analysis

File FinderTest.java

 

Code metrics

0
522
20
1
992
650
20
0.04
26.1
20
1

Classes

Class Line # Actions
FinderTest 53 522 20
1.0100%
 

Contributing tests

This file is covered by 17 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.analysis;
22   
23    import static org.testng.Assert.assertEquals;
24    import static org.testng.Assert.assertSame;
25    import static org.testng.Assert.assertTrue;
26   
27    import java.util.List;
28   
29    import org.testng.annotations.AfterMethod;
30    import org.testng.annotations.BeforeClass;
31    import org.testng.annotations.Test;
32   
33    import jalview.api.AlignViewportI;
34    import jalview.api.FinderI;
35    import jalview.bin.Cache;
36    import jalview.datamodel.Alignment;
37    import jalview.datamodel.AlignmentI;
38    import jalview.datamodel.ColumnSelection;
39    import jalview.datamodel.HiddenColumns;
40    import jalview.datamodel.SearchResultMatchI;
41    import jalview.datamodel.SearchResultsI;
42    import jalview.datamodel.Sequence;
43    import jalview.datamodel.SequenceFeature;
44    import jalview.datamodel.SequenceGroup;
45    import jalview.datamodel.SequenceI;
46    import jalview.gui.AlignFrame;
47    import jalview.gui.AlignViewport;
48    import jalview.gui.JvOptionPane;
49    import jalview.io.DataSourceType;
50    import jalview.io.FileLoader;
51    import junit.extensions.PA;
52   
 
53    public class FinderTest
54    {
 
55  1 toggle @BeforeClass(alwaysRun = true)
56    public void setUpJvOptionPane()
57    {
58  1 JvOptionPane.setInteractiveMode(false);
59  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
60    }
61   
62    private AlignFrame af;
63   
64    private AlignmentI al;
65   
66    private AlignViewportI av;
67   
 
68  1 toggle @BeforeClass(groups = "Functional")
69    public void setUp()
70    {
71  1 Cache.loadProperties("test/jalview/io/testProps.jvprops");
72  1 Cache.applicationProperties.setProperty("PAD_GAPS",
73    Boolean.FALSE.toString());
74   
75    //@formatter:off
76  1 String seqData =
77    "seq1/8-18 ABCD--EF-GHIJI\n" +
78    "seq2 A--BCDefHI\n" +
79    "seq3 --bcdEFH\n" +
80    "seq4 aa---aMMMMMaaa\n";
81    //@formatter:on
82  1 af = new FileLoader().LoadFileWaitTillLoaded(seqData,
83    DataSourceType.PASTE);
84  1 av = af.getViewport();
85  1 al = av.getAlignment();
86  1 al.getSequenceAt(0).addSequenceFeature(
87    new SequenceFeature("BBBB", "FeatureB", 9, 11, ""));
88  1 al.getSequenceAt(3).addSequenceFeature(
89    new SequenceFeature("BBAB", "FeatureA", 1, 3, ""));
90  1 al.getSequenceAt(3).addSequenceFeature(
91    new SequenceFeature("AAAA", "FeatureA", 9, 11, ""));
92    }
93   
 
94  17 toggle @AfterMethod(alwaysRun = true)
95    public void tearDownAfterTest()
96    {
97  17 av.setSelectionGroup(null);
98    }
99   
100    /**
101    * Test for find matches of a regular expression
102    */
 
103  1 toggle @Test(groups = "Functional")
104    public void testFind_regex()
105    {
106    /*
107    * find next match only
108    */
109  1 Finder f = new Finder(av);
110  1 f.findNext("E.H", false, false, false, false); // 'E, any character, H'
111    // should match seq2 efH only
112  1 SearchResultsI sr = f.getSearchResults();
113  1 assertEquals(sr.getCount(), 1);
114  1 List<SearchResultMatchI> matches = sr.getResults();
115  1 assertSame(matches.get(0).getSequence(), al.getSequenceAt(1));
116  1 assertEquals(matches.get(0).getStart(), 5);
117  1 assertEquals(matches.get(0).getEnd(), 7);
118   
119  1 f = new Finder(av);
120  1 f.findAll("E.H", false, false, false, false); // 'E, any character, H'
121    // should match seq2 efH and seq3 EFH
122  1 sr = f.getSearchResults();
123  1 assertEquals(sr.getCount(), 2);
124  1 matches = sr.getResults();
125  1 assertSame(matches.get(0).getSequence(), al.getSequenceAt(1));
126  1 assertSame(matches.get(1).getSequence(), al.getSequenceAt(2));
127  1 assertEquals(matches.get(0).getStart(), 5);
128  1 assertEquals(matches.get(0).getEnd(), 7);
129  1 assertEquals(matches.get(1).getStart(), 4);
130  1 assertEquals(matches.get(1).getEnd(), 6);
131    }
132   
 
133  1 toggle @Test(groups = "Functional")
134    public void testFind_findAll()
135    {
136    /*
137    * simple JAL-3765 test
138    * single symbol should find *all* matching symbols
139    */
140  1 Finder f = new Finder(av);
141  1 f.findAll("M", false, false, false, false);
142  1 SearchResultsI sr = f.getSearchResults();
143  1 assertEquals(sr.getCount(), 5);
144   
145    }
146   
147    /**
148    * Test for (undocumented) find residue by position
149    */
 
150  1 toggle @Test(groups = "Functional")
151    public void testFind_residueNumber()
152    {
153  1 Finder f = new Finder(av);
154   
155    /*
156    * find first match should return seq1 residue 9
157    */
158  1 f.findNext("9", false, false, false, false);
159  1 SearchResultsI sr = f.getSearchResults();
160  1 assertEquals(sr.getCount(), 1);
161  1 List<SearchResultMatchI> matches = sr.getResults();
162  1 assertSame(matches.get(0).getSequence(), al.getSequenceAt(0));
163  1 assertEquals(matches.get(0).getStart(), 9);
164  1 assertEquals(matches.get(0).getEnd(), 9);
165   
166    /*
167    * find all matches should return seq1 and seq4 (others are too short)
168    * (and not matches in sequence ids)
169    */
170  1 f = new Finder(av);
171  1 String name = al.getSequenceAt(0).getName();
172  1 al.getSequenceAt(0).setName("Q9XA0");
173  1 f.findAll("9", false, false, false, false);
174  1 sr = f.getSearchResults();
175  1 assertEquals(sr.getCount(), 2);
176  1 matches = sr.getResults();
177  1 assertSame(matches.get(0).getSequence(), al.getSequenceAt(0));
178  1 assertSame(matches.get(1).getSequence(), al.getSequenceAt(3));
179  1 assertEquals(matches.get(0).getStart(), 9);
180  1 assertEquals(matches.get(0).getEnd(), 9);
181  1 assertEquals(matches.get(1).getStart(), 9);
182  1 assertEquals(matches.get(1).getEnd(), 9);
183  1 al.getSequenceAt(0).setName(name);
184   
185    /*
186    * parsing of search string as integer is strict
187    */
188  1 f = new Finder(av);
189  1 f.findNext(" 9", false, false, false, false);
190  1 assertTrue(f.getSearchResults().isEmpty());
191    }
192   
193    /**
194    * Test for find next action
195    */
 
196  1 toggle @Test(groups = "Functional")
197    public void testFindNext()
198    {
199    /*
200    * start at second sequence; residueIndex of -1
201    * means sequence id / description is searched
202    */
203  1 Finder f = new Finder(av);
204  1 PA.setValue(f, "sequenceIndex", 1);
205  1 PA.setValue(f, "residueIndex", -1);
206  1 f.findNext("e", false, false, false, false); // matches id
207   
208  1 assertTrue(f.getSearchResults().isEmpty());
209  1 assertEquals(f.getIdMatches().size(), 1);
210  1 assertSame(f.getIdMatches().get(0), al.getSequenceAt(1));
211   
212    // residueIndex is now 0 - for use in next find next
213    // searching A--BCDefHI
214  1 assertEquals(PA.getValue(f, "residueIndex"), 0);
215  1 f = new Finder(av);
216  1 PA.setValue(f, "sequenceIndex", 1);
217  1 PA.setValue(f, "residueIndex", 0);
218  1 f.findNext("e", false, false, false, false); // matches in sequence
219  1 assertTrue(f.getIdMatches().isEmpty());
220  1 assertEquals(f.getSearchResults().getCount(), 1);
221  1 List<SearchResultMatchI> matches = f.getSearchResults().getResults();
222  1 assertEquals(matches.get(0).getStart(), 5);
223  1 assertEquals(matches.get(0).getEnd(), 5);
224  1 assertSame(matches.get(0).getSequence(), al.getSequenceAt(1));
225    // still in the second sequence
226  1 assertEquals(PA.getValue(f, "sequenceIndex"), 1);
227    // next residue offset to search from is 5
228  1 assertEquals(PA.getValue(f, "residueIndex"), 5);
229   
230    // find next from end of sequence - finds next sequence id
231  1 f = new Finder(av);
232  1 PA.setValue(f, "sequenceIndex", 1);
233  1 PA.setValue(f, "residueIndex", 7);
234  1 f.findNext("e", false, false, false, false);
235  1 assertEquals(f.getIdMatches().size(), 1);
236  1 assertSame(f.getIdMatches().get(0), al.getSequenceAt(2));
237  1 assertTrue(f.getSearchResults().isEmpty());
238    }
239   
240    /**
241    * Test for matching within sequence descriptions
242    */
 
243  1 toggle @Test(groups = "Functional")
244    public void testFind_inDescription()
245    {
246  1 AlignmentI al2 = new Alignment(al);
247  1 al2.getSequenceAt(0).setDescription("BRAF");
248  1 al2.getSequenceAt(1).setDescription("braf");
249   
250  1 AlignViewportI av2 = new AlignViewport(al2);
251   
252    /*
253    * find first match only
254    */
255  1 Finder f = new Finder(av2);
256  1 f.findNext("rAF", false, true, false, false);
257  1 assertEquals(f.getIdMatches().size(), 1);
258  1 assertSame(f.getIdMatches().get(0), al2.getSequenceAt(0));
259  1 assertTrue(f.getSearchResults().isEmpty());
260   
261    /*
262    * find all matches
263    */
264  1 f = new Finder(av2);
265  1 f.findAll("rAF", false, true, false, false);
266  1 assertEquals(f.getIdMatches().size(), 2);
267  1 assertSame(f.getIdMatches().get(0), al2.getSequenceAt(0));
268  1 assertSame(f.getIdMatches().get(1), al2.getSequenceAt(1));
269  1 assertTrue(f.getSearchResults().isEmpty());
270   
271    /*
272    * case sensitive
273    */
274  1 f = new Finder(av2);
275  1 f.findAll("RAF", true, true, false, false);
276  1 assertEquals(f.getIdMatches().size(), 1);
277  1 assertSame(f.getIdMatches().get(0), al2.getSequenceAt(0));
278  1 assertTrue(f.getSearchResults().isEmpty());
279   
280    /*
281    * match sequence id, description and sequence!
282    */
283  1 al2.getSequenceAt(0).setDescription("the efh sequence");
284  1 al2.getSequenceAt(0).setName("mouseEFHkinase");
285  1 al2.getSequenceAt(1).setName("humanEFHkinase");
286  1 f = new Finder(av2);
287   
288    /*
289    * sequence matches should have no duplicates
290    */
291  1 f.findAll("EFH", false, true, false, false);
292  1 assertEquals(f.getIdMatches().size(), 2);
293  1 assertSame(f.getIdMatches().get(0), al2.getSequenceAt(0));
294  1 assertSame(f.getIdMatches().get(1), al2.getSequenceAt(1));
295   
296  1 assertEquals(f.getSearchResults().getCount(), 2);
297  1 SearchResultMatchI match = f.getSearchResults().getResults().get(0);
298  1 assertSame(match.getSequence(), al2.getSequenceAt(1));
299  1 assertEquals(match.getStart(), 5);
300  1 assertEquals(match.getEnd(), 7);
301  1 match = f.getSearchResults().getResults().get(1);
302  1 assertSame(match.getSequence(), al2.getSequenceAt(2));
303  1 assertEquals(match.getStart(), 4);
304  1 assertEquals(match.getEnd(), 6);
305    }
306   
307    /**
308    * Test for matching within sequence ids
309    */
 
310  1 toggle @Test(groups = "Functional")
311    public void testFindAll_sequenceIds()
312    {
313  1 Finder f = new Finder(av);
314   
315    /*
316    * case insensitive; seq1 occurs twice in sequence id but
317    * only one match should be returned
318    */
319  1 f.findAll("SEQ1", false, false, false, false);
320  1 assertEquals(f.getIdMatches().size(), 1);
321  1 assertSame(f.getIdMatches().get(0), al.getSequenceAt(0));
322  1 SearchResultsI searchResults = f.getSearchResults();
323  1 assertTrue(searchResults.isEmpty());
324   
325    /*
326    * case sensitive
327    */
328  1 f = new Finder(av);
329  1 f.findAll("SEQ1", true, false, false, false);
330  1 searchResults = f.getSearchResults();
331  1 assertTrue(searchResults.isEmpty());
332   
333    /*
334    * match both sequence id and sequence
335    */
336  1 AlignmentI al2 = new Alignment(al);
337  1 AlignViewportI av2 = new AlignViewport(al2);
338  1 al2.addSequence(new Sequence("aBz", "xyzabZpqrAbZ"));
339  1 f = new Finder(av2);
340  1 f.findAll("ABZ", false, false, false, false);
341  1 assertEquals(f.getIdMatches().size(), 1);
342  1 assertSame(f.getIdMatches().get(0), al2.getSequenceAt(4));
343  1 searchResults = f.getSearchResults();
344  1 assertEquals(searchResults.getCount(), 2);
345  1 SearchResultMatchI match = searchResults.getResults().get(0);
346  1 assertSame(match.getSequence(), al2.getSequenceAt(4));
347  1 assertEquals(match.getStart(), 4);
348  1 assertEquals(match.getEnd(), 6);
349  1 match = searchResults.getResults().get(1);
350  1 assertSame(match.getSequence(), al2.getSequenceAt(4));
351  1 assertEquals(match.getStart(), 10);
352  1 assertEquals(match.getEnd(), 12);
353    }
354   
355    /**
356    * Test finding next match of a sequence pattern in an alignment
357    */
 
358  1 toggle @Test(groups = "Functional")
359    public void testFind_findNext()
360    {
361    // "seq1/8-18 ABCD--EF-GHIJI\n" +
362    // "seq2 A--BCDefHI\n" +
363    // "seq3 --bcdEFH\n" +
364    // "seq4 aa---aMMMMMaaa\n";
365    /*
366    * efh should be matched in seq2 only
367    */
368  1 FinderI f = new Finder(av);
369  1 f.findNext("EfH", false, false, false, false);
370  1 SearchResultsI searchResults = f.getSearchResults();
371  1 assertEquals(searchResults.getCount(), 1);
372  1 SearchResultMatchI match = searchResults.getResults().get(0);
373  1 assertSame(match.getSequence(), al.getSequenceAt(1));
374  1 assertEquals(match.getStart(), 5);
375  1 assertEquals(match.getEnd(), 7);
376   
377    /*
378    * I should be found in seq1 (twice) and seq2 (once)
379    */
380  1 f = new Finder(av);
381  1 f.findNext("I", false, false, false, false); // find next: seq1/16
382  1 searchResults = f.getSearchResults();
383  1 assertEquals(searchResults.getCount(), 1);
384  1 match = searchResults.getResults().get(0);
385  1 assertSame(match.getSequence(), al.getSequenceAt(0));
386  1 assertEquals(match.getStart(), 16);
387  1 assertEquals(match.getEnd(), 16);
388   
389  1 f.findNext("I", false, false, false, false); // find next: seq1/18
390  1 searchResults = f.getSearchResults();
391  1 assertEquals(searchResults.getCount(), 1);
392  1 match = searchResults.getResults().get(0);
393  1 assertSame(match.getSequence(), al.getSequenceAt(0));
394  1 assertEquals(match.getStart(), 18);
395  1 assertEquals(match.getEnd(), 18);
396   
397  1 f.findNext("I", false, false, false, false); // find next: seq2/8
398  1 searchResults = f.getSearchResults();
399  1 assertEquals(searchResults.getCount(), 1);
400  1 match = searchResults.getResults().get(0);
401  1 assertSame(match.getSequence(), al.getSequenceAt(1));
402  1 assertEquals(match.getStart(), 8);
403  1 assertEquals(match.getEnd(), 8);
404   
405  1 f.findNext("I", false, false, false, false);
406  1 assertTrue(f.getSearchResults().isEmpty());
407   
408    /*
409    * find should reset to start of alignment after a failed search
410    */
411  1 f.findNext("I", false, false, false, false); // find next: seq1/16
412  1 searchResults = f.getSearchResults();
413  1 assertEquals(searchResults.getCount(), 1);
414  1 match = searchResults.getResults().get(0);
415  1 assertSame(match.getSequence(), al.getSequenceAt(0));
416  1 assertEquals(match.getStart(), 16);
417  1 assertEquals(match.getEnd(), 16);
418    }
419   
420    /**
421    * Test for JAL-2302 to verify that sub-matches are not included in a find all
422    * result
423    */
 
424  1 toggle @Test(groups = "Functional")
425    public void testFindAll_maximalResultOnly()
426    {
427  1 Finder f = new Finder(av);
428  1 f.findAll("M+", false, false, false, false);
429  1 SearchResultsI searchResults = f.getSearchResults();
430  1 assertEquals(searchResults.getCount(), 1);
431  1 SearchResultMatchI match = searchResults.getResults().get(0);
432  1 assertSame(match.getSequence(), al.getSequenceAt(3));
433  1 assertEquals(match.getStart(), 4); // dataset sequence positions
434  1 assertEquals(match.getEnd(), 8); // base 1
435    }
436   
437    /**
438    * Test finding all matches of a sequence pattern in an alignment
439    */
 
440  1 toggle @Test(groups = "Functional")
441    public void testFindAll()
442    {
443  1 Finder f = new Finder(av);
444  1 f.findAll("EfH", false, false, false, false);
445  1 SearchResultsI searchResults = f.getSearchResults();
446  1 assertEquals(searchResults.getCount(), 2);
447  1 SearchResultMatchI match = searchResults.getResults().get(0);
448  1 assertSame(match.getSequence(), al.getSequenceAt(1));
449  1 assertEquals(match.getStart(), 5);
450  1 assertEquals(match.getEnd(), 7);
451  1 match = searchResults.getResults().get(1);
452  1 assertSame(match.getSequence(), al.getSequenceAt(2));
453  1 assertEquals(match.getStart(), 4);
454  1 assertEquals(match.getEnd(), 6);
455   
456    /*
457    * find all I should find 2 positions in seq1, 1 in seq2
458    */
459  1 f.findAll("I", false, false, false, false);
460  1 searchResults = f.getSearchResults();
461  1 assertEquals(searchResults.getCount(), 3);
462  1 match = searchResults.getResults().get(0);
463  1 assertSame(match.getSequence(), al.getSequenceAt(0));
464  1 assertEquals(match.getStart(), 16);
465  1 assertEquals(match.getEnd(), 16);
466  1 match = searchResults.getResults().get(1);
467  1 assertSame(match.getSequence(), al.getSequenceAt(0));
468  1 assertEquals(match.getStart(), 18);
469  1 assertEquals(match.getEnd(), 18);
470  1 match = searchResults.getResults().get(2);
471  1 assertSame(match.getSequence(), al.getSequenceAt(1));
472  1 assertEquals(match.getStart(), 8);
473  1 assertEquals(match.getEnd(), 8);
474    }
475   
476    /**
477    * Test finding all matches, case-sensitive
478    */
 
479  1 toggle @Test(groups = "Functional")
480    public void testFindAll_caseSensitive()
481    {
482  1 Finder f = new Finder(av);
483   
484    /*
485    * BC should match seq1/9-10 and seq2/2-3
486    */
487  1 f.findAll("BC", true, false, false, false);
488  1 SearchResultsI searchResults = f.getSearchResults();
489  1 assertEquals(searchResults.getCount(), 2);
490  1 SearchResultMatchI match = searchResults.getResults().get(0);
491  1 assertSame(match.getSequence(), al.getSequenceAt(0));
492  1 assertEquals(match.getStart(), 9);
493  1 assertEquals(match.getEnd(), 10);
494  1 match = searchResults.getResults().get(1);
495  1 assertSame(match.getSequence(), al.getSequenceAt(1));
496  1 assertEquals(match.getStart(), 2);
497  1 assertEquals(match.getEnd(), 3);
498   
499    /*
500    * bc should match seq3/1-2
501    */
502  1 f = new Finder(av);
503  1 f.findAll("bc", true, false, false, false);
504  1 searchResults = f.getSearchResults();
505  1 assertEquals(searchResults.getCount(), 1);
506  1 match = searchResults.getResults().get(0);
507  1 assertSame(match.getSequence(), al.getSequenceAt(2));
508  1 assertEquals(match.getStart(), 1);
509  1 assertEquals(match.getEnd(), 2);
510   
511  1 f.findAll("bC", true, false, false, false);
512  1 assertTrue(f.getSearchResults().isEmpty());
513    }
514   
515    /**
516    * Test finding next match of a sequence pattern in a selection group
517    */
 
518  1 toggle @Test(groups = "Functional")
519    public void testFindNext_inSelection()
520    {
521    /*
522    * select sequences 2 and 3, columns 4-6 which contains
523    * BCD
524    * cdE
525    */
526  1 SequenceGroup sg = new SequenceGroup();
527  1 sg.setStartRes(3);
528  1 sg.setEndRes(5);
529  1 sg.addSequence(al.getSequenceAt(1), false);
530  1 sg.addSequence(al.getSequenceAt(2), false);
531  1 av.setSelectionGroup(sg);
532   
533  1 FinderI f = new Finder(av);
534  1 f.findNext("b", false, false, false, false);
535  1 assertTrue(f.getIdMatches().isEmpty());
536  1 SearchResultsI searchResults = f.getSearchResults();
537  1 assertEquals(searchResults.getCount(), 1);
538  1 SearchResultMatchI match = searchResults.getResults().get(0);
539  1 assertSame(match.getSequence(), al.getSequenceAt(1));
540  1 assertEquals(match.getStart(), 2);
541  1 assertEquals(match.getEnd(), 2);
542   
543    /*
544    * a second Find should not return the 'b' in seq3 as outside the selection
545    */
546  1 f.findNext("b", false, false, false, false);
547  1 assertTrue(f.getSearchResults().isEmpty());
548  1 assertTrue(f.getIdMatches().isEmpty());
549   
550  1 f = new Finder(av);
551  1 f.findNext("d", false, false, false, false);
552  1 assertTrue(f.getIdMatches().isEmpty());
553  1 searchResults = f.getSearchResults();
554  1 assertEquals(searchResults.getCount(), 1);
555  1 match = searchResults.getResults().get(0);
556  1 assertSame(match.getSequence(), al.getSequenceAt(1));
557  1 assertEquals(match.getStart(), 4);
558  1 assertEquals(match.getEnd(), 4);
559  1 f.findNext("d", false, false, false, false);
560  1 assertTrue(f.getIdMatches().isEmpty());
561  1 searchResults = f.getSearchResults();
562  1 assertEquals(searchResults.getCount(), 1);
563  1 match = searchResults.getResults().get(0);
564  1 assertSame(match.getSequence(), al.getSequenceAt(2));
565  1 assertEquals(match.getStart(), 3);
566  1 assertEquals(match.getEnd(), 3);
567    }
568   
569    /**
570    * Test finding all matches of a search pattern in a selection group
571    */
 
572  1 toggle @Test(groups = "Functional")
573    public void testFindAll_inSelection()
574    {
575    /*
576    * select sequences 2 and 3, columns 4-6 which contains
577    * BCD
578    * cdE
579    */
580  1 SequenceGroup sg = new SequenceGroup();
581  1 sg.setStartRes(3);
582  1 sg.setEndRes(5);
583  1 sg.addSequence(al.getSequenceAt(1), false);
584  1 sg.addSequence(al.getSequenceAt(2), false);
585  1 av.setSelectionGroup(sg);
586   
587    /*
588    * search for 'e' should match two sequence ids and one residue
589    */
590  1 Finder f = new Finder(av);
591  1 f.findAll("e", false, false, false, false);
592  1 assertEquals(f.getIdMatches().size(), 2);
593  1 assertSame(f.getIdMatches().get(0), al.getSequenceAt(1));
594  1 assertSame(f.getIdMatches().get(1), al.getSequenceAt(2));
595  1 SearchResultsI searchResults = f.getSearchResults();
596  1 assertEquals(searchResults.getCount(), 1);
597  1 SearchResultMatchI match = searchResults.getResults().get(0);
598  1 assertSame(match.getSequence(), al.getSequenceAt(2));
599  1 assertEquals(match.getStart(), 4);
600  1 assertEquals(match.getEnd(), 4);
601   
602    /*
603    * search for 'Q' should match two sequence ids only
604    */
605  1 f = new Finder(av);
606  1 f.findAll("Q", false, false, false, false);
607  1 assertEquals(f.getIdMatches().size(), 2);
608  1 assertSame(f.getIdMatches().get(0), al.getSequenceAt(1));
609  1 assertSame(f.getIdMatches().get(1), al.getSequenceAt(2));
610  1 assertTrue(f.getSearchResults().isEmpty());
611    }
612   
613    /**
614    * Test finding in selection with a sequence too short to reach it
615    */
 
616  1 toggle @Test(groups = "Functional")
617    public void testFind_findAllInSelectionWithShortSequence()
618    {
619    /*
620    * select all sequences, columns 10-12
621    * BCD
622    * cdE
623    */
624  1 SequenceGroup sg = new SequenceGroup();
625  1 sg.setStartRes(9);
626  1 sg.setEndRes(11);
627  1 sg.addSequence(al.getSequenceAt(0), false);
628  1 sg.addSequence(al.getSequenceAt(1), false);
629  1 sg.addSequence(al.getSequenceAt(2), false);
630  1 sg.addSequence(al.getSequenceAt(3), false);
631  1 av.setSelectionGroup(sg);
632   
633    /*
634    * search for 'I' should match two sequence positions
635    */
636  1 Finder f = new Finder(av);
637  1 f.findAll("I", false, false, false, false);
638  1 assertTrue(f.getIdMatches().isEmpty());
639  1 SearchResultsI searchResults = f.getSearchResults();
640  1 assertEquals(searchResults.getCount(), 2);
641  1 SearchResultMatchI match = searchResults.getResults().get(0);
642  1 assertSame(match.getSequence(), al.getSequenceAt(0));
643  1 assertEquals(match.getStart(), 16);
644  1 assertEquals(match.getEnd(), 16);
645  1 match = searchResults.getResults().get(1);
646  1 assertSame(match.getSequence(), al.getSequenceAt(1));
647  1 assertEquals(match.getStart(), 8);
648  1 assertEquals(match.getEnd(), 8);
649    }
650   
651    /**
652    * Test that find does not report hidden positions, but does report matches
653    * that span hidden gaps
654    */
 
655  1 toggle @Test(groups = "Functional")
656    public void testFind_withHiddenColumns()
657    {
658    /*
659    * 0 5 9
660    * ABCD--EF-GHI
661    * A--BCDefHI
662    * --bcdEFH
663    * aa---aMMMMMaaa
664    */
665   
666    /*
667    * hide column 3 only, search for aaa
668    * should find two matches: aa-[-]-aa and trailing aaa
669    */
670  1 HiddenColumns hc = new HiddenColumns();
671  1 hc.hideColumns(3, 3);
672  1 al.setHiddenColumns(hc);
673  1 Finder f = new Finder(av);
674  1 f.findAll("aaa", false, false, false, false);
675  1 SearchResultsI searchResults = f.getSearchResults();
676  1 assertEquals(searchResults.getCount(), 2);
677  1 SearchResultMatchI match = searchResults.getResults().get(0);
678  1 assertSame(match.getSequence(), al.getSequenceAt(3));
679  1 assertEquals(match.getStart(), 1);
680  1 assertEquals(match.getEnd(), 3);
681  1 match = searchResults.getResults().get(1);
682  1 assertSame(match.getSequence(), al.getSequenceAt(3));
683  1 assertEquals(match.getStart(), 9);
684  1 assertEquals(match.getEnd(), 11);
685   
686    /*
687    * hide 2-4 (CD- -BC bcd ---)
688    */
689  1 hc.hideColumns(2, 4);
690   
691    /*
692    * find all search for D should ignore hidden positions in seq1 and seq3,
693    * find the visible D in seq2
694    */
695  1 f = new Finder(av);
696  1 f.findAll("D", false, false, false, false);
697  1 searchResults = f.getSearchResults();
698  1 assertEquals(searchResults.getCount(), 1);
699  1 match = searchResults.getResults().get(0);
700  1 assertSame(match.getSequence(), al.getSequenceAt(1));
701  1 assertEquals(match.getStart(), 4);
702  1 assertEquals(match.getEnd(), 4);
703   
704    /*
705    * search for AD should fail although these are now
706    * consecutive in the visible columns
707    */
708  1 f = new Finder(av);
709  1 f.findAll("AD", false, false, false, false);
710  1 searchResults = f.getSearchResults();
711  1 assertTrue(searchResults.isEmpty());
712   
713    /*
714    * find all 'aaa' should find both start and end of seq4
715    * (first run includes hidden gaps)
716    */
717  1 f = new Finder(av);
718  1 f.findAll("aaa", false, false, false, false);
719  1 searchResults = f.getSearchResults();
720  1 assertEquals(searchResults.getCount(), 2);
721  1 match = searchResults.getResults().get(0);
722  1 assertSame(match.getSequence(), al.getSequenceAt(3));
723  1 assertEquals(match.getStart(), 1);
724  1 assertEquals(match.getEnd(), 3);
725  1 match = searchResults.getResults().get(1);
726  1 assertSame(match.getSequence(), al.getSequenceAt(3));
727  1 assertEquals(match.getStart(), 9);
728  1 assertEquals(match.getEnd(), 11);
729   
730    /*
731    * hide columns 2-5:
732    * find all 'aaa' should match twice in seq4
733    * (first match partly hidden, second all visible)
734    */
735  1 hc.hideColumns(2, 5);
736  1 f = new Finder(av);
737  1 f.findAll("aaa", false, false, false, false);
738  1 searchResults = f.getSearchResults();
739  1 assertEquals(searchResults.getCount(), 2);
740  1 match = searchResults.getResults().get(0);
741  1 assertSame(match.getSequence(), al.getSequenceAt(3));
742  1 assertEquals(match.getStart(), 1);
743  1 assertEquals(match.getEnd(), 3);
744  1 match = searchResults.getResults().get(1);
745  1 assertSame(match.getSequence(), al.getSequenceAt(3));
746  1 assertEquals(match.getStart(), 9);
747  1 assertEquals(match.getEnd(), 11);
748   
749    /*
750    * find all 'BE' should not match across hidden columns in seq1
751    */
752  1 f.findAll("BE", false, false, false, false);
753  1 assertTrue(f.getSearchResults().isEmpty());
754   
755    /*
756    * boundary case: hide columns at end of alignment
757    * search for H should match seq3/6 only
758    */
759  1 hc.revealAllHiddenColumns(new ColumnSelection());
760  1 hc.hideColumns(8, 13);
761  1 f = new Finder(av);
762  1 f.findNext("H", false, false, false, false);
763  1 searchResults = f.getSearchResults();
764  1 assertEquals(searchResults.getCount(), 1);
765  1 match = searchResults.getResults().get(0);
766  1 assertSame(match.getSequence(), al.getSequenceAt(2));
767  1 assertEquals(match.getStart(), 6);
768  1 assertEquals(match.getEnd(), 6);
769    }
770   
 
771  1 toggle @Test(groups = "Functional")
772    public void testFind_withHiddenColumnsAndSelection()
773    {
774    /*
775    * 0 5 9
776    * ABCD--EF-GHI
777    * A--BCDefHI
778    * --bcdEFH
779    * aa---aMMMMMaaa
780    */
781   
782    /*
783    * hide columns 2-4 and 6-7
784    */
785  1 HiddenColumns hc = new HiddenColumns();
786  1 hc.hideColumns(2, 4);
787  1 hc.hideColumns(6, 7);
788  1 al.setHiddenColumns(hc);
789   
790    /*
791    * select rows 2-3
792    */
793  1 SequenceGroup sg = new SequenceGroup();
794  1 sg.addSequence(al.getSequenceAt(1), false);
795  1 sg.addSequence(al.getSequenceAt(2), false);
796  1 sg.setStartRes(0);
797  1 sg.setEndRes(13);
798  1 av.setSelectionGroup(sg);
799   
800    /*
801    * find all search for A or H
802    * should match seq2/1, seq2/7, not seq3/6
803    */
804  1 Finder f = new Finder(av);
805  1 f.findAll("[AH]", false, false, false, false);
806  1 SearchResultsI searchResults = f.getSearchResults();
807  1 assertEquals(searchResults.getCount(), 2);
808  1 SearchResultMatchI match = searchResults.getResults().get(0);
809  1 assertSame(match.getSequence(), al.getSequenceAt(1));
810  1 assertEquals(match.getStart(), 1);
811  1 assertEquals(match.getEnd(), 1);
812  1 match = searchResults.getResults().get(1);
813  1 assertSame(match.getSequence(), al.getSequenceAt(1));
814  1 assertEquals(match.getStart(), 7);
815  1 assertEquals(match.getEnd(), 7);
816    }
817   
 
818  1 toggle @Test(groups = "Functional")
819    public void testFind_ignoreHiddenColumns()
820    {
821    /*
822    * 0 5 9
823    * ABCD--EF-GHI
824    * A--BCDefHI
825    * --bcdEFH
826    * aa---aMMMMMaaa
827    */
828  1 HiddenColumns hc = new HiddenColumns();
829  1 hc.hideColumns(2, 4);
830  1 hc.hideColumns(7, 7);
831  1 al.setHiddenColumns(hc);
832   
833    /*
834    * now have
835    * 015689
836    * AB-E-GHI
837    * A-DeHI
838    * --EF
839    * aaaMMMMaaa
840    */
841  1 Finder f = new Finder(av);
842  1 f.findAll("abe", false, false, false, true); // true = ignore hidden
843  1 SearchResultsI searchResults = f.getSearchResults();
844   
845    /*
846    * match of seq1 ABE made up of AB and E
847    * note only one match is counted
848    */
849  1 assertEquals(searchResults.getCount(), 1);
850  1 assertEquals(searchResults.getResults().size(), 2);
851  1 SearchResultMatchI match = searchResults.getResults().get(0);
852  1 assertSame(match.getSequence(), al.getSequenceAt(0));
853  1 assertEquals(match.getStart(), 8); // A
854  1 assertEquals(match.getEnd(), 9); // B
855  1 match = searchResults.getResults().get(1);
856  1 assertSame(match.getSequence(), al.getSequenceAt(0));
857  1 assertEquals(match.getStart(), 12); // E
858  1 assertEquals(match.getEnd(), 12);
859   
860  1 f = new Finder(av);
861  1 f.findNext("a.E", false, false, false, true);
862  1 searchResults = f.getSearchResults();
863  1 assertEquals(searchResults.getCount(), 1);
864  1 assertEquals(searchResults.getResults().size(), 2);
865  1 match = searchResults.getResults().get(0);
866  1 assertSame(match.getSequence(), al.getSequenceAt(0));
867  1 assertEquals(match.getStart(), 8); // A
868  1 assertEquals(match.getEnd(), 9); // B
869  1 match = searchResults.getResults().get(1);
870  1 assertSame(match.getSequence(), al.getSequenceAt(0));
871  1 assertEquals(match.getStart(), 12); // E
872  1 assertEquals(match.getEnd(), 12);
873   
874  1 f.findNext("a.E", false, false, false, true);
875  1 searchResults = f.getSearchResults();
876  1 assertEquals(searchResults.getCount(), 1);
877  1 assertEquals(searchResults.getResults().size(), 2);
878  1 match = searchResults.getResults().get(0);
879  1 assertSame(match.getSequence(), al.getSequenceAt(1));
880  1 assertEquals(match.getStart(), 1); // a
881  1 assertEquals(match.getEnd(), 1);
882  1 match = searchResults.getResults().get(1);
883  1 assertSame(match.getSequence(), al.getSequenceAt(1));
884  1 assertEquals(match.getStart(), 4); // D
885  1 assertEquals(match.getEnd(), 5); // e
886   
887    /*
888    * find all matching across two hidden column regions
889    * note one 'match' is returned as three contiguous matches
890    */
891  1 f.findAll("BEG", false, false, false, true);
892  1 searchResults = f.getSearchResults();
893  1 assertEquals(searchResults.getCount(), 1);
894  1 assertEquals(searchResults.getResults().size(), 3);
895  1 match = searchResults.getResults().get(0);
896  1 assertSame(match.getSequence(), al.getSequenceAt(0));
897  1 assertEquals(match.getStart(), 9); // B
898  1 assertEquals(match.getEnd(), 9);
899  1 match = searchResults.getResults().get(1);
900  1 assertSame(match.getSequence(), al.getSequenceAt(0));
901  1 assertEquals(match.getStart(), 12); // E
902  1 assertEquals(match.getEnd(), 12);
903  1 match = searchResults.getResults().get(2);
904  1 assertSame(match.getSequence(), al.getSequenceAt(0));
905  1 assertEquals(match.getStart(), 14); // G
906  1 assertEquals(match.getEnd(), 14);
907   
908    /*
909    * now select columns 0-9 and search for A.*H
910    * this should match in the second sequence (split as 3 matches)
911    * but not the first (as H is outside the selection)
912    */
913  1 SequenceGroup selection = new SequenceGroup();
914  1 selection.setStartRes(0);
915  1 selection.setEndRes(9);
916  1 al.getSequences().forEach(seq -> selection.addSequence(seq, false));
917  1 av.setSelectionGroup(selection);
918  1 f.findAll("A.*H", false, false, false, true);
919  1 searchResults = f.getSearchResults();
920  1 assertEquals(searchResults.getCount(), 1);
921  1 assertEquals(searchResults.getResults().size(), 3);
922    // match made of contiguous matches A, DE, H
923  1 match = searchResults.getResults().get(0);
924  1 assertSame(match.getSequence(), al.getSequenceAt(1));
925  1 assertEquals(match.getStart(), 1); // A
926  1 assertEquals(match.getEnd(), 1);
927  1 match = searchResults.getResults().get(1);
928  1 assertSame(match.getSequence(), al.getSequenceAt(1));
929  1 assertEquals(match.getStart(), 4); // D
930  1 assertEquals(match.getEnd(), 5); // E
931  1 match = searchResults.getResults().get(2);
932  1 assertSame(match.getSequence(), al.getSequenceAt(1));
933  1 assertEquals(match.getStart(), 7); // H (there is no G)
934  1 assertEquals(match.getEnd(), 7);
935    }
936   
 
937  1 toggle @Test(groups = "Functional")
938    public void testFind_featuresOnly()
939    {
940  1 Finder f = new Finder(av);
941    // no match when not searching feature descriptions
942  1 f.findAll("Feature", false, false, false, true);
943  1 assertEquals(f.getSearchResults().getCount(), 0);
944   
945    // no match when case sensitive on feature descriptions
946  1 f.findAll("feature", true, false, true, true);
947  1 assertEquals(f.getSearchResults().getCount(), 0);
948   
949    // search feature descriptions - all match
950  1 f.findAll("Feature", false, false, true, true);
951  1 assertEquals(f.getSearchResults().getCount(), 3);
952   
953  1 List<SequenceI> seqs = f.getSearchResults().getMatchingSubSequences();
954    // assume order is preserved in results
955  1 assertEquals(al.getSequenceAt(0).getDatasetSequence(),
956    seqs.get(0).getDatasetSequence());
957  1 assertEquals(seqs.get(0).getStart(), 9);
958  1 assertEquals(seqs.get(0).getEnd(), 11);
959  1 assertEquals(al.getSequenceAt(3).getDatasetSequence(),
960    seqs.get(1).getDatasetSequence());
961  1 assertEquals(seqs.get(1).getStart(), 9);
962  1 assertEquals(seqs.get(1).getEnd(), 11);
963  1 assertEquals(al.getSequenceAt(3).getDatasetSequence(),
964    seqs.get(2).getDatasetSequence());
965  1 assertEquals(seqs.get(2).getStart(), 1);
966  1 assertEquals(seqs.get(2).getEnd(), 3);
967   
968  1 SequenceI sq = null;
969    // search feature descriptions incrementally
970    // assume same order as before
971  1 f.findNext("Feature", false, false, true, true);
972  1 assertEquals(f.getSearchResults().getCount(), 1);
973  1 sq = f.getSearchResults().getMatchingSubSequences().get(0);
974  1 assertEquals(sq.getSequenceAsString(),
975    seqs.get(0).getSequenceAsString());
976   
977    // ..
978  1 f.findNext("Feature", false, false, true, true);
979  1 assertEquals(f.getSearchResults().getCount(), 1);
980  1 sq = f.getSearchResults().getMatchingSubSequences().get(0);
981  1 assertEquals(sq.getSequenceAsString(),
982    seqs.get(1).getSequenceAsString());
983   
984    // ..
985  1 f.findNext("Feature", false, false, true, true);
986  1 assertEquals(f.getSearchResults().getCount(), 1);
987  1 sq = f.getSearchResults().getMatchingSubSequences().get(0);
988  1 assertEquals(sq.getSequenceAsString(),
989    seqs.get(2).getSequenceAsString());
990   
991    }
992    }