7fb716f3bfddf9a087aca71aff52dfbe51648343
[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.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.gui.JvOptionPane;
28
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
35
36 public class GffHelperBaseTest
37 {
38
39   @BeforeClass(alwaysRun = true)
40   public void setUpJvOptionPane()
41   {
42     JvOptionPane.setInteractiveMode(false);
43     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
44   }
45
46   /**
47    * Test the method that parses lines like <br>
48    * ID=2345;Name=Something,Another thing;Notes=Hello;Notes=World
49    */
50   @Test(groups = { "Functional" })
51   public void testParseNameValuePairs()
52   {
53     assertTrue(GffHelperBase.parseNameValuePairs(null, ";", ' ', ",")
54             .isEmpty());
55     assertTrue(GffHelperBase.parseNameValuePairs("", ";", ' ', ",")
56             .isEmpty());
57     assertTrue(GffHelperBase.parseNameValuePairs("hello=world", ";", ' ',
58             ",").isEmpty());
59
60     Map<String, List<String>> map = GffHelperBase.parseNameValuePairs(
61             "hello world", ";", ' ', ", ");
62     assertEquals(1, map.size());
63     assertEquals(1, map.get("hello").size());
64     assertEquals("world", map.get("hello").get(0));
65
66     map = GffHelperBase
67             .parseNameValuePairs(
68                     "Method= manual curation ;nothing; Notes=F2 S ; Notes=Metal,Shiny; Type=",
69                     ";", '=', ",");
70
71     // Type is ignored as no value was supplied
72     assertEquals(2, map.size());
73
74     assertEquals(1, map.get("Method").size());
75     assertEquals("manual curation", map.get("Method").get(0)); // trimmed
76
77     assertEquals(3, map.get("Notes").size());
78     assertEquals("F2 S", map.get("Notes").get(0));
79     assertEquals("Metal", map.get("Notes").get(1));
80     assertEquals("Shiny", map.get("Notes").get(2));
81   }
82
83   /**
84    * Test for the method that tries to trim mappings to equivalent lengths
85    */
86   @Test(groups = "Functional")
87   public void testTrimMapping()
88   {
89     int[] from = { 1, 12 };
90     int[] to = { 20, 31 };
91     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
92     assertEquals("[1, 12]", Arrays.toString(from)); // unchanged
93     assertEquals("[20, 31]", Arrays.toString(to)); // unchanged
94
95     // from too long:
96     from = new int[] { 1, 13 };
97     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
98     assertEquals("[1, 12]", Arrays.toString(from)); // trimmed
99     assertEquals("[20, 31]", Arrays.toString(to)); // unchanged
100
101     // to too long:
102     to = new int[] { 20, 33 };
103     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
104     assertEquals("[1, 12]", Arrays.toString(from)); // unchanged
105     assertEquals("[20, 31]", Arrays.toString(to)); // trimmed
106
107     // from reversed:
108     from = new int[] { 12, 1 };
109     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
110     assertEquals("[12, 1]", Arrays.toString(from)); // unchanged
111     assertEquals("[20, 31]", Arrays.toString(to)); // unchanged
112
113     // to reversed:
114     to = new int[] { 31, 20 };
115     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
116     assertEquals("[12, 1]", Arrays.toString(from)); // unchanged
117     assertEquals("[31, 20]", Arrays.toString(to)); // unchanged
118
119     // from reversed and too long:
120     from = new int[] { 14, 1 };
121     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
122     assertEquals("[14, 3]", Arrays.toString(from)); // end trimmed
123     assertEquals("[31, 20]", Arrays.toString(to)); // unchanged
124
125     // to reversed and too long:
126     to = new int[] { 31, 10 };
127     assertTrue(GffHelperBase.trimMapping(from, to, 1, 1));
128     assertEquals("[14, 3]", Arrays.toString(from)); // unchanged
129     assertEquals("[31, 20]", Arrays.toString(to)); // end trimmed
130
131     // cdna to peptide (matching)
132     from = new int[] { 1, 18 };
133     to = new int[] { 4, 9 };
134     assertTrue(GffHelperBase.trimMapping(from, to, 3, 1));
135     assertEquals("[1, 18]", Arrays.toString(from)); // unchanged
136     assertEquals("[4, 9]", Arrays.toString(to)); // unchanged
137
138     // overlong cdna to peptide
139     from = new int[] { 1, 20 };
140     assertTrue(GffHelperBase.trimMapping(from, to, 3, 1));
141     assertEquals("[1, 18]", Arrays.toString(from)); // end trimmed
142     assertEquals("[4, 9]", Arrays.toString(to)); // unchanged
143
144     // overlong cdna (reversed) to peptide
145     from = new int[] { 20, 1 };
146     assertTrue(GffHelperBase.trimMapping(from, to, 3, 1));
147     assertEquals("[20, 3]", Arrays.toString(from)); // end trimmed
148     assertEquals("[4, 9]", Arrays.toString(to)); // unchanged
149
150     // overlong cdna (reversed) to peptide (reversed)
151     from = new int[] { 20, 1 };
152     to = new int[] { 9, 4 };
153     assertTrue(GffHelperBase.trimMapping(from, to, 3, 1));
154     assertEquals("[20, 3]", Arrays.toString(from)); // end trimmed
155     assertEquals("[9, 4]", Arrays.toString(to)); // unchanged
156
157     // peptide to cdna (matching)
158     from = new int[] { 4, 9 };
159     to = new int[] { 1, 18 };
160     assertTrue(GffHelperBase.trimMapping(from, to, 1, 3));
161     assertEquals("[4, 9]", Arrays.toString(from)); // unchanged
162     assertEquals("[1, 18]", Arrays.toString(to)); // unchanged
163
164     // peptide to overlong cdna
165     to = new int[] { 1, 20 };
166     assertTrue(GffHelperBase.trimMapping(from, to, 1, 3));
167     assertEquals("[4, 9]", Arrays.toString(from)); // unchanged
168     assertEquals("[1, 18]", Arrays.toString(to)); // end trimmed
169
170     // peptide to overlong cdna (reversed)
171     to = new int[] { 20, 1 };
172     assertTrue(GffHelperBase.trimMapping(from, to, 1, 3));
173     assertEquals("[4, 9]", Arrays.toString(from)); // unchanged
174     assertEquals("[20, 3]", Arrays.toString(to)); // end trimmed
175
176     // peptide (reversed) to overlong cdna (reversed)
177     from = new int[] { 9, 4 };
178     to = new int[] { 20, 1 };
179     assertTrue(GffHelperBase.trimMapping(from, to, 1, 3));
180     assertEquals("[9, 4]", Arrays.toString(from)); // unchanged
181     assertEquals("[20, 3]", Arrays.toString(to)); // end trimmed
182
183     // overlong peptide to word-length cdna
184     from = new int[] { 4, 10 };
185     to = new int[] { 1, 18 };
186     assertTrue(GffHelperBase.trimMapping(from, to, 1, 3));
187     assertEquals("[4, 9]", Arrays.toString(from)); // end trimmed
188     assertEquals("[1, 18]", Arrays.toString(to)); // unchanged
189
190     // overlong peptide to non-word-length cdna
191     from = new int[] { 4, 10 };
192     to = new int[] { 1, 19 };
193     assertFalse(GffHelperBase.trimMapping(from, to, 1, 3));
194     assertEquals("[4, 10]", Arrays.toString(from)); // unchanged
195     assertEquals("[1, 19]", Arrays.toString(to)); // unchanged
196
197   }
198 }