JAL-2722 unit test
[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.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertNull;
27
28 import jalview.datamodel.PDBEntry;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceI;
31 import jalview.datamodel.UniprotEntry;
32 import jalview.gui.JvOptionPane;
33
34 import java.io.Reader;
35 import java.io.StringReader;
36 import java.util.Vector;
37
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.Test;
40
41 public class UniprotTest
42 {
43
44   @BeforeClass(alwaysRun = true)
45   public void setUpJvOptionPane()
46   {
47     JvOptionPane.setInteractiveMode(false);
48     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
49   }
50
51   // adapted from http://www.uniprot.org/uniprot/A9CKP4.xml
52   private static final String UNIPROT_XML = "<?xml version='1.0' encoding='UTF-8'?>"
53           + "<uniprot>"
54           + "<entry dataset=\"TrEMBL\" created=\"2008-01-15\" modified=\"2015-03-04\" version=\"38\">"
55           + "<accession>A9CKP4</accession>"
56           + "<accession>A9CKP5</accession>"
57           + "<name>A9CKP4_AGRT5</name>"
58           + "<name>A9CKP4_AGRT6</name>"
59           + "<protein><recommendedName><fullName>Mitogen-activated protein kinase 13</fullName><fullName>Henry</fullName></recommendedName></protein>"
60           + "<dbReference type=\"PDB\" id=\"2FSQ\"><property type=\"method\" value=\"X-ray\"/><property type=\"resolution\" value=\"1.40\"/></dbReference>"
61           + "<dbReference type=\"PDBsum\" id=\"2FSR\"/>"
62           + "<dbReference type=\"EMBL\" id=\"AE007869\"><property type=\"protein sequence ID\" value=\"AAK85932.1\"/><property type=\"molecule type\" value=\"Genomic_DNA\"/></dbReference>"
63           + "<feature type=\"signal peptide\" evidence=\"7\"><location><begin position=\"1\"/><end position=\"18\"/></location></feature>"
64           + "<feature type=\"propeptide\" description=\"Activation peptide\" id=\"PRO_0000027399\" evidence=\"9 16 17 18\"><location><begin position=\"19\"/><end position=\"20\"/></location></feature>"
65           + "<feature type=\"chain\" description=\"Granzyme B\" id=\"PRO_0000027400\"><location><begin position=\"21\"/><end position=\"247\"/></location></feature>"
66           + "<sequence length=\"10\" mass=\"27410\" checksum=\"8CB760AACF88FE6C\" modified=\"2008-01-15\" version=\"1\">MHAPL VSKDL</sequence></entry>"
67           + "</uniprot>";
68
69   /**
70    * Test the method that unmarshals XML to a Uniprot model
71    */
72   @Test(groups = { "Functional" })
73   public void testGetUniprotEntries()
74   {
75     Uniprot u = new Uniprot();
76     Reader reader = new StringReader(UNIPROT_XML);
77     Vector<UniprotEntry> entries = u.getUniprotEntries(reader);
78     assertEquals(1, entries.size());
79     UniprotEntry entry = entries.get(0);
80     assertEquals(2, entry.getName().size());
81     assertEquals("A9CKP4_AGRT5", entry.getName().get(0));
82     assertEquals("A9CKP4_AGRT6", entry.getName().get(1));
83     assertEquals(2, entry.getAccession().size());
84     assertEquals("A9CKP4", entry.getAccession().get(0));
85     assertEquals("A9CKP5", entry.getAccession().get(1));
86
87     /*
88      * UniprotSequence drops any space characters
89      */
90     assertEquals("MHAPLVSKDL", entry.getUniprotSequence().getContent());
91
92     assertEquals(2, entry.getProtein().getName().size());
93     assertEquals("Mitogen-activated protein kinase 13", entry.getProtein()
94             .getName().get(0));
95     assertEquals("Henry", entry.getProtein().getName().get(1));
96
97     /*
98      * Check sequence features
99      */
100     Vector<SequenceFeature> features = entry.getFeature();
101     assertEquals(3, features.size());
102     SequenceFeature sf = features.get(0);
103     assertEquals("signal peptide", sf.getType());
104     assertNull(sf.getDescription());
105     assertNull(sf.getStatus());
106     assertEquals(1, sf.getPosition());
107     assertEquals(1, sf.getBegin());
108     assertEquals(18, sf.getEnd());
109     sf = features.get(1);
110     assertEquals("propeptide", sf.getType());
111     assertEquals("Activation peptide", sf.getDescription());
112     assertEquals(19, sf.getPosition());
113     assertEquals(19, sf.getBegin());
114     assertEquals(20, sf.getEnd());
115     sf = features.get(2);
116     assertEquals("chain", sf.getType());
117     assertEquals("Granzyme B", sf.getDescription());
118     assertEquals(21, sf.getPosition());
119     assertEquals(21, sf.getBegin());
120     assertEquals(247, sf.getEnd());
121
122     /*
123      * Check cross-references
124      */
125     Vector<PDBEntry> xrefs = entry.getDbReference();
126     assertEquals(3, xrefs.size());
127
128     PDBEntry xref = xrefs.get(0);
129     assertEquals("2FSQ", xref.getId());
130     assertEquals("PDB", xref.getType());
131     assertEquals("X-ray", xref.getProperty("method"));
132     assertEquals("1.40", xref.getProperty("resolution"));
133
134     xref = xrefs.get(1);
135     assertEquals("2FSR", xref.getId());
136     assertEquals("PDBsum", xref.getType());
137     assertFalse(xref.getProperties().hasMoreElements());
138
139     xref = xrefs.get(2);
140     assertEquals("AE007869", xref.getId());
141     assertEquals("EMBL", xref.getType());
142     assertEquals("AAK85932.1",
143  xref.getProperty("protein sequence ID"));
144     assertEquals("Genomic_DNA",
145  xref.getProperty("molecule type"));
146   }
147
148   @Test(groups = { "Functional" })
149   public void testGetUniprotSequence()
150   {
151     UniprotEntry entry = new Uniprot().getUniprotEntries(
152             new StringReader(UNIPROT_XML)).get(0);
153     SequenceI seq = new Uniprot().uniprotEntryToSequenceI(entry);
154     assertNotNull(seq);
155     assertEquals(6, seq.getDBRefs().length); // 2*Uniprot, PDB, PDBsum, 2*EMBL
156
157   }
158
159   /**
160    * Test the method that formats the sequence id
161    */
162   @Test(groups = { "Functional" })
163   public void testGetUniprotEntryId()
164   {
165     UniprotEntry entry = new Uniprot().getUniprotEntries(
166             new StringReader(UNIPROT_XML)).get(0);
167
168     /*
169      * name formatted as source | accession ids | names
170      * source database converted to Jalview canonical name
171      */
172     String expectedName = "UNIPROT|A9CKP4|A9CKP5|A9CKP4_AGRT5|A9CKP4_AGRT6";
173     assertEquals(expectedName, Uniprot.getUniprotEntryId(entry));
174   }
175
176   /**
177    * Test the method that formats the sequence description
178    */
179   @Test(groups = { "Functional" })
180   public void testGetUniprotEntryDescription()
181   {
182     UniprotEntry entry = new Uniprot().getUniprotEntries(
183             new StringReader(UNIPROT_XML)).get(0);
184
185     /*
186      * recommended names concatenated with space separator
187      */
188     String expectedDescription = "Mitogen-activated protein kinase 13 Henry";
189     assertEquals(expectedDescription,
190             Uniprot.getUniprotEntryDescription(entry));
191   }
192 }