Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
RnaTest | 40 | 121 | 81 |
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 java.util.Locale; | |
24 | ||
25 | import static org.testng.AssertJUnit.assertEquals; | |
26 | import static org.testng.AssertJUnit.assertFalse; | |
27 | import static org.testng.AssertJUnit.assertNull; | |
28 | import static org.testng.AssertJUnit.assertTrue; | |
29 | import static org.testng.AssertJUnit.fail; | |
30 | ||
31 | import jalview.analysis.SecStrConsensus.SimpleBP; | |
32 | import jalview.datamodel.SequenceFeature; | |
33 | import jalview.gui.JvOptionPane; | |
34 | ||
35 | import java.util.List; | |
36 | ||
37 | import org.testng.annotations.BeforeClass; | |
38 | import org.testng.annotations.Test; | |
39 | ||
40 | public class RnaTest | |
41 | { | |
42 | ||
43 | 1 | @BeforeClass(alwaysRun = true) |
44 | public void setUpJvOptionPane() | |
45 | { | |
46 | 1 | JvOptionPane.setInteractiveMode(false); |
47 | 1 | JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); |
48 | } | |
49 | ||
50 | 1 | @Test(groups = { "Functional" }) |
51 | public void testGetSimpleBPs() throws WUSSParseException | |
52 | { | |
53 | 1 | String rna = "([{})]"; // JAL-1081 example |
54 | 1 | List<SimpleBP> bps = Rna.getSimpleBPs(rna); |
55 | 1 | assertEquals(3, bps.size()); |
56 | ||
57 | /* | |
58 | * the base pairs are added in the order in which the matching base is found | |
59 | * (popping the stack of unmatched opening brackets) | |
60 | */ | |
61 | 1 | assertEquals(2, bps.get(0).bp5); // { |
62 | 1 | assertEquals(3, bps.get(0).bp3); // } |
63 | 1 | assertEquals(0, bps.get(1).bp5); // ( |
64 | 1 | assertEquals(4, bps.get(1).bp3); // ) |
65 | 1 | assertEquals(1, bps.get(2).bp5); // [ |
66 | 1 | assertEquals(5, bps.get(2).bp3); // ] |
67 | } | |
68 | ||
69 | 1 | @Test(groups = { "Functional" }) |
70 | public void testGetSimpleBPs_unmatchedOpener() | |
71 | { | |
72 | 1 | String rna = "(([{})]"; |
73 | 1 | try |
74 | { | |
75 | 1 | Rna.getSimpleBPs(rna); |
76 | 0 | fail("expected exception"); |
77 | } catch (WUSSParseException e) | |
78 | { | |
79 | // error reported as after end of input string | |
80 | 1 | assertEquals(rna.length(), e.getProblemPos()); |
81 | } | |
82 | } | |
83 | ||
84 | 1 | @Test(groups = { "Functional" }) |
85 | public void testGetSimpleBPs_unmatchedCloser() | |
86 | { | |
87 | 1 | String rna = "([{})]]]"; |
88 | 1 | try |
89 | { | |
90 | 1 | Rna.getSimpleBPs(rna); |
91 | 0 | fail("expected exception"); |
92 | } catch (WUSSParseException e) | |
93 | { | |
94 | // error reported as at first unmatched close | |
95 | 1 | assertEquals(6, e.getProblemPos()); |
96 | } | |
97 | ||
98 | /* | |
99 | * a variant where we have no opening bracket of the same type | |
100 | * as the unmatched closing bracket (no stack rather than empty stack) | |
101 | */ | |
102 | 1 | rna = "((()])"; |
103 | 1 | try |
104 | { | |
105 | 1 | Rna.getSimpleBPs(rna); |
106 | 0 | fail("expected exception"); |
107 | } catch (WUSSParseException e) | |
108 | { | |
109 | 1 | assertEquals(4, e.getProblemPos()); |
110 | } | |
111 | } | |
112 | ||
113 | 1 | @Test(groups = { "Functional" }) |
114 | public void testGetRNASecStrucState() | |
115 | { | |
116 | 1 | assertNull(Rna.getRNASecStrucState(null)); |
117 | 257 | for (int i = 0; i <= 255; i++) |
118 | { | |
119 | 256 | String s = String.valueOf((char) i); |
120 | 256 | String ss = Rna.getRNASecStrucState(s); |
121 | ||
122 | /* | |
123 | * valid SS chars are a-z, A-Z, and various brackets; | |
124 | * anything else is returned as a space | |
125 | */ | |
126 | 256 | if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z') |
127 | || "()[]{}<>".indexOf(s) > -1) | |
128 | { | |
129 | 60 | assertEquals("" + i, s, ss); |
130 | } | |
131 | else | |
132 | { | |
133 | 196 | assertEquals(" ", ss); |
134 | } | |
135 | } | |
136 | ||
137 | /* | |
138 | * a string is processed character by character | |
139 | */ | |
140 | 1 | assertEquals("a [K ]z} {Q b(w)p><i", |
141 | Rna.getRNASecStrucState("a.[K-]z}?{Q b(w)p><i")); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Tests for isClosingParenthesis with char or String argument | |
146 | */ | |
147 | 1 | @Test(groups = { "Functional" }) |
148 | public void testIsClosingParenthesis() | |
149 | { | |
150 | 1 | assertFalse(Rna.isClosingParenthesis(null)); |
151 | ||
152 | /* | |
153 | * only a-z, )]}> are closing bracket symbols | |
154 | */ | |
155 | 257 | for (int i = 0; i <= 255; i++) |
156 | { | |
157 | 256 | boolean isClosingChar = Rna.isClosingParenthesis((char) i); |
158 | 256 | boolean isClosingString = Rna |
159 | .isClosingParenthesis(String.valueOf((char) i)); | |
160 | 256 | if ((i >= 'a' && i <= 'z') || i == ')' || i == '}' || i == ']' |
161 | || i == '>') | |
162 | { | |
163 | 30 | assertTrue(String.format("close base pair %c", i), isClosingChar); |
164 | 30 | assertTrue(String.format("close base pair %c", i), isClosingString); |
165 | } | |
166 | else | |
167 | { | |
168 | 226 | assertFalse(String.format("close base pair %c", i), isClosingChar); |
169 | 226 | assertFalse(String.format("close base pair %c", i), |
170 | isClosingString); | |
171 | } | |
172 | 256 | assertFalse(Rna.isClosingParenthesis(String.valueOf((char) i) + " ")); |
173 | } | |
174 | } | |
175 | ||
176 | 1 | @Test(groups = { "Functional" }) |
177 | public void testIsCanonicalOrWobblePair() | |
178 | { | |
179 | 1 | String bases = "acgtuACGTU"; |
180 | 11 | for (int i = 0; i < bases.length(); i++) |
181 | { | |
182 | 110 | for (int j = 0; j < bases.length(); j++) |
183 | { | |
184 | 100 | char first = bases.charAt(i); |
185 | 100 | char second = bases.charAt(j); |
186 | 100 | boolean result = Rna.isCanonicalOrWobblePair(first, second); |
187 | 100 | String pair = new String(new char[] { first, second }) |
188 | .toUpperCase(Locale.ROOT); | |
189 | 100 | if (pair.equals("AT") || pair.equals("TA") || pair.equals("AU") |
190 | || pair.equals("UA") || pair.equals("GC") | |
191 | || pair.equals("CG") || pair.equals("GT") | |
192 | || pair.equals("TG") || pair.equals("GU") | |
193 | || pair.equals("UG")) | |
194 | { | |
195 | 40 | assertTrue(pair + " should be valid", result); |
196 | } | |
197 | else | |
198 | { | |
199 | 60 | assertFalse(pair + " should be invalid", result); |
200 | } | |
201 | } | |
202 | } | |
203 | } | |
204 | ||
205 | 1 | @Test(groups = { "Functional" }) |
206 | public void testIsCanonicalPair() | |
207 | { | |
208 | 1 | String bases = "acgtuACGTU"; |
209 | 11 | for (int i = 0; i < bases.length(); i++) |
210 | { | |
211 | 110 | for (int j = 0; j < bases.length(); j++) |
212 | { | |
213 | 100 | char first = bases.charAt(i); |
214 | 100 | char second = bases.charAt(j); |
215 | 100 | boolean result = Rna.isCanonicalPair(first, second); |
216 | 100 | String pair = new String(new char[] { first, second }) |
217 | .toUpperCase(Locale.ROOT); | |
218 | 100 | if (pair.equals("AT") || pair.equals("TA") || pair.equals("AU") |
219 | || pair.equals("UA") || pair.equals("GC") | |
220 | || pair.equals("CG")) | |
221 | { | |
222 | 24 | assertTrue(pair + " should be valid", result); |
223 | } | |
224 | else | |
225 | { | |
226 | 76 | assertFalse(pair + " should be invalid", result); |
227 | } | |
228 | } | |
229 | } | |
230 | } | |
231 | ||
232 | /** | |
233 | * Tests for isOpeningParenthesis with char or String argument | |
234 | */ | |
235 | 1 | @Test(groups = { "Functional" }) |
236 | public void testIsOpeningParenthesis() | |
237 | { | |
238 | /* | |
239 | * only A-Z, ([{< are opening bracket symbols | |
240 | */ | |
241 | 257 | for (int i = 0; i <= 255; i++) |
242 | { | |
243 | 256 | boolean isOpeningChar = Rna.isOpeningParenthesis((char) i); |
244 | 256 | boolean isOpeningString = Rna |
245 | .isOpeningParenthesis(String.valueOf((char) i)); | |
246 | 256 | if ((i >= 'A' && i <= 'Z') || i == '(' || i == '{' || i == '[' |
247 | || i == '<') | |
248 | { | |
249 | 30 | assertTrue(String.format("Open base pair %c", i), isOpeningChar); |
250 | 30 | assertTrue(String.format("Open base pair %c", i), isOpeningString); |
251 | } | |
252 | else | |
253 | { | |
254 | 226 | assertFalse(String.format("Open base pair %c", i), isOpeningChar); |
255 | 226 | assertFalse(String.format("Open base pair %c", i), isOpeningString); |
256 | } | |
257 | 256 | assertFalse(Rna.isOpeningParenthesis(String.valueOf((char) i) + " ")); |
258 | } | |
259 | } | |
260 | ||
261 | 1 | @Test(groups = { "Functional" }) |
262 | public void testGetMatchingOpeningParenthesis() throws WUSSParseException | |
263 | { | |
264 | 257 | for (int i = 0; i <= 255; i++) |
265 | { | |
266 | 256 | boolean isClosing = Rna.isClosingParenthesis((char) i); |
267 | 256 | if (isClosing) |
268 | { | |
269 | 30 | char opening = Rna.getMatchingOpeningParenthesis((char) i); |
270 | 30 | if (i >= 'a' && i <= 'z') |
271 | { | |
272 | 26 | assertEquals(i + 'A' - 'a', opening); |
273 | } | |
274 | 4 | else if (i == ')' && opening == '(' || i == ']' && opening == '[' |
275 | || i == '}' && opening == '{' || i == '>' && opening == '<') | |
276 | { | |
277 | // ok | |
278 | } | |
279 | else | |
280 | { | |
281 | 0 | fail("Got " + opening + " as opening bracket pair for " |
282 | + ((char) i)); | |
283 | } | |
284 | } | |
285 | } | |
286 | } | |
287 | ||
288 | /** | |
289 | * Tests for isRnaSecondaryStructureSymbol with char or String argument | |
290 | */ | |
291 | 1 | @Test(groups = { "Functional" }) |
292 | public void testIsRnaSecondaryStructureSymbol() | |
293 | { | |
294 | 1 | assertFalse(Rna.isRnaSecondaryStructureSymbol(null)); |
295 | ||
296 | /* | |
297 | * only A-Z, a-z, ()[]{}<> are valid symbols | |
298 | */ | |
299 | 257 | for (int i = 0; i <= 255; i++) |
300 | { | |
301 | 256 | boolean isValidChar = Rna.isRnaSecondaryStructureSymbol((char) i); |
302 | 256 | boolean isValidString = Rna |
303 | .isRnaSecondaryStructureSymbol(String.valueOf((char) i)); | |
304 | 256 | if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z') || i == '(' |
305 | || i == ')' || i == '{' || i == '}' || i == '[' || i == ']' | |
306 | || i == '<' || i == '>') | |
307 | { | |
308 | 60 | assertTrue(String.format("close base pair %c", i), isValidChar); |
309 | 60 | assertTrue(String.format("close base pair %c", i), isValidString); |
310 | } | |
311 | else | |
312 | { | |
313 | 196 | assertFalse(String.format("close base pair %c", i), isValidChar); |
314 | 196 | assertFalse(String.format("close base pair %c", i), isValidString); |
315 | } | |
316 | 256 | assertFalse(Rna.isRnaSecondaryStructureSymbol( |
317 | String.valueOf((char) i) + " ")); | |
318 | } | |
319 | } | |
320 | ||
321 | 1 | @Test(groups = "Functional") |
322 | public void testGetHelixMap_oneHelix() throws WUSSParseException | |
323 | { | |
324 | 1 | String rna = ".(..[{.<..>}..].)"; |
325 | 1 | SequenceFeature[] sfs = Rna.getHelixMap(rna); |
326 | 1 | assertEquals(4, sfs.length); |
327 | ||
328 | /* | |
329 | * pairs are added in the order in which the closing bracket is found | |
330 | * (see testGetSimpleBPs) | |
331 | */ | |
332 | 1 | assertEquals(7, sfs[0].getBegin()); |
333 | 1 | assertEquals(10, sfs[0].getEnd()); |
334 | 1 | assertEquals("0", sfs[0].getFeatureGroup()); |
335 | 1 | assertEquals(5, sfs[1].getBegin()); |
336 | 1 | assertEquals(11, sfs[1].getEnd()); |
337 | 1 | assertEquals("0", sfs[1].getFeatureGroup()); |
338 | 1 | assertEquals(4, sfs[2].getBegin()); |
339 | 1 | assertEquals(14, sfs[2].getEnd()); |
340 | 1 | assertEquals("0", sfs[2].getFeatureGroup()); |
341 | 1 | assertEquals(1, sfs[3].getBegin()); |
342 | 1 | assertEquals(16, sfs[3].getEnd()); |
343 | 1 | assertEquals("0", sfs[3].getFeatureGroup()); |
344 | } | |
345 | ||
346 | 1 | @Test(groups = "Functional") |
347 | public void testGetHelixMap_twoHelices() throws WUSSParseException | |
348 | { | |
349 | 1 | String rna = ".([.)]..{.<}.>"; |
350 | 1 | SequenceFeature[] sfs = Rna.getHelixMap(rna); |
351 | 1 | assertEquals(4, sfs.length); |
352 | ||
353 | /* | |
354 | * pairs are added in the order in which the closing bracket is found | |
355 | * (see testGetSimpleBPs) | |
356 | */ | |
357 | 1 | assertEquals(1, sfs[0].getBegin()); |
358 | 1 | assertEquals(4, sfs[0].getEnd()); |
359 | 1 | assertEquals("0", sfs[0].getFeatureGroup()); |
360 | 1 | assertEquals(2, sfs[1].getBegin()); |
361 | 1 | assertEquals(5, sfs[1].getEnd()); |
362 | 1 | assertEquals("0", sfs[1].getFeatureGroup()); |
363 | 1 | assertEquals(8, sfs[2].getBegin()); |
364 | 1 | assertEquals(11, sfs[2].getEnd()); |
365 | 1 | assertEquals("1", sfs[2].getFeatureGroup()); |
366 | 1 | assertEquals(10, sfs[3].getBegin()); |
367 | 1 | assertEquals(13, sfs[3].getEnd()); |
368 | 1 | assertEquals("1", sfs[3].getFeatureGroup()); |
369 | } | |
370 | } |