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