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