JAL-1957 JAL-1479 Further optimisation of SIFTs Client and addition of support for...
[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.datamodel.DBRefEntry;
24 import jalview.datamodel.Sequence;
25 import jalview.datamodel.SequenceI;
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.File;
29 import java.io.PrintStream;
30
31 import org.testng.Assert;
32 import org.testng.FileAssert;
33 import org.testng.annotations.AfterTest;
34 import org.testng.annotations.BeforeTest;
35 import org.testng.annotations.Test;
36
37 import MCview.PDBfile;
38
39 public class SiftsClientTest
40 {
41   private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
42
43   private String testPDBId = "1a70";
44
45   private SiftsClient siftsClient = null;
46
47   SequenceI testSeq = new Sequence(
48           "P00221",
49           "MAAT..TTTMMG..MATTFVPKPQAPPMMAALPSNTGR..SLFGLKT.GSR..GGRMTMA"
50                   + "AYKVTLVTPTGNVEFQCPDDVYILDAAEEEGIDLPYSCRAGSCSSCAGKLKTGSLNQDD"
51                   + "QSFLDDDQIDEGWVLTCAAYPVSDVTIETHKEEELTA.", 1, 147);
52
53   int u = SiftsClient.UNASSIGNED;
54
55   int[][] expectedMapping = { { u, u }, { u, u }, { u, u }, { u, u },
56       { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { u, u },
57       { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { u, u },
58       { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { u, u },
59       { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { u, u },
60       { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { u, u },
61       { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { u, u },
62       { u, u }, { u, u }, { u, u }, { u, u }, { u, u }, { 1, u }, { 2, u },
63       { 3, u }, { 4, u }, { 5, u }, { 6, u }, { 7, u }, { 8, u }, { 9, u },
64       { 10, u }, { 11, u }, { 12, u }, { 13, u }, { 14, u }, { 15, u },
65       { 16, u }, { 17, u }, { 18, u }, { 19, u }, { 20, u }, { 21, u },
66       { 22, u }, { 23, u }, { 24, u }, { 25, u }, { 26, u }, { 27, u },
67       { 28, u }, { 29, u }, { 30, u }, { 31, u }, { 32, u }, { 33, u },
68       { 34, u }, { 35, u }, { 36, u }, { 37, u }, { 38, u }, { 39, u },
69       { 40, u }, { 41, u }, { 42, u }, { 43, u }, { 44, u }, { 45, u },
70       { 46, u }, { 47, u }, { 48, u }, { 49, u }, { 50, u }, { 51, u },
71       { 52, u }, { 53, u }, { 54, u }, { 55, u }, { 56, u }, { 57, u },
72       { 58, u }, { 59, u }, { 60, u }, { 61, u }, { 62, u }, { 63, u },
73       { 64, u }, { 65, u }, { 66, u }, { 67, u }, { 68, u }, { 69, u },
74       { 70, u }, { 71, u }, { 72, u }, { 73, u }, { 74, u }, { 75, u },
75       { 76, u }, { 77, u }, { 78, u }, { 79, u }, { 80, u }, { 81, u },
76       { 82, u }, { 83, u }, { 84, u }, { 85, u }, { 86, u }, { 87, u },
77       { 88, u }, { 89, u }, { 90, u }, { 91, u }, { 92, u }, { 93, u },
78       { 94, u }, { 95, u }, { 96, u }, { 97, u } };
79
80   @BeforeTest(alwaysRun = true)
81   public void setUpSiftsClient() throws SiftsException
82   {
83     // SIFTs entries are updated weekly - so use saved SIFTs file to enforce
84     // test reproducibility
85     File testSiftsFile = new File("test/jalview/io/" + testPDBId
86             + ".xml.gz");
87     PDBfile pdbFile = new PDBfile(false, false, false);
88     siftsClient = new SiftsClient(pdbFile, testSiftsFile);
89   }
90
91   @AfterTest(alwaysRun = true)
92   public void cleanUpSiftsClient()
93   {
94     siftsClient = null;
95   }
96
97   @BeforeTest(alwaysRun = true)
98   public void setUpStreams()
99   {
100     System.setOut(new PrintStream(outContent));
101   }
102
103   @AfterTest(alwaysRun = true)
104   public void cleanUpStreams()
105   {
106     System.setOut(null);
107   }
108
109   @Test(groups = { "Functional" })
110   public void getSIFTsFileTest()
111   {
112     Assert.assertTrue(SiftsClient.deleteSiftsFileByPDBId(testPDBId));
113     SiftsClient.getSiftsFile(testPDBId);
114     Assert.assertFalse(outContent.toString().contains(
115             ">>> SIFTS File already downloaded for " + testPDBId));
116
117     // test for SIFTs file caching
118     SiftsClient.getSiftsFile(testPDBId);
119     Assert.assertTrue(outContent.toString().contains(
120             ">>> SIFTS File already downloaded for " + testPDBId));
121   }
122
123   @Test(groups = { "Functional" })
124   public void downloadSiftsFileTest()
125   {
126     // Assert that file isn't yet downloaded - if already downloaded, assert it
127     // is deleted
128     Assert.assertTrue(SiftsClient.deleteSiftsFileByPDBId(testPDBId));
129     File siftsFile = SiftsClient.downloadSiftsFile(testPDBId);
130     FileAssert.assertFile(siftsFile);
131     SiftsClient.downloadSiftsFile(testPDBId);
132   }
133
134   @Test(groups = { "Functional" })
135   public void getAllMappingAccessionTest()
136   {
137     Assert.assertNotNull(siftsClient);
138     Assert.assertNotNull(siftsClient.getAllMappingAccession());
139     Assert.assertTrue(siftsClient.getAllMappingAccession().size() > 1);
140   }
141
142   @Test(groups = { "Functional" })
143   public void getGreedyMappingTest()
144   {
145     Assert.assertNotNull(siftsClient);
146     Assert.assertNotNull(testSeq);
147     Assert.assertNotNull(expectedMapping);
148
149     // TODO delete when auto-fetching of DBRefEntry is implemented
150     DBRefEntry dbRef = new DBRefEntry("uniprot", "", "P00221");
151     dbRef.setStartRes(1);
152     dbRef.setEndRes(147);
153     testSeq.addDBRef(dbRef);
154     // testSeq.setSourceDBRef(dbRef);
155
156     try
157     {
158       int[][] actualMapping = siftsClient.getGreedyMapping("A", testSeq,
159               null);
160       Assert.assertEquals(actualMapping, expectedMapping);
161       Assert.assertEquals(testSeq.getStart(), 1);
162       Assert.assertEquals(testSeq.getEnd(), 147);
163     } catch (Exception e)
164     {
165       e.printStackTrace();
166       Assert.fail("Exception thrown while generating mapping...");
167     }
168   }
169
170   @Test(groups = { "Functional" })
171   private void getAtomIndexTest()
172   {
173     // siftsClient.getAtomIndex(1, null);
174     // Assert.assertTrue(true);
175   }
176
177   @Test(
178     groups = { "Functional" },
179     expectedExceptions = IllegalArgumentException.class)
180   private void getAtomIndexNullTest()
181   {
182     siftsClient.getAtomIndex(1, null);
183   }
184
185   @Test(groups = { "Functional" })
186   private void padWithGapsTest()
187   {
188
189   }
190
191   @Test(groups = { "Functional" })
192   private void populateAtomPositionsTest()
193   {
194
195   }
196
197   @Test(groups = { "Functional" })
198   public void getValidSourceDBRefTest()
199   {
200
201   }
202
203   @Test(groups = { "Functional" })
204   public void isValidDBRefEntryTest()
205   {
206
207   }
208 }