76e34d97c234ae97b936ce9889ad81f171f62635
[jalview.git] / test / jalview / analysis / SequenceIdMatcherTest.java
1 package jalview.analysis;
2
3 import jalview.datamodel.Sequence;
4 import jalview.datamodel.SequenceI;
5
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.List;
9
10 import org.testng.Assert;
11 import org.testng.annotations.Test;
12
13 public class SequenceIdMatcherTest
14 {
15   private static SequenceI[] someseqs = {
16       new Sequence("A|ComplexId", "dummy"),
17       new Sequence("A|ComplexId|confused", "dummy"),
18       new Sequence("A|ComplexId|bits_of", "dummy"),
19       new Sequence("ComplexId", "dummy"),
20       new Sequence("A|ComplexIdNot", "dummy"),
21       new Sequence("A ComplexId Id", "dummy"),
22       new Sequence("complexid", "dummy") };
23
24   private static SequenceIdMatcher getMatcher()
25   {
26     return new SequenceIdMatcher(Arrays.asList(someseqs));
27   }
28
29   private static SequenceIdMatcher getWordMatcher()
30   {
31     return new SequenceIdMatcher(true, Arrays.asList(someseqs));
32   }
33
34   @Test(groups = { "Functional" })
35   public void findSelfAndOthers()
36   {
37     for (SequenceI sq : SequenceIdMatcherTest.someseqs)
38     {
39       System.out.println("Searching with '" + sq.getName() + "'");
40       SequenceI[] idmatches = getMatcher().findAllIdMatches(sq.getName());
41       Assert.assertTrue(
42               idmatches.length >= 1,
43               "Couldn't recover at least one sequence for string '"
44                       + sq.getName() + "'");
45       for (SequenceI f : idmatches)
46       {
47         System.out.println("For '" + sq.getName() + "' found '"
48                 + f.getName() + "'");
49       }
50
51       SequenceI[] seqmatches = getMatcher().findIdMatch(
52               new SequenceI[] { sq });
53       Assert.assertEquals(1, seqmatches.length,
54               "Expected to recover one sequence for sequence object called '"
55                       + sq.getName() + "'");
56       Assert.assertEquals(sq, seqmatches[0],
57               "Expected to recover the sequence queried with findIdMatch(SequenceI[])");
58       // TODO: complexid and ComplexId are identical with case-insensitive
59       // matching. This assert fails because of this.
60       // Assert.assertTrue(seqmatches.length == idmatches.length,
61       // "Different matches found for '" + sq.getName() + "'");
62       for (SequenceI sid : seqmatches)
63       {
64         boolean found = false;
65         for (SequenceI sobj : idmatches)
66         {
67           if (sid == sobj)
68           {
69             found = true;
70           }
71         }
72         Assert.assertTrue(
73                 found,
74                 "Different sequences recovered for Id "
75                         + "and SequenceI (Couldn't find match for '"
76                         + sid.getName() + "')");
77
78       }
79     }
80   }
81
82   @Test(groups = { "Functional" })
83   public void testExactMatch()
84   {
85     SequenceI[] matches = getMatcher().findAllIdMatches("A|ComplexId");
86     Assert.assertTrue(matches.length == 1,
87             "Exact match failed for 'A|ComplexId'");
88     matches = getMatcher().findAllIdMatches("A|ComplexId|confused");
89     Assert.assertTrue(matches.length == 1,
90             "Exact match failed for 'A|ComplexId|confused'");
91     matches = getMatcher().findAllIdMatches("A|ComplexId|bits_of");
92     Assert.assertTrue(matches.length == 1,
93             "Exact match failed for 'A|ComplexId|bits_of'");
94     matches = getMatcher().findAllIdMatches("A ComplexId Id");
95     Assert.assertTrue(matches.length == 1,
96             "Exact match failed for 'A Complex Id'");
97
98   }
99
100   @Test(groups = { "Functional" })
101   public void testCaseInsensitiveMatch()
102   {
103     Assert.assertNotNull(getMatcher().findIdMatch("a|complexid"),
104             "Couldn't retrieve a single case insensitive match.");
105   }
106
107   @Test(groups = { "Functional" })
108   public void testWordSplit()
109   {
110     String[] words = new String[] { "several", "words", "separated",
111         "fully" };
112     String full = "";
113     for (String word : words)
114     {
115       if (full.length() > 0)
116       {
117         full += "|";
118       }
119       full += word;
120     }
121     List<SeqIdName> bits = SequenceIdMatcher.getWordsFor(new Sequence(full,
122             "dummy"));
123     for (String word : words)
124     {
125       List<SeqIdName> equals = new ArrayList<SeqIdName>();
126       for (SeqIdName bit : bits)
127       {
128         if (bit.equals(word))
129         {
130           equals.add(bit);
131         }
132       }
133       Assert.assertTrue(equals.size() > 0,
134               "Word generation has broken. Expected at least one match for '"
135                       + word + "'");
136     }
137   }
138   @Test(groups = { "Functional" })
139   public void testFlankingMatch()
140   {
141     SequenceI[] match = getMatcher().findAllIdMatches("complexId");
142     // should find two matches - one case exact, the other case inexact.
143     Assert.assertNotNull(match, "Exact matches not found.");
144     Assert.assertEquals(match.length, 2,
145             "Expected two exact matches to be found.");
146     SequenceI[] fmatch = getWordMatcher()
147             .findAllIdMatches("complexId");
148     // should find 6 distinct sequences
149     Assert.assertNotNull(fmatch, "Flanking matches not found.");
150     for (SequenceI f:fmatch)
151     {
152       System.out.println("Flanking 'complexId' match: '" + f.getName()
153               + "'");
154     }
155     Assert.assertEquals(fmatch.length, 6,
156             "Couldn't find all entries with IDs containing 'complexId' word match");
157
158   }
159
160   @Test(groups = { "Functional" })
161   public void testPartialNotMatch()
162   {
163     SequenceI[] match = getWordMatcher().findAllIdMatches("complex");
164     Assert.assertNull(match,
165             "Partial match of 'complex' to any of sequences shouldn't yield a match.");
166   }
167
168 }