JAL-1926 JAL-2106 remove startRes/endRes fields - not used
[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 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", AppletFormatAdapter.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     testSeq.addDBRef(dbRef);
255     // testSeq.setSourceDBRef(dbRef);
256
257     try
258     {
259       HashMap<Integer, int[]> actualMapping = siftsClient.getGreedyMapping(
260               "A", testSeq,
261               null);
262       Assert.assertEquals(testSeq.getStart(), 1);
263       Assert.assertEquals(testSeq.getEnd(), 147);
264       Assert.assertEquals(actualMapping, expectedMapping);
265     } catch (Exception e)
266     {
267       e.printStackTrace();
268       Assert.fail("Exception thrown while generating mapping...");
269     }
270   }
271
272   @Test(groups = { "Functional" })
273   private void getAtomIndexTest()
274   {
275     ArrayList<Atom> atoms = new ArrayList<Atom>();
276     Atom atom = new Atom(u, u, u);
277     atom.resNumber = 43;
278     atom.atomIndex = 7;
279     atoms.add(atom);
280     int actualAtomIndex = siftsClient.getAtomIndex(1, atoms);
281     Assert.assertEquals(actualAtomIndex, -1);
282     actualAtomIndex = siftsClient.getAtomIndex(43, atoms);
283     Assert.assertEquals(actualAtomIndex, 7);
284   }
285
286   @Test(
287     groups = { "Functional" },
288     expectedExceptions = IllegalArgumentException.class)
289   private void getAtomIndexNullTest()
290   {
291     siftsClient.getAtomIndex(1, null);
292   }
293
294   @Test(groups = { "Functional" })
295   private void padWithGapsTest()
296   {
297
298   }
299
300   @Test(
301     groups = { "Functional" },
302     expectedExceptions = SiftsException.class)
303   private void populateAtomPositionsNullTest1()
304           throws IllegalArgumentException, SiftsException
305   {
306       siftsClient.populateAtomPositions(null, null);
307   }
308
309   @Test(
310     groups = { "Functional" },
311     expectedExceptions = SiftsException.class)
312   private void populateAtomPositionsNullTest2()
313           throws IllegalArgumentException, SiftsException
314   {
315     siftsClient.populateAtomPositions("A", null);
316   }
317
318   @Test(groups = { "Functional" })
319   public void getValidSourceDBRefTest()
320   {
321     try
322     {
323       DBRefEntryI actualValidSrcDBRef = siftsClient
324               .getValidSourceDBRef(testSeq);
325       DBRefEntryI expectedDBRef = new DBRefEntry();
326       expectedDBRef.setSource(DBRefSource.UNIPROT);
327       expectedDBRef.setAccessionId("P00221");
328       expectedDBRef.setVersion("");
329       Assert.assertEquals(actualValidSrcDBRef, expectedDBRef);
330     } catch (Exception e)
331     {
332     }
333   }
334
335   @Test(
336     groups = { "Functional" },
337     expectedExceptions = SiftsException.class)
338   public void getValidSourceDBRefExceptionTest() throws SiftsException
339   {
340       SequenceI invalidTestSeq = new Sequence("testSeq", "ABCDEFGH");
341     try
342     {
343       siftsClient.getValidSourceDBRef(invalidTestSeq);
344     } catch (SiftsException e)
345     {
346       throw new SiftsException(e.getMessage());
347     }
348   }
349
350   @Test(
351     groups = { "Functional" },
352     expectedExceptions = SiftsException.class)
353   public void getValidSourceDBRefExceptionXTest() throws SiftsException
354   {
355     SequenceI invalidTestSeq = new Sequence("testSeq", "ABCDEFGH");
356     DBRefEntry invalidDBRef = new DBRefEntry();
357     invalidDBRef.setAccessionId("BLAR");
358     invalidTestSeq.addDBRef(invalidDBRef);
359     try
360     {
361       siftsClient.getValidSourceDBRef(invalidTestSeq);
362     } catch (SiftsException e)
363     {
364       throw new SiftsException(e.getMessage());
365     }
366
367   }
368
369   @Test(groups = { "Functional" })
370   public void isValidDBRefEntryTest()
371   {
372     DBRefEntryI validDBRef = new DBRefEntry();
373     validDBRef.setSource(DBRefSource.UNIPROT);
374     validDBRef.setAccessionId("P00221");
375     validDBRef.setVersion("");
376     Assert.assertTrue(siftsClient.isValidDBRefEntry(validDBRef));
377   }
378
379   @Test(groups = { "Functional" })
380   public void getSiftsStructureMappingTest()
381   {
382     try
383     {
384       Assert.assertTrue(SiftsSettings.isMapWithSifts());
385       StructureMapping strucMapping = siftsClient.getSiftsStructureMapping(
386               testSeq, testPDBId, "A");
387       String expectedMappingOutput = "\nSequence ⟷ Structure mapping details\n"
388               + "Method: SIFTS\n\n"
389               + "P00221 :  1 - 97 Maps to \n"
390               + "1A70|A :  51 - 147\n\n"
391               + "P00221 AAYKVTLVTPTGNVEFQCPDDVYILDAAEEEGIDLPYSCRAGSCSSCAGKLKTGSLNQDDQSFLD\n"
392               + "       |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n"
393               + "1A70|A AAYKVTLVTPTGNVEFQCPDDVYILDAAEEEGIDLPYSCRAGSCSSCAGKLKTGSLNQDDQSFLD\n\n"
394
395               + "P00221 DDQIDEGWVLTCAAYPVSDVTIETHKEEELTA\n"
396               + "       |||||||||||||||||||||||||| |||||\n"
397               + "1A70|A DDQIDEGWVLTCAAYPVSDVTIETHKKEELTA\n\n" +
398
399               "Length of alignment = 97\n" + "Percentage ID = 98.97\n";
400
401       Assert.assertEquals(strucMapping.getMappingDetailsOutput(),
402               expectedMappingOutput);
403       Assert.assertEquals(strucMapping.getMapping(), expectedMapping);
404     } catch (SiftsException e)
405     {
406       e.printStackTrace();
407     }
408   }
409
410   @Test(groups = { "Functional" })
411   public void getEntityCountTest()
412   {
413     int actualEntityCount = siftsClient.getEntityCount();
414     System.out.println("actual entity count : " + actualEntityCount);
415     Assert.assertEquals(actualEntityCount, 1);
416   }
417
418   @Test(groups = { "Functional" })
419   public void getDbAccessionIdTest()
420   {
421     String actualDbAccId = siftsClient.getDbAccessionId();
422     System.out.println("Actual Db Accession Id: " + actualDbAccId);
423     Assert.assertEquals(actualDbAccId, "1a70");
424   }
425
426   @Test(groups = { "Functional" })
427   public void getDbCoordSysTest()
428   {
429     String actualDbCoordSys = siftsClient.getDbCoordSys();
430     System.out.println("Actual DbCoordSys: " + actualDbCoordSys);
431     Assert.assertEquals(actualDbCoordSys, "PDBe");
432   }
433
434   @Test(groups = { "Functional" })
435   public void getDbSourceTest()
436   {
437     String actualDbSource = siftsClient.getDbSource();
438     System.out.println("Actual DbSource: " + actualDbSource);
439     Assert.assertEquals(actualDbSource, "PDBe");
440   }
441
442   @Test(groups = { "Functional" })
443   public void getDbVersionTest()
444   {
445     String actualDbVersion = siftsClient.getDbVersion();
446     System.out.println("Actual DbVersion: " + actualDbVersion);
447     Assert.assertEquals(actualDbVersion, "2.0");
448   }
449
450   @Test(groups = { "Functional" })
451   public void getEntityByMostOptimalMatchedIdTest()
452   {
453     SiftsClient siftsClientX = null;
454     PDBfile pdbFile;
455     try
456     {
457       pdbFile = new PDBfile(false, false, false, "test/jalview/io/2nq2"
458               + ".pdb", AppletFormatAdapter.FILE);
459       siftsClientX = new SiftsClient(pdbFile);
460     } catch (Exception e)
461     {
462       e.printStackTrace();
463     }
464     Entity entityA = siftsClientX.getEntityByMostOptimalMatchedId("A");
465     Assert.assertEquals(entityA.getEntityId(), "A");
466     Entity entityB = siftsClientX.getEntityByMostOptimalMatchedId("B");
467     Assert.assertEquals(entityB.getEntityId(), "C");
468     Entity entityC = siftsClientX.getEntityByMostOptimalMatchedId("C");
469     Assert.assertEquals(entityC.getEntityId(), "B");
470     Entity entityD = siftsClientX.getEntityByMostOptimalMatchedId("D");
471     Assert.assertEquals(entityD.getEntityId(), "D");
472
473   }
474 }