c95597928b4490fa0c72683f84054c7f93f4b57e
[jalview.git] / SequenceFeatureTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNull;
26 import static org.testng.AssertJUnit.assertSame;
27 import static org.testng.AssertJUnit.assertTrue;
28
29 import jalview.gui.JvOptionPane;
30
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.Test;
33
34 public class SequenceFeatureTest
35 {
36
37   @BeforeClass(alwaysRun = true)
38   public void setUpJvOptionPane()
39   {
40     JvOptionPane.setInteractiveMode(false);
41     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
42   }
43
44   @Test(groups = { "Functional" })
45   public void testCopyConstructors()
46   {
47     SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
48             12.5f, "group");
49     sf1.setValue("STRAND", "+");
50     sf1.setValue("Note", "Testing");
51     Integer count = new Integer(7);
52     sf1.setValue("Count", count);
53
54     SequenceFeature sf2 = new SequenceFeature(sf1);
55     assertEquals("type", sf2.getType());
56     assertEquals("desc", sf2.getDescription());
57     assertEquals(22, sf2.getBegin());
58     assertEquals(33, sf2.getEnd());
59     assertEquals(12.5f, sf2.getScore());
60     assertEquals("+", sf2.getValue("STRAND"));
61     assertEquals("Testing", sf2.getValue("Note"));
62     // shallow clone of otherDetails map - contains the same object values!
63     assertSame(count, sf2.getValue("Count"));
64
65     /*
66      * copy constructor modifying begin/end/group/score
67      */
68     SequenceFeature sf3 = new SequenceFeature(sf1, 11, 14, "group2", 17.4f);
69     assertEquals("type", sf3.getType());
70     assertEquals("desc", sf3.getDescription());
71     assertEquals(11, sf3.getBegin());
72     assertEquals(14, sf3.getEnd());
73     assertEquals(17.4f, sf3.getScore());
74     assertEquals("+", sf3.getValue("STRAND"));
75     assertEquals("Testing", sf3.getValue("Note"));
76     // shallow clone of otherDetails map - contains the same object values!
77     assertSame(count, sf3.getValue("Count"));
78
79     /*
80      * copy constructor modifying type/begin/end/group/score
81      */
82     SequenceFeature sf4 = new SequenceFeature(sf1, "Disulfide bond", 12,
83             15, "group3", -9.1f);
84     assertEquals("Disulfide bond", sf4.getType());
85     assertTrue(sf4.isContactFeature());
86     assertEquals("desc", sf4.getDescription());
87     assertEquals(12, sf4.getBegin());
88     assertEquals(15, sf4.getEnd());
89     assertEquals(-9.1f, sf4.getScore());
90     assertEquals("+", sf4.getValue("STRAND"));
91     assertEquals("Testing", sf4.getValue("Note"));
92     // shallow clone of otherDetails map - contains the same object values!
93     assertSame(count, sf4.getValue("Count"));
94   }
95
96   /**
97    * Tests for retrieving a 'miscellaneous details' property value, with or
98    * without a supplied default
99    */
100   @Test(groups = { "Functional" })
101   public void testGetValue()
102   {
103     SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
104             12.5f, "group");
105     sf1.setValue("STRAND", "+");
106     assertEquals("+", sf1.getValue("STRAND"));
107     assertNull(sf1.getValue("strand")); // case-sensitive
108     assertEquals(".", sf1.getValue("unknown", "."));
109     Integer i = new Integer(27);
110     assertSame(i, sf1.getValue("Unknown", i));
111   }
112
113   /**
114    * Tests the method that returns 1 / -1 / 0 for strand "+" / "-" / other
115    */
116   @Test(groups = { "Functional" })
117   public void testGetStrand()
118   {
119     SequenceFeature sf = new SequenceFeature("type", "desc", 22, 33, 12.5f,
120             "group");
121     assertEquals(0, sf.getStrand());
122     sf.setValue("STRAND", "+");
123     assertEquals(1, sf.getStrand());
124     sf.setValue("STRAND", "-");
125     assertEquals(-1, sf.getStrand());
126     sf.setValue("STRAND", ".");
127     assertEquals(0, sf.getStrand());
128   }
129
130   /**
131    * Tests for equality, and that equal objects have the same hashCode
132    */
133   @Test(groups = { "Functional" })
134   public void testEqualsAndHashCode()
135   {
136     SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
137             12.5f, "group");
138     sf1.setValue("ID", "id");
139     sf1.setValue("Name", "name");
140     sf1.setValue("Parent", "parent");
141     sf1.setStrand("+");
142     sf1.setPhase("1");
143     SequenceFeature sf2 = new SequenceFeature("type", "desc", 22, 33,
144             12.5f, "group");
145     sf2.setValue("ID", "id");
146     sf2.setValue("Name", "name");
147     sf2.setValue("Parent", "parent");
148     sf2.setStrand("+");
149     sf2.setPhase("1");
150
151     assertFalse(sf1.equals(null));
152     assertTrue(sf1.equals(sf2));
153     assertTrue(sf2.equals(sf1));
154     assertEquals(sf1.hashCode(), sf2.hashCode());
155
156     // changing type breaks equals:
157     SequenceFeature sf3 = new SequenceFeature("type", "desc", 22, 33,
158             12.5f, "group");
159     SequenceFeature sf4 = new SequenceFeature("Type", "desc", 22, 33,
160             12.5f, "group");
161     assertFalse(sf3.equals(sf4));
162
163     // changing description breaks equals:
164     String restores = sf2.getDescription();
165     sf2.setDescription("Desc");
166     assertFalse(sf1.equals(sf2));
167     sf2.setDescription(restores);
168
169     // changing score breaks equals:
170     float restoref = sf2.getScore();
171     sf2 = new SequenceFeature(sf2, sf2.getBegin(), sf2.getEnd(),
172             sf2.getFeatureGroup(), 10f);
173     assertFalse(sf1.equals(sf2));
174     sf2 = new SequenceFeature(sf2, sf2.getBegin(), sf2.getEnd(),
175             sf2.getFeatureGroup(), restoref);
176
177     // NaN doesn't match a number
178     restoref = sf2.getScore();
179     sf2 = new SequenceFeature(sf2, sf2.getBegin(), sf2.getEnd(),
180             sf2.getFeatureGroup(), Float.NaN);
181     assertFalse(sf1.equals(sf2));
182
183     // NaN matches NaN
184     sf1 = new SequenceFeature(sf1, sf1.getBegin(), sf1.getEnd(),
185             sf1.getFeatureGroup(), Float.NaN);
186     assertTrue(sf1.equals(sf2));
187     sf1 = new SequenceFeature(sf1, sf1.getBegin(), sf1.getEnd(),
188             sf1.getFeatureGroup(), restoref);
189     sf2 = new SequenceFeature(sf2, sf2.getBegin(), sf2.getEnd(),
190             sf2.getFeatureGroup(), restoref);
191
192     // changing start position breaks equals:
193     int restorei = sf2.getBegin();
194     sf2 = new SequenceFeature(sf2, 21, sf2.getEnd(), sf2.getFeatureGroup(), sf2.getScore());
195     assertFalse(sf1.equals(sf2));
196     sf2 = new SequenceFeature(sf2, restorei, sf2.getEnd(),
197             sf2.getFeatureGroup(), sf2.getScore());
198
199     // changing end position breaks equals:
200     restorei = sf2.getEnd();
201     sf2 = new SequenceFeature(sf2, sf2.getBegin(), 32,
202             sf2.getFeatureGroup(), sf2.getScore());
203     assertFalse(sf1.equals(sf2));
204     sf2 = new SequenceFeature(sf2, sf2.getBegin(), restorei,
205             sf2.getFeatureGroup(), sf2.getScore());
206
207     // changing feature group breaks equals:
208     restores = sf2.getFeatureGroup();
209     sf2 = new SequenceFeature(sf2, sf2.getBegin(), sf2.getEnd(), "Group", sf2.getScore());
210     assertFalse(sf1.equals(sf2));
211     sf2 = new SequenceFeature(sf2, sf2.getBegin(), sf2.getEnd(), restores, sf2.getScore());
212
213     // changing ID breaks equals:
214     restores = (String) sf2.getValue("ID");
215     sf2.setValue("ID", "id2");
216     assertFalse(sf1.equals(sf2));
217     sf2.setValue("ID", restores);
218
219     // changing Name breaks equals:
220     restores = (String) sf2.getValue("Name");
221     sf2.setValue("Name", "Name");
222     assertFalse(sf1.equals(sf2));
223     sf2.setValue("Name", restores);
224
225     // changing Parent breaks equals:
226     restores = (String) sf1.getValue("Parent");
227     sf1.setValue("Parent", "Parent");
228     assertFalse(sf1.equals(sf2));
229     sf1.setValue("Parent", restores);
230
231     // changing strand breaks equals:
232     restorei = sf2.getStrand();
233     sf2.setStrand("-");
234     assertFalse(sf1.equals(sf2));
235     sf2.setStrand(restorei == 1 ? "+" : "-");
236
237     // changing phase breaks equals:
238     restores = sf1.getPhase();
239     sf1.setPhase("2");
240     assertFalse(sf1.equals(sf2));
241     sf1.setPhase(restores);
242
243     // restore equality as sanity check:
244     assertTrue(sf1.equals(sf2));
245     assertTrue(sf2.equals(sf1));
246     assertEquals(sf1.hashCode(), sf2.hashCode());
247
248     // changing status doesn't change equals:
249     sf1.setStatus("new");
250     assertTrue(sf1.equals(sf2));
251   }
252
253   @Test(groups = { "Functional" })
254   public void testIsContactFeature()
255   {
256     SequenceFeature sf = new SequenceFeature("type", "desc", 22, 33, 12.5f,
257             "group");
258     assertFalse(sf.isContactFeature());
259     sf = new SequenceFeature("", "desc", 22, 33, 12.5f, "group");
260     assertFalse(sf.isContactFeature());
261     sf = new SequenceFeature(null, "desc", 22, 33, 12.5f, "group");
262     assertFalse(sf.isContactFeature());
263     sf = new SequenceFeature("Disulfide Bond", "desc", 22, 33, 12.5f,
264             "group");
265     assertTrue(sf.isContactFeature());
266     sf = new SequenceFeature("disulfide bond", "desc", 22, 33, 12.5f,
267             "group");
268     assertTrue(sf.isContactFeature());
269     sf = new SequenceFeature("Disulphide Bond", "desc", 22, 33, 12.5f,
270             "group");
271     assertTrue(sf.isContactFeature());
272     sf = new SequenceFeature("disulphide bond", "desc", 22, 33, 12.5f,
273             "group");
274     assertTrue(sf.isContactFeature());
275   }
276
277   @Test(groups = { "Functional" })
278   public void testGetDetailsReport()
279   {
280     // single locus, no group, no score
281     SequenceFeature sf = new SequenceFeature("variant", "G,C", 22, 22, null);
282     String expected = "<br><table><tr><td>Type</td><td>variant</td><td></td></tr>"
283             + "<tr><td>Start/end</td><td>22</td><td></td></tr>"
284             + "<tr><td>Description</td><td>G,C</td><td></td></tr></table>";
285     assertEquals(expected, sf.getDetailsReport());
286
287     // contact feature
288     sf = new SequenceFeature("Disulphide Bond", "a description", 28, 31,
289             null);
290     expected = "<br><table><tr><td>Type</td><td>Disulphide Bond</td><td></td></tr>"
291             + "<tr><td>Start/end</td><td>28:31</td><td></td></tr>"
292             + "<tr><td>Description</td><td>a description</td><td></td></tr></table>";
293     assertEquals(expected, sf.getDetailsReport());
294
295     sf = new SequenceFeature("variant", "G,C", 22, 33,
296             12.5f, "group");
297     sf.setValue("Parent", "ENSG001");
298     sf.setValue("Child", "ENSP002");
299     expected = "<br><table><tr><td>Type</td><td>variant</td><td></td></tr>"
300             + "<tr><td>Start/end</td><td>22-33</td><td></td></tr>"
301             + "<tr><td>Description</td><td>G,C</td><td></td></tr>"
302             + "<tr><td>Score</td><td>12.5</td><td></td></tr>"
303             + "<tr><td>Group</td><td>group</td><td></td></tr>"
304             + "<tr><td>Child</td><td></td><td>ENSP002</td></tr>"
305             + "<tr><td>Parent</td><td></td><td>ENSG001</td></tr></table>";
306     assertEquals(expected, sf.getDetailsReport());
307
308     /*
309      * feature with embedded html link in description
310      */
311     String desc = "<html>Fer2 Status: True Positive <a href=\"http://pfam.xfam.org/family/PF00111\">Pfam 8_8</a></html>";
312     sf = new SequenceFeature("Pfam", desc, 8, 83, "Uniprot");
313     expected = "<br><table><tr><td>Type</td><td>Pfam</td><td></td></tr>"
314             + "<tr><td>Start/end</td><td>8-83</td><td></td></tr>"
315             + "<tr><td>Description</td><td>Fer2 Status: True Positive <a href=\"http://pfam.xfam.org/family/PF00111\">Pfam 8_8</a></td><td></td></tr>"
316             + "<tr><td>Group</td><td>Uniprot</td><td></td></tr></table>";
317     assertEquals(expected, sf.getDetailsReport());
318   }
319 }