394ea6997f929694746b45a0d3acd3558f28e4bb
[jalview.git] / test / jalview / ws / phyre2 / Phyre2ClientTest.java
1 package jalview.ws.phyre2;
2
3 import jalview.datamodel.Sequence;
4 import jalview.datamodel.SequenceI;
5 import jalview.ext.jmol.JmolParser;
6 import jalview.io.DataSourceType;
7 import jalview.io.StructureFile;
8
9 import java.io.IOException;
10 import java.util.Arrays;
11 import java.util.HashMap;
12
13 import org.testng.Assert;
14 import org.testng.annotations.Test;
15
16 public class Phyre2ClientTest
17 {
18   private Phyre2Client phyre2Client = null;
19
20
21
22
23   @Test(groups = { "Functional" })
24   public void getPhyre2FastaMappingTest()
25   {
26     String phyre2ModelFile = "examples/testdata/phyre2results/56da5616b4559c93/c4n58A_.1.pdb";
27     String fastaMappingFile = "examples/testdata/phyre2results/56da5616b4559c93/c4n58A_.1.fasta";
28
29     SequenceI testSeq = new Sequence(
30             "FER_CAPAN",
31             "MASVSATMISTSFMPRKPAVTSLKPIPNVGEALFGLKSANGGKVTCMASYKVKLITPDGPIEF"
32                     + "DCPDNVYILDQAEEAGHDLPYSCRAGSCSSCAGKIAGGAVDQTDGNFLDDDQL"
33                     + "EEGWVLTCVAYPQSDVTIETHKEAELVG", 1, 144);
34
35     HashMap<Integer, int[]> expectedMapping = new HashMap<Integer, int[]>();
36     // PDB sequence starts with residue 48
37     expectedMapping.put(48, new int[] { 48, 1 });
38     expectedMapping.put(49, new int[] { 49, 6 });
39     expectedMapping.put(50, new int[] { 50, 12 });
40     expectedMapping.put(51, new int[] { 51, 24 });
41     expectedMapping.put(52, new int[] { 52, 33 });
42     expectedMapping.put(53, new int[] { 53, 40 });
43     expectedMapping.put(54, new int[] { 54, 49 });
44     expectedMapping.put(55, new int[] { 55, 57 });
45     expectedMapping.put(56, new int[] { 56, 65 });
46     expectedMapping.put(57, new int[] { 57, 72 });
47     expectedMapping.put(58, new int[] { 58, 79 });
48     expectedMapping.put(59, new int[] { 59, 87 });
49     // residues 60, 61 absent in PDB file
50     // residues 62, 63 also skipped in map (is this right?)
51     expectedMapping.put(64, new int[] { 62, 91 });
52     expectedMapping.put(65, new int[] { 63, 100 });
53     expectedMapping.put(66, new int[] { 64, 111 });
54     expectedMapping.put(67, new int[] { 65, 119 });
55     expectedMapping.put(68, new int[] { 66, 125 });
56     expectedMapping.put(69, new int[] { 67, 132 });
57     expectedMapping.put(70, new int[] { 68, 140 });
58     expectedMapping.put(71, new int[] { 69, 148 });
59     expectedMapping.put(72, new int[] { 70, 155 });
60     expectedMapping.put(73, new int[] { 71, 167 });
61     expectedMapping.put(74, new int[] { 72, 175 });
62     expectedMapping.put(75, new int[] { 73, 183 });
63     expectedMapping.put(76, new int[] { 74, 191 });
64     expectedMapping.put(77, new int[] { 75, 200 });
65     expectedMapping.put(78, new int[] { 76, 205 });
66     expectedMapping.put(79, new int[] { 77, 214 });
67     expectedMapping.put(80, new int[] { 78, 223 });
68     expectedMapping.put(81, new int[] { 79, 228 });
69     expectedMapping.put(82, new int[] { 80, 232 });
70     expectedMapping.put(83, new int[] { 81, 242 });
71     expectedMapping.put(84, new int[] { 82, 250 });
72     expectedMapping.put(85, new int[] { 83, 258 });
73     expectedMapping.put(86, new int[] { 84, 265 });
74     expectedMapping.put(87, new int[] { 85, 277 });
75     expectedMapping.put(88, new int[] { 86, 283 });
76     expectedMapping.put(89, new int[] { 87, 289 });
77     expectedMapping.put(90, new int[] { 88, 300 });
78     expectedMapping.put(91, new int[] { 89, 305 });
79     expectedMapping.put(92, new int[] { 90, 309 });
80     expectedMapping.put(93, new int[] { 91, 315 });
81     expectedMapping.put(94, new int[] { 92, 321 });
82     expectedMapping.put(95, new int[] { 93, 327 });
83     expectedMapping.put(96, new int[] { 94, 333 });
84     expectedMapping.put(97, new int[] { 95, 339 });
85     expectedMapping.put(98, new int[] { 96, 344 });
86     expectedMapping.put(99, new int[] { 97, 348 });
87     expectedMapping.put(100, new int[] { 98, 357 });
88     expectedMapping.put(101, new int[] { 99, 365 });
89     expectedMapping.put(102, new int[] { 100, 370 });
90     expectedMapping.put(103, new int[] { 101, 374 });
91     expectedMapping.put(104, new int[] { 102, 378 });
92     expectedMapping.put(105, new int[] { 103, 383 });
93     expectedMapping.put(106, new int[] { 104, 390 });
94     expectedMapping.put(107, new int[] { 105, 398 });
95     expectedMapping.put(108, new int[] { 106, 407 });
96     expectedMapping.put(109, new int[] { 107, 414 });
97     expectedMapping.put(110, new int[] { 108, 422 });
98     expectedMapping.put(111, new int[] { 109, 426 });
99     expectedMapping.put(112, new int[] { 110, 434 });
100     expectedMapping.put(113, new int[] { 111, 445 });
101     expectedMapping.put(114, new int[] { 112, 453 });
102     expectedMapping.put(115, new int[] { 113, 461 });
103     // residue 116 absent in PDB file
104     expectedMapping.put(117, new int[] { 114, 469 });
105     expectedMapping.put(118, new int[] { 115, 477 });
106     // residue 119 gets removed as mapped to 116 - is this right?
107     expectedMapping.put(120, new int[] { 117, 486 });
108     expectedMapping.put(121, new int[] { 118, 495 });
109     expectedMapping.put(122, new int[] { 119, 504 });
110     expectedMapping.put(123, new int[] { 120, 508 });
111     expectedMapping.put(124, new int[] { 121, 522 });
112     expectedMapping.put(125, new int[] { 122, 529 });
113     expectedMapping.put(126, new int[] { 123, 537 });
114     expectedMapping.put(127, new int[] { 124, 544 });
115     expectedMapping.put(128, new int[] { 125, 550 });
116     expectedMapping.put(129, new int[] { 126, 557 });
117     expectedMapping.put(130, new int[] { 127, 562 });
118     expectedMapping.put(131, new int[] { 128, 574 });
119     expectedMapping.put(132, new int[] { 129, 581 });
120     expectedMapping.put(133, new int[] { 130, 590 });
121     expectedMapping.put(134, new int[] { 131, 596 });
122     expectedMapping.put(135, new int[] { 132, 604 });
123     expectedMapping.put(136, new int[] { 133, 611 });
124     expectedMapping.put(137, new int[] { 134, 618 });
125     expectedMapping.put(138, new int[] { 135, 626 });
126     expectedMapping.put(139, new int[] { 136, 635 });
127     expectedMapping.put(140, new int[] { 137, 642 });
128     expectedMapping.put(141, new int[] { 138, 652 });
129     expectedMapping.put(142, new int[] { 139, 661 });
130     expectedMapping.put(143, new int[] { 140, 670 });
131     // residue 144 absent in PDB file
132
133     StructureFile structureFile;
134     try
135     {
136       structureFile = new JmolParser(phyre2ModelFile, DataSourceType.FILE);
137       phyre2Client = new Phyre2Client(structureFile);
138       phyre2Client.setFastaMappingFile(fastaMappingFile);
139     } catch (IOException e1)
140     {
141       e1.printStackTrace();
142     }
143     Assert.assertNotNull(phyre2Client);
144     Assert.assertNotNull(testSeq);
145     Assert.assertNotNull(expectedMapping);
146     try
147     {
148       HashMap<Integer, int[]> actualMapping = phyre2Client
149               .getPhyre2FastaMapping(testSeq, null);
150
151       Assert.assertEquals(testSeq.getStart(), 1);
152       Assert.assertEquals(testSeq.getEnd(), 144);
153       testMappings(actualMapping, expectedMapping);
154     } catch (Exception e)
155     {
156       e.printStackTrace();
157       Assert.fail("Exception thrown while performing Phyre2 model mapping...");
158     }
159   }
160
161   @Test(groups = { "Functional" })
162   public void getPhyre2FastaMappingTest2()
163   {
164     String phyre2ModelFile = "examples/testdata/phyre2results/56da5616b4559c93/d1a70a_.pdb";
165     String fastaMappingFile = "examples/testdata/phyre2results/56da5616b4559c93/d1a70a_.fasta";
166
167     SequenceI testSeq = new Sequence(
168             "FER_CAPAN",
169             "APPPCFSSPLRLRVAVAKPLAAPMRRQLLRAQATYNVKLITPEGEVELQVPDDVYILDFAEEEGIDLPFSCRAGSCSSCAGKVVSGSVDQSDQSFLNDNQVADGWVLTCAAYPTSDVVIETHKEDDL",
170             13, 139);
171
172     HashMap<Integer, int[]> expectedMapping = new HashMap<Integer, int[]>();
173     // PDB sequence starts with residue 33
174     expectedMapping.put(45, new int[] { 33, 1 });
175     expectedMapping.put(46, new int[] { 34, 6 });
176     expectedMapping.put(47, new int[] { 35, 13 });
177     expectedMapping.put(48, new int[] { 36, 25 });
178     expectedMapping.put(49, new int[] { 37, 33 });
179     expectedMapping.put(50, new int[] { 38, 40 });
180     expectedMapping.put(51, new int[] { 39, 49 });
181     expectedMapping.put(52, new int[] { 40, 57 });
182     expectedMapping.put(53, new int[] { 41, 65 });
183     expectedMapping.put(54, new int[] { 42, 72 });
184     expectedMapping.put(55, new int[] { 43, 79 });
185     expectedMapping.put(56, new int[] { 44, 88 });
186     expectedMapping.put(57, new int[] { 45, 92 });
187     expectedMapping.put(58, new int[] { 46, 101 });
188     expectedMapping.put(59, new int[] { 47, 108 });
189     expectedMapping.put(60, new int[] { 48, 117 });
190     expectedMapping.put(61, new int[] { 49, 125 });
191     expectedMapping.put(62, new int[] { 50, 134 });
192     expectedMapping.put(63, new int[] { 51, 141 });
193     expectedMapping.put(64, new int[] { 52, 148 });
194     expectedMapping.put(65, new int[] { 53, 156 });
195     expectedMapping.put(66, new int[] { 54, 164 });
196     expectedMapping.put(67, new int[] { 55, 171 });
197     expectedMapping.put(68, new int[] { 56, 183 });
198     expectedMapping.put(69, new int[] { 57, 191 });
199     expectedMapping.put(70, new int[] { 58, 199 });
200     expectedMapping.put(71, new int[] { 59, 207 });
201     expectedMapping.put(72, new int[] { 60, 218 });
202     expectedMapping.put(73, new int[] { 61, 223 });
203     expectedMapping.put(74, new int[] { 62, 232 });
204     expectedMapping.put(75, new int[] { 63, 241 });
205     expectedMapping.put(76, new int[] { 64, 250 });
206     expectedMapping.put(77, new int[] { 65, 254 });
207     expectedMapping.put(78, new int[] { 66, 262 });
208     expectedMapping.put(79, new int[] { 67, 270 });
209     expectedMapping.put(80, new int[] { 68, 278 });
210     expectedMapping.put(81, new int[] { 69, 285 });
211     expectedMapping.put(82, new int[] { 70, 296 });
212     expectedMapping.put(83, new int[] { 71, 302 });
213     expectedMapping.put(84, new int[] { 72, 308 });
214     expectedMapping.put(85, new int[] { 73, 319 });
215     expectedMapping.put(86, new int[] { 74, 324 });
216     expectedMapping.put(87, new int[] { 75, 328 });
217     expectedMapping.put(88, new int[] { 76, 334 });
218     expectedMapping.put(89, new int[] { 77, 340 });
219     expectedMapping.put(90, new int[] { 78, 346 });
220     expectedMapping.put(91, new int[] { 79, 352 });
221     expectedMapping.put(92, new int[] { 80, 358 });
222     expectedMapping.put(93, new int[] { 81, 363 });
223     expectedMapping.put(94, new int[] { 82, 367 });
224     expectedMapping.put(95, new int[] { 83, 376 });
225     expectedMapping.put(96, new int[] { 84, 383 });
226     expectedMapping.put(97, new int[] { 85, 390 });
227     expectedMapping.put(98, new int[] { 86, 396 });
228     expectedMapping.put(99, new int[] { 87, 400 });
229     expectedMapping.put(100, new int[] { 88, 406 });
230     expectedMapping.put(101, new int[] { 89, 413 });
231     expectedMapping.put(102, new int[] { 90, 421 });
232     expectedMapping.put(103, new int[] { 91, 430 });
233     expectedMapping.put(104, new int[] { 92, 436 });
234     expectedMapping.put(105, new int[] { 93, 444 });
235     expectedMapping.put(106, new int[] { 94, 453 });
236     expectedMapping.put(107, new int[] { 95, 459 });
237     expectedMapping.put(108, new int[] { 96, 470 });
238     expectedMapping.put(109, new int[] { 97, 478 });
239     expectedMapping.put(110, new int[] { 98, 486 });
240     expectedMapping.put(111, new int[] { 99, 494 });
241     expectedMapping.put(112, new int[] { 100, 502 });
242     expectedMapping.put(113, new int[] { 101, 511 });
243     expectedMapping.put(114, new int[] { 102, 518 });
244     expectedMapping.put(115, new int[] { 103, 523 });
245     expectedMapping.put(116, new int[] { 104, 531 });
246     expectedMapping.put(117, new int[] { 105, 535 });
247     expectedMapping.put(118, new int[] { 106, 549 });
248     expectedMapping.put(119, new int[] { 107, 556 });
249     expectedMapping.put(120, new int[] { 108, 564 });
250     expectedMapping.put(121, new int[] { 109, 571 });
251     expectedMapping.put(122, new int[] { 110, 577 });
252     expectedMapping.put(123, new int[] { 111, 582 });
253     expectedMapping.put(124, new int[] { 112, 587 });
254     expectedMapping.put(125, new int[] { 113, 599 });
255     expectedMapping.put(126, new int[] { 114, 606 });
256     expectedMapping.put(127, new int[] { 115, 613 });
257     expectedMapping.put(128, new int[] { 116, 619 });
258     expectedMapping.put(129, new int[] { 117, 627 });
259     expectedMapping.put(130, new int[] { 118, 634 });
260     expectedMapping.put(131, new int[] { 119, 641 });
261     expectedMapping.put(132, new int[] { 120, 649 });
262     expectedMapping.put(133, new int[] { 121, 658 });
263     expectedMapping.put(134, new int[] { 122, 665 });
264     expectedMapping.put(135, new int[] { 123, 675 });
265     expectedMapping.put(136, new int[] { 124, 684 });
266     expectedMapping.put(137, new int[] { 125, 693 });
267     expectedMapping.put(138, new int[] { 126, 701 });
268     expectedMapping.put(139, new int[] { 127, 709 });
269
270     StructureFile structureFile;
271     try
272     {
273       structureFile = new JmolParser(phyre2ModelFile, DataSourceType.FILE);
274       phyre2Client = new Phyre2Client(structureFile);
275       phyre2Client.setFastaMappingFile(fastaMappingFile);
276     } catch (IOException e1)
277     {
278       e1.printStackTrace();
279     }
280     Assert.assertNotNull(phyre2Client);
281     Assert.assertNotNull(testSeq);
282     Assert.assertNotNull(expectedMapping);
283     try
284     {
285       HashMap<Integer, int[]> actualMapping = phyre2Client
286               .getPhyre2FastaMapping(testSeq, null);
287
288       Assert.assertEquals(testSeq.getStart(), 13);
289       Assert.assertEquals(testSeq.getEnd(), 139);
290       Assert.assertEquals(actualMapping, expectedMapping);
291       testMappings(actualMapping, expectedMapping);
292     } catch (Exception e)
293     {
294       e.printStackTrace();
295       Assert.fail("Exception thrown while performing Phyre2 model mapping...");
296     }
297   }
298
299   public void testMappings(HashMap<Integer, int[]> actualMapping,
300           HashMap<Integer, int[]> expectedMapping)
301   {
302     System.out.println("Expected Mapping size: " + expectedMapping.size());
303     System.out.println("Actual   Mapping size: " + actualMapping.size());
304     // Test size
305     Assert.assertEquals(actualMapping.size(), expectedMapping.size());
306     // Test key set
307     Assert.assertEquals(actualMapping.keySet(), expectedMapping.keySet());
308     // Test entry set
309     for (int key : expectedMapping.keySet())
310     {
311       System.out.println(key + " ---> [" + expectedMapping.get(key)[0]
312               + ", " + expectedMapping.get(key)[1] + "] = ["
313               + actualMapping.get(key)[0] + ", "
314               + actualMapping.get(key)[1] + "]");
315       Assert.assertTrue(Arrays.equals(expectedMapping.get(key),
316               actualMapping.get(key)));
317     }
318   }
319
320
321 }