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.assertFalse;
25 import static org.testng.AssertJUnit.assertNull;
26 import static org.testng.AssertJUnit.assertSame;
27 import static org.testng.AssertJUnit.assertTrue;
29 import jalview.datamodel.DBRefEntry;
30 import jalview.datamodel.DBRefSource;
31 import jalview.datamodel.Mapping;
32 import jalview.datamodel.PDBEntry;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceI;
36 import java.util.List;
38 import org.testng.annotations.Test;
40 public class DBRefUtilsTest
44 * Test the method that selects DBRefEntry items whose source is in a supplied
47 @Test(groups = { "Functional" })
48 public void testSelectRefs()
50 assertNull(DBRefUtils.selectRefs(null, null));
51 assertNull(DBRefUtils.selectRefs(null, DBRefSource.CODINGDBS));
53 DBRefEntry ref1 = new DBRefEntry("EMBL", "1.2", "A12345");
54 DBRefEntry ref2 = new DBRefEntry("UNIPROT", "1.2", "A12346");
55 // Source is converted to upper-case by this constructor!
56 DBRefEntry ref3 = new DBRefEntry("Uniprot", "1.2", "A12347");
57 DBRefEntry[] dbrefs = new DBRefEntry[] { ref1, ref2, ref3 };
58 String[] sources = new String[] { "EMBL", "UNIPROT" };
60 DBRefEntry[] selected = DBRefUtils.selectRefs(dbrefs, sources);
61 assertEquals(3, selected.length);
62 assertSame(ref1, selected[0]);
63 assertSame(ref2, selected[1]);
64 assertSame(ref3, selected[2]);
66 sources = new String[] { "EMBL" };
67 selected = DBRefUtils.selectRefs(dbrefs, sources);
68 assertEquals(1, selected.length);
69 assertSame(ref1, selected[0]);
71 sources = new String[] { "UNIPROT" };
72 selected = DBRefUtils.selectRefs(dbrefs, sources);
73 assertEquals(2, selected.length);
74 assertSame(ref2, selected[0]);
75 assertSame(ref3, selected[1]);
77 sources = new String[] { "Uniprot", "EMBLCDS" };
78 selected = DBRefUtils.selectRefs(dbrefs, sources);
83 * Test the method that converts (currently three) database names to a
84 * canonical name (not case-sensitive)
86 @Test(groups = { "Functional" })
87 public void testGetCanonicalName()
89 assertNull(DBRefUtils.getCanonicalName(null));
90 assertEquals("", DBRefUtils.getCanonicalName(""));
91 assertEquals("PDB", DBRefUtils.getCanonicalName("pdb"));
92 assertEquals("PDB", DBRefUtils.getCanonicalName("Pdb"));
93 assertEquals("UNIPROT",
94 DBRefUtils.getCanonicalName("uniprotkb/swiss-prot"));
95 assertEquals("UNIPROT", DBRefUtils.getCanonicalName("uniprotkb/trembl"));
96 assertEquals("UNIPROT",
97 DBRefUtils.getCanonicalName("UNIPROTKB/SWISS-PROT"));
98 assertEquals("UNIPROT", DBRefUtils.getCanonicalName("UNIPROTKB/TREMBL"));
99 assertEquals("UNIPROTKB/SWISS-CHEESE",
100 DBRefUtils.getCanonicalName("UNIPROTKB/SWISS-CHEESE"));
101 assertEquals("ENSEMBL", DBRefUtils.getCanonicalName("Ensembl"));
104 @Test(groups = { "Functional" })
105 public void testIsDasCoordinateSystem()
107 assertFalse(DBRefUtils.isDasCoordinateSystem(null, null));
108 assertFalse(DBRefUtils.isDasCoordinateSystem("pdbresnum", null));
109 assertFalse(DBRefUtils.isDasCoordinateSystem(null, new DBRefEntry(
110 "PDB", "v1", "a1")));
112 assertTrue(DBRefUtils.isDasCoordinateSystem("pdbresnum",
113 new DBRefEntry("PDB", "v1", "a1")));
114 assertTrue(DBRefUtils.isDasCoordinateSystem("PDBRESNUM",
115 new DBRefEntry("PDB", "v1", "a1")));
116 // "pdb" is converted to upper-case in DBRefEntry constructor
117 assertTrue(DBRefUtils.isDasCoordinateSystem("pdbresnum",
118 new DBRefEntry("pdb", "v1", "a1")));
119 assertFalse(DBRefUtils.isDasCoordinateSystem("pdb", new DBRefEntry(
120 "pdb", "v1", "a1")));
122 assertTrue(DBRefUtils.isDasCoordinateSystem("UNIPROT", new DBRefEntry(
123 "Uniprot", "v1", "a1")));
124 assertTrue(DBRefUtils.isDasCoordinateSystem("Uniprot", new DBRefEntry(
125 "UNIPROT", "v1", "a1")));
126 assertFalse(DBRefUtils.isDasCoordinateSystem("UNIPROTKB",
127 new DBRefEntry("pdb", "v1", "a1")));
129 assertTrue(DBRefUtils.isDasCoordinateSystem("EMBL", new DBRefEntry(
130 "EMBL", "v1", "a1")));
131 assertTrue(DBRefUtils.isDasCoordinateSystem("embl", new DBRefEntry(
132 "embl", "v1", "a1")));
136 * Test 'parsing' a DBRef - non PDB case
138 @Test(groups = { "Functional" })
139 public void testParseToDbRef()
141 SequenceI seq = new Sequence("Seq1", "ABCD");
142 DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "EMBL", "1.2", "a7890");
143 DBRefEntry[] refs = seq.getDBRefs();
144 assertEquals(1, refs.length);
145 assertSame(ref, refs[0]);
146 assertEquals("EMBL", ref.getSource());
147 assertEquals("1.2", ref.getVersion());
148 assertEquals("a7890", ref.getAccessionId());
149 assertTrue(seq.getAllPDBEntries().isEmpty());
153 * Test 'parsing' a DBRef - Stockholm PDB format
155 @Test(groups = { "Functional" })
156 public void testParseToDbRef_PDB()
158 SequenceI seq = new Sequence("Seq1", "ABCD");
159 DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "pdb", "1.2",
161 // TODO: correct PDBEntry and PDB DBRef accessions need to be generated for
162 // PDB ref in Stockholm
164 DBRefEntry[] refs = seq.getDBRefs();
165 assertEquals(1, refs.length);
166 assertSame(ref, refs[0]);
167 assertEquals("PDB", ref.getSource());
168 assertEquals("1.2", ref.getVersion());
169 // DBRef id is pdbId + chain code
170 assertEquals("1WRIA", ref.getAccessionId());
171 assertEquals(1, seq.getAllPDBEntries().size());
172 PDBEntry pdbRef = seq.getAllPDBEntries().get(0);
173 assertEquals("1WRI", pdbRef.getId());
174 assertNull(pdbRef.getFile());
175 assertEquals("A", pdbRef.getChainCode());
176 assertEquals("PDB", pdbRef.getType());
180 * Test the method that searches for matches references - case when we are
181 * matching a reference with no mappings
183 @Test(groups = { "Functional" })
184 public void testSearchRefs_noMapping()
186 DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
188 DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
189 // constructor changes embl to EMBL
190 DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
191 // constructor does not upper-case accession id
192 DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
193 DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
194 // ref5 matches although it has a mapping - ignored
195 DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
196 ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
199 List<DBRefEntry> matches = DBRefUtils.searchRefs(new DBRefEntry[] {
201 ref2, ref3, ref4, ref5 }, target);
202 assertEquals(3, matches.size());
203 assertSame(ref1, matches.get(0));
204 assertSame(ref2, matches.get(1));
205 assertSame(ref5, matches.get(2));
209 * Test the method that searches for matches references - case when we are
210 * matching a reference with a mapping
212 @Test(groups = { "Functional" })
213 public void testSearchRefs_withMapping()
215 DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
216 final Mapping map1 = new Mapping(new MapList(new int[] { 1, 1 },
217 new int[] { 1, 1 }, 1, 1));
220 // these all match target iff mappings match
221 DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // no map: matches
222 DBRefEntry ref2 = new DBRefEntry("EMBL", "1", "A1234"); // =map: matches
223 final Mapping map2 = new Mapping(new MapList(new int[] { 1, 1 },
224 new int[] { 1, 1 }, 1, 1));
227 // different map: no match
228 DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1234");
229 final Mapping map3 = new Mapping(new MapList(new int[] { 1, 1 },
230 new int[] { 1, 1 }, 2, 2));
233 List<DBRefEntry> matches = DBRefUtils.searchRefs(new DBRefEntry[] {
235 ref2, ref3 }, target);
236 assertEquals(2, matches.size());
237 assertSame(ref1, matches.get(0));
238 assertSame(ref2, matches.get(1));
242 * Test the method that searches for matching references based on accession id
245 @Test(groups = { "Functional" })
246 public void testSearchRefs_accessionid()
249 DBRefEntry ref1 = new DBRefEntry("Uniprot", "1", "A1234"); // matches
250 DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
251 // constructor does not upper-case accession id
252 DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
253 DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1235"); // no match
254 // ref5 matches although it has a mapping - ignored
255 DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
256 ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
259 DBRefEntry[] dbrefs = new DBRefEntry[] { ref1,
260 ref2, ref3, ref4, ref5 };
261 List<DBRefEntry> matches = DBRefUtils.searchRefs(dbrefs, "A1234");
262 assertEquals(3, matches.size());
263 assertSame(ref1, matches.get(0));
264 assertSame(ref2, matches.get(1));
265 assertSame(ref5, matches.get(2));
269 * Test the method that searches for matches references - case when we are
270 * matching a reference with null (any) accession id
272 @Test(groups = { "Functional" })
273 public void testSearchRefs_wildcardAccessionid()
275 DBRefEntry target = new DBRefEntry("EMBL", "2", null);
277 DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
278 // constructor changes embl to EMBL
279 DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1235"); // matches
280 // constructor does not upper-case accession id
281 DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1236"); // matches
282 DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
283 // ref5 matches although it has a mapping - ignored
284 DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1237");
285 ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
288 List<DBRefEntry> matches = DBRefUtils.searchRefs(new DBRefEntry[] {
290 ref2, ref3, ref4, ref5 }, target);
291 assertEquals(4, matches.size());
292 assertSame(ref1, matches.get(0));
293 assertSame(ref2, matches.get(1));
294 assertSame(ref3, matches.get(2));
295 assertSame(ref5, matches.get(3));