clean up mistake in the test file allhits.ann
[jalview.git] / test / jalview / ws / sifts / SiftsClientTest.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.ws.sifts;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertTrue;
25
26 import jalview.api.DBRefEntryI;
27 import jalview.bin.Cache;
28 import jalview.datamodel.DBRefEntry;
29 import jalview.datamodel.DBRefSource;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceI;
32 import jalview.gui.JvOptionPane;
33 import jalview.io.DataSourceType;
34 import jalview.structure.StructureMapping;
35 import jalview.structure.StructureMappingClient.StructureMappingException;
36 import jalview.xml.binding.sifts.Entry.Entity;
37
38 import java.io.File;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.HashMap;
43
44 import org.testng.Assert;
45 import org.testng.FileAssert;
46 import org.testng.annotations.AfterTest;
47 import org.testng.annotations.BeforeClass;
48 import org.testng.annotations.BeforeTest;
49 import org.testng.annotations.Test;
50
51 import MCview.Atom;
52 import MCview.PDBfile;
53
54 public class SiftsClientTest
55 {
56
57   @BeforeClass(alwaysRun = true)
58   public void setUpJvOptionPane()
59   {
60     JvOptionPane.setInteractiveMode(false);
61     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
62   }
63
64   public static final String DEFAULT_SIFTS_DOWNLOAD_DIR = System
65           .getProperty("user.home")
66           + File.separatorChar
67           + ".sifts_downloads" + File.separatorChar;
68
69   private String testPDBId = "1a70";
70
71   private SiftsClient siftsClient = null;
72
73   SequenceI testSeq = new Sequence(
74           "P00221",
75           "MAAT..TTTMMG..MATTFVPKPQAPPMMAALPSNTGR..SLFGLKT.GSR..GGRMTMA"
76                   + "AYKVTLVTPTGNVEFQCPDDVYILDAAEEEGIDLPYSCRAGSCSSCAGKLKTGSLNQDD"
77                   + "QSFLDDDQIDEGWVLTCAAYPVSDVTIETHKEEELTA.", 1, 147);
78
79   int u = SiftsClient.UNASSIGNED;
80
81   HashMap<Integer, int[]> expectedMapping = new HashMap<Integer, int[]>();
82
83   @BeforeTest(alwaysRun = true)
84   public void populateExpectedMapping() throws SiftsException
85   {
86     expectedMapping.put(51, new int[] { 1, 2 });
87     expectedMapping.put(52, new int[] { 2, 7 });
88     expectedMapping.put(53, new int[] { 3, 12 });
89     expectedMapping.put(54, new int[] { 4, 24 });
90     expectedMapping.put(55, new int[] { 5, 33 });
91     expectedMapping.put(56, new int[] { 6, 40 });
92     expectedMapping.put(57, new int[] { 7, 47 });
93     expectedMapping.put(58, new int[] { 8, 55 });
94     expectedMapping.put(59, new int[] { 9, 62 });
95     expectedMapping.put(60, new int[] { 10, 69 });
96     expectedMapping.put(61, new int[] { 11, 76 });
97     expectedMapping.put(62, new int[] { 12, 83 });
98     expectedMapping.put(63, new int[] { 13, 87 });
99     expectedMapping.put(64, new int[] { 14, 95 });
100     expectedMapping.put(65, new int[] { 15, 102 });
101     expectedMapping.put(66, new int[] { 16, 111 });
102     expectedMapping.put(67, new int[] { 17, 122 });
103     expectedMapping.put(68, new int[] { 18, 131 });
104     expectedMapping.put(69, new int[] { 19, 137 });
105     expectedMapping.put(70, new int[] { 20, 144 });
106     expectedMapping.put(71, new int[] { 21, 152 });
107     expectedMapping.put(72, new int[] { 22, 160 });
108     expectedMapping.put(73, new int[] { 23, 167 });
109     expectedMapping.put(74, new int[] { 24, 179 });
110     expectedMapping.put(75, new int[] { 25, 187 });
111     expectedMapping.put(76, new int[] { 26, 195 });
112     expectedMapping.put(77, new int[] { 27, 203 });
113     expectedMapping.put(78, new int[] { 28, 208 });
114     expectedMapping.put(79, new int[] { 29, 213 });
115     expectedMapping.put(80, new int[] { 30, 222 });
116     expectedMapping.put(81, new int[] { 31, 231 });
117     expectedMapping.put(82, new int[] { 32, 240 });
118     expectedMapping.put(83, new int[] { 33, 244 });
119     expectedMapping.put(84, new int[] { 34, 252 });
120     expectedMapping.put(85, new int[] { 35, 260 });
121     expectedMapping.put(86, new int[] { 36, 268 });
122     expectedMapping.put(87, new int[] { 37, 275 });
123     expectedMapping.put(88, new int[] { 38, 287 });
124     expectedMapping.put(89, new int[] { 39, 293 });
125     expectedMapping.put(90, new int[] { 40, 299 });
126     expectedMapping.put(91, new int[] { 41, 310 });
127     expectedMapping.put(92, new int[] { 42, 315 });
128     expectedMapping.put(93, new int[] { 43, 319 });
129     expectedMapping.put(94, new int[] { 44, 325 });
130     expectedMapping.put(95, new int[] { 45, 331 });
131     expectedMapping.put(96, new int[] { 46, 337 });
132     expectedMapping.put(97, new int[] { 47, 343 });
133     expectedMapping.put(98, new int[] { 48, 349 });
134     expectedMapping.put(99, new int[] { 49, 354 });
135     expectedMapping.put(100, new int[] { 50, 358 });
136     expectedMapping.put(101, new int[] { 51, 367 });
137     expectedMapping.put(102, new int[] { 52, 375 });
138     expectedMapping.put(103, new int[] { 53, 384 });
139     expectedMapping.put(104, new int[] { 54, 391 });
140     expectedMapping.put(105, new int[] { 55, 395 });
141     expectedMapping.put(106, new int[] { 56, 401 });
142     expectedMapping.put(107, new int[] { 57, 409 });
143     expectedMapping.put(108, new int[] { 58, 417 });
144     expectedMapping.put(109, new int[] { 59, 426 });
145     expectedMapping.put(110, new int[] { 60, 434 });
146     expectedMapping.put(111, new int[] { 61, 442 });
147     expectedMapping.put(112, new int[] { 62, 451 });
148     expectedMapping.put(113, new int[] { 63, 457 });
149     expectedMapping.put(114, new int[] { 64, 468 });
150     expectedMapping.put(115, new int[] { 65, 476 });
151     expectedMapping.put(116, new int[] { 66, 484 });
152     expectedMapping.put(117, new int[] { 67, 492 });
153     expectedMapping.put(118, new int[] { 68, 500 });
154     expectedMapping.put(119, new int[] { 69, 509 });
155     expectedMapping.put(120, new int[] { 70, 517 });
156     expectedMapping.put(121, new int[] { 71, 525 });
157     expectedMapping.put(122, new int[] { 72, 534 });
158     expectedMapping.put(123, new int[] { 73, 538 });
159     expectedMapping.put(124, new int[] { 74, 552 });
160     expectedMapping.put(125, new int[] { 75, 559 });
161     expectedMapping.put(126, new int[] { 76, 567 });
162     expectedMapping.put(127, new int[] { 77, 574 });
163     expectedMapping.put(128, new int[] { 78, 580 });
164     expectedMapping.put(129, new int[] { 79, 585 });
165     expectedMapping.put(130, new int[] { 80, 590 });
166     expectedMapping.put(131, new int[] { 81, 602 });
167     expectedMapping.put(132, new int[] { 82, 609 });
168     expectedMapping.put(133, new int[] { 83, 616 });
169     expectedMapping.put(134, new int[] { 84, 622 });
170     expectedMapping.put(135, new int[] { 85, 630 });
171     expectedMapping.put(136, new int[] { 86, 637 });
172     expectedMapping.put(137, new int[] { 87, 644 });
173     expectedMapping.put(138, new int[] { 88, 652 });
174     expectedMapping.put(139, new int[] { 89, 661 });
175     expectedMapping.put(140, new int[] { 90, 668 });
176     expectedMapping.put(141, new int[] { 91, 678 });
177     expectedMapping.put(142, new int[] { 92, 687 });
178     expectedMapping.put(143, new int[] { 93, 696 });
179     expectedMapping.put(144, new int[] { 94, 705 });
180     expectedMapping.put(145, new int[] { 95, 714 });
181     expectedMapping.put(146, new int[] { 96, 722 });
182     expectedMapping.put(147, new int[] { 97, 729 });
183   }
184
185   @BeforeTest(alwaysRun = true)
186   public void setUpSiftsClient() throws SiftsException, IOException
187   {
188     // read test props before manipulating config
189     Cache.loadProperties("test/jalview/io/testProps.jvprops");
190     // SIFTs entries are updated weekly - so use saved SIFTs file to enforce
191     // test reproducibility
192     new SiftsSettings();
193     SiftsSettings.setSiftDownloadDirectory(jalview.bin.Cache.getDefault(
194             "sifts_download_dir", DEFAULT_SIFTS_DOWNLOAD_DIR));
195     SiftsSettings.setMapWithSifts(true);
196     SiftsSettings.setCacheThresholdInDays("2");
197     SiftsSettings.setFailSafePIDThreshold("70");
198     PDBfile pdbFile;
199     pdbFile = new PDBfile(false, false, false, "test/jalview/io/"
200             + testPDBId + ".pdb", DataSourceType.FILE);
201     siftsClient = new SiftsClient(pdbFile);
202   }
203
204   @AfterTest(alwaysRun = true)
205   public void cleanUpSiftsClient()
206   {
207     siftsClient = null;
208   }
209
210   @Test(groups = { "Network" })
211   public void getSIFTsFileTest() throws SiftsException, IOException
212   {
213     File siftsFile;
214     siftsFile = SiftsClient.downloadSiftsFile(testPDBId);
215     FileAssert.assertFile(siftsFile);
216     long t1 = siftsFile.lastModified();
217
218     // re-read file should be returned from cache
219     siftsFile = SiftsClient.downloadSiftsFile(testPDBId);
220     FileAssert.assertFile(siftsFile);
221     long t2 = siftsFile.lastModified();
222     assertEquals(t1, t2);
223
224     /*
225      * force fetch by having 0 expiry of cache
226      * also wait one second, because file timestamp does not
227      * give millisecond resolution :-(
228      */
229     synchronized (this)
230     {
231       try
232       {
233         wait(1000);
234       } catch (InterruptedException e)
235       {
236       }
237     }
238     SiftsSettings.setCacheThresholdInDays("0");
239     siftsFile = SiftsClient.getSiftsFile(testPDBId);
240     FileAssert.assertFile(siftsFile);
241     long t3 = siftsFile.lastModified();
242     assertTrue(t3 > t2, "file timestamp unchanged at " + t3);
243
244     SiftsSettings.setCacheThresholdInDays("2");
245   }
246
247   @Test(groups = { "Network" })
248   public void downloadSiftsFileTest() throws SiftsException, IOException
249   {
250     // Assert that file isn't yet downloaded - if already downloaded, assert it
251     // is deleted
252     Assert.assertTrue(SiftsClient.deleteSiftsFileByPDBId(testPDBId));
253     File siftsFile;
254     siftsFile = SiftsClient.downloadSiftsFile(testPDBId);
255     FileAssert.assertFile(siftsFile);
256     SiftsClient.downloadSiftsFile(testPDBId);
257   }
258
259   @Test(groups = { "Network" })
260   public void getAllMappingAccessionTest()
261   {
262     Assert.assertNotNull(siftsClient);
263     Assert.assertNotNull(siftsClient.getAllMappingAccession());
264     Assert.assertTrue(siftsClient.getAllMappingAccession().size() > 1);
265   }
266
267   @Test(groups = { "Network" })
268   public void getGreedyMappingTest()
269   {
270     Assert.assertNotNull(siftsClient);
271     Assert.assertNotNull(testSeq);
272     Assert.assertNotNull(expectedMapping);
273
274     // TODO delete when auto-fetching of DBRefEntry is implemented
275     DBRefEntry dbRef = new DBRefEntry("uniprot", "", "P00221");
276     testSeq.addDBRef(dbRef);
277     // testSeq.setSourceDBRef(dbRef);
278
279     try
280     {
281       HashMap<Integer, int[]> actualMapping = siftsClient.getGreedyMapping(
282               "A", testSeq, null);
283       Assert.assertEquals(testSeq.getStart(), 1);
284       Assert.assertEquals(testSeq.getEnd(), 147);
285       // Assert.assertEquals(actualMapping, expectedMapping);
286       Assert.assertEquals(actualMapping.size(), expectedMapping.size());
287       // Test key set
288       Assert.assertEquals(actualMapping.keySet(), expectedMapping.keySet());
289       // Test entry set
290       for (int key : expectedMapping.keySet())
291       {
292         Assert.assertTrue(Arrays.equals(expectedMapping.get(key),
293                 actualMapping.get(key)));
294       }
295     } catch (Exception e)
296     {
297       e.printStackTrace();
298       Assert.fail("Exception thrown while generating mapping...");
299     }
300   }
301
302   @Test(groups = { "Network" })
303   private void getAtomIndexTest()
304   {
305     ArrayList<Atom> atoms = new ArrayList<Atom>();
306     Atom atom = new Atom(u, u, u);
307     atom.resNumber = 43;
308     atom.atomIndex = 7;
309     atoms.add(atom);
310     int actualAtomIndex = siftsClient.getAtomIndex(1, atoms);
311     Assert.assertEquals(actualAtomIndex, -1);
312     actualAtomIndex = siftsClient.getAtomIndex(43, atoms);
313     Assert.assertEquals(actualAtomIndex, 7);
314   }
315
316   @Test(
317     groups = { "Network" },
318     expectedExceptions = IllegalArgumentException.class)
319   private void getAtomIndexNullTest()
320   {
321     siftsClient.getAtomIndex(1, null);
322   }
323
324   @Test(groups = { "Network" })
325   private void padWithGapsTest()
326   {
327
328   }
329
330   @Test(
331 groups = { "Network" },
332     expectedExceptions = StructureMappingException.class)
333   private void populateAtomPositionsNullTest1()
334           throws IllegalArgumentException, StructureMappingException
335   {
336     siftsClient.populateAtomPositions(null, null);
337   }
338
339   @Test(
340 groups = { "Network" },
341     expectedExceptions = StructureMappingException.class)
342   private void populateAtomPositionsNullTest2()
343           throws IllegalArgumentException, StructureMappingException
344   {
345     siftsClient.populateAtomPositions("A", null);
346   }
347
348   @Test(groups = { "Network" })
349   public void getValidSourceDBRefTest() throws SiftsException
350   {
351     DBRefEntryI actualValidSrcDBRef = siftsClient
352             .getValidSourceDBRef(testSeq);
353     DBRefEntryI expectedDBRef = new DBRefEntry();
354     expectedDBRef.setSource(DBRefSource.UNIPROT);
355     expectedDBRef.setAccessionId("P00221");
356     expectedDBRef.setVersion("");
357     Assert.assertEquals(actualValidSrcDBRef, expectedDBRef);
358   }
359
360   @Test(
361 groups = { "Network" },
362     expectedExceptions = SiftsException.class)
363   public void getValidSourceDBRefExceptionTest() throws SiftsException
364   {
365     SequenceI invalidTestSeq = new Sequence("testSeq", "ABCDEFGH");
366     siftsClient.getValidSourceDBRef(invalidTestSeq);
367   }
368
369   @Test(
370 groups = { "Network" },
371     expectedExceptions = SiftsException.class)
372   public void getValidSourceDBRefExceptionXTest() throws SiftsException
373   {
374     SequenceI invalidTestSeq = new Sequence("testSeq", "ABCDEFGH");
375     DBRefEntry invalidDBRef = new DBRefEntry();
376     invalidDBRef.setAccessionId("BLAR");
377     invalidTestSeq.addDBRef(invalidDBRef);
378     siftsClient.getValidSourceDBRef(invalidTestSeq);
379   }
380
381   @Test(groups = { "Network" })
382   public void isValidDBRefEntryTest()
383   {
384     DBRefEntryI validDBRef = new DBRefEntry();
385     validDBRef.setSource(DBRefSource.UNIPROT);
386     validDBRef.setAccessionId("P00221");
387     validDBRef.setVersion("");
388     Assert.assertTrue(siftsClient.isValidDBRefEntry(validDBRef));
389   }
390
391   @Test(groups = { "Network" })
392   public void getSiftsStructureMappingTest()
393           throws StructureMappingException, Exception
394   {
395     Assert.assertTrue(SiftsSettings.isMapWithSifts());
396     StructureMapping strucMapping = siftsClient.getStructureMapping(
397             testSeq, testPDBId, "A");
398     String expectedMappingOutput = "\nSequence ⟷ Structure mapping details\n"
399             + "Method: SIFTS\n\n"
400             + "P00221 :  51 - 147 Maps to \n"
401             + "1A70|A :  1 - 97\n\n"
402             + "P00221 AAYKVTLVTPTGNVEFQCPDDVYILDAAEEEGIDLPYSCRAGSCSSCAGKLKTGSLNQDDQSFLD\n"
403             + "       |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n"
404             + "1A70|A AAYKVTLVTPTGNVEFQCPDDVYILDAAEEEGIDLPYSCRAGSCSSCAGKLKTGSLNQDDQSFLD\n\n"
405
406             + "P00221 DDQIDEGWVLTCAAYPVSDVTIETHKEEELTA\n"
407             + "       |||||||||||||||||||||||||| |||||\n"
408             + "1A70|A DDQIDEGWVLTCAAYPVSDVTIETHKKEELTA\n\n" +
409
410             "Length of alignment = 97\n" + "Percentage ID = 98.97\n";
411
412     Assert.assertEquals(strucMapping.getMappingDetailsOutput(),
413             expectedMappingOutput);
414
415     // Assert.assertEquals(strucMapping.getMapping(), expectedMapping);
416     Assert.assertEquals(strucMapping.getMapping().size(),
417             expectedMapping.size());
418     // Test key set
419     Assert.assertEquals(strucMapping.getMapping().keySet(),
420             expectedMapping.keySet());
421     // Test entry set
422     for (int key : expectedMapping.keySet())
423     {
424       Assert.assertTrue(Arrays.equals(expectedMapping.get(key),
425               strucMapping.getMapping().get(key)));
426     }
427   }
428
429   @Test(groups = { "Network" })
430   public void getEntityCountTest()
431   {
432     int actualEntityCount = siftsClient.getEntityCount();
433     System.out.println("actual entity count : " + actualEntityCount);
434     Assert.assertEquals(actualEntityCount, 1);
435   }
436
437   @Test(groups = { "Network" })
438   public void getDbAccessionIdTest()
439   {
440     String actualDbAccId = siftsClient.getDbAccessionId();
441     System.out.println("Actual Db Accession Id: " + actualDbAccId);
442     Assert.assertEquals(actualDbAccId, "1a70");
443   }
444
445   @Test(groups = { "Network" })
446   public void getDbCoordSysTest()
447   {
448     String actualDbCoordSys = siftsClient.getDbCoordSys();
449     System.out.println("Actual DbCoordSys: " + actualDbCoordSys);
450     Assert.assertEquals(actualDbCoordSys, "PDBe");
451   }
452
453   @Test(groups = { "Network" })
454   public void getDbSourceTest()
455   {
456     String actualDbSource = siftsClient.getDbSource();
457     System.out.println("Actual DbSource: " + actualDbSource);
458     Assert.assertEquals(actualDbSource, "PDBe");
459   }
460
461   @Test(groups = { "Network" })
462   public void getDbVersionTest()
463   {
464     String actualDbVersion = siftsClient.getDbVersion();
465     System.out.println("Actual DbVersion: " + actualDbVersion);
466     Assert.assertEquals(actualDbVersion, "2.0");
467   }
468
469   @Test(groups = { "Network" })
470   public void getEntityByMostOptimalMatchedIdTest1() throws IOException,
471           SiftsException
472   {
473     SiftsClient siftsClientX = null;
474     PDBfile pdbFile;
475     pdbFile = new PDBfile(false, false, false, "test/jalview/io/2nq2"
476             + ".pdb", DataSourceType.FILE);
477     siftsClientX = new SiftsClient(pdbFile);
478     Entity entityA = siftsClientX.getEntityByMostOptimalMatchedId("A");
479     Assert.assertEquals(entityA.getEntityId(), "A");
480     Entity entityB = siftsClientX.getEntityByMostOptimalMatchedId("B");
481     Assert.assertEquals(entityB.getEntityId(), "C");
482     Entity entityC = siftsClientX.getEntityByMostOptimalMatchedId("C");
483     Assert.assertEquals(entityC.getEntityId(), "B");
484     Entity entityD = siftsClientX.getEntityByMostOptimalMatchedId("D");
485     Assert.assertEquals(entityD.getEntityId(), "D");
486
487   }
488
489   @Test(groups = { "Network" })
490   public void getEntityByMostOptimalMatchedIdTest2() throws IOException,
491           SiftsException
492   {
493     // This test is for a SIFTS file in which entity A should map to chain P for
494     // the given PDB Id. All the other chains shouldn't be mapped as there are
495     // no SIFTS entity records for them.
496     SiftsClient siftsClientX = null;
497     PDBfile pdbFile;
498     pdbFile = new PDBfile(false, false, false, "test/jalview/io/3ucu.cif",
499             DataSourceType.FILE);
500     siftsClientX = new SiftsClient(pdbFile);
501     Entity entityA = siftsClientX.getEntityByMostOptimalMatchedId("P");
502     Entity entityP = siftsClientX.getEntityByMostOptimalMatchedId("A");
503     Entity entityR = siftsClientX.getEntityByMostOptimalMatchedId("R");
504     Assert.assertEquals(entityA.getEntityId(), "A");
505     Assert.assertNotEquals(entityR, "A");
506     Assert.assertNotEquals(entityP, "A");
507     Assert.assertNotEquals(entityR, "R");
508     Assert.assertNotEquals(entityP, "P");
509     Assert.assertNull(entityR);
510     Assert.assertNull(entityP);
511
512   }
513 }