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