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