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