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