8bfaef0982e86d93456d5d28f567b2066f930547
[jalview.git] / test / jalview / ws / dbsources / UniprotTest.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.dbsources;
22
23 import static org.testng.Assert.assertFalse;
24 import static org.testng.AssertJUnit.assertEquals;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertNull;
27 import static org.testng.AssertJUnit.assertTrue;
28
29 import jalview.datamodel.DBRefEntry;
30 import jalview.datamodel.DBRefSource;
31 import jalview.datamodel.SequenceI;
32 import jalview.gui.JvOptionPane;
33 import jalview.util.DBRefUtils;
34 import jalview.xml.binding.uniprot.DbReferenceType;
35 import jalview.xml.binding.uniprot.Entry;
36 import jalview.xml.binding.uniprot.FeatureType;
37 import jalview.xml.binding.uniprot.LocationType;
38 import jalview.xml.binding.uniprot.PositionType;
39
40 import java.io.ByteArrayInputStream;
41 import java.io.InputStream;
42 import java.io.UnsupportedEncodingException;
43 import java.math.BigInteger;
44 import java.util.List;
45
46 import org.testng.Assert;
47 import org.testng.annotations.BeforeClass;
48 import org.testng.annotations.Test;
49
50 public class UniprotTest
51 {
52
53   @BeforeClass(alwaysRun = true)
54   public void setUpJvOptionPane()
55   {
56     JvOptionPane.setInteractiveMode(false);
57     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
58   }
59
60   // adapted from http://www.uniprot.org/uniprot/A9CKP4.xml
61   private static final String UNIPROT_XML = "<?xml version='1.0' encoding='UTF-8'?>"
62           + "<uniprot xmlns=\"http://uniprot.org/uniprot\">"
63           + "<entry dataset=\"TrEMBL\" created=\"2008-01-15\" modified=\"2015-03-04\" version=\"38\">"
64           + "<accession>A9CKP4</accession>"
65           + "<accession>A9CKP5</accession>"
66           + "<name>A9CKP4_AGRT5</name>"
67           + "<name>A9CKP4_AGRT6</name>"
68           + "<protein><recommendedName><fullName>Mitogen-activated protein kinase 13</fullName></recommendedName></protein>"
69           + "<dbReference type=\"PDB\" id=\"2FSQ\"><property type=\"method\" value=\"X-ray\"/><property type=\"resolution\" value=\"1.40\"/></dbReference>"
70           + "<dbReference type=\"PDBsum\" id=\"2FSR\"/>"
71           + "<dbReference type=\"EMBL\" id=\"AE007869\"><property type=\"protein sequence ID\" value=\"AAK85932.1\"/><property type=\"molecule type\" value=\"Genomic_DNA\"/></dbReference>"
72           + "<feature type=\"signal peptide\" evidence=\"7\"><location><begin position=\"1\"/><end position=\"18\"/></location></feature>"
73           + "<feature type=\"propeptide\" description=\"Activation peptide\" id=\"PRO_0000027399\" evidence=\"9 16 17 18\"><location><begin position=\"19\"/><end position=\"20\"/></location></feature>"
74           + "<feature type=\"chain\" description=\"Granzyme B\" id=\"PRO_0000027400\"><location><begin position=\"21\"/><end position=\"247\"/></location></feature>"
75           + "<feature type=\"sequence variant\"><original>M</original><variation>L</variation><location><position position=\"41\"/></location></feature>"
76           + "<feature type=\"sequence variant\" description=\"Pathogenic\"><original>M</original><variation>L</variation><location><position position=\"41\"/></location></feature>"
77           + "<feature type=\"sequence variant\" description=\"Pathogenic\"><original>M</original><location><position position=\"41\"/></location></feature>"
78           + "<feature type=\"sequence variant\" description=\"Foo\"><variation>L</variation><variation>LMV</variation><original>M</original><location><position position=\"42\"/></location></feature>"
79           + "<feature type=\"sequence variant\" description=\"Foo\"><variation>LL</variation><variation>LMV</variation><original>ML</original><location><begin position=\"42\"/><end position=\"43\"/></location></feature>"
80           + "<feature type=\"sequence variant\" description=\"Foo Too\"><variation>LL</variation><variation>LMVK</variation><original>MLML</original><location><begin position=\"42\"/><end position=\"45\"/></location></feature>"
81           + "<sequence length=\"10\" mass=\"27410\" checksum=\"8CB760AACF88FE6C\" modified=\"2008-01-15\" version=\"1\">MHAPL VSKDL</sequence></entry>"
82           + "</uniprot>";
83
84   /**
85    * Test the method that unmarshals XML to a Uniprot model
86    * 
87    * @throws UnsupportedEncodingException
88    */
89   @Test(groups = { "Functional" })
90   public void testGetUniprotEntries() throws UnsupportedEncodingException
91   {
92     Uniprot u = new Uniprot();
93     InputStream is = new ByteArrayInputStream(UNIPROT_XML.getBytes());
94     List<Entry> entries = u.getUniprotEntries(is);
95     assertEquals(1, entries.size());
96     Entry entry = entries.get(0);
97     assertEquals(2, entry.getName().size());
98     assertEquals("A9CKP4_AGRT5", entry.getName().get(0));
99     assertEquals("A9CKP4_AGRT6", entry.getName().get(1));
100     assertEquals(2, entry.getAccession().size());
101     assertEquals("A9CKP4", entry.getAccession().get(0));
102     assertEquals("A9CKP5", entry.getAccession().get(1));
103
104     assertEquals("MHAPL VSKDL", entry.getSequence().getValue());
105
106     assertEquals("Mitogen-activated protein kinase 13", entry.getProtein()
107             .getRecommendedName().getFullName().getValue());
108
109     /*
110      * Check sequence features
111      */
112     List<FeatureType> features = entry.getFeature();
113     assertEquals(9, features.size());
114     FeatureType sf = features.get(0);
115     assertEquals("signal peptide", sf.getType());
116     assertNull(sf.getDescription());
117     assertNull(sf.getStatus());
118     assertNull(sf.getLocation().getPosition());
119     assertEquals(1, sf.getLocation().getBegin().getPosition().intValue());
120     assertEquals(18, sf.getLocation().getEnd().getPosition().intValue());
121     sf = features.get(1);
122     assertEquals("propeptide", sf.getType());
123     assertEquals("Activation peptide", sf.getDescription());
124     assertNull(sf.getLocation().getPosition());
125     assertEquals(19, sf.getLocation().getBegin().getPosition().intValue());
126     assertEquals(20, sf.getLocation().getEnd().getPosition().intValue());
127     sf = features.get(2);
128     assertEquals("chain", sf.getType());
129     assertEquals("Granzyme B", sf.getDescription());
130     assertNull(sf.getLocation().getPosition());
131     assertEquals(21, sf.getLocation().getBegin().getPosition().intValue());
132     assertEquals(247, sf.getLocation().getEnd().getPosition().intValue());
133
134     sf = features.get(3);
135     assertEquals("sequence variant", sf.getType());
136     assertNull(sf.getDescription());
137     assertEquals(41,
138             sf.getLocation().getPosition().getPosition().intValue());
139     assertNull(sf.getLocation().getBegin());
140     assertNull(sf.getLocation().getEnd());
141
142     sf = features.get(4);
143     assertEquals("sequence variant", sf.getType());
144     assertEquals("Pathogenic", sf.getDescription());
145     assertEquals(41,
146             sf.getLocation().getPosition().getPosition().intValue());
147     assertNull(sf.getLocation().getBegin());
148     assertNull(sf.getLocation().getEnd());
149
150     sf = features.get(5);
151     assertEquals("sequence variant", sf.getType());
152     assertEquals("Pathogenic", sf.getDescription());
153     assertEquals(41,
154             sf.getLocation().getPosition().getPosition().intValue());
155     assertNull(sf.getLocation().getBegin());
156     assertNull(sf.getLocation().getEnd());
157
158     sf = features.get(6);
159     assertEquals("sequence variant", sf.getType());
160     assertEquals("Foo",
161             sf.getDescription());
162     assertEquals(42,
163             sf.getLocation().getPosition().getPosition().intValue());
164     assertNull(sf.getLocation().getBegin());
165     assertNull(sf.getLocation().getEnd());
166     Assert.assertEquals(Uniprot.getDescription(sf),
167             "<html>p.Met42Leu" + "<br/>&nbsp;&nbsp;"
168                     + "p.Met42LeuMetVal Foo</html>");
169
170     sf = features.get(7);
171     assertNull(sf.getLocation().getPosition());
172     assertEquals(42, sf.getLocation().getBegin().getPosition().intValue());
173     assertEquals(43, sf.getLocation().getEnd().getPosition().intValue());
174     Assert.assertEquals(Uniprot.getDescription(sf),
175             "<html>p.MetLeu42LeuLeu" + "<br/>&nbsp;&nbsp;"
176                     + "p.MetLeu42LeuMetVal Foo</html>");
177
178     sf = features.get(8);
179     assertNull(sf.getLocation().getPosition());
180     assertEquals(42, sf.getLocation().getBegin().getPosition().intValue());
181     assertEquals(45, sf.getLocation().getEnd().getPosition().intValue());
182     Assert.assertEquals(Uniprot.getDescription(sf),
183             "<html>p.MLML42LeuLeu" + "<br/>&nbsp;&nbsp;"
184                     + "p.MLML42LMVK Foo Too</html>");
185
186     /*
187      * Check cross-references
188      */
189     List<DbReferenceType> xrefs = entry.getDbReference();
190     assertEquals(3, xrefs.size());
191
192     DbReferenceType xref = xrefs.get(0);
193     assertEquals("2FSQ", xref.getId());
194     assertEquals("PDB", xref.getType());
195     assertEquals("X-ray",
196             Uniprot.getProperty(xref.getProperty(), "method"));
197     assertEquals("1.40",
198             Uniprot.getProperty(xref.getProperty(), "resolution"));
199
200     xref = xrefs.get(1);
201     assertEquals("2FSR", xref.getId());
202     assertEquals("PDBsum", xref.getType());
203     assertTrue(xref.getProperty().isEmpty());
204
205     xref = xrefs.get(2);
206     assertEquals("AE007869", xref.getId());
207     assertEquals("EMBL", xref.getType());
208     assertEquals("AAK85932.1",
209             Uniprot.getProperty(xref.getProperty(), "protein sequence ID"));
210     assertEquals("Genomic_DNA",
211             Uniprot.getProperty(xref.getProperty(), "molecule type"));
212   }
213
214   @Test(groups = { "Functional" })
215   public void testGetUniprotSequence() throws UnsupportedEncodingException
216   {
217     InputStream is = new ByteArrayInputStream(UNIPROT_XML.getBytes());
218     Entry entry = new Uniprot().getUniprotEntries(
219             is).get(0);
220     SequenceI seq = new Uniprot().uniprotEntryToSequence(entry);
221     assertNotNull(seq);
222     assertEquals(6, seq.getDBRefs().size()); // 2*Uniprot, PDB, PDBsum, 2*EMBL
223     assertEquals(seq.getSequenceAsString(),
224             seq.createDatasetSequence().getSequenceAsString());
225     assertEquals(2,seq.getPrimaryDBRefs().size());
226     List<DBRefEntry> res = DBRefUtils.searchRefs(seq.getPrimaryDBRefs(), "A9CKP4");
227     assertEquals(1,res.size());
228     assertTrue(res.get(0).isCanonical());
229     res = DBRefUtils.searchRefsForSource(seq.getDBRefs(), DBRefSource.UNIPROT);
230     assertEquals(2,res.size());
231     /*
232      * NB this test fragile - relies on ordering being preserved
233      */
234     assertTrue(res.get(0).isCanonical());
235     assertFalse(res.get(1).isCanonical());
236   }
237
238   /**
239    * Test the method that formats the sequence id
240    * 
241    * @throws UnsupportedEncodingException
242    */
243   @Test(groups = { "Functional" })
244   public void testGetUniprotEntryId() throws UnsupportedEncodingException
245   {
246     InputStream is = new ByteArrayInputStream(UNIPROT_XML.getBytes());
247     Entry entry = new Uniprot().getUniprotEntries(is).get(0);
248
249     /*
250      * name formatted with Uniprot Entry name
251      */
252     String expectedName = "A9CKP4_AGRT5|A9CKP4_AGRT6";
253     assertEquals(expectedName,
254             Uniprot.getUniprotEntryId(entry));
255   }
256
257   /**
258    * Test the method that formats the sequence description
259    * 
260    * @throws UnsupportedEncodingException
261    */
262   @Test(groups = { "Functional" })
263   public void testGetUniprotEntryDescription()
264           throws UnsupportedEncodingException
265   {
266     InputStream is = new ByteArrayInputStream(UNIPROT_XML.getBytes());
267     Entry entry = new Uniprot().getUniprotEntries(is).get(0);
268
269     assertEquals("Mitogen-activated protein kinase 13",
270             Uniprot.getUniprotEntryDescription(entry));
271   }
272
273   @Test(groups = { "Functional" })
274   public void testGetDescription()
275   {
276     FeatureType ft = new FeatureType();
277     assertEquals("", Uniprot.getDescription(ft));
278
279     ft.setDescription("Hello");
280     assertEquals("Hello", Uniprot.getDescription(ft));
281
282     ft.setLocation(new LocationType());
283     ft.getLocation().setPosition(new PositionType());
284     ft.getLocation().getPosition().setPosition(BigInteger.valueOf(23));
285     ft.setOriginal("K");
286     ft.getVariation().add("y");
287     assertEquals("p.Lys23Tyr Hello", Uniprot.getDescription(ft));
288
289     // multiple variants generate an html description over more than one line
290     ft.getVariation().add("W");
291     assertEquals("<html>p.Lys23Tyr<br/>&nbsp;&nbsp;p.Lys23Trp Hello</html>",
292             Uniprot.getDescription(ft));
293
294     /*
295      * indel cases
296      * up to 3 bases (original or variant) are shown using 3 letter code
297      */
298     ft.getVariation().clear();
299     ft.getVariation().add("KWE");
300     ft.setOriginal("KLS");
301     assertEquals("p.LysLeuSer23LysTrpGlu Hello",
302             Uniprot.getDescription(ft));
303
304     // adding a fourth original base switches to single letter code
305     ft.setOriginal("KLST");
306     assertEquals("p.KLST23LysTrpGlu Hello", Uniprot.getDescription(ft));
307
308     // adding a fourth variant switches to single letter code
309     ft.getVariation().clear();
310     ft.getVariation().add("KWES");
311     assertEquals("p.KLST23KWES Hello", Uniprot.getDescription(ft));
312
313     ft.getVariation().clear();
314     ft.getVariation().add("z"); // unknown variant - fails gracefully
315     ft.setOriginal("K");
316     assertEquals("p.Lys23z Hello", Uniprot.getDescription(ft));
317
318     ft.getVariation().clear(); // variant missing - is ignored
319     assertEquals("Hello", Uniprot.getDescription(ft));
320   }
321 }