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