Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.analysis

File RnaTest.java

 

Code metrics

36
121
13
1
367
277
81
0.67
9.31
13
6.23

Classes

Class Line # Actions
RnaTest 38 121 81 5
0.970588297.1%
 

Contributing tests

This file is covered by 12 tests. .

Source view

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