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 DBRefEntry[] refs = seq.getDBRefs();
162 assertEquals(1, refs.length);
163 assertSame(ref, refs[0]);
164 assertEquals("PDB", ref.getSource());
165 assertEquals("1.2", ref.getVersion());
166 // DBRef id is pdbId + chain code
167 assertEquals("1WRIA", ref.getAccessionId());
168 assertEquals(1, seq.getAllPDBEntries().size());
169 PDBEntry pdbRef = seq.getAllPDBEntries().get(0);
170 assertEquals("1WRI", pdbRef.getId());
171 assertNull(pdbRef.getFile());
172 assertEquals("A", pdbRef.getChainCode());
173 assertEquals("PDB", pdbRef.getType());
177 * Test the method that searches for matches references - case when we are
178 * matching a reference with no mappings
180 @Test(groups = { "Functional" })
181 public void testSearchRefs_noMapping()
183 DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
185 DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
186 // constructor changes embl to EMBL
187 DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
188 // constructor does not upper-case accession id
189 DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
190 DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
191 // ref5 matches although it has a mapping - ignored
192 DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
193 ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
196 List<DBRefEntry> matches = DBRefUtils.searchRefs(new DBRefEntry[] {
198 ref2, ref3, ref4, ref5 }, target);
199 assertEquals(3, matches.size());
200 assertSame(ref1, matches.get(0));
201 assertSame(ref2, matches.get(1));
202 assertSame(ref5, matches.get(2));
206 * Test the method that searches for matches references - case when we are
207 * matching a reference with a mapping
209 @Test(groups = { "Functional" })
210 public void testSearchRefs_withMapping()
212 DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
213 final Mapping map1 = new Mapping(new MapList(new int[] { 1, 1 },
214 new int[] { 1, 1 }, 1, 1));
217 // these all match target iff mappings match
218 DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // no map: matches
219 DBRefEntry ref2 = new DBRefEntry("EMBL", "1", "A1234"); // =map: matches
220 final Mapping map2 = new Mapping(new MapList(new int[] { 1, 1 },
221 new int[] { 1, 1 }, 1, 1));
224 // different map: no match
225 DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1234");
226 final Mapping map3 = new Mapping(new MapList(new int[] { 1, 1 },
227 new int[] { 1, 1 }, 2, 2));
230 List<DBRefEntry> matches = DBRefUtils.searchRefs(new DBRefEntry[] {
232 ref2, ref3 }, target);
233 assertEquals(2, matches.size());
234 assertSame(ref1, matches.get(0));
235 assertSame(ref2, matches.get(1));
239 * Test the method that searches for matching references based on accession id
242 @Test(groups = { "Functional" })
243 public void testSearchRefs_accessionid()
246 DBRefEntry ref1 = new DBRefEntry("Uniprot", "1", "A1234"); // matches
247 DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
248 // constructor does not upper-case accession id
249 DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
250 DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1235"); // no match
251 // ref5 matches although it has a mapping - ignored
252 DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
253 ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
256 DBRefEntry[] dbrefs = new DBRefEntry[] { ref1,
257 ref2, ref3, ref4, ref5 };
258 List<DBRefEntry> matches = DBRefUtils.searchRefs(dbrefs, "A1234");
259 assertEquals(3, matches.size());
260 assertSame(ref1, matches.get(0));
261 assertSame(ref2, matches.get(1));
262 assertSame(ref5, matches.get(2));
266 * Test the method that searches for matches references - case when we are
267 * matching a reference with null (any) accession id
269 @Test(groups = { "Functional" })
270 public void testSearchRefs_wildcardAccessionid()
272 DBRefEntry target = new DBRefEntry("EMBL", "2", null);
274 DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
275 // constructor changes embl to EMBL
276 DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1235"); // matches
277 // constructor does not upper-case accession id
278 DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1236"); // matches
279 DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
280 // ref5 matches although it has a mapping - ignored
281 DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1237");
282 ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
285 List<DBRefEntry> matches = DBRefUtils.searchRefs(new DBRefEntry[] {
287 ref2, ref3, ref4, ref5 }, target);
288 assertEquals(4, matches.size());
289 assertSame(ref1, matches.get(0));
290 assertSame(ref2, matches.get(1));
291 assertSame(ref3, matches.get(2));
292 assertSame(ref5, matches.get(3));