a23518d302efe5890b65b48ce6e9ed9b09ab2267
[jalview.git] / test / jalview / io / gff / GffHelperBaseTest.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.io.gff;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertTrue;
26 import static org.testng.Assert.fail;
27
28 import jalview.gui.JvOptionPane;
29
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
36
37 public class GffHelperBaseTest
38 {
39
40   @BeforeClass(alwaysRun = true)
41   public void setUpJvOptionPane()
42   {
43     JvOptionPane.setInteractiveMode(false);
44     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
45   }
46
47   /**
48    * Test the method that parses lines like <br>
49    * ID=2345;Name=Something,Another thing;Notes=Hello;Notes=World
50    */
51   @Test(groups = { "Functional" })
52   public void testParseNameValuePairs()
53   {
54     assertTrue(GffHelperBase.parseNameValuePairs(null, ";", ' ', ",")
55             .isEmpty());
56     assertTrue(GffHelperBase.parseNameValuePairs("", ";", ' ', ",")
57             .isEmpty());
58     assertTrue(GffHelperBase.parseNameValuePairs("hello=world", ";", ' ',
59             ",").isEmpty());
60
61     Map<String, List<String>> map = GffHelperBase.parseNameValuePairs(
62             "hello world", ";", ' ', ", ");
63     assertEquals(map.size(), 1);
64     assertEquals(map.get("hello").size(), 1);
65     assertEquals(map.get("hello").get(0), "world");
66
67     map = GffHelperBase
68             .parseNameValuePairs(
69                     "Method= manual curation ;nothing; Notes=F2 S ; Notes=Metal,Shiny%2Csmooth; Type=",
70                     ";", '=', ",");
71
72     // Type is ignored as no value was supplied
73     assertEquals(map.size(), 2);
74
75     assertEquals(map.get("Method").size(), 1);
76     assertEquals(map.get("Method").get(0), "manual curation"); // trimmed
77
78     assertEquals(map.get("Notes").size(), 3);
79     assertEquals(map.get("Notes").get(0), "F2 S");
80     assertEquals(map.get("Notes").get(1), "Metal");
81     assertEquals(map.get("Notes").get(2), "Shiny%2Csmooth"); // not decoded here
82
83     /*
84      * gff3 style with nested attribute values
85      */
86     String csqValue = "POLYPHEN=possibly_damaging,probably_damaging,SIFT=tolerated%2Cdeleterious";
87     map = GffHelperBase.parseNameValuePairs("hello=world;CSQ=" + csqValue,
88             ";", '=', ",");
89     assertEquals(map.size(), 2); // keys hello, CSQ
90     assertEquals(map.get("hello").size(), 1);
91     assertEquals(map.get("hello").get(0), "world");
92     // CSQ values is read 'raw' here, and parsed further elsewhere
93     assertEquals(map.get("CSQ").size(), 1);
94     assertEquals(map.get("CSQ").get(0), csqValue);
95   }
96
97   /**
98    * Test for the method that tries to trim mappings to equivalent lengths
99    */
100   @Test(groups = "Functional")
101   public void testTrimMapping()
102   {
103     int[] from = { 1, 12 };
104     int[] to = { 20, 31 };
105     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
106     assertEquals(Arrays.toString(from), "[1, 12]"); // unchanged
107     assertEquals(Arrays.toString(to), "[20, 31]"); // unchanged
108
109     // from too long:
110     from = new int[] { 1, 13 };
111     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
112     assertEquals(Arrays.toString(from), "[1, 12]"); // trimmed
113     assertEquals(Arrays.toString(to), "[20, 31]"); // unchanged
114
115     // to too long:
116     to = new int[] { 20, 33 };
117     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
118     assertEquals(Arrays.toString(from), "[1, 12]"); // unchanged
119     assertEquals(Arrays.toString(to), "[20, 31]"); // trimmed
120
121     // from reversed:
122     from = new int[] { 12, 1 };
123     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
124     assertEquals(Arrays.toString(from), "[12, 1]"); // unchanged
125     assertEquals(Arrays.toString(to), "[20, 31]"); // unchanged
126
127     // to reversed:
128     to = new int[] { 31, 20 };
129     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
130     assertEquals(Arrays.toString(from), "[12, 1]"); // unchanged
131     assertEquals(Arrays.toString(to), "[31, 20]"); // unchanged
132
133     // from reversed and too long:
134     from = new int[] { 14, 1 };
135     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
136     assertEquals(Arrays.toString(from), "[14, 3]"); // end trimmed
137     assertEquals(Arrays.toString(to), "[31, 20]"); // unchanged
138
139     // to reversed and too long:
140     to = new int[] { 31, 10 };
141     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
142     assertEquals(Arrays.toString(from), "[14, 3]"); // unchanged
143     assertEquals(Arrays.toString(to), "[31, 20]"); // end trimmed
144
145     // cdna to peptide (matching)
146     from = new int[] { 1, 18 };
147     to = new int[] { 4, 9 };
148     assertTrue(GffHelperBase.trimMapping(from, to, 3, 1));
149     assertEquals(Arrays.toString(from), "[1, 18]"); // unchanged
150     assertEquals(Arrays.toString(to), "[4, 9]"); // unchanged
151
152     // overlong cdna to peptide
153     from = new int[] { 1, 20 };
154     assertTrue(GffHelperBase.trimMapping(from, to, 3, 1));
155     assertEquals(Arrays.toString(from), "[1, 18]"); // end trimmed
156     assertEquals(Arrays.toString(to), "[4, 9]"); // unchanged
157
158     // overlong cdna (reversed) to peptide
159     from = new int[] { 20, 1 };
160     assertTrue(GffHelperBase.trimMapping(from, to, 3, 1));
161     assertEquals(Arrays.toString(from), "[20, 3]"); // end trimmed
162     assertEquals(Arrays.toString(to), "[4, 9]"); // unchanged
163
164     // overlong cdna (reversed) to peptide (reversed)
165     from = new int[] { 20, 1 };
166     to = new int[] { 9, 4 };
167     assertTrue(GffHelperBase.trimMapping(from, to, 3, 1));
168     assertEquals(Arrays.toString(from), "[20, 3]"); // end trimmed
169     assertEquals(Arrays.toString(to), "[9, 4]"); // unchanged
170
171     // peptide to cdna (matching)
172     from = new int[] { 4, 9 };
173     to = new int[] { 1, 18 };
174     assertTrue(GffHelperBase.trimMapping(from, to, 1, 3));
175     assertEquals(Arrays.toString(from), "[4, 9]"); // unchanged
176     assertEquals(Arrays.toString(to), "[1, 18]"); // unchanged
177
178     // peptide to overlong cdna
179     to = new int[] { 1, 20 };
180     assertTrue(GffHelperBase.trimMapping(from, to, 1, 3));
181     assertEquals(Arrays.toString(from), "[4, 9]"); // unchanged
182     assertEquals(Arrays.toString(to), "[1, 18]"); // end trimmed
183
184     // peptide to overlong cdna (reversed)
185     to = new int[] { 20, 1 };
186     assertTrue(GffHelperBase.trimMapping(from, to, 1, 3));
187     assertEquals(Arrays.toString(from), "[4, 9]"); // unchanged
188     assertEquals(Arrays.toString(to), "[20, 3]"); // end trimmed
189
190     // peptide (reversed) to overlong cdna (reversed)
191     from = new int[] { 9, 4 };
192     to = new int[] { 20, 1 };
193     assertTrue(GffHelperBase.trimMapping(from, to, 1, 3));
194     assertEquals(Arrays.toString(from), "[9, 4]"); // unchanged
195     assertEquals(Arrays.toString(to), "[20, 3]"); // end trimmed
196
197     // overlong peptide to word-length cdna
198     from = new int[] { 4, 10 };
199     to = new int[] { 1, 18 };
200     assertTrue(GffHelperBase.trimMapping(from, to, 1, 3));
201     assertEquals(Arrays.toString(from), "[4, 9]"); // end trimmed
202     assertEquals(Arrays.toString(to), "[1, 18]"); // unchanged
203
204     // overlong peptide to non-word-length cdna
205     from = new int[] { 4, 10 };
206     to = new int[] { 1, 19 };
207     assertFalse(GffHelperBase.trimMapping(from, to, 1, 3));
208     assertEquals(Arrays.toString(from), "[4, 10]"); // unchanged
209     assertEquals(Arrays.toString(to), "[1, 19]"); // unchanged
210   }
211
212   @Test(groups = { "Functional" })
213   public void testParseAttributeMap()
214   {
215     Map<String, String> map = GffHelperBase
216             .parseAttributeMap("A=B,C%2C%3D%3B%09%25D,X=Y");
217     assertEquals(map.size(), 2);
218     // value of A is everything up to and excluding ,X=
219     assertEquals(map.get("A"), "B,C,=;\t%D");
220     assertEquals(map.get("X"), "Y");
221
222     /*
223      * malformed cases should result in an empty map
224      */
225     map = GffHelperBase.parseAttributeMap("=B=Y");
226     assertTrue(map.isEmpty());
227     // first token should be an attribute name only, no commas
228     map = GffHelperBase.parseAttributeMap("A,B=C");
229     assertTrue(map.isEmpty());
230     // intermediate tokens need at least one comma (value,name=)
231     map = GffHelperBase.parseAttributeMap("A=B=C");
232     assertTrue(map.isEmpty());
233     // last token may have a comma or not
234     map = GffHelperBase.parseAttributeMap("A=B");
235     assertEquals(map.get("A"), "B");
236     map = GffHelperBase.parseAttributeMap("A=B,C");
237     assertEquals(map.get("A"), "B,C");
238     map = GffHelperBase.parseAttributeMap("A");
239     assertTrue(map.isEmpty());
240     map = GffHelperBase.parseAttributeMap("A=");
241     assertTrue(map.isEmpty());
242     map = GffHelperBase.parseAttributeMap("A==C");
243     assertTrue(map.isEmpty());
244     map = GffHelperBase.parseAttributeMap("=A");
245     assertTrue(map.isEmpty());
246     map = GffHelperBase.parseAttributeMap("=");
247     assertTrue(map.isEmpty());
248     map = GffHelperBase.parseAttributeMap(",");
249     assertTrue(map.isEmpty());
250     map = GffHelperBase.parseAttributeMap(" ");
251     assertTrue(map.isEmpty());
252     map = GffHelperBase.parseAttributeMap("");
253     assertTrue(map.isEmpty());
254     map = GffHelperBase.parseAttributeMap("A=B, =C");
255     assertTrue(map.isEmpty());
256
257     try
258     {
259       GffHelperBase.parseAttributeMap(null);
260       fail("expected exception");
261     } catch (NullPointerException e)
262     {
263       // expected
264     }
265   }
266 }