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