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