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