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