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