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;
31 import java.util.Vector;
33 import org.testng.annotations.Test;
37 @Test(groups = { "Functional" })
38 public void testGetSimpleBPs() throws WUSSParseException
40 String rna = "([{})]"; // JAL-1081 example
41 Vector<SimpleBP> bps = Rna.getSimpleBPs(rna);
42 assertEquals(3, bps.size());
45 * the base pairs are added in the order in which the matching base is found
46 * (popping the stack of unmatched opening brackets)
48 assertEquals(2, bps.get(0).bp5); // {
49 assertEquals(3, bps.get(0).bp3); // }
50 assertEquals(0, bps.get(1).bp5); // (
51 assertEquals(4, bps.get(1).bp3); // )
52 assertEquals(1, bps.get(2).bp5); // [
53 assertEquals(5, bps.get(2).bp3); // ]
56 @Test(groups = { "Functional" })
57 public void testGetSimpleBPs_unmatchedOpener()
59 String rna = "(([{})]";
62 Rna.getSimpleBPs(rna);
63 fail("expected exception");
64 } catch (WUSSParseException e)
66 // error reported as after end of input string
67 assertEquals(rna.length(), e.getProblemPos());
71 @Test(groups = { "Functional" })
72 public void testGetSimpleBPs_unmatchedCloser()
74 String rna = "([{})]]]";
77 Rna.getSimpleBPs(rna);
78 fail("expected exception");
79 } catch (WUSSParseException e)
81 // error reported as at first unmatched close
82 assertEquals(6, e.getProblemPos());
86 * a variant where we have no opening bracket of the same type
87 * as the unmatched closing bracket (no stack rather than empty stack)
92 Rna.getSimpleBPs(rna);
93 fail("expected exception");
94 } catch (WUSSParseException e)
96 assertEquals(4, e.getProblemPos());
100 @Test(groups = { "Functional" })
101 public void testGetRNASecStrucState()
103 assertNull(Rna.getRNASecStrucState(null));
104 for (int i = 0; i <= 255; i++)
106 String s = String.valueOf((char) i);
107 String ss = Rna.getRNASecStrucState(s);
110 * valid SS chars are a-z, A-Z, and various brackets;
111 * anything else is returned as a space
113 if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z')
114 || "()[]{}<>".indexOf(s) > -1)
116 assertEquals("" + i, s, ss);
120 assertEquals(" ", ss);
125 * a string is processed character by character
127 assertEquals("a [K ]z} {Q b(w)p><i",
128 Rna.getRNASecStrucState("a.[K-]z}?{Q b(w)p><i"));
132 * Tests for isClosingParenthesis with char or String argument
134 @Test(groups = { "Functional" })
135 public void testIsClosingParenthesis()
137 assertFalse(Rna.isClosingParenthesis(null));
140 * only a-z, )]}> are closing bracket symbols
142 for (int i = 0; i <= 255; i++)
144 boolean isClosingChar = Rna.isClosingParenthesis((char) i);
145 boolean isClosingString = Rna.isClosingParenthesis(String
147 if ((i >= 'a' && i <= 'z') || i == ')' || i == '}' || i == ']'
150 assertTrue(String.format("close base pair %c", i), isClosingChar);
151 assertTrue(String.format("close base pair %c", i), isClosingString);
155 assertFalse(String.format("close base pair %c", i), isClosingChar);
156 assertFalse(String.format("close base pair %c", i), isClosingString);
158 assertFalse(Rna.isClosingParenthesis(String.valueOf((char) i) + " "));
162 @Test(groups = { "Functional" })
163 public void testIsCanonicalOrWobblePair()
165 String bases = "acgtuACGTU";
166 for (int i = 0; i < bases.length(); i++)
168 for (int j = 0; j < bases.length(); j++)
170 char first = bases.charAt(i);
171 char second = bases.charAt(j);
172 boolean result = Rna.isCanonicalOrWobblePair(first, second);
173 String pair = new String(new char[] { first, second })
175 if (pair.equals("AT") || pair.equals("TA") || pair.equals("AU")
176 || pair.equals("UA") || pair.equals("GC")
177 || pair.equals("CG") || pair.equals("GT")
178 || pair.equals("TG") || pair.equals("GU")
179 || pair.equals("UG"))
181 assertTrue(pair + " should be valid", result);
185 assertFalse(pair + " should be invalid", result);
191 @Test(groups = { "Functional" })
192 public void testIsCanonicalPair()
194 String bases = "acgtuACGTU";
195 for (int i = 0; i < bases.length(); i++)
197 for (int j = 0; j < bases.length(); j++)
199 char first = bases.charAt(i);
200 char second = bases.charAt(j);
201 boolean result = Rna.isCanonicalPair(first, second);
202 String pair = new String(new char[] { first, second })
204 if (pair.equals("AT") || pair.equals("TA") || pair.equals("AU")
205 || pair.equals("UA") || pair.equals("GC")
206 || pair.equals("CG"))
208 assertTrue(pair + " should be valid", result);
212 assertFalse(pair + " should be invalid", result);
219 * Tests for isOpeningParenthesis with char or String argument
221 @Test(groups = { "Functional" })
222 public void testIsOpeningParenthesis()
225 * only A-Z, ([{< are opening bracket symbols
227 for (int i = 0; i <= 255; i++)
229 boolean isOpeningChar = Rna.isOpeningParenthesis((char) i);
230 boolean isOpeningString = Rna.isOpeningParenthesis(String
232 if ((i >= 'A' && i <= 'Z') || i == '(' || i == '{' || i == '['
235 assertTrue(String.format("Open base pair %c", i), isOpeningChar);
236 assertTrue(String.format("Open base pair %c", i), isOpeningString);
240 assertFalse(String.format("Open base pair %c", i), isOpeningChar);
241 assertFalse(String.format("Open base pair %c", i), isOpeningString);
243 assertFalse(Rna.isOpeningParenthesis(String.valueOf((char) i) + " "));
247 @Test(groups = { "Functional" })
248 public void testGetMatchingOpeningParenthesis() throws WUSSParseException
250 for (int i = 0; i <= 255; i++)
252 boolean isClosing = Rna.isClosingParenthesis((char) i);
255 char opening = Rna.getMatchingOpeningParenthesis((char) i);
256 if (i >= 'a' && i <= 'z')
258 assertEquals(i + 'A' - 'a', opening);
260 else if (i == ')' && opening == '(' || i == ']' && opening == '['
261 || i == '}' && opening == '{' || i == '>' && opening == '<')
267 fail("Got " + opening + " as opening bracket pair for "
275 * Tests for isRnaSecondaryStructureSymbol with char or String argument
277 @Test(groups = { "Functional" })
278 public void testIsRnaSecondaryStructureSymbol()
280 assertFalse(Rna.isRnaSecondaryStructureSymbol(null));
283 * only A-Z, a-z, ()[]{}<> are valid symbols
285 for (int i = 0; i <= 255; i++)
287 boolean isValidChar = Rna.isRnaSecondaryStructureSymbol((char) i);
288 boolean isValidString = Rna.isRnaSecondaryStructureSymbol(String
290 if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z') || i == '('
291 || i == ')' || i == '{' || i == '}' || i == '[' || i == ']'
292 || i == '<' || i == '>')
294 assertTrue(String.format("close base pair %c", i), isValidChar);
295 assertTrue(String.format("close base pair %c", i), isValidString);
299 assertFalse(String.format("close base pair %c", i), isValidChar);
300 assertFalse(String.format("close base pair %c", i), isValidString);
302 assertFalse(Rna.isRnaSecondaryStructureSymbol(String
303 .valueOf((char) i) + " "));