Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.io.gff

File GffHelperBaseTest.java

 

Code metrics

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