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