2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNull;
25 import static org.testng.AssertJUnit.assertSame;
26 import static org.testng.AssertJUnit.assertTrue;
28 import jalview.datamodel.DBRefEntry;
29 import jalview.datamodel.DBRefSource;
30 import jalview.datamodel.Mapping;
31 import jalview.datamodel.PDBEntry;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceI;
34 import jalview.gui.JvOptionPane;
36 import java.util.Arrays;
37 import java.util.List;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
42 public class DBRefUtilsTest
45 @BeforeClass(alwaysRun = true)
46 public void setUpJvOptionPane()
48 JvOptionPane.setInteractiveMode(false);
49 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
53 * Test the method that selects DBRefEntry items whose source is in a supplied
56 @Test(groups = { "Functional" })
57 public void testSelectRefs()
59 assertNull(DBRefUtils.selectRefs(null, null));
60 assertNull(DBRefUtils.selectRefs(null, DBRefSource.CODINGDBS));
62 DBRefEntry ref1 = new DBRefEntry("EMBL", "1.2", "A12345");
63 DBRefEntry ref2 = new DBRefEntry("UNIPROT", "1.2", "A12346");
64 // Source is converted to upper-case by this constructor!
65 DBRefEntry ref3 = new DBRefEntry("Uniprot", "1.2", "A12347");
66 List<DBRefEntry> dbrefs = Arrays
67 .asList(new DBRefEntry[]
68 { ref1, ref2, ref3 });
69 String[] sources = new String[] { "EMBL", "UNIPROT" };
71 List<DBRefEntry> selected = DBRefUtils.selectRefs(dbrefs, sources);
72 assertEquals(3, selected.size());
73 assertSame(ref1, selected.get(0));
74 assertSame(ref2, selected.get(1));
75 assertSame(ref3, selected.get(2));
77 sources = new String[] { "EMBL" };
78 selected = DBRefUtils.selectRefs(dbrefs, sources);
79 assertEquals(1, selected.size());
80 assertSame(ref1, selected.get(0));
82 sources = new String[] { "UNIPROT" };
83 selected = DBRefUtils.selectRefs(dbrefs, sources);
84 assertEquals(2, selected.size());
85 assertSame(ref2, selected.get(0));
86 assertSame(ref3, selected.get(1));
88 sources = new String[] { "EMBLCDS" };
89 selected = DBRefUtils.selectRefs(dbrefs, sources);
92 sources = new String[] { "embl", "uniprot" };
93 selected = DBRefUtils.selectRefs(dbrefs, sources);
94 assertEquals(3, selected.size());
95 assertSame(ref1, selected.get(0));
96 assertSame(ref2, selected.get(1));
97 assertSame(ref3, selected.get(2));
101 * Test the method that converts (currently three) database names to a
102 * canonical name (not case-sensitive)
104 @Test(groups = { "Functional" })
105 public void testGetCanonicalName()
107 assertNull(DBRefUtils.getCanonicalName(null));
108 assertEquals("", DBRefUtils.getCanonicalName(""));
109 assertEquals("PDB", DBRefUtils.getCanonicalName("pdb"));
110 assertEquals("PDB", DBRefUtils.getCanonicalName("Pdb"));
111 assertEquals("UNIPROT",
112 DBRefUtils.getCanonicalName("uniprotkb/swiss-prot"));
113 assertEquals("UNIPROT",
114 DBRefUtils.getCanonicalName("uniprotkb/trembl"));
115 assertEquals("UNIPROT",
116 DBRefUtils.getCanonicalName("UNIPROTKB/SWISS-PROT"));
117 assertEquals("UNIPROT",
118 DBRefUtils.getCanonicalName("UNIPROTKB/TREMBL"));
119 assertEquals("UNIPROTKB/SWISS-CHEESE",
120 DBRefUtils.getCanonicalName("UNIPROTKB/SWISS-CHEESE"));
121 assertEquals("ENSEMBL", DBRefUtils.getCanonicalName("Ensembl"));
123 // these are not 'known' to Jalview
124 assertEquals("PFAM", DBRefUtils.getCanonicalName("PFAM"));
125 assertEquals("pfam", DBRefUtils.getCanonicalName("pfam"));
130 * Test 'parsing' a DBRef - non PDB case
132 @Test(groups = { "Functional" })
133 public void testParseToDbRef()
135 SequenceI seq = new Sequence("Seq1", "ABCD");
136 DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "EMBL", "1.2", "a7890");
137 List<DBRefEntry> refs = seq.getDBRefs();
138 assertEquals(1, refs.size());
139 assertSame(ref, refs.get(0));
140 assertEquals("EMBL", ref.getSource());
141 assertEquals("1.2", ref.getVersion());
142 assertEquals("a7890", ref.getAccessionId());
143 assertTrue(seq.getAllPDBEntries().isEmpty());
144 SequenceI seq2 = new Sequence("Seq2", "ABCD");
145 // Check that whitespace doesn't confuse parseToDbRef
146 DBRefEntry ref2 = DBRefUtils.parseToDbRef(seq2, "EMBL", "1.2",
148 assertEquals(ref, ref2);
152 * Test 'parsing' a DBRef - Stockholm PDB format
154 @Test(groups = { "Functional" })
155 public void testParseToDbRef_PDB()
157 SequenceI seq = new Sequence("Seq1", "ABCD");
158 DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "pdb", "1.2",
160 // TODO: correct PDBEntry and PDB DBRef accessions need to be generated for
161 // PDB ref in Stockholm
163 List<DBRefEntry> refs = seq.getDBRefs();
164 assertEquals(1, refs.size());
165 assertSame(ref, refs.get(0));
166 assertEquals("PDB", ref.getSource());
167 assertEquals("1.2", ref.getVersion());
168 // DBRef id is pdbId + chain code
169 assertEquals("1WRIA", ref.getAccessionId());
170 assertEquals(1, seq.getAllPDBEntries().size());
171 PDBEntry pdbRef = seq.getAllPDBEntries().get(0);
172 assertEquals("1WRI", pdbRef.getId());
173 assertNull(pdbRef.getFile());
174 assertEquals("A", pdbRef.getChainCode());
175 assertEquals("PDB", pdbRef.getType());
179 * Test the method that searches for matches references - case when we are
180 * matching a reference with no mappings
182 @Test(groups = { "Functional" })
183 public void testSearchRefs_noMapping()
185 DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
187 DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
188 // constructor changes embl to EMBL
189 DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
190 // constructor does not upper-case accession id
191 DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
192 DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
193 // ref5 matches although it has a mapping - ignored
194 DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
196 new Mapping(new MapList(new int[]
197 { 1, 1 }, new int[] { 1, 1 }, 1, 1)));
199 List<DBRefEntry> matches = DBRefUtils
200 .searchRefs(Arrays.asList(new DBRefEntry[]
201 { ref1, ref2, ref3, ref4, ref5 }), target,
202 DBRefUtils.SEARCH_MODE_FULL);
203 assertEquals(3, matches.size());
204 assertSame(ref1, matches.get(0));
205 assertSame(ref2, matches.get(1));
206 assertSame(ref5, matches.get(2));
210 * Test the method that searches for matches references - case when we are
211 * matching a reference with a mapping
213 @Test(groups = { "Functional" })
214 public void testSearchRefs_withMapping()
216 DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
217 final Mapping map1 = new Mapping(
218 new MapList(new int[]
219 { 1, 1 }, new int[] { 1, 1 }, 1, 1));
222 // these all match target iff mappings match
223 DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // no map: matches
224 DBRefEntry ref2 = new DBRefEntry("EMBL", "1", "A1234"); // =map: matches
225 final Mapping map2 = new Mapping(
226 new MapList(new int[]
227 { 1, 1 }, new int[] { 1, 1 }, 1, 1));
230 // different map: no match
231 DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1234");
232 final Mapping map3 = new Mapping(
233 new MapList(new int[]
234 { 1, 1 }, new int[] { 1, 1 }, 2, 2));
237 List<DBRefEntry> matches = DBRefUtils
238 .searchRefs(Arrays.asList(new DBRefEntry[]
239 { ref1, ref2, ref3 }), target, DBRefUtils.SEARCH_MODE_FULL);
240 assertEquals(2, matches.size());
241 assertSame(ref1, matches.get(0));
242 assertSame(ref2, matches.get(1));
246 * Test the method that searches for matching references based on accession id
249 @Test(groups = { "Functional" })
250 public void testSearchRefs_accessionid()
253 DBRefEntry ref1 = new DBRefEntry("Uniprot", "1", "A1234"); // matches
254 DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
255 // constructor does not upper-case accession id
256 DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
257 DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1235"); // no match
258 // ref5 matches although it has a mapping - ignored
259 DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
261 new Mapping(new MapList(new int[]
262 { 1, 1 }, new int[] { 1, 1 }, 1, 1)));
264 List<DBRefEntry> dbrefs = Arrays
265 .asList(new DBRefEntry[]
266 { ref1, ref2, ref3, ref4, ref5 });
267 List<DBRefEntry> matches = DBRefUtils.searchRefs(dbrefs, "A1234");
268 assertEquals(3, matches.size());
269 assertSame(ref1, matches.get(0));
270 assertSame(ref2, matches.get(1));
271 assertSame(ref5, matches.get(2));
275 * Test the method that searches for matches references - case when we are
276 * matching a reference with null (any) accession id
278 @Test(groups = { "Functional" })
279 public void testSearchRefs_wildcardAccessionid()
281 DBRefEntry target = new DBRefEntry("EMBL", "2", null);
283 DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
284 // constructor changes embl to EMBL
285 DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1235"); // matches
286 // constructor does not upper-case accession id
287 DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1236"); // matches
288 DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
289 // ref5 matches although it has a mapping - ignored
290 DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1237");
292 new Mapping(new MapList(new int[]
293 { 1, 1 }, new int[] { 1, 1 }, 1, 1)));
295 List<DBRefEntry> matches = DBRefUtils
296 .searchRefs(Arrays.asList(new DBRefEntry[]
297 { ref1, ref2, ref3, ref4, ref5 }), target,
298 DBRefUtils.SEARCH_MODE_FULL);
299 assertEquals(4, matches.size());
300 assertSame(ref1, matches.get(0));
301 assertSame(ref2, matches.get(1));
302 assertSame(ref3, matches.get(2));
303 assertSame(ref5, matches.get(3));