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