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 static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNull;
26 import static org.testng.AssertJUnit.assertTrue;
27 import static org.testng.AssertJUnit.fail;
29 import jalview.analysis.SecStrConsensus.SimpleBP;
30 import jalview.datamodel.SequenceFeature;
31 import jalview.gui.JvOptionPane;
33 import java.util.List;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.Test;
41 @BeforeClass(alwaysRun = true)
42 public void setUpJvOptionPane()
44 JvOptionPane.setInteractiveMode(false);
45 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
48 @Test(groups = { "Functional" })
49 public void testGetSimpleBPs() throws WUSSParseException
51 String rna = "([{})]"; // JAL-1081 example
52 List<SimpleBP> bps = Rna.getSimpleBPs(rna);
53 assertEquals(3, bps.size());
56 * the base pairs are added in the order in which the matching base is found
57 * (popping the stack of unmatched opening brackets)
59 assertEquals(2, bps.get(0).bp5); // {
60 assertEquals(3, bps.get(0).bp3); // }
61 assertEquals(0, bps.get(1).bp5); // (
62 assertEquals(4, bps.get(1).bp3); // )
63 assertEquals(1, bps.get(2).bp5); // [
64 assertEquals(5, bps.get(2).bp3); // ]
67 @Test(groups = { "Functional" })
68 public void testGetSimpleBPs_unmatchedOpener()
70 String rna = "(([{})]";
73 Rna.getSimpleBPs(rna);
74 fail("expected exception");
75 } catch (WUSSParseException e)
77 // error reported as after end of input string
78 assertEquals(rna.length(), e.getProblemPos());
82 @Test(groups = { "Functional" })
83 public void testGetSimpleBPs_unmatchedCloser()
85 String rna = "([{})]]]";
88 Rna.getSimpleBPs(rna);
89 fail("expected exception");
90 } catch (WUSSParseException e)
92 // error reported as at first unmatched close
93 assertEquals(6, e.getProblemPos());
97 * a variant where we have no opening bracket of the same type
98 * as the unmatched closing bracket (no stack rather than empty stack)
103 Rna.getSimpleBPs(rna);
104 fail("expected exception");
105 } catch (WUSSParseException e)
107 assertEquals(4, e.getProblemPos());
111 @Test(groups = { "Functional" })
112 public void testGetRNASecStrucState()
114 assertNull(Rna.getRNASecStrucState(null));
115 for (int i = 0; i <= 255; i++)
117 String s = String.valueOf((char) i);
118 String ss = Rna.getRNASecStrucState(s);
121 * valid SS chars are a-z, A-Z, and various brackets;
122 * anything else is returned as a space
124 if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z')
125 || "()[]{}<>".indexOf(s) > -1)
127 assertEquals("" + i, s, ss);
131 assertEquals(" ", ss);
136 * a string is processed character by character
138 assertEquals("a [K ]z} {Q b(w)p><i",
139 Rna.getRNASecStrucState("a.[K-]z}?{Q b(w)p><i"));
143 * Tests for isClosingParenthesis with char or String argument
145 @Test(groups = { "Functional" })
146 public void testIsClosingParenthesis()
148 assertFalse(Rna.isClosingParenthesis(null));
151 * only a-z, )]}> are closing bracket symbols
153 for (int i = 0; i <= 255; i++)
155 boolean isClosingChar = Rna.isClosingParenthesis((char) i);
156 boolean isClosingString = Rna.isClosingParenthesis(String
158 if ((i >= 'a' && i <= 'z') || i == ')' || i == '}' || i == ']'
161 assertTrue(String.format("close base pair %c", i), isClosingChar);
162 assertTrue(String.format("close base pair %c", i), isClosingString);
166 assertFalse(String.format("close base pair %c", i), isClosingChar);
167 assertFalse(String.format("close base pair %c", i), isClosingString);
169 assertFalse(Rna.isClosingParenthesis(String.valueOf((char) i) + " "));
173 @Test(groups = { "Functional" })
174 public void testIsCanonicalOrWobblePair()
176 String bases = "acgtuACGTU";
177 for (int i = 0; i < bases.length(); i++)
179 for (int j = 0; j < bases.length(); j++)
181 char first = bases.charAt(i);
182 char second = bases.charAt(j);
183 boolean result = Rna.isCanonicalOrWobblePair(first, second);
184 String pair = new String(new char[] { first, second })
186 if (pair.equals("AT") || pair.equals("TA") || pair.equals("AU")
187 || pair.equals("UA") || pair.equals("GC")
188 || pair.equals("CG") || pair.equals("GT")
189 || pair.equals("TG") || pair.equals("GU")
190 || pair.equals("UG"))
192 assertTrue(pair + " should be valid", result);
196 assertFalse(pair + " should be invalid", result);
202 @Test(groups = { "Functional" })
203 public void testIsCanonicalPair()
205 String bases = "acgtuACGTU";
206 for (int i = 0; i < bases.length(); i++)
208 for (int j = 0; j < bases.length(); j++)
210 char first = bases.charAt(i);
211 char second = bases.charAt(j);
212 boolean result = Rna.isCanonicalPair(first, second);
213 String pair = new String(new char[] { first, second })
215 if (pair.equals("AT") || pair.equals("TA") || pair.equals("AU")
216 || pair.equals("UA") || pair.equals("GC")
217 || pair.equals("CG"))
219 assertTrue(pair + " should be valid", result);
223 assertFalse(pair + " should be invalid", result);
230 * Tests for isOpeningParenthesis with char or String argument
232 @Test(groups = { "Functional" })
233 public void testIsOpeningParenthesis()
236 * only A-Z, ([{< are opening bracket symbols
238 for (int i = 0; i <= 255; i++)
240 boolean isOpeningChar = Rna.isOpeningParenthesis((char) i);
241 boolean isOpeningString = Rna.isOpeningParenthesis(String
243 if ((i >= 'A' && i <= 'Z') || i == '(' || i == '{' || i == '['
246 assertTrue(String.format("Open base pair %c", i), isOpeningChar);
247 assertTrue(String.format("Open base pair %c", i), isOpeningString);
251 assertFalse(String.format("Open base pair %c", i), isOpeningChar);
252 assertFalse(String.format("Open base pair %c", i), isOpeningString);
254 assertFalse(Rna.isOpeningParenthesis(String.valueOf((char) i) + " "));
258 @Test(groups = { "Functional" })
259 public void testGetMatchingOpeningParenthesis() throws WUSSParseException
261 for (int i = 0; i <= 255; i++)
263 boolean isClosing = Rna.isClosingParenthesis((char) i);
266 char opening = Rna.getMatchingOpeningParenthesis((char) i);
267 if (i >= 'a' && i <= 'z')
269 assertEquals(i + 'A' - 'a', opening);
271 else if (i == ')' && opening == '(' || i == ']' && opening == '['
272 || i == '}' && opening == '{' || i == '>' && opening == '<')
278 fail("Got " + opening + " as opening bracket pair for "
286 * Tests for isRnaSecondaryStructureSymbol with char or String argument
288 @Test(groups = { "Functional" })
289 public void testIsRnaSecondaryStructureSymbol()
291 assertFalse(Rna.isRnaSecondaryStructureSymbol(null));
294 * only A-Z, a-z, ()[]{}<> are valid symbols
296 for (int i = 0; i <= 255; i++)
298 boolean isValidChar = Rna.isRnaSecondaryStructureSymbol((char) i);
299 boolean isValidString = Rna.isRnaSecondaryStructureSymbol(String
301 if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z') || i == '('
302 || i == ')' || i == '{' || i == '}' || i == '[' || i == ']'
303 || i == '<' || i == '>')
305 assertTrue(String.format("close base pair %c", i), isValidChar);
306 assertTrue(String.format("close base pair %c", i), isValidString);
310 assertFalse(String.format("close base pair %c", i), isValidChar);
311 assertFalse(String.format("close base pair %c", i), isValidString);
313 assertFalse(Rna.isRnaSecondaryStructureSymbol(String
314 .valueOf((char) i) + " "));
318 @Test(groups = "Functional")
319 public void testGetHelixMap_oneHelix() throws WUSSParseException
321 String rna = ".(..[{.<..>}..].)";
322 SequenceFeature[] sfs = Rna.getHelixMap(rna);
323 assertEquals(4, sfs.length);
326 * pairs are added in the order in which the closing bracket is found
327 * (see testGetSimpleBPs)
329 assertEquals(7, sfs[0].getBegin());
330 assertEquals(10, sfs[0].getEnd());
331 assertEquals("0", sfs[0].getFeatureGroup());
332 assertEquals(5, sfs[1].getBegin());
333 assertEquals(11, sfs[1].getEnd());
334 assertEquals("0", sfs[1].getFeatureGroup());
335 assertEquals(4, sfs[2].getBegin());
336 assertEquals(14, sfs[2].getEnd());
337 assertEquals("0", sfs[2].getFeatureGroup());
338 assertEquals(1, sfs[3].getBegin());
339 assertEquals(16, sfs[3].getEnd());
340 assertEquals("0", sfs[3].getFeatureGroup());
343 @Test(groups = "Functional")
344 public void testGetHelixMap_twoHelices() throws WUSSParseException
346 String rna = ".([.)]..{.<}.>";
347 SequenceFeature[] sfs = Rna.getHelixMap(rna);
348 assertEquals(4, sfs.length);
351 * pairs are added in the order in which the closing bracket is found
352 * (see testGetSimpleBPs)
354 assertEquals(1, sfs[0].getBegin());
355 assertEquals(4, sfs[0].getEnd());
356 assertEquals("0", sfs[0].getFeatureGroup());
357 assertEquals(2, sfs[1].getBegin());
358 assertEquals(5, sfs[1].getEnd());
359 assertEquals("0", sfs[1].getFeatureGroup());
360 assertEquals(8, sfs[2].getBegin());
361 assertEquals(11, sfs[2].getEnd());
362 assertEquals("1", sfs[2].getFeatureGroup());
363 assertEquals(10, sfs[3].getBegin());
364 assertEquals(13, sfs[3].getEnd());
365 assertEquals("1", sfs[3].getFeatureGroup());