JAL-2171 matching bracket default character for rna SS entry
[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
31 import java.util.Vector;
32
33 import org.testng.annotations.Test;
34
35 public class RnaTest
36 {
37   @Test(groups = { "Functional" })
38   public void testGetSimpleBPs() throws WUSSParseException
39   {
40     String rna = "([{})]"; // JAL-1081 example
41     Vector<SimpleBP> bps = Rna.getSimpleBPs(rna);
42     assertEquals(3, bps.size());
43
44     /*
45      * the base pairs are added in the order in which the matching base is found
46      * (popping the stack of unmatched opening brackets)
47      */
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); // ]
54   }
55
56   @Test(groups = { "Functional" })
57   public void testGetSimpleBPs_unmatchedOpener()
58   {
59     String rna = "(([{})]";
60     try
61     {
62       Rna.getSimpleBPs(rna);
63       fail("expected exception");
64     } catch (WUSSParseException e)
65     {
66       // error reported as after end of input string
67       assertEquals(rna.length(), e.getProblemPos());
68     }
69   }
70
71   @Test(groups = { "Functional" })
72   public void testGetSimpleBPs_unmatchedCloser()
73   {
74     String rna = "([{})]]]";
75     try
76     {
77       Rna.getSimpleBPs(rna);
78       fail("expected exception");
79     } catch (WUSSParseException e)
80     {
81       // error reported as at first unmatched close
82       assertEquals(6, e.getProblemPos());
83     }
84
85     /*
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)
88      */
89     rna = "((()])";
90     try
91     {
92       Rna.getSimpleBPs(rna);
93       fail("expected exception");
94     } catch (WUSSParseException e)
95     {
96       assertEquals(4, e.getProblemPos());
97     }
98   }
99
100   @Test(groups = { "Functional" })
101   public void testGetRNASecStrucState()
102   {
103     assertNull(Rna.getRNASecStrucState(null));
104     for (int i = 0; i <= 255; i++)
105     {
106       String s = String.valueOf((char) i);
107       String ss = Rna.getRNASecStrucState(s);
108   
109       /*
110        * valid SS chars are a-z, A-Z, and various brackets;
111        * anything else is returned as a space
112        */
113       if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z')
114               || "()[]{}<>".indexOf(s) > -1)
115       {
116         assertEquals("" + i, s, ss);
117       }
118       else
119       {
120         assertEquals(" ", ss);
121       }
122     }
123   
124     /*
125      * a string is processed character by character
126      */
127     assertEquals("a [K ]z} {Q b(w)p><i",
128             Rna.getRNASecStrucState("a.[K-]z}?{Q b(w)p><i"));
129   }
130
131   @Test(groups = { "Functional" })
132   public void testIsClosingParenthesis()
133   {
134     /*
135      * only a-z, )]}> are closing bracket symbols
136      */
137     for (int i = 0; i <= 255; i++)
138     {
139       boolean isClosing = Rna.isClosingParenthesis((char) i);
140       if ((i >= 'a' && i <= 'z') || i == ')' || i == '}' || i == ']'
141               || i == '>')
142       {
143         assertTrue(String.format("close base pair %c", i), isClosing);
144       }
145       else
146       {
147         assertFalse(String.format("close base pair %c", i), isClosing);
148       }
149     }
150   }
151
152   @Test(groups = { "Functional" })
153   public void testIsCanonicalOrWobblePair()
154   {
155     String bases = "acgtuACGTU";
156     for (int i = 0; i < bases.length(); i++)
157     {
158       for (int j = 0; j < bases.length(); j++)
159       {
160         char first = bases.charAt(i);
161         char second = bases.charAt(j);
162         boolean result = Rna.isCanonicalOrWobblePair(first, second);
163         String pair = new String(new char[] { first, second })
164                 .toUpperCase();
165         if (pair.equals("AT") || pair.equals("TA") || pair.equals("AU")
166                 || pair.equals("UA") || pair.equals("GC")
167                 || pair.equals("CG") || pair.equals("GT")
168                 || pair.equals("TG") || pair.equals("GU")
169                 || pair.equals("UG"))
170         {
171           assertTrue(pair + " should be valid", result);
172         }
173         else
174         {
175           assertFalse(pair + " should be invalid", result);
176         }
177       }
178     }
179   }
180
181   @Test(groups = { "Functional" })
182   public void testIsOpeningParenthesis()
183   {
184     /*
185      * only A-Z, ([{< are opening bracket symbols
186      */
187     for (int i = 0; i <= 255; i++)
188     {
189       boolean isOpening = Rna.isOpeningParenthesis((char) i);
190       if ((i >= 'A' && i <= 'Z') || i == '(' || i == '{' || i == '['
191               || i == '<')
192       {
193         assertTrue(String.format("Open base pair %c", i), isOpening);
194       }
195       else
196       {
197         assertFalse(String.format("Open base pair %c", i), isOpening);
198       }
199     }
200   }
201
202   @Test(groups = { "Functional" })
203   public void testGetMatchingOpeningParenthesis() throws WUSSParseException
204   {
205     for (int i = 0; i <= 255; i++)
206     {
207       boolean isClosing = Rna.isClosingParenthesis((char) i);
208       if (isClosing)
209       {
210         char opening = Rna.getMatchingOpeningParenthesis((char) i);
211         if (i >= 'a' && i <= 'z')
212         {
213           assertEquals(i + 'A' - 'a', opening);
214         }
215         else if (i == ')' && opening == '(' || i == ']' && opening == '['
216                 || i == '}' && opening == '{' || i == '>' && opening == '<')
217         {
218           // ok
219         }
220         else
221         {
222           fail("Got " + opening + " as opening bracket pair for "
223                   + ((char) i));
224         }
225       }
226     }
227   }
228 }