Merge branch 'bug/JAL-3574filterByPOS' into documentation/JAL-3407_2.11.1_release
[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     SequenceI seq2 = new Sequence("Seq2", "ABCD");
139     // Check that whitespace doesn't confuse parseToDbRef
140     DBRefEntry ref2 = DBRefUtils.parseToDbRef(seq2, "EMBL", "1.2",
141             " a7890");
142     assertEquals(ref, ref2);
143   }
144
145   /**
146    * Test 'parsing' a DBRef - Stockholm PDB format
147    */
148   @Test(groups = { "Functional" })
149   public void testParseToDbRef_PDB()
150   {
151     SequenceI seq = new Sequence("Seq1", "ABCD");
152     DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "pdb", "1.2",
153             "1WRI A; 7-80;");
154     // TODO: correct PDBEntry and PDB DBRef accessions need to be generated for
155     // PDB ref in Stockholm
156
157     DBRefEntry[] refs = seq.getDBRefs();
158     assertEquals(1, refs.length);
159     assertSame(ref, refs[0]);
160     assertEquals("PDB", ref.getSource());
161     assertEquals("1.2", ref.getVersion());
162     // DBRef id is pdbId + chain code
163     assertEquals("1WRIA", ref.getAccessionId());
164     assertEquals(1, seq.getAllPDBEntries().size());
165     PDBEntry pdbRef = seq.getAllPDBEntries().get(0);
166     assertEquals("1WRI", pdbRef.getId());
167     assertNull(pdbRef.getFile());
168     assertEquals("A", pdbRef.getChainCode());
169     assertEquals("PDB", pdbRef.getType());
170   }
171
172   /**
173    * Test the method that searches for matches references - case when we are
174    * matching a reference with no mappings
175    */
176   @Test(groups = { "Functional" })
177   public void testSearchRefs_noMapping()
178   {
179     DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
180
181     DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
182     // constructor changes embl to EMBL
183     DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
184     // constructor does not upper-case accession id
185     DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
186     DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
187     // ref5 matches although it has a mapping - ignored
188     DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
189     ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
190         1 }, 1, 1)));
191
192     List<DBRefEntry> matches = DBRefUtils.searchRefs(new DBRefEntry[] {
193         ref1, ref2, ref3, ref4, ref5 }, target);
194     assertEquals(3, matches.size());
195     assertSame(ref1, matches.get(0));
196     assertSame(ref2, matches.get(1));
197     assertSame(ref5, matches.get(2));
198   }
199
200   /**
201    * Test the method that searches for matches references - case when we are
202    * matching a reference with a mapping
203    */
204   @Test(groups = { "Functional" })
205   public void testSearchRefs_withMapping()
206   {
207     DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
208     final Mapping map1 = new Mapping(new MapList(new int[] { 1, 1 },
209             new int[] { 1, 1 }, 1, 1));
210     target.setMap(map1);
211
212     // these all match target iff mappings match
213     DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // no map: matches
214     DBRefEntry ref2 = new DBRefEntry("EMBL", "1", "A1234"); // =map: matches
215     final Mapping map2 = new Mapping(new MapList(new int[] { 1, 1 },
216             new int[] { 1, 1 }, 1, 1));
217     ref2.setMap(map2);
218
219     // different map: no match
220     DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1234");
221     final Mapping map3 = new Mapping(new MapList(new int[] { 1, 1 },
222             new int[] { 1, 1 }, 2, 2));
223     ref3.setMap(map3);
224
225     List<DBRefEntry> matches = DBRefUtils.searchRefs(new DBRefEntry[] {
226         ref1, ref2, ref3 }, target);
227     assertEquals(2, matches.size());
228     assertSame(ref1, matches.get(0));
229     assertSame(ref2, matches.get(1));
230   }
231
232   /**
233    * Test the method that searches for matching references based on accession id
234    * only
235    */
236   @Test(groups = { "Functional" })
237   public void testSearchRefs_accessionid()
238   {
239
240     DBRefEntry ref1 = new DBRefEntry("Uniprot", "1", "A1234"); // matches
241     DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
242     // constructor does not upper-case accession id
243     DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
244     DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1235"); // no match
245     // ref5 matches although it has a mapping - ignored
246     DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
247     ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
248         1 }, 1, 1)));
249
250     DBRefEntry[] dbrefs = new DBRefEntry[] { ref1, ref2, ref3, ref4, ref5 };
251     List<DBRefEntry> matches = DBRefUtils.searchRefs(dbrefs, "A1234");
252     assertEquals(3, matches.size());
253     assertSame(ref1, matches.get(0));
254     assertSame(ref2, matches.get(1));
255     assertSame(ref5, matches.get(2));
256   }
257
258   /**
259    * Test the method that searches for matches references - case when we are
260    * matching a reference with null (any) accession id
261    */
262   @Test(groups = { "Functional" })
263   public void testSearchRefs_wildcardAccessionid()
264   {
265     DBRefEntry target = new DBRefEntry("EMBL", "2", null);
266
267     DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
268     // constructor changes embl to EMBL
269     DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1235"); // matches
270     // constructor does not upper-case accession id
271     DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1236"); // matches
272     DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
273     // ref5 matches although it has a mapping - ignored
274     DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1237");
275     ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
276         1 }, 1, 1)));
277
278     List<DBRefEntry> matches = DBRefUtils.searchRefs(new DBRefEntry[] {
279         ref1, ref2, ref3, ref4, ref5 }, target);
280     assertEquals(4, matches.size());
281     assertSame(ref1, matches.get(0));
282     assertSame(ref2, matches.get(1));
283     assertSame(ref3, matches.get(2));
284     assertSame(ref5, matches.get(3));
285   }
286 }