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