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