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