JAL-2282 test assertions for 'other' dbsource
[jalview.git] / test / jalview / util / DBRefUtilsTest.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.util;
22
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;
28
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;
35
36 import java.util.List;
37
38 import org.testng.annotations.Test;
39
40 public class DBRefUtilsTest
41 {
42
43   /**
44    * Test the method that selects DBRefEntry items whose source is in a supplied
45    * list
46    */
47   @Test(groups = { "Functional" })
48   public void testSelectRefs()
49   {
50     assertNull(DBRefUtils.selectRefs(null, null));
51     assertNull(DBRefUtils.selectRefs(null, DBRefSource.CODINGDBS));
52
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" };
59
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]);
65
66     sources = new String[] { "EMBL" };
67     selected = DBRefUtils.selectRefs(dbrefs, sources);
68     assertEquals(1, selected.length);
69     assertSame(ref1, selected[0]);
70
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]);
76
77     sources = new String[] { "Uniprot", "EMBLCDS" };
78     selected = DBRefUtils.selectRefs(dbrefs, sources);
79     assertNull(selected);
80   }
81
82   /**
83    * Test the method that converts (currently three) database names to a
84    * canonical name (not case-sensitive)
85    */
86   @Test(groups = { "Functional" })
87   public void testGetCanonicalName()
88   {
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"));
102
103     // these are not 'known' to Jalview
104     assertEquals("PFAM", DBRefUtils.getCanonicalName("PFAM"));
105     assertEquals("pfam", DBRefUtils.getCanonicalName("pfam"));
106
107   }
108
109   @Test(groups = { "Functional" })
110   public void testIsDasCoordinateSystem()
111   {
112     assertFalse(DBRefUtils.isDasCoordinateSystem(null, null));
113     assertFalse(DBRefUtils.isDasCoordinateSystem("pdbresnum", null));
114     assertFalse(DBRefUtils.isDasCoordinateSystem(null, new DBRefEntry(
115             "PDB", "v1", "a1")));
116
117     assertTrue(DBRefUtils.isDasCoordinateSystem("pdbresnum",
118             new DBRefEntry("PDB", "v1", "a1")));
119     assertTrue(DBRefUtils.isDasCoordinateSystem("PDBRESNUM",
120             new DBRefEntry("PDB", "v1", "a1")));
121     // "pdb" is converted to upper-case in DBRefEntry constructor
122     assertTrue(DBRefUtils.isDasCoordinateSystem("pdbresnum",
123             new DBRefEntry("pdb", "v1", "a1")));
124     assertFalse(DBRefUtils.isDasCoordinateSystem("pdb", new DBRefEntry(
125             "pdb", "v1", "a1")));
126
127     assertTrue(DBRefUtils.isDasCoordinateSystem("UNIPROT", new DBRefEntry(
128             "Uniprot", "v1", "a1")));
129     assertTrue(DBRefUtils.isDasCoordinateSystem("Uniprot", new DBRefEntry(
130             "UNIPROT", "v1", "a1")));
131     assertFalse(DBRefUtils.isDasCoordinateSystem("UNIPROTKB",
132             new DBRefEntry("pdb", "v1", "a1")));
133
134     assertTrue(DBRefUtils.isDasCoordinateSystem("EMBL", new DBRefEntry(
135             "EMBL", "v1", "a1")));
136     assertTrue(DBRefUtils.isDasCoordinateSystem("embl", new DBRefEntry(
137             "embl", "v1", "a1")));
138   }
139
140   /**
141    * Test 'parsing' a DBRef - non PDB case
142    */
143   @Test(groups = { "Functional" })
144   public void testParseToDbRef()
145   {
146     SequenceI seq = new Sequence("Seq1", "ABCD");
147     DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "EMBL", "1.2", "a7890");
148     DBRefEntry[] refs = seq.getDBRefs();
149     assertEquals(1, refs.length);
150     assertSame(ref, refs[0]);
151     assertEquals("EMBL", ref.getSource());
152     assertEquals("1.2", ref.getVersion());
153     assertEquals("a7890", ref.getAccessionId());
154     assertTrue(seq.getAllPDBEntries().isEmpty());
155   }
156
157   /**
158    * Test 'parsing' a DBRef - Stockholm PDB format
159    */
160   @Test(groups = { "Functional" })
161   public void testParseToDbRef_PDB()
162   {
163     SequenceI seq = new Sequence("Seq1", "ABCD");
164     DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "pdb", "1.2",
165             "1WRI A; 7-80;");
166     // TODO: correct PDBEntry and PDB DBRef accessions need to be generated for
167     // PDB ref in Stockholm
168
169     DBRefEntry[] refs = seq.getDBRefs();
170     assertEquals(1, refs.length);
171     assertSame(ref, refs[0]);
172     assertEquals("PDB", ref.getSource());
173     assertEquals("1.2", ref.getVersion());
174     // DBRef id is pdbId + chain code
175     assertEquals("1WRIA", ref.getAccessionId());
176     assertEquals(1, seq.getAllPDBEntries().size());
177     PDBEntry pdbRef = seq.getAllPDBEntries().get(0);
178     assertEquals("1WRI", pdbRef.getId());
179     assertNull(pdbRef.getFile());
180     assertEquals("A", pdbRef.getChainCode());
181     assertEquals("PDB", pdbRef.getType());
182   }
183
184   /**
185    * Test the method that searches for matches references - case when we are
186    * matching a reference with no mappings
187    */
188   @Test(groups = { "Functional" })
189   public void testSearchRefs_noMapping()
190   {
191     DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
192
193     DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
194     // constructor changes embl to EMBL
195     DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
196     // constructor does not upper-case accession id
197     DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
198     DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
199     // ref5 matches although it has a mapping - ignored
200     DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
201     ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
202         1 }, 1, 1)));
203
204     List<DBRefEntry> matches = DBRefUtils.searchRefs(new DBRefEntry[] {
205         ref1, ref2, ref3, ref4, ref5 }, target);
206     assertEquals(3, matches.size());
207     assertSame(ref1, matches.get(0));
208     assertSame(ref2, matches.get(1));
209     assertSame(ref5, matches.get(2));
210   }
211
212   /**
213    * Test the method that searches for matches references - case when we are
214    * matching a reference with a mapping
215    */
216   @Test(groups = { "Functional" })
217   public void testSearchRefs_withMapping()
218   {
219     DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
220     final Mapping map1 = new Mapping(new MapList(new int[] { 1, 1 },
221             new int[] { 1, 1 }, 1, 1));
222     target.setMap(map1);
223
224     // these all match target iff mappings match
225     DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // no map: matches
226     DBRefEntry ref2 = new DBRefEntry("EMBL", "1", "A1234"); // =map: matches
227     final Mapping map2 = new Mapping(new MapList(new int[] { 1, 1 },
228             new int[] { 1, 1 }, 1, 1));
229     ref2.setMap(map2);
230
231     // different map: no match
232     DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1234");
233     final Mapping map3 = new Mapping(new MapList(new int[] { 1, 1 },
234             new int[] { 1, 1 }, 2, 2));
235     ref3.setMap(map3);
236
237     List<DBRefEntry> matches = DBRefUtils.searchRefs(new DBRefEntry[] {
238         ref1, ref2, ref3 }, target);
239     assertEquals(2, matches.size());
240     assertSame(ref1, matches.get(0));
241     assertSame(ref2, matches.get(1));
242   }
243
244   /**
245    * Test the method that searches for matching references based on accession id
246    * only
247    */
248   @Test(groups = { "Functional" })
249   public void testSearchRefs_accessionid()
250   {
251
252     DBRefEntry ref1 = new DBRefEntry("Uniprot", "1", "A1234"); // matches
253     DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
254     // constructor does not upper-case accession id
255     DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
256     DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1235"); // no match
257     // ref5 matches although it has a mapping - ignored
258     DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
259     ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
260         1 }, 1, 1)));
261
262     DBRefEntry[] dbrefs = new DBRefEntry[] { ref1, ref2, ref3, ref4, ref5 };
263     List<DBRefEntry> matches = DBRefUtils.searchRefs(dbrefs, "A1234");
264     assertEquals(3, matches.size());
265     assertSame(ref1, matches.get(0));
266     assertSame(ref2, matches.get(1));
267     assertSame(ref5, matches.get(2));
268   }
269
270   /**
271    * Test the method that searches for matches references - case when we are
272    * matching a reference with null (any) accession id
273    */
274   @Test(groups = { "Functional" })
275   public void testSearchRefs_wildcardAccessionid()
276   {
277     DBRefEntry target = new DBRefEntry("EMBL", "2", null);
278
279     DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
280     // constructor changes embl to EMBL
281     DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1235"); // matches
282     // constructor does not upper-case accession id
283     DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1236"); // matches
284     DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
285     // ref5 matches although it has a mapping - ignored
286     DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1237");
287     ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
288         1 }, 1, 1)));
289
290     List<DBRefEntry> matches = DBRefUtils.searchRefs(new DBRefEntry[] {
291         ref1, ref2, ref3, ref4, ref5 }, target);
292     assertEquals(4, matches.size());
293     assertSame(ref1, matches.get(0));
294     assertSame(ref2, matches.get(1));
295     assertSame(ref3, matches.get(2));
296     assertSame(ref5, matches.get(3));
297   }
298 }