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