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.analysis;
23 import java.util.Locale;
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;
31 import jalview.analysis.SecStrConsensus.SimpleBP;
32 import jalview.datamodel.SequenceFeature;
33 import jalview.gui.JvOptionPane;
35 import java.util.List;
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.Test;
43 @BeforeClass(alwaysRun = true)
44 public void setUpJvOptionPane()
46 JvOptionPane.setInteractiveMode(false);
47 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
50 @Test(groups = { "Functional" })
51 public void testGetSimpleBPs() throws WUSSParseException
53 String rna = "([{})]"; // JAL-1081 example
54 List<SimpleBP> bps = Rna.getSimpleBPs(rna);
55 assertEquals(3, bps.size());
58 * the base pairs are added in the order in which the matching base is found
59 * (popping the stack of unmatched opening brackets)
61 assertEquals(2, bps.get(0).bp5); // {
62 assertEquals(3, bps.get(0).bp3); // }
63 assertEquals(0, bps.get(1).bp5); // (
64 assertEquals(4, bps.get(1).bp3); // )
65 assertEquals(1, bps.get(2).bp5); // [
66 assertEquals(5, bps.get(2).bp3); // ]
69 @Test(groups = { "Functional" })
70 public void testGetSimpleBPs_unmatchedOpener()
72 String rna = "(([{})]";
75 Rna.getSimpleBPs(rna);
76 fail("expected exception");
77 } catch (WUSSParseException e)
79 // error reported as after end of input string
80 assertEquals(rna.length(), e.getProblemPos());
84 @Test(groups = { "Functional" })
85 public void testGetSimpleBPs_unmatchedCloser()
87 String rna = "([{})]]]";
90 Rna.getSimpleBPs(rna);
91 fail("expected exception");
92 } catch (WUSSParseException e)
94 // error reported as at first unmatched close
95 assertEquals(6, e.getProblemPos());
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)
105 Rna.getSimpleBPs(rna);
106 fail("expected exception");
107 } catch (WUSSParseException e)
109 assertEquals(4, e.getProblemPos());
113 @Test(groups = { "Functional" })
114 public void testGetRNASecStrucState()
116 assertNull(Rna.getRNASecStrucState(null));
117 for (int i = 0; i <= 255; i++)
119 String s = String.valueOf((char) i);
120 String ss = Rna.getRNASecStrucState(s);
123 * valid SS chars are a-z, A-Z, and various brackets;
124 * anything else is returned as a space
126 if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z')
127 || "()[]{}<>".indexOf(s) > -1)
129 assertEquals("" + i, s, ss);
133 assertEquals(" ", ss);
138 * a string is processed character by character
140 assertEquals("a [K ]z} {Q b(w)p><i",
141 Rna.getRNASecStrucState("a.[K-]z}?{Q b(w)p><i"));
145 * Tests for isClosingParenthesis with char or String argument
147 @Test(groups = { "Functional" })
148 public void testIsClosingParenthesis()
150 assertFalse(Rna.isClosingParenthesis(null));
153 * only a-z, )]}> are closing bracket symbols
155 for (int i = 0; i <= 255; i++)
157 boolean isClosingChar = Rna.isClosingParenthesis((char) i);
158 boolean isClosingString = Rna
159 .isClosingParenthesis(String.valueOf((char) i));
160 if ((i >= 'a' && i <= 'z') || i == ')' || i == '}' || i == ']'
163 assertTrue(String.format("close base pair %c", i), isClosingChar);
164 assertTrue(String.format("close base pair %c", i), isClosingString);
168 assertFalse(String.format("close base pair %c", i), isClosingChar);
169 assertFalse(String.format("close base pair %c", i),
172 assertFalse(Rna.isClosingParenthesis(String.valueOf((char) i) + " "));
176 @Test(groups = { "Functional" })
177 public void testIsCanonicalOrWobblePair()
179 String bases = "acgtuACGTU";
180 for (int i = 0; i < bases.length(); i++)
182 for (int j = 0; j < bases.length(); j++)
184 char first = bases.charAt(i);
185 char second = bases.charAt(j);
186 boolean result = Rna.isCanonicalOrWobblePair(first, second);
187 String pair = new String(new char[] { first, second })
188 .toUpperCase(Locale.ROOT);
189 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"))
195 assertTrue(pair + " should be valid", result);
199 assertFalse(pair + " should be invalid", result);
205 @Test(groups = { "Functional" })
206 public void testIsCanonicalPair()
208 String bases = "acgtuACGTU";
209 for (int i = 0; i < bases.length(); i++)
211 for (int j = 0; j < bases.length(); j++)
213 char first = bases.charAt(i);
214 char second = bases.charAt(j);
215 boolean result = Rna.isCanonicalPair(first, second);
216 String pair = new String(new char[] { first, second })
217 .toUpperCase(Locale.ROOT);
218 if (pair.equals("AT") || pair.equals("TA") || pair.equals("AU")
219 || pair.equals("UA") || pair.equals("GC")
220 || pair.equals("CG"))
222 assertTrue(pair + " should be valid", result);
226 assertFalse(pair + " should be invalid", result);
233 * Tests for isOpeningParenthesis with char or String argument
235 @Test(groups = { "Functional" })
236 public void testIsOpeningParenthesis()
239 * only A-Z, ([{< are opening bracket symbols
241 for (int i = 0; i <= 255; i++)
243 boolean isOpeningChar = Rna.isOpeningParenthesis((char) i);
244 boolean isOpeningString = Rna
245 .isOpeningParenthesis(String.valueOf((char) i));
246 if ((i >= 'A' && i <= 'Z') || i == '(' || i == '{' || i == '['
249 assertTrue(String.format("Open base pair %c", i), isOpeningChar);
250 assertTrue(String.format("Open base pair %c", i), isOpeningString);
254 assertFalse(String.format("Open base pair %c", i), isOpeningChar);
255 assertFalse(String.format("Open base pair %c", i), isOpeningString);
257 assertFalse(Rna.isOpeningParenthesis(String.valueOf((char) i) + " "));
261 @Test(groups = { "Functional" })
262 public void testGetMatchingOpeningParenthesis() throws WUSSParseException
264 for (int i = 0; i <= 255; i++)
266 boolean isClosing = Rna.isClosingParenthesis((char) i);
269 char opening = Rna.getMatchingOpeningParenthesis((char) i);
270 if (i >= 'a' && i <= 'z')
272 assertEquals(i + 'A' - 'a', opening);
274 else if (i == ')' && opening == '(' || i == ']' && opening == '['
275 || i == '}' && opening == '{' || i == '>' && opening == '<')
281 fail("Got " + opening + " as opening bracket pair for "
289 * Tests for isRnaSecondaryStructureSymbol with char or String argument
291 @Test(groups = { "Functional" })
292 public void testIsRnaSecondaryStructureSymbol()
294 assertFalse(Rna.isRnaSecondaryStructureSymbol(null));
297 * only A-Z, a-z, ()[]{}<> are valid symbols
299 for (int i = 0; i <= 255; i++)
301 boolean isValidChar = Rna.isRnaSecondaryStructureSymbol((char) i);
302 boolean isValidString = Rna
303 .isRnaSecondaryStructureSymbol(String.valueOf((char) i));
304 if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z') || i == '('
305 || i == ')' || i == '{' || i == '}' || i == '[' || i == ']'
306 || i == '<' || i == '>')
308 assertTrue(String.format("close base pair %c", i), isValidChar);
309 assertTrue(String.format("close base pair %c", i), isValidString);
313 assertFalse(String.format("close base pair %c", i), isValidChar);
314 assertFalse(String.format("close base pair %c", i), isValidString);
316 assertFalse(Rna.isRnaSecondaryStructureSymbol(
317 String.valueOf((char) i) + " "));
321 @Test(groups = "Functional")
322 public void testGetHelixMap_oneHelix() throws WUSSParseException
324 String rna = ".(..[{.<..>}..].)";
325 SequenceFeature[] sfs = Rna.getHelixMap(rna);
326 assertEquals(4, sfs.length);
329 * pairs are added in the order in which the closing bracket is found
330 * (see testGetSimpleBPs)
332 assertEquals(7, sfs[0].getBegin());
333 assertEquals(10, sfs[0].getEnd());
334 assertEquals("0", sfs[0].getFeatureGroup());
335 assertEquals(5, sfs[1].getBegin());
336 assertEquals(11, sfs[1].getEnd());
337 assertEquals("0", sfs[1].getFeatureGroup());
338 assertEquals(4, sfs[2].getBegin());
339 assertEquals(14, sfs[2].getEnd());
340 assertEquals("0", sfs[2].getFeatureGroup());
341 assertEquals(1, sfs[3].getBegin());
342 assertEquals(16, sfs[3].getEnd());
343 assertEquals("0", sfs[3].getFeatureGroup());
346 @Test(groups = "Functional")
347 public void testGetHelixMap_twoHelices() throws WUSSParseException
349 String rna = ".([.)]..{.<}.>";
350 SequenceFeature[] sfs = Rna.getHelixMap(rna);
351 assertEquals(4, sfs.length);
354 * pairs are added in the order in which the closing bracket is found
355 * (see testGetSimpleBPs)
357 assertEquals(1, sfs[0].getBegin());
358 assertEquals(4, sfs[0].getEnd());
359 assertEquals("0", sfs[0].getFeatureGroup());
360 assertEquals(2, sfs[1].getBegin());
361 assertEquals(5, sfs[1].getEnd());
362 assertEquals("0", sfs[1].getFeatureGroup());
363 assertEquals(8, sfs[2].getBegin());
364 assertEquals(11, sfs[2].getEnd());
365 assertEquals("1", sfs[2].getFeatureGroup());
366 assertEquals(10, sfs[3].getBegin());
367 assertEquals(13, sfs[3].getEnd());
368 assertEquals("1", sfs[3].getFeatureGroup());