JAL-1705 canonicalise ENSEMBL name; code tidy
[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 org.testng.annotations.Test;
37
38 public class DBRefUtilsTest
39 {
40
41   /**
42    * Test the method that selects DBRefEntry items whose source is in a supplied
43    * list
44    */
45   @Test(groups = { "Functional" })
46   public void testSelectRefs()
47   {
48     assertNull(DBRefUtils.selectRefs(null, null));
49     assertNull(DBRefUtils.selectRefs(null, DBRefSource.CODINGDBS));
50
51     DBRefEntry ref1 = new DBRefEntry("EMBL", "1.2", "A12345");
52     DBRefEntry ref2 = new DBRefEntry("UNIPROT", "1.2", "A12346");
53     // Source is converted to upper-case by this constructor!
54     DBRefEntry ref3 = new DBRefEntry("Uniprot", "1.2", "A12347");
55     DBRefEntry[] dbrefs = new DBRefEntry[] { ref1, ref2, ref3 };
56     String[] sources = new String[] { "EMBL", "UNIPROT" };
57
58     DBRefEntry[] selected = DBRefUtils.selectRefs(dbrefs, sources);
59     assertEquals(3, selected.length);
60     assertSame(ref1, selected[0]);
61     assertSame(ref2, selected[1]);
62     assertSame(ref3, selected[2]);
63
64     sources = new String[] { "EMBL" };
65     selected = DBRefUtils.selectRefs(dbrefs, sources);
66     assertEquals(1, selected.length);
67     assertSame(ref1, selected[0]);
68
69     sources = new String[] { "UNIPROT" };
70     selected = DBRefUtils.selectRefs(dbrefs, sources);
71     assertEquals(2, selected.length);
72     assertSame(ref2, selected[0]);
73     assertSame(ref3, selected[1]);
74
75     sources = new String[] { "Uniprot", "EMBLCDS" };
76     selected = DBRefUtils.selectRefs(dbrefs, sources);
77     assertNull(selected);
78   }
79
80   /**
81    * Test the method that converts (currently three) database names to a
82    * canonical name (not case-sensitive)
83    */
84   @Test(groups = { "Functional" })
85   public void testGetCanonicalName()
86   {
87     assertNull(DBRefUtils.getCanonicalName(null));
88     assertEquals("", DBRefUtils.getCanonicalName(""));
89     assertEquals("PDB", DBRefUtils.getCanonicalName("pdb"));
90     assertEquals("PDB", DBRefUtils.getCanonicalName("Pdb"));
91     assertEquals("UNIPROT",
92             DBRefUtils.getCanonicalName("uniprotkb/swiss-prot"));
93     assertEquals("UNIPROT", DBRefUtils.getCanonicalName("uniprotkb/trembl"));
94     assertEquals("UNIPROT",
95             DBRefUtils.getCanonicalName("UNIPROTKB/SWISS-PROT"));
96     assertEquals("UNIPROT", DBRefUtils.getCanonicalName("UNIPROTKB/TREMBL"));
97     assertEquals("UNIPROTKB/SWISS-CHEESE",
98             DBRefUtils.getCanonicalName("UNIPROTKB/SWISS-CHEESE"));
99     assertEquals("ENSEMBL", DBRefUtils.getCanonicalName("Ensembl"));
100   }
101
102   @Test(groups = { "Functional" })
103   public void testIsDasCoordinateSystem()
104   {
105     assertFalse(DBRefUtils.isDasCoordinateSystem(null, null));
106     assertFalse(DBRefUtils.isDasCoordinateSystem("pdbresnum", null));
107     assertFalse(DBRefUtils.isDasCoordinateSystem(null, new DBRefEntry(
108             "PDB", "v1", "a1")));
109
110     assertTrue(DBRefUtils.isDasCoordinateSystem("pdbresnum",
111             new DBRefEntry("PDB", "v1", "a1")));
112     assertTrue(DBRefUtils.isDasCoordinateSystem("PDBRESNUM",
113             new DBRefEntry("PDB", "v1", "a1")));
114     // "pdb" is converted to upper-case in DBRefEntry constructor
115     assertTrue(DBRefUtils.isDasCoordinateSystem("pdbresnum",
116             new DBRefEntry("pdb", "v1", "a1")));
117     assertFalse(DBRefUtils.isDasCoordinateSystem("pdb", new DBRefEntry(
118             "pdb", "v1", "a1")));
119
120     assertTrue(DBRefUtils.isDasCoordinateSystem("UNIPROT", new DBRefEntry(
121             "Uniprot", "v1", "a1")));
122     assertTrue(DBRefUtils.isDasCoordinateSystem("Uniprot", new DBRefEntry(
123             "UNIPROT", "v1", "a1")));
124     assertFalse(DBRefUtils.isDasCoordinateSystem("UNIPROTKB",
125             new DBRefEntry("pdb", "v1", "a1")));
126
127     assertTrue(DBRefUtils.isDasCoordinateSystem("EMBL", new DBRefEntry(
128             "EMBL", "v1", "a1")));
129     assertTrue(DBRefUtils.isDasCoordinateSystem("embl", new DBRefEntry(
130             "embl", "v1", "a1")));
131   }
132
133   /**
134    * Test 'parsing' a DBRef - non PDB case
135    */
136   @Test(groups = { "Functional" })
137   public void testParseToDbRef()
138   {
139     SequenceI seq = new Sequence("Seq1", "ABCD");
140     DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "EMBL", "1.2", "a7890");
141     DBRefEntry[] refs = seq.getDBRef();
142     assertEquals(1, refs.length);
143     assertSame(ref, refs[0]);
144     assertEquals("EMBL", ref.getSource());
145     assertEquals("1.2", ref.getVersion());
146     assertEquals("a7890", ref.getAccessionId());
147     assertNull(seq.getAllPDBEntries());
148   }
149
150   /**
151    * Test 'parsing' a DBRef - Stockholm PDB format
152    */
153   @Test(groups = { "Functional" })
154   public void testParseToDbRef_PDB()
155   {
156     SequenceI seq = new Sequence("Seq1", "ABCD");
157     DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "pdb", "1.2",
158             "1WRI A; 7-80;");
159     DBRefEntry[] refs = seq.getDBRef();
160     assertEquals(1, refs.length);
161     assertSame(ref, refs[0]);
162     assertEquals("PDB", ref.getSource());
163     assertEquals("1.2", ref.getVersion());
164     // DBRef id is pdbId + chain code
165     assertEquals("1WRIA", ref.getAccessionId());
166     assertEquals(1, seq.getAllPDBEntries().size());
167     PDBEntry pdbRef = seq.getAllPDBEntries().get(0);
168     assertEquals("1WRI", pdbRef.getId());
169     assertNull(pdbRef.getFile());
170     assertEquals("A", pdbRef.getChainCode());
171     assertEquals("PDB", pdbRef.getType());
172   }
173
174   /**
175    * Test the method that searches for matches references - case when we are
176    * matching a reference with no mappings
177    */
178   @Test(groups = { "Functional" })
179   public void testSearchRefs_noMapping()
180   {
181     DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
182
183     DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
184     // constructor changes embl to EMBL
185     DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
186     // constructor does not upper-case accession id
187     DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
188     DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
189     // ref5 matches although it has a mapping - ignored
190     DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
191     ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
192         1 }, 1, 1)));
193
194     DBRefEntry[] matches = DBRefUtils.searchRefs(new DBRefEntry[] { ref1,
195         ref2, ref3, ref4, ref5 }, target);
196     assertEquals(3, matches.length);
197     assertSame(ref1, matches[0]);
198     assertSame(ref2, matches[1]);
199     assertSame(ref5, matches[2]);
200   }
201
202   /**
203    * Test the method that searches for matches references - case when we are
204    * matching a reference with a mapping
205    */
206   @Test(groups = { "Functional" })
207   public void testSearchRefs_withMapping()
208   {
209     DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
210     final Mapping map1 = new Mapping(new MapList(new int[] { 1, 1 },
211             new int[] { 1, 1 }, 1, 1));
212     target.setMap(map1);
213
214     // these all match target iff mappings match
215     DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // no map: matches
216     DBRefEntry ref2 = new DBRefEntry("EMBL", "1", "A1234"); // =map: matches
217     final Mapping map2 = new Mapping(new MapList(new int[] { 1, 1 },
218             new int[] { 1, 1 }, 1, 1));
219     ref2.setMap(map2);
220
221     // different map: no match
222     DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1234");
223     final Mapping map3 = new Mapping(new MapList(new int[] { 1, 1 },
224             new int[] { 1, 1 }, 2, 2));
225     ref3.setMap(map3);
226
227     DBRefEntry[] matches = DBRefUtils.searchRefs(new DBRefEntry[] { ref1,
228         ref2, ref3 }, target);
229     assertEquals(2, matches.length);
230     assertSame(ref1, matches[0]);
231     assertSame(ref2, matches[1]);
232   }
233 }