1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.io.gff

File GffHelperBaseTest.java

 

Code metrics

0
134
4
1
265
176
5
0.04
33.5
4
1.25

Classes

Class
Line #
Actions
GffHelperBaseTest 37 134 5
0.992753699.3%
 

Contributing tests

This file is covered by 3 tests. .

Source view

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