4a55a0d8e62369a182cd718f1a90924c1ca42119
[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   }
100
101   @Test(groups = { "Functional" })
102   public void testIsDasCoordinateSystem()
103   {
104     assertFalse(DBRefUtils.isDasCoordinateSystem(null, null));
105     assertFalse(DBRefUtils.isDasCoordinateSystem("pdbresnum", null));
106     assertFalse(DBRefUtils.isDasCoordinateSystem(null, new DBRefEntry(
107             "PDB", "v1", "a1")));
108
109     assertTrue(DBRefUtils.isDasCoordinateSystem("pdbresnum",
110             new DBRefEntry("PDB", "v1", "a1")));
111     assertTrue(DBRefUtils.isDasCoordinateSystem("PDBRESNUM",
112             new DBRefEntry("PDB", "v1", "a1")));
113     // "pdb" is converted to upper-case in DBRefEntry constructor
114     assertTrue(DBRefUtils.isDasCoordinateSystem("pdbresnum",
115             new DBRefEntry("pdb", "v1", "a1")));
116     assertFalse(DBRefUtils.isDasCoordinateSystem("pdb", new DBRefEntry(
117             "pdb", "v1", "a1")));
118
119     assertTrue(DBRefUtils.isDasCoordinateSystem("UNIPROT", new DBRefEntry(
120             "Uniprot", "v1", "a1")));
121     assertTrue(DBRefUtils.isDasCoordinateSystem("Uniprot", new DBRefEntry(
122             "UNIPROT", "v1", "a1")));
123     assertFalse(DBRefUtils.isDasCoordinateSystem("UNIPROTKB",
124             new DBRefEntry("pdb", "v1", "a1")));
125
126     assertTrue(DBRefUtils.isDasCoordinateSystem("EMBL", new DBRefEntry(
127             "EMBL", "v1", "a1")));
128     assertTrue(DBRefUtils.isDasCoordinateSystem("embl", new DBRefEntry(
129             "embl", "v1", "a1")));
130   }
131
132   /**
133    * Test 'parsing' a DBRef - non PDB case
134    */
135   @Test(groups = { "Functional" })
136   public void testParseToDbRef()
137   {
138     SequenceI seq = new Sequence("Seq1", "ABCD");
139     DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "EMBL", "1.2", "a7890");
140     DBRefEntry[] refs = seq.getDBRef();
141     assertEquals(1, refs.length);
142     assertSame(ref, refs[0]);
143     assertEquals("EMBL", ref.getSource());
144     assertEquals("1.2", ref.getVersion());
145     assertEquals("a7890", ref.getAccessionId());
146     assertNull(seq.getAllPDBEntries());
147   }
148
149   /**
150    * Test 'parsing' a DBRef - Stockholm PDB format
151    */
152   @Test(groups = { "Functional" })
153   public void testParseToDbRef_PDB()
154   {
155     SequenceI seq = new Sequence("Seq1", "ABCD");
156     DBRefEntry ref = DBRefUtils.parseToDbRef(seq, "pdb", "1.2",
157             "1WRI A; 7-80;");
158     DBRefEntry[] refs = seq.getDBRef();
159     assertEquals(1, refs.length);
160     assertSame(ref, refs[0]);
161     assertEquals("PDB", ref.getSource());
162     assertEquals("1.2", ref.getVersion());
163     // DBRef id is pdbId + chain code
164     assertEquals("1WRIA", ref.getAccessionId());
165     assertEquals(1, seq.getAllPDBEntries().size());
166     PDBEntry pdbRef = seq.getAllPDBEntries().get(0);
167     assertEquals("1WRI", pdbRef.getId());
168     assertNull(pdbRef.getFile());
169     assertEquals("A", pdbRef.getChainCode());
170     assertEquals("PDB", pdbRef.getType());
171   }
172
173   /**
174    * Test the method that searches for matches references - case when we are
175    * matching a reference with no mappings
176    */
177   @Test(groups = { "Functional" })
178   public void testSearchRefs_noMapping()
179   {
180     DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
181
182     DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // matches
183     // constructor changes embl to EMBL
184     DBRefEntry ref2 = new DBRefEntry("embl", "1", "A1234"); // matches
185     // constructor does not upper-case accession id
186     DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "a1234"); // no match
187     DBRefEntry ref4 = new DBRefEntry("EMBLCDS", "1", "A1234"); // no match
188     // ref5 matches although it has a mapping - ignored
189     DBRefEntry ref5 = new DBRefEntry("EMBL", "1", "A1234");
190     ref5.setMap(new Mapping(new MapList(new int[] { 1, 1 }, new int[] { 1,
191         1 }, 1, 1)));
192
193     DBRefEntry[] matches = DBRefUtils.searchRefs(new DBRefEntry[] { ref1,
194         ref2, ref3, ref4, ref5 }, target);
195     assertEquals(3, matches.length);
196     assertSame(ref1, matches[0]);
197     assertSame(ref2, matches[1]);
198     assertSame(ref5, matches[2]);
199   }
200
201   /**
202    * Test the method that searches for matches references - case when we are
203    * matching a reference with a mapping
204    */
205   @Test(groups = { "Functional" })
206   public void testSearchRefs_withMapping()
207   {
208     DBRefEntry target = new DBRefEntry("EMBL", "2", "A1234");
209     final Mapping map1 = new Mapping(new MapList(new int[] { 1, 1 },
210             new int[] { 1, 1 }, 1, 1));
211     target.setMap(map1);
212
213     // these all match target iff mappings match
214     DBRefEntry ref1 = new DBRefEntry("EMBL", "1", "A1234"); // no map: matches
215     DBRefEntry ref2 = new DBRefEntry("EMBL", "1", "A1234"); // =map: matches
216     final Mapping map2 = new Mapping(new MapList(new int[] { 1, 1 },
217             new int[] { 1, 1 }, 1, 1));
218     ref2.setMap(map2);
219
220     // different map: no match
221     DBRefEntry ref3 = new DBRefEntry("EMBL", "1", "A1234");
222     final Mapping map3 = new Mapping(new MapList(new int[] { 1, 1 },
223             new int[] { 1, 1 }, 2, 2));
224     ref3.setMap(map3);
225
226     DBRefEntry[] matches = DBRefUtils.searchRefs(new DBRefEntry[] { ref1,
227         ref2, ref3 }, target);
228     assertEquals(2, matches.length);
229     assertSame(ref1, matches[0]);
230     assertSame(ref2, matches[1]);
231   }
232 }