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