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