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