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