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