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