2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.datamodel;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertSame;
26 import static org.testng.AssertJUnit.assertTrue;
28 import java.util.BitSet;
29 import java.util.List;
31 import org.junit.Assert;
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.Test;
35 import jalview.gui.JvOptionPane;
37 public class SearchResultsTest
40 @BeforeClass(alwaysRun = true)
41 public void setUpJvOptionPane()
43 JvOptionPane.setInteractiveMode(false);
44 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
47 @Test(groups = { "Functional" })
48 public void testToString()
50 SequenceI seq = new Sequence("Seq1", "abcdefghijklm");
51 SearchResultsI sr = new SearchResults();
52 sr.addResult(seq, 1, 1);
53 assertEquals("[Seq1/1-1]", sr.toString());
54 sr.addResult(seq, 3, 5);
55 assertEquals("[Seq1/1-1, Seq1/3-5]", sr.toString());
57 seq = new Sequence("Seq2", "pqrstuvwxy");
58 sr.addResult(seq, 6, 7);
59 assertEquals("[Seq1/1-1, Seq1/3-5, Seq2/6-7]", sr.toString());
62 @Test(groups = { "Functional" })
63 public void testEquals()
65 SequenceI seq1 = new Sequence("", "abcdefghijklm");
66 SearchResultsI sr1 = new SearchResults();
67 SearchResultsI sr2 = new SearchResults();
69 assertFalse(sr1.equals(null)); // null object
70 assertFalse(sr1.equals(seq1)); // wrong type
71 assertTrue(sr1.equals(sr1)); // self
72 assertTrue(sr1.equals(sr2)); // empty
73 assertTrue(sr2.equals(sr1)); // reflexive
76 * if only one result is not empty
78 sr1.addResult(seq1, 1, 1);
79 assertTrue(sr1.equals(sr1));
80 assertFalse(sr1.equals(sr2));
81 assertFalse(sr2.equals(sr1));
86 sr2.addResult(seq1, 1, 1);
87 assertTrue(sr1.equals(sr2));
88 assertTrue(sr2.equals(sr1));
91 * both have three matches
93 sr1.addResult(seq1, 3, 4);
94 sr1.addResult(seq1, 6, 8);
95 sr2.addResult(seq1, 3, 4);
96 sr2.addResult(seq1, 6, 8);
97 assertTrue(sr1.equals(sr1));
98 assertTrue(sr2.equals(sr2));
99 assertTrue(sr1.equals(sr2));
100 assertTrue(sr2.equals(sr1));
104 * Matches that are similar but for distinct sequences are not equal
106 @Test(groups = { "Functional" })
107 public void testEquals_distinctSequences()
109 SequenceI seq1 = new Sequence("", "abcdefghijklm");
110 SequenceI seq2 = new Sequence("", "abcdefghijklm");
111 SearchResultsI sr1 = new SearchResults();
112 SearchResultsI sr2 = new SearchResults();
114 sr1.addResult(seq1, 1, 1);
115 sr2.addResult(seq2, 1, 1);
116 assertFalse(sr1.equals(sr2));
117 assertFalse(sr2.equals(sr1));
121 * Matches that are the same except for ordering are not equal
123 @Test(groups = { "Functional" })
124 public void testEquals_orderDiffers()
126 SequenceI seq1 = new Sequence("", "abcdefghijklm");
127 SearchResultsI sr1 = new SearchResults();
128 SearchResultsI sr2 = new SearchResults();
130 sr1.addResult(seq1, 1, 1);
131 sr1.addResult(seq1, 2, 2);
132 sr2.addResult(seq1, 2, 2);
133 sr2.addResult(seq1, 1, 1);
134 assertFalse(sr1.equals(sr2));
135 assertFalse(sr2.equals(sr1));
139 * Verify that hashCode matches for equal objects
141 @Test(groups = { "Functional" })
142 public void testHashcode()
144 SequenceI seq1 = new Sequence("", "abcdefghijklm");
145 SearchResultsI sr1 = new SearchResults();
146 SearchResultsI sr2 = new SearchResults();
151 assertEquals(sr1.hashCode(), sr2.hashCode());
156 sr1.addResult(seq1, 1, 1);
157 sr2.addResult(seq1, 1, 1);
158 assertEquals(sr1.hashCode(), sr2.hashCode());
163 sr1.addResult(seq1, 3, 4);
164 sr1.addResult(seq1, 6, 8);
165 sr2.addResult(seq1, 3, 4);
166 sr2.addResult(seq1, 6, 8);
167 assertEquals(sr1.hashCode(), sr2.hashCode());
171 * Verify that SearchResults$Match constructor normalises start/end to the
172 * 'forwards' direction
174 @Test(groups = { "Functional" })
175 public void testMatchConstructor()
177 SequenceI seq1 = new Sequence("", "abcdefghijklm");
178 SearchResultMatchI m = new SearchResults().new Match(seq1, 2, 5);
179 assertSame(seq1, m.getSequence());
180 assertEquals(2, m.getStart());
181 assertEquals(5, m.getEnd());
183 // now a reverse mapping:
184 m = new SearchResults().new Match(seq1, 5, 2);
185 assertSame(seq1, m.getSequence());
186 assertEquals(2, m.getStart());
187 assertEquals(5, m.getEnd());
190 @Test(groups = { "Functional" })
191 public void testMatchContains()
193 SequenceI seq1 = new Sequence("", "abcdefghijklm");
194 SequenceI seq2 = new Sequence("", "abcdefghijklm");
195 SearchResultMatchI m = new SearchResults().new Match(seq1, 2, 5);
197 assertTrue(m.contains(seq1, 2, 5));
198 assertTrue(m.contains(seq1, 3, 5));
199 assertTrue(m.contains(seq1, 2, 4));
200 assertTrue(m.contains(seq1, 3, 3));
202 assertFalse(m.contains(seq1, 2, 6));
203 assertFalse(m.contains(seq1, 1, 5));
204 assertFalse(m.contains(seq1, 1, 8));
205 assertFalse(m.contains(seq2, 3, 3));
206 assertFalse(m.contains(null, 3, 3));
210 * test markColumns for creating column selections
212 @Test(groups = { "Functional" })
213 public void testMarkColumns()
216 SequenceI seq1 = new Sequence("", "abcdefghijklm");
217 SequenceI seq2 = new Sequence("", "abcdefghijklm");
218 SequenceGroup s1g = new SequenceGroup(), s2g = new SequenceGroup(),
219 sallg = new SequenceGroup();
220 s1g.addSequence(seq1, false);
221 s2g.addSequence(seq2, false);
222 sallg.addSequence(seq1, false);
223 sallg.addSequence(seq2, false);
225 SearchResultsI sr = new SearchResults();
226 BitSet bs = new BitSet();
228 SearchResultMatchI srm = null;
229 srm = sr.addResult(seq1, 1, 1);
230 Assert.assertNotNull("addResult didn't return Match", srm);
231 srm = sr.addResult(seq2, 1, 2);
232 assertEquals("Sequence reference not set", seq2, srm.getSequence());
233 assertEquals("match start incorrect", 1, srm.getStart());
234 assertEquals("match end incorrect", 2, srm.getEnd());
236 // set start/end range for groups to cover matches
242 sallg.setStartRes(0);
248 marked = sr.markColumns(s1g, bs);
249 // check the bitset cardinality before checking the return value
250 assertEquals("Didn't mark expected number", 1, bs.cardinality());
251 assertEquals("Didn't return count of number of bits marked", 1, marked);
252 assertTrue("Didn't mark expected position", bs.get(0));
253 // now check return value for marking the same again
255 "Didn't count number of bits marked for existing marked set", 0,
256 sr.markColumns(s1g, bs));
262 marked = sr.markColumns(s2g, bs);
263 assertEquals("Didn't mark expected number", 2, bs.cardinality());
264 assertEquals("Didn't return count of number of bits marked", 2, marked);
265 assertTrue("Didn't mark expected position (1)", bs.get(0));
266 assertTrue("Didn't mark expected position (2)", bs.get(1));
270 * should be same as seq2
272 BitSet allbs = new BitSet();
273 assertEquals(2, sr.markColumns(sallg, allbs));
274 assertEquals(bs, allbs);
276 // now check range selection
279 * limit s2g to just the second column, sallg to the first column
284 BitSet tbs = new BitSet();
285 assertEquals("Group start/end didn't select columns to mark", 1,
286 sr.markColumns(s2g, tbs));
287 assertEquals("Group start/end didn't select columns to mark", 1,
288 sr.markColumns(sallg, tbs));
290 "Didn't set expected number of columns in total for two successive marks",
291 2, tbs.cardinality());
295 * Test to verify adding doesn't create duplicate results
297 @Test(groups = { "Functional" })
298 public void testAddResult()
300 SequenceI seq1 = new Sequence("", "abcdefghijklm");
301 SearchResultsI sr = new SearchResults();
302 sr.addResult(seq1, 3, 5);
303 assertEquals(1, sr.getCount());
304 sr.addResult(seq1, 3, 5);
305 assertEquals(1, sr.getCount());
306 sr.addResult(seq1, 3, 6);
307 assertEquals(2, sr.getCount());
311 * Test for method that checks if search results matches a sequence region
313 @Test(groups = { "Functional" })
314 public void testInvolvesSequence()
316 SequenceI dataset = new Sequence("genome", "ATGGCCCTTTAAGCAACATTT");
318 SequenceI cds1 = new Sequence("cds1/1-12", "ATGGCCCTTTAA");
319 cds1.setDatasetSequence(dataset);
320 // overlapping second 'exon':
321 SequenceI cds2 = new Sequence("cds2/7-18", "CTTTAAGCAACA");
322 cds2.setDatasetSequence(dataset);
323 // unrelated sequence
324 SequenceI cds3 = new Sequence("cds3", "ATGGCCCTTTAAGCAACA");
326 SearchResults sr = new SearchResults();
327 assertFalse(sr.involvesSequence(cds1));
330 * cds1 and cds2 share the same dataset sequence, but
331 * only cds1 overlaps match 4:6 (fixes bug JAL-3613)
333 sr.addResult(dataset, 4, 6);
334 assertTrue(sr.involvesSequence(cds1));
335 assertFalse(sr.involvesSequence(cds2));
336 assertFalse(sr.involvesSequence(cds3));
339 * search results overlap cds2 only
341 sr = new SearchResults();
342 sr.addResult(dataset, 18, 18);
343 assertFalse(sr.involvesSequence(cds1));
344 assertTrue(sr.involvesSequence(cds2));
347 * add a search result overlapping cds1
349 sr.addResult(dataset, 1, 1);
350 assertTrue(sr.involvesSequence(cds1));
351 assertTrue(sr.involvesSequence(cds2));
354 * single search result overlapping both
356 sr = new SearchResults();
357 sr.addResult(dataset, 10, 12);
358 assertTrue(sr.involvesSequence(cds1));
359 assertTrue(sr.involvesSequence(cds2));
362 * search results matching aligned sequence
364 sr = new SearchResults();
365 sr.addResult(cds1, 10, 12);
366 assertTrue(sr.involvesSequence(cds1));
367 assertFalse(sr.involvesSequence(cds2));
368 sr.addResult(cds2, 1, 3); // no start-end overlap
369 assertFalse(sr.involvesSequence(cds2));
370 sr.addResult(cds2, 7, 9); // start-end overlap
371 assertTrue(sr.involvesSequence(cds2));
375 * Test extraction of Sequence objects for matched ranges on a sequence
377 @Test(groups = { "Functional" })
378 public void testGetSequences()
380 SequenceI seq1 = new Sequence("", "abcdefghijklm");
381 SequenceI seq2 = new Sequence("", "nopqrstuvwxyz");
384 List<SequenceI> seqres = null;
386 SearchResultsI sr = new SearchResults();
387 seqres = sr.getMatchingSubSequences();
388 assertEquals(0, seqres.size());
390 sr.addResult(seq1, 3, 5);
391 seqres = sr.getMatchingSubSequences();
393 assertEquals(1, seqres.size());
394 assertEquals("cde", seqres.get(0).getSequenceAsString());
395 assertEquals(3, seqres.get(0).getStart());
396 assertEquals(seq1, seqres.get(0).getDatasetSequence());
398 sr.addResult(seq1, 3, 6);
399 seqres = sr.getMatchingSubSequences();
401 assertEquals(2, seqres.size());
402 assertEquals("cdef", seqres.get(1).getSequenceAsString());
403 assertEquals(3, seqres.get(1).getStart());
405 // this is a quirk - match on 26-29 yields subsequence 27-30
406 sr.addResult(seq2, 26, 29);
407 seqres = sr.getMatchingSubSequences();
408 assertEquals(3, seqres.size());
409 assertEquals("qrst", seqres.get(2).getSequenceAsString());
410 assertEquals(26, seqres.get(2).getStart());