9815aa033ba87254bea2513dfc0c0ac8f76038c5
[jalview.git] / test / jalview / util / DnaUtilsTest.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.util;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNull;
25 import static org.testng.AssertJUnit.fail;
26
27 import jalview.gui.JvOptionPane;
28
29 import java.text.ParseException;
30 import java.util.List;
31
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.Test;
34
35 public class DnaUtilsTest
36 {
37
38   @BeforeClass(alwaysRun = true)
39   public void setUpJvOptionPane()
40   {
41     JvOptionPane.setInteractiveMode(false);
42     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
43   }
44
45   /**
46    * Tests for parsing an ENA/GenBank location specifier
47    * 
48    * @throws ParseException
49    * 
50    * @see http://www.insdc.org/files/feature_table.html#3.4
51    */
52   @Test(groups = { "Functional" })
53   public void testParseLocation() throws ParseException
54   {
55     /*
56      * single locus
57      */
58     List<int[]> ranges = DnaUtils.parseLocation("467");
59     assertEquals(1, ranges.size());
60     assertEquals(467, ranges.get(0)[0]);
61     assertEquals(467, ranges.get(0)[1]);
62
63     /*
64      * simple range
65      */
66     ranges = DnaUtils.parseLocation("12..78");
67     assertEquals(1, ranges.size());
68     assertEquals(12, ranges.get(0)[0]);
69     assertEquals(78, ranges.get(0)[1]);
70
71     /*
72      * join of simple ranges
73      */
74     ranges = DnaUtils.parseLocation("join(12..78,134..202,322..345)");
75     assertEquals(3, ranges.size());
76     assertEquals(12, ranges.get(0)[0]);
77     assertEquals(78, ranges.get(0)[1]);
78     assertEquals(134, ranges.get(1)[0]);
79     assertEquals(202, ranges.get(1)[1]);
80     assertEquals(322, ranges.get(2)[0]);
81     assertEquals(345, ranges.get(2)[1]);
82
83     /*
84      * complement of a simple range
85      */
86     ranges = DnaUtils.parseLocation("complement(34..126)");
87     assertEquals(1, ranges.size());
88     assertEquals(126, ranges.get(0)[0]);
89     assertEquals(34, ranges.get(0)[1]);
90
91     /*
92      * complement of a join
93      */
94     ranges = DnaUtils
95             .parseLocation("complement(join(2691..4571,4918..5163))");
96     assertEquals(2, ranges.size());
97     assertEquals(5163, ranges.get(0)[0]);
98     assertEquals(4918, ranges.get(0)[1]);
99     assertEquals(4571, ranges.get(1)[0]);
100     assertEquals(2691, ranges.get(1)[1]);
101
102     /*
103      * join of two complements
104      */
105     ranges = DnaUtils
106             .parseLocation("join(complement(4918..5163),complement(2691..4571))");
107     assertEquals(2, ranges.size());
108     assertEquals(5163, ranges.get(0)[0]);
109     assertEquals(4918, ranges.get(0)[1]);
110     assertEquals(4571, ranges.get(1)[0]);
111     assertEquals(2691, ranges.get(1)[1]);
112
113     /*
114      * join complement to non-complement
115      * @see http://www.ncbi.nlm.nih.gov/genbank/genomesubmit_annotation/ Transpliced Genes
116      */
117     ranges = DnaUtils
118             .parseLocation("join(complement(36618..36700),86988..87064)");
119     assertEquals(2, ranges.size());
120     assertEquals(36700, ranges.get(0)[0]);
121     assertEquals(36618, ranges.get(0)[1]);
122     assertEquals(86988, ranges.get(1)[0]);
123     assertEquals(87064, ranges.get(1)[1]);
124
125     /*
126      * valid things we don't yet handle
127      */
128     checkForParseException("<34..126");
129     checkForParseException("35..>126");
130     checkForParseException("34.126");
131     checkForParseException("34^126");
132     checkForParseException("order(34..126,130..180)");
133
134     /*
135      * invalid things
136      */
137     checkForParseException("");
138     checkForParseException("JOIN(1..2)");
139     checkForParseException("join(1..2");
140     checkForParseException("join(1..2(");
141     checkForParseException("complement(1..2");
142     checkForParseException("complement(1..2(");
143     try
144     {
145       assertNull(DnaUtils.parseLocation(null));
146       fail("Expected exception");
147     } catch (NullPointerException e)
148     {
149       // expected
150     }
151
152     /*
153      * nested joins are not allowed; just as well since this fails to parse
154      * (splitting tokens by comma fragments the inner join expression)
155      */
156     checkForParseException("join(1..2,join(4..5,10..12),18..22)");
157     /*
158      * complement may not enclose multiple ranges 
159      * parsing fails for the same reason
160      */
161     checkForParseException("join(complement(36618..36700,4000..4200),86988..87064)");
162   }
163
164   /**
165    * Verifies that a ParseException is thrown when the given location is parsed
166    * 
167    * @param location
168    */
169   void checkForParseException(String location)
170   {
171     try
172     {
173       DnaUtils.parseLocation(location);
174       fail("Expected exception");
175     } catch (ParseException e)
176     {
177       // expected;
178     }
179   }
180
181 }