JAL-2157 JAL-1803 refactored Sequence.updatePDBIds, parse DBRefs with
[jalview.git] / test / jalview / datamodel / PDBEntryTest.java
1 /*
2     assertEquals(case7, case9);
3  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
4  * Copyright (C) $$Year-Rel$$ The Jalview Authors
5  * 
6  * This file is part of Jalview.
7  * 
8  * Jalview is free software: you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License 
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *  
13  * Jalview is distributed in the hope that it will be useful, but 
14  * WITHOUT ANY WARRANTY; without even the implied warranty 
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
16  * PURPOSE.  See the GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
20  * The Jalview Authors are detailed in the 'AUTHORS' file.
21  */
22 package jalview.datamodel;
23
24 import static org.testng.Assert.assertEquals;
25 import static org.testng.Assert.assertFalse;
26 import static org.testng.Assert.assertNotEquals;
27 import static org.testng.Assert.assertNotSame;
28 import static org.testng.Assert.assertNull;
29 import static org.testng.Assert.assertSame;
30 import static org.testng.Assert.assertTrue;
31 import static org.testng.Assert.fail;
32
33 import jalview.datamodel.PDBEntry.Type;
34
35 import java.util.Hashtable;
36
37 //import org.testng.Assert;
38 import org.testng.annotations.AfterMethod;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
41
42 public class PDBEntryTest
43 {
44
45   @BeforeMethod(alwaysRun = true)
46   public void setUp() throws Exception
47   {
48   }
49
50   @AfterMethod(alwaysRun = true)
51   public void tearDown() throws Exception
52   {
53   }
54
55   @Test(groups = { "Functional" })
56   public void testEquals()
57   {
58     PDBEntry pdbEntry = new PDBEntry("1xyz", "A", PDBEntry.Type.PDB,
59             "x/y/z/File");
60
61     // id comparison is not case sensitive
62     PDBEntry case1 = new PDBEntry("1XYZ", "A", PDBEntry.Type.PDB,
63             "x/y/z/File");
64     // chain code comparison is not case sensitive
65     PDBEntry case2 = new PDBEntry("1xyz", "a", PDBEntry.Type.PDB,
66             "x/y/z/File");
67     // different type
68     PDBEntry case3 = new PDBEntry("1xyz", "A", PDBEntry.Type.FILE,
69             "x/y/z/File");
70     // different everything
71     PDBEntry case4 = new PDBEntry(null, null, null, null);
72     // null id
73     PDBEntry case5 = new PDBEntry(null, "A", PDBEntry.Type.PDB,
74             "x/y/z/File");
75     // null chain
76     PDBEntry case6 = new PDBEntry("1xyz", null, PDBEntry.Type.PDB,
77             "x/y/z/File");
78     // null type
79     PDBEntry case7 = new PDBEntry("1xyz", "A", null, "x/y/z/File");
80     // null file
81     PDBEntry case8 = new PDBEntry("1xyz", "A", PDBEntry.Type.PDB, null);
82     // identical to case7
83     PDBEntry case9 = new PDBEntry("1xyz", "A", null, "x/y/z/File");
84     // different file only
85     PDBEntry case10 = new PDBEntry("1xyz", "A", null, "a/b/c/File");
86
87     /*
88      * assertEquals will invoke PDBEntry.equals()
89      */
90     assertFalse(pdbEntry.equals(null));
91     assertFalse(pdbEntry.equals("a"));
92     assertEquals(case1, pdbEntry);
93     assertEquals(case2, pdbEntry);
94     assertNotEquals(case3, pdbEntry);
95     assertNotEquals(case4, pdbEntry);
96     assertNotEquals(case5, pdbEntry);
97     assertNotEquals(case6, pdbEntry);
98     assertNotEquals(case7, pdbEntry);
99     assertNotEquals(case8, pdbEntry);
100     assertEquals(case7, case9);
101     assertNotEquals(case9, case10);
102
103     // add properties
104     case7.getProperty().put("hello", "world");
105     assertNotEquals(case7, case9);
106     case9.getProperty().put("hello", "world");
107     assertEquals(case7, case9);
108     case9.getProperty().put("hello", "WORLD");
109     assertNotEquals(case7, case9);
110
111     /*
112      * change string wrapper property to string...
113      */
114     case1.getProperty().put("chain_code", "a");
115     assertFalse(pdbEntry.equals(case1));
116     assertFalse(case1.equals(pdbEntry));
117   }
118
119   @Test(groups = { "Functional" })
120   public void testSetChainCode()
121   {
122     PDBEntry pdbEntry = new PDBEntry("1xyz", null, PDBEntry.Type.PDB,
123             "x/y/z/File");
124     assertNull(pdbEntry.getChainCode());
125
126     pdbEntry.setChainCode("a");
127     assertEquals("a", pdbEntry.getChainCode());
128
129     pdbEntry.setChainCode(null);
130     assertNull(pdbEntry.getChainCode());
131   }
132
133   @Test(groups = { "Functional" })
134   public void testGetType()
135   {
136     assertSame(PDBEntry.Type.FILE, PDBEntry.Type.getType("FILE"));
137     assertSame(PDBEntry.Type.FILE, PDBEntry.Type.getType("File"));
138     assertSame(PDBEntry.Type.FILE, PDBEntry.Type.getType("file"));
139     assertNotSame(PDBEntry.Type.FILE, PDBEntry.Type.getType("file "));
140   }
141
142   @Test(groups = { "Functional" })
143   public void testTypeMatches()
144   {
145     // TODO Type.matches() is not used - delete?
146     assertTrue(PDBEntry.Type.FILE.matches("FILE"));
147     assertTrue(PDBEntry.Type.FILE.matches("File"));
148     assertTrue(PDBEntry.Type.FILE.matches("file"));
149     assertFalse(PDBEntry.Type.FILE.matches("FILE "));
150   }
151
152   @Test(groups = { "Functional" })
153   public void testUpdateFrom()
154   {
155     PDBEntry pdb1 = new PDBEntry("3A6S", null, null, null);
156     PDBEntry pdb2 = new PDBEntry("3A6S", null, null, null);
157     assertTrue(pdb1.updateFrom(pdb2));
158
159     /*
160      * mismatch of pdb id not allowed
161      */
162     pdb2 = new PDBEntry("1A70", "A", null, null);
163     assertFalse(pdb1.updateFrom(pdb2));
164     assertNull(pdb1.getChainCode());
165
166     /*
167      * match of pdb id is not case sensitive
168      */
169     pdb2 = new PDBEntry("3a6s", "A", null, null);
170     assertTrue(pdb1.updateFrom(pdb2));
171     assertEquals(pdb1.getChainCode(), "A");
172     assertEquals(pdb1.getId(), "3A6S");
173
174     /*
175      * add chain - with differing case for id
176      */
177     pdb1 = new PDBEntry("3A6S", null, null, null);
178     pdb2 = new PDBEntry("3a6s", "A", null, null);
179     assertTrue(pdb1.updateFrom(pdb2));
180     assertEquals(pdb1.getChainCode(), "A");
181
182     /*
183      * change of chain is not allowed
184      */
185     pdb2 = new PDBEntry("3A6S", "B", null, null);
186     assertFalse(pdb1.updateFrom(pdb2));
187     assertEquals(pdb1.getChainCode(), "A");
188
189     /*
190      * change chain from null
191      */
192     pdb1 = new PDBEntry("3A6S", null, null, null);
193     pdb2 = new PDBEntry("3A6S", "B", null, null);
194     assertTrue(pdb1.updateFrom(pdb2));
195     assertEquals(pdb1.getChainCode(), "B");
196
197     /*
198      * set file and type
199      */
200     pdb2 = new PDBEntry("3A6S", "B", Type.FILE, "filePath");
201     assertTrue(pdb1.updateFrom(pdb2));
202     assertEquals(pdb1.getFile(), "filePath");
203     assertEquals(pdb1.getType(), Type.FILE.toString());
204
205     /*
206      * change of file is not allowed
207      */
208     pdb1 = new PDBEntry("3A6S", null, null, "file1");
209     pdb2 = new PDBEntry("3A6S", "A", null, "file2");
210     assertFalse(pdb1.updateFrom(pdb2));
211     assertNull(pdb1.getChainCode());
212     assertEquals(pdb1.getFile(), "file1");
213
214     /*
215      * set type without change of file
216      */
217     pdb1 = new PDBEntry("3A6S", null, null, "file1");
218     pdb2 = new PDBEntry("3A6S", null, Type.PDB, "file1");
219     assertTrue(pdb1.updateFrom(pdb2));
220     assertEquals(pdb1.getType(), Type.PDB.toString());
221
222     /*
223      * set file with differing case of id and chain code
224      */
225     pdb1 = new PDBEntry("3A6S", "A", null, null);
226     pdb2 = new PDBEntry("3a6s", "a", Type.PDB, "file1");
227     assertTrue(pdb1.updateFrom(pdb2));
228     assertEquals(pdb1.getType(), Type.PDB.toString());
229     assertEquals(pdb1.getId(), "3A6S"); // unchanged
230     assertEquals(pdb1.getFile(), "file1"); // updated
231     assertEquals(pdb1.getChainCode(), "A"); // unchanged
232
233     /*
234      * changing nothing returns true
235      */
236     pdb1 = new PDBEntry("3A6S", "A", Type.PDB, "file1");
237     pdb2 = new PDBEntry("3A6S", null, null, null);
238     assertTrue(pdb1.updateFrom(pdb2));
239     assertEquals(pdb1.getChainCode(), "A");
240     assertEquals(pdb1.getType(), Type.PDB.toString());
241     assertEquals(pdb1.getFile(), "file1");
242
243     /*
244      * add and update properties only
245      */
246     pdb1 = new PDBEntry("3A6S", null, null, null);
247     pdb2 = new PDBEntry("3A6S", null, null, null);
248     // ughh properties not null if chain code has been set...
249     // JAL-2196 addresses this
250     pdb1.properties = new Hashtable();
251     pdb2.properties = new Hashtable();
252     pdb1.properties.put("destination", "mars");
253     pdb1.properties.put("hello", "world");
254     pdb2.properties.put("hello", "moon");
255     pdb2.properties.put("goodbye", "world");
256     assertTrue(pdb1.updateFrom(pdb2));
257     assertEquals(pdb1.properties.get("destination"), "mars");
258     assertEquals(pdb1.properties.get("hello"), "moon");
259     assertEquals(pdb1.properties.get("goodbye"), "world");
260
261     /*
262      * add properties only
263      */
264     pdb1 = new PDBEntry("3A6S", null, null, null);
265     pdb2 = new PDBEntry("3A6S", null, null, null);
266     pdb2.properties = new Hashtable();
267     pdb2.properties.put("hello", "moon");
268     assertTrue(pdb1.updateFrom(pdb2));
269     assertEquals(pdb1.properties.get("hello"), "moon");
270   }
271
272   @Test(groups = { "Functional" })
273   public void testConstructor_fromDbref()
274   {
275     PDBEntry pdb = new PDBEntry(new DBRefEntry("PDB", "0", "1A70"));
276     assertEquals(pdb.getId(), "1A70");
277     assertNull(pdb.getChainCode());
278     assertNull(pdb.getType());
279     assertNull(pdb.getFile());
280
281     /*
282      * from dbref with chain code appended
283      */
284     pdb = new PDBEntry(new DBRefEntry("PDB", "0", "1A70B"));
285     assertEquals(pdb.getId(), "1A70");
286     assertEquals(pdb.getChainCode(), "B");
287
288     /*
289      * from dbref with overlong accession
290      */
291     pdb = new PDBEntry(new DBRefEntry("PDB", "0", "1A70BC"));
292     assertEquals(pdb.getId(), "1A70BC");
293     assertNull(pdb.getChainCode());
294
295     /*
296      * from dbref which is not for PDB
297      */
298     try
299     {
300       pdb = new PDBEntry(new DBRefEntry("PDBe", "0", "1A70"));
301       fail("Expected exception");
302     } catch (IllegalArgumentException e)
303     {
304       // expected;
305     }
306   }
307
308 }