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