JAL-2446 merged to spike branch
[jalview.git] / test / jalview / analysis / RnaTest.java
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   @BeforeClass(alwaysRun = true)
42   public void setUpJvOptionPane()
43   {
44     JvOptionPane.setInteractiveMode(false);
45     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
46   }
47
48   @Test(groups = { "Functional" })
49   public void testGetSimpleBPs() throws WUSSParseException
50   {
51     String rna = "([{})]"; // JAL-1081 example
52     List<SimpleBP> bps = Rna.getSimpleBPs(rna);
53     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     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); // ]
65   }
66
67   @Test(groups = { "Functional" })
68   public void testGetSimpleBPs_unmatchedOpener()
69   {
70     String rna = "(([{})]";
71     try
72     {
73       Rna.getSimpleBPs(rna);
74       fail("expected exception");
75     } catch (WUSSParseException e)
76     {
77       // error reported as after end of input string
78       assertEquals(rna.length(), e.getProblemPos());
79     }
80   }
81
82   @Test(groups = { "Functional" })
83   public void testGetSimpleBPs_unmatchedCloser()
84   {
85     String rna = "([{})]]]";
86     try
87     {
88       Rna.getSimpleBPs(rna);
89       fail("expected exception");
90     } catch (WUSSParseException e)
91     {
92       // error reported as at first unmatched close
93       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     rna = "((()])";
101     try
102     {
103       Rna.getSimpleBPs(rna);
104       fail("expected exception");
105     } catch (WUSSParseException e)
106     {
107       assertEquals(4, e.getProblemPos());
108     }
109   }
110
111   @Test(groups = { "Functional" })
112   public void testGetRNASecStrucState()
113   {
114     assertNull(Rna.getRNASecStrucState(null));
115     for (int i = 0; i <= 255; i++)
116     {
117       String s = String.valueOf((char) i);
118       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       if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z')
125               || "()[]{}<>".indexOf(s) > -1)
126       {
127         assertEquals("" + i, s, ss);
128       }
129       else
130       {
131         assertEquals(" ", ss);
132       }
133     }
134
135     /*
136      * a string is processed character by character
137      */
138     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   @Test(groups = { "Functional" })
146   public void testIsClosingParenthesis()
147   {
148     assertFalse(Rna.isClosingParenthesis(null));
149
150     /*
151      * only a-z, )]}> are closing bracket symbols
152      */
153     for (int i = 0; i <= 255; i++)
154     {
155       boolean isClosingChar = Rna.isClosingParenthesis((char) i);
156       boolean isClosingString = Rna.isClosingParenthesis(String
157               .valueOf((char) i));
158       if ((i >= 'a' && i <= 'z') || i == ')' || i == '}' || i == ']'
159               || i == '>')
160       {
161         assertTrue(String.format("close base pair %c", i), isClosingChar);
162         assertTrue(String.format("close base pair %c", i), isClosingString);
163       }
164       else
165       {
166         assertFalse(String.format("close base pair %c", i), isClosingChar);
167         assertFalse(String.format("close base pair %c", i), isClosingString);
168       }
169       assertFalse(Rna.isClosingParenthesis(String.valueOf((char) i) + " "));
170     }
171   }
172
173   @Test(groups = { "Functional" })
174   public void testIsCanonicalOrWobblePair()
175   {
176     String bases = "acgtuACGTU";
177     for (int i = 0; i < bases.length(); i++)
178     {
179       for (int j = 0; j < bases.length(); j++)
180       {
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 })
185                 .toUpperCase();
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"))
191         {
192           assertTrue(pair + " should be valid", result);
193         }
194         else
195         {
196           assertFalse(pair + " should be invalid", result);
197         }
198       }
199     }
200   }
201
202   @Test(groups = { "Functional" })
203   public void testIsCanonicalPair()
204   {
205     String bases = "acgtuACGTU";
206     for (int i = 0; i < bases.length(); i++)
207     {
208       for (int j = 0; j < bases.length(); j++)
209       {
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 })
214                 .toUpperCase();
215         if (pair.equals("AT") || pair.equals("TA") || pair.equals("AU")
216                 || pair.equals("UA") || pair.equals("GC")
217                 || pair.equals("CG"))
218         {
219           assertTrue(pair + " should be valid", result);
220         }
221         else
222         {
223           assertFalse(pair + " should be invalid", result);
224         }
225       }
226     }
227   }
228
229   /**
230    * Tests for isOpeningParenthesis with char or String argument
231    */
232   @Test(groups = { "Functional" })
233   public void testIsOpeningParenthesis()
234   {
235     /*
236      * only A-Z, ([{< are opening bracket symbols
237      */
238     for (int i = 0; i <= 255; i++)
239     {
240       boolean isOpeningChar = Rna.isOpeningParenthesis((char) i);
241       boolean isOpeningString = Rna.isOpeningParenthesis(String
242               .valueOf((char) i));
243       if ((i >= 'A' && i <= 'Z') || i == '(' || i == '{' || i == '['
244               || i == '<')
245       {
246         assertTrue(String.format("Open base pair %c", i), isOpeningChar);
247         assertTrue(String.format("Open base pair %c", i), isOpeningString);
248       }
249       else
250       {
251         assertFalse(String.format("Open base pair %c", i), isOpeningChar);
252         assertFalse(String.format("Open base pair %c", i), isOpeningString);
253       }
254       assertFalse(Rna.isOpeningParenthesis(String.valueOf((char) i) + " "));
255     }
256   }
257
258   @Test(groups = { "Functional" })
259   public void testGetMatchingOpeningParenthesis() throws WUSSParseException
260   {
261     for (int i = 0; i <= 255; i++)
262     {
263       boolean isClosing = Rna.isClosingParenthesis((char) i);
264       if (isClosing)
265       {
266         char opening = Rna.getMatchingOpeningParenthesis((char) i);
267         if (i >= 'a' && i <= 'z')
268         {
269           assertEquals(i + 'A' - 'a', opening);
270         }
271         else if (i == ')' && opening == '(' || i == ']' && opening == '['
272                 || i == '}' && opening == '{' || i == '>' && opening == '<')
273         {
274           // ok
275         }
276         else
277         {
278           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   @Test(groups = { "Functional" })
289   public void testIsRnaSecondaryStructureSymbol()
290   {
291     assertFalse(Rna.isRnaSecondaryStructureSymbol(null));
292
293     /*
294      * only A-Z,  a-z, ()[]{}<> are valid symbols
295      */
296     for (int i = 0; i <= 255; i++)
297     {
298       boolean isValidChar = Rna.isRnaSecondaryStructureSymbol((char) i);
299       boolean isValidString = Rna.isRnaSecondaryStructureSymbol(String
300               .valueOf((char) i));
301       if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z') || i == '('
302               || i == ')' || i == '{' || i == '}' || i == '[' || i == ']'
303               || i == '<' || i == '>')
304       {
305         assertTrue(String.format("close base pair %c", i), isValidChar);
306         assertTrue(String.format("close base pair %c", i), isValidString);
307       }
308       else
309       {
310         assertFalse(String.format("close base pair %c", i), isValidChar);
311         assertFalse(String.format("close base pair %c", i), isValidString);
312       }
313       assertFalse(Rna.isRnaSecondaryStructureSymbol(String
314               .valueOf((char) i) + " "));
315     }
316   }
317
318   @Test(groups = "Functional")
319   public void testGetHelixMap_oneHelix() throws WUSSParseException
320   {
321     String rna = ".(..[{.<..>}..].)";
322     SequenceFeature[] sfs = Rna.getHelixMap(rna);
323     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     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());
341   }
342
343   @Test(groups = "Functional")
344   public void testGetHelixMap_twoHelices() throws WUSSParseException
345   {
346     String rna = ".([.)]..{.<}.>";
347     SequenceFeature[] sfs = Rna.getHelixMap(rna);
348     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     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());
366   }
367 }