Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
ParsePropertiesTest | 39 | 81 | 5 |
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.AssertJUnit.assertEquals; | |
24 | import static org.testng.AssertJUnit.assertNull; | |
25 | ||
26 | import jalview.datamodel.Alignment; | |
27 | import jalview.datamodel.AlignmentAnnotation; | |
28 | import jalview.datamodel.Sequence; | |
29 | import jalview.datamodel.SequenceI; | |
30 | import jalview.gui.AlignFrame; | |
31 | import jalview.gui.JvOptionPane; | |
32 | ||
33 | import java.util.List; | |
34 | ||
35 | import org.testng.annotations.BeforeClass; | |
36 | import org.testng.annotations.BeforeMethod; | |
37 | import org.testng.annotations.Test; | |
38 | ||
39 | public class ParsePropertiesTest | |
40 | { | |
41 | ||
42 | 1 | @BeforeClass(alwaysRun = true) |
43 | public void setUpJvOptionPane() | |
44 | { | |
45 | 1 | JvOptionPane.setInteractiveMode(false); |
46 | 1 | JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); |
47 | } | |
48 | ||
49 | private Alignment al; | |
50 | ||
51 | private ParseProperties pp; | |
52 | ||
53 | /** | |
54 | * Construct an alignment with 4 sequences with varying description format | |
55 | */ | |
56 | 3 | @BeforeMethod(alwaysRun = true) |
57 | public void setUp() | |
58 | { | |
59 | 3 | SequenceI[] seqs = new SequenceI[] { |
60 | new Sequence("sq1", "THISISAPLACEHOLDER"), | |
61 | new Sequence("sq2", "THISISAPLACEHOLDER"), | |
62 | new Sequence("sq3", "THISISAPLACEHOLDER"), | |
63 | new Sequence("sq4", "THISISAPLACEHOLDER") }; | |
64 | 3 | seqs[0].setDescription("1 mydescription1"); |
65 | 3 | seqs[1].setDescription("mydescription2"); |
66 | 3 | seqs[2].setDescription("2. 0.1 mydescription+3"); |
67 | 3 | seqs[3].setDescription("3 0.01 mydescription4"); |
68 | 3 | al = new Alignment(seqs); |
69 | ||
70 | 3 | pp = new ParseProperties(al); |
71 | ||
72 | } | |
73 | ||
74 | /** | |
75 | * Test with a description pattern that matches any string ending in one or | |
76 | * more 'number characters' (0-9+.), i.e. greedily matches any trailing | |
77 | * numeric part of the string | |
78 | */ | |
79 | 1 | @Test(groups = { "Functional" }) |
80 | public void testGetScoresFromDescription() | |
81 | { | |
82 | 1 | String regex = ".*([-0-9.+]+)"; |
83 | 1 | final int count = pp.getScoresFromDescription("my Score", |
84 | "my Score Description", regex, true); | |
85 | 1 | System.out.println("Matched " + count + " for " + regex); |
86 | 1 | assertEquals(4, count); |
87 | ||
88 | /* | |
89 | * Verify values 1/2/3/4 have been parsed from sequence descriptions | |
90 | */ | |
91 | 1 | AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation(); |
92 | 1 | assertEquals(1, anns.length); |
93 | 1 | assertEquals(1d, anns[0].getScore(), 0.001d); |
94 | 1 | assertEquals("my Score Description", anns[0].description); |
95 | 1 | assertEquals("my Score", anns[0].label); |
96 | 1 | anns = al.getSequenceAt(1).getAnnotation(); |
97 | 1 | assertEquals(1, anns.length); |
98 | 1 | assertEquals(2d, anns[0].getScore(), 0.001d); |
99 | 1 | assertEquals("my Score Description", anns[0].description); |
100 | 1 | assertEquals("my Score", anns[0].label); |
101 | 1 | anns = al.getSequenceAt(2).getAnnotation(); |
102 | 1 | assertEquals(1, anns.length); |
103 | 1 | assertEquals(3d, anns[0].getScore(), 0.001d); |
104 | 1 | anns = al.getSequenceAt(3).getAnnotation(); |
105 | 1 | assertEquals(1, anns.length); |
106 | 1 | assertEquals(4d, anns[0].getScore(), 0.001d); |
107 | } | |
108 | ||
109 | /** | |
110 | * Test with a description pattern that matches any string (or none), followed | |
111 | * by a 'number character' (0-9+.), followed by at least one separator | |
112 | * character, followed by at least one 'number character', then any trailing | |
113 | * characters. | |
114 | */ | |
115 | 1 | @Test(groups = { "Functional" }) |
116 | public void testGetScoresFromDescription_twoScores() | |
117 | { | |
118 | 1 | String regex = ".*([-0-9.+]+).+([-0-9.+]+).*"; |
119 | 1 | final int count = pp.getScoresFromDescription("my Score", |
120 | "my Score Description", regex, true); | |
121 | 1 | System.out.println("Matched " + count + " for " + regex); |
122 | 1 | assertEquals(3, count); |
123 | ||
124 | /* | |
125 | * Seq1 has two score values parsed out | |
126 | */ | |
127 | 1 | AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation(); |
128 | 1 | assertEquals(2, anns.length); |
129 | 1 | assertEquals(1d, anns[0].getScore(), 0.001d); |
130 | 1 | assertEquals("my Score Description", anns[0].description); |
131 | 1 | assertEquals("my Score", anns[0].label); |
132 | 1 | assertEquals(1d, anns[1].getScore(), 0.001d); |
133 | 1 | assertEquals("my Score Description (column 1)", anns[1].description); |
134 | 1 | assertEquals("my Score_1", anns[1].label); |
135 | ||
136 | /* | |
137 | * Seq2 has no score parsed out (is this right?) | |
138 | */ | |
139 | 1 | assertNull(al.getSequenceAt(1).getAnnotation()); |
140 | ||
141 | /* | |
142 | * Seq3 has two score values parsed out | |
143 | */ | |
144 | // TODO parsed values (1.0 and 3.0) look wrong v description | |
145 | // would expect 2.0 and 0.1 | |
146 | // undesired 'greedy' behaviour of regex? | |
147 | 1 | anns = al.getSequenceAt(2).getAnnotation(); |
148 | 1 | assertEquals(2, anns.length); |
149 | 1 | assertEquals(1d, anns[0].getScore(), 0.001d); |
150 | 1 | assertEquals("my Score Description", anns[0].description); |
151 | 1 | assertEquals("my Score", anns[0].label); |
152 | 1 | assertEquals(3d, anns[1].getScore(), 0.001d); |
153 | 1 | assertEquals("my Score Description (column 1)", anns[1].description); |
154 | 1 | assertEquals("my Score_1", anns[1].label); |
155 | ||
156 | /* | |
157 | * Seq3 has two score values parsed out | |
158 | */ | |
159 | // TODO parsed values (1.0 and 4.0) look wrong v description | |
160 | // would expect 3 and 0.01 | |
161 | 1 | anns = al.getSequenceAt(3).getAnnotation(); |
162 | 1 | assertEquals(2, anns.length); |
163 | 1 | assertEquals(1d, anns[0].getScore(), 0.001d); |
164 | 1 | assertEquals("my Score Description", anns[0].description); |
165 | 1 | assertEquals("my Score", anns[0].label); |
166 | 1 | assertEquals(4d, anns[1].getScore(), 0.001d); |
167 | 1 | assertEquals("my Score Description (column 1)", anns[1].description); |
168 | 1 | assertEquals("my Score_1", anns[1].label); |
169 | } | |
170 | ||
171 | /** | |
172 | * Test with a regex that looks for numbers separated by words - as currently | |
173 | * used in Jalview (May 2015) | |
174 | * | |
175 | * @see AlignFrame.extractScores_actionPerformed | |
176 | */ | |
177 | 1 | @Test(groups = { "Functional" }) |
178 | public void testGetScoresFromDescription_wordBoundaries() | |
179 | { | |
180 | 1 | String regex = "\\W*([-+eE0-9.]+)"; |
181 | 1 | List<SequenceI> seqs = al.getSequences(); |
182 | 1 | seqs.get(0).setDescription("Ferredoxin"); |
183 | 1 | seqs.get(1).setDescription(" Ferredoxin-1, chloroplast precursor"); |
184 | 1 | seqs.get(2).setDescription("GH28E30p"); |
185 | 1 | seqs.get(3).setDescription("At1g10960/T19D16_12"); |
186 | 1 | final int count = pp.getScoresFromDescription("description column", |
187 | "score in description column ", regex, true); | |
188 | 1 | assertEquals(3, count); |
189 | ||
190 | /* | |
191 | * No score parsable from seq1 description | |
192 | */ | |
193 | 1 | AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation(); |
194 | 1 | assertNull(anns); |
195 | ||
196 | /* | |
197 | * Seq2 description has a '1' in it | |
198 | */ | |
199 | 1 | anns = al.getSequenceAt(1).getAnnotation(); |
200 | 1 | assertEquals(1, anns.length); |
201 | 1 | assertEquals(1d, anns[0].getScore(), 0.001d); |
202 | ||
203 | /* | |
204 | * Seq3 description has '28E30' in it | |
205 | * | |
206 | * Note: 1.8E308 or larger would result in 'Infinity' | |
207 | */ | |
208 | 1 | anns = al.getSequenceAt(2).getAnnotation(); |
209 | 1 | assertEquals(1, anns.length); |
210 | 1 | assertEquals(2.8E31d, anns[0].getScore(), 0.001d); |
211 | ||
212 | /* | |
213 | * Seq4 description has several numbers in it | |
214 | */ | |
215 | 1 | anns = al.getSequenceAt(3).getAnnotation(); |
216 | 1 | assertEquals(5, anns.length); |
217 | 1 | assertEquals(1d, anns[0].getScore(), 0.001d); |
218 | 1 | assertEquals(10960d, anns[1].getScore(), 0.001d); |
219 | 1 | assertEquals(19d, anns[2].getScore(), 0.001d); |
220 | 1 | assertEquals(16d, anns[3].getScore(), 0.001d); |
221 | 1 | assertEquals(12d, anns[4].getScore(), 0.001d); |
222 | } | |
223 | } |