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