JAL-2492 use SequenceFeatures.getNonPositionalFeatures()
[jalview.git] / test / jalview / io / SequenceAnnotationReportTest.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.io;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.JvOptionPane;
31 import jalview.io.gff.GffConstants;
32
33 import java.util.HashMap;
34 import java.util.Hashtable;
35 import java.util.Map;
36
37 import junit.extensions.PA;
38
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
41
42 public class SequenceAnnotationReportTest
43 {
44
45   @BeforeClass(alwaysRun = true)
46   public void setUpJvOptionPane()
47   {
48     JvOptionPane.setInteractiveMode(false);
49     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
50   }
51
52   @Test(groups = "Functional")
53   public void testAppendFeature_disulfideBond()
54   {
55     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
56     StringBuilder sb = new StringBuilder();
57     sb.append("123456");
58     SequenceFeature sf = new SequenceFeature("disulfide bond", "desc", 1,
59             3, 1.2f, "group");
60
61     // residuePos == 2 does not match start or end of feature, nothing done:
62     sar.appendFeature(sb, 2, null, sf);
63     assertEquals("123456", sb.toString());
64
65     // residuePos == 1 matches start of feature, text appended (but no <br>)
66     // feature score is not included
67     sar.appendFeature(sb, 1, null, sf);
68     assertEquals("123456disulfide bond 1:3", sb.toString());
69
70     // residuePos == 3 matches end of feature, text appended
71     // <br> is prefixed once sb.length() > 6
72     sar.appendFeature(sb, 3, null, sf);
73     assertEquals("123456disulfide bond 1:3<br>disulfide bond 1:3",
74             sb.toString());
75   }
76
77   @Test(groups = "Functional")
78   public void testAppendFeature_status()
79   {
80     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
81     StringBuilder sb = new StringBuilder();
82     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
83             Float.NaN, "group");
84     sf.setStatus("Confirmed");
85
86     sar.appendFeature(sb, 1, null, sf);
87     assertEquals("METAL 1 3; Fe2-S; (Confirmed)", sb.toString());
88   }
89
90   @Test(groups = "Functional")
91   public void testAppendFeature_withScore()
92   {
93     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
94     StringBuilder sb = new StringBuilder();
95     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
96             "group");
97
98     Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
99     sar.appendFeature(sb, 1, minmax, sf);
100     /*
101      * map has no entry for this feature type - score is not shown:
102      */
103     assertEquals("METAL 1 3; Fe2-S", sb.toString());
104
105     /*
106      * map has entry for this feature type - score is shown:
107      */
108     minmax.put("METAL", new float[][] { { 0f, 1f }, null });
109     sar.appendFeature(sb, 1, minmax, sf);
110     // <br> is appended to a buffer > 6 in length
111     assertEquals("METAL 1 3; Fe2-S<br>METAL 1 3; Fe2-S Score=1.3",
112             sb.toString());
113
114     /*
115      * map has min == max for this feature type - score is not shown:
116      */
117     minmax.put("METAL", new float[][] { { 2f, 2f }, null });
118     sb.setLength(0);
119     sar.appendFeature(sb, 1, minmax, sf);
120     assertEquals("METAL 1 3; Fe2-S", sb.toString());
121   }
122
123   @Test(groups = "Functional")
124   public void testAppendFeature_noScore()
125   {
126     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
127     StringBuilder sb = new StringBuilder();
128     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
129             Float.NaN, "group");
130
131     sar.appendFeature(sb, 1, null, sf);
132     assertEquals("METAL 1 3; Fe2-S", sb.toString());
133   }
134
135   @Test(groups = "Functional")
136   public void testAppendFeature_clinicalSignificance()
137   {
138     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
139     StringBuilder sb = new StringBuilder();
140     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
141             Float.NaN, "group");
142     sf.setValue("clinical_significance", "Benign");
143
144     sar.appendFeature(sb, 1, null, sf);
145     assertEquals("METAL 1 3; Fe2-S; Benign", sb.toString());
146   }
147
148   @Test(groups = "Functional")
149   public void testAppendFeature_withScoreStatusClinicalSignificance()
150   {
151     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
152     StringBuilder sb = new StringBuilder();
153     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
154             "group");
155     sf.setStatus("Confirmed");
156     sf.setValue("clinical_significance", "Benign");
157     Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
158     minmax.put("METAL", new float[][] { { 0f, 1f }, null });
159     sar.appendFeature(sb, 1, minmax, sf);
160
161     assertEquals("METAL 1 3; Fe2-S Score=1.3; (Confirmed); Benign",
162             sb.toString());
163   }
164
165   @Test(groups = "Functional")
166   public void testAppendFeature_DescEqualsType()
167   {
168     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
169     StringBuilder sb = new StringBuilder();
170     SequenceFeature sf = new SequenceFeature("METAL", "METAL", 1, 3,
171             Float.NaN, "group");
172
173     // description is not included if it duplicates type:
174     sar.appendFeature(sb, 1, null, sf);
175     assertEquals("METAL 1 3", sb.toString());
176
177     sb.setLength(0);
178     sf.setDescription("Metal");
179     // test is case-sensitive:
180     sar.appendFeature(sb, 1, null, sf);
181     assertEquals("METAL 1 3; Metal", sb.toString());
182   }
183
184   @Test(groups = "Functional")
185   public void testAppendFeature_stripHtml()
186   {
187     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
188     StringBuilder sb = new StringBuilder();
189     SequenceFeature sf = new SequenceFeature("METAL",
190             "<html><body>hello<em>world</em></body></html>", 1, 3,
191             Float.NaN, "group");
192
193     sar.appendFeature(sb, 1, null, sf);
194     // !! strips off </body> but not <body> ??
195     assertEquals("METAL 1 3; <body>hello<em>world</em>", sb.toString());
196
197     sb.setLength(0);
198     sf.setDescription("<br>&kHD>6");
199     sar.appendFeature(sb, 1, null, sf);
200     // if no <html> tag, html-encodes > and < (only):
201     assertEquals("METAL 1 3; &lt;br&gt;&kHD&gt;6", sb.toString());
202   }
203
204   @Test(groups = "Functional")
205   public void testCreateSequenceAnnotationReport()
206   {
207     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
208     StringBuilder sb = new StringBuilder();
209
210     SequenceI seq = new Sequence("s1", "MAKLKRFQSSTLL");
211     seq.setDescription("SeqDesc");
212
213     sar.createSequenceAnnotationReport(sb, seq, true, true, null);
214
215     /*
216      * positional features are ignored
217      */
218     seq.addSequenceFeature(new SequenceFeature("Domain", "Ferredoxin", 5,
219             10, 1f, null));
220     assertEquals("<i><br>SeqDesc</i>", sb.toString());
221
222     /*
223      * non-positional feature
224      */
225     seq.addSequenceFeature(new SequenceFeature("Type1", "Nonpos", 0, 0, 1f,
226             null));
227     sb.setLength(0);
228     sar.createSequenceAnnotationReport(sb, seq, true, true, null);
229     String expected = "<i><br>SeqDesc<br>Type1 ; Nonpos</i>";
230     assertEquals(expected, sb.toString());
231
232     /*
233      * non-positional features not wanted
234      */
235     sb.setLength(0);
236     sar.createSequenceAnnotationReport(sb, seq, true, false, null);
237     assertEquals("<i><br>SeqDesc</i>", sb.toString());
238
239     /*
240      * add non-pos feature with score inside min-max range for feature type
241      * minmax holds { [positionalMin, positionalMax], [nonPosMin, nonPosMax] }
242      * score is only appended for positional features so ignored here!
243      * minMax are not recorded for non-positional features
244      */
245     seq.addSequenceFeature(new SequenceFeature("Metal", "Desc", 0, 0, 5f,
246             null));
247     Map<String, float[][]> minmax = new HashMap<String, float[][]>();
248     minmax.put("Metal", new float[][] { null, new float[] { 2f, 5f } });
249     sb.setLength(0);
250     sar.createSequenceAnnotationReport(sb, seq, true, true, minmax);
251     expected = "<i><br>SeqDesc<br>Metal ; Desc<br>Type1 ; Nonpos</i>";
252     assertEquals(expected, sb.toString());
253     
254     /*
255      * 'linkonly' features are ignored; this is obsolete, as linkonly
256      * is only set by DasSequenceFetcher, and DAS is history
257      */
258     SequenceFeature sf = new SequenceFeature("Metal", "Desc", 0, 0, 5f,
259             null);
260     sf.setValue("linkonly", Boolean.TRUE);
261     seq.addSequenceFeature(sf);
262     sb.setLength(0);
263     sar.createSequenceAnnotationReport(sb, seq, true, true, minmax);
264     assertEquals(expected, sb.toString()); // unchanged!
265
266     /*
267      * 'clinical_significance' currently being specially included
268      */
269     SequenceFeature sf2 = new SequenceFeature("Variant", "Havana", 0, 0,
270             5f, null);
271     sf2.setValue(GffConstants.CLINICAL_SIGNIFICANCE, "benign");
272     seq.addSequenceFeature(sf2);
273     sb.setLength(0);
274     sar.createSequenceAnnotationReport(sb, seq, true, true, minmax);
275     expected = "<i><br>SeqDesc<br>Metal ; Desc<br>Type1 ; Nonpos<br>Variant ; Havana; benign</i>";
276     assertEquals(expected, sb.toString());
277
278     /*
279      * add dbrefs
280      */
281     seq.addDBRef(new DBRefEntry("PDB", "0", "3iu1"));
282     seq.addDBRef(new DBRefEntry("Uniprot", "1", "P30419"));
283     // with showDbRefs = false
284     sb.setLength(0);
285     sar.createSequenceAnnotationReport(sb, seq, false, true, minmax);
286     assertEquals(expected, sb.toString()); // unchanged
287     // with showDbRefs = true
288     sb.setLength(0);
289     sar.createSequenceAnnotationReport(sb, seq, true, true, minmax);
290     expected = "<i><br>SeqDesc<br>UNIPROT P30419<br>PDB 3iu1<br>Metal ; Desc<br>Type1 ; Nonpos<br>Variant ; Havana; benign</i>";
291     assertEquals(expected, sb.toString());
292     // with showNonPositionalFeatures = false
293     sb.setLength(0);
294     sar.createSequenceAnnotationReport(sb, seq, true, false, minmax);
295     expected = "<i><br>SeqDesc<br>UNIPROT P30419<br>PDB 3iu1</i>";
296     assertEquals(expected, sb.toString());
297
298     // see other tests for treatment of status and html
299   }
300
301   /**
302    * Test that exercises an abbreviated sequence details report, with ellipsis
303    * where there are more than 40 different sources, or more than 4 dbrefs for a
304    * single source
305    */
306   @Test(groups = "Functional")
307   public void testCreateSequenceAnnotationReport_withEllipsis()
308   {
309     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
310     StringBuilder sb = new StringBuilder();
311   
312     SequenceI seq = new Sequence("s1", "ABC");
313
314     int maxSources = (int) PA.getValue(sar, "MAX_SOURCES");
315     for (int i = 0; i <= maxSources; i++)
316     {
317       seq.addDBRef(new DBRefEntry("PDB" + i, "0", "3iu1"));
318     }
319     
320     int maxRefs = (int) PA.getValue(sar, "MAX_REFS_PER_SOURCE");
321     for (int i = 0; i <= maxRefs; i++)
322     {
323       seq.addDBRef(new DBRefEntry("Uniprot", "0", "P3041" + i));
324     }
325   
326     sar.createSequenceAnnotationReport(sb, seq, true, true, null, true);
327     String report = sb.toString();
328     assertTrue(report
329             .startsWith("<i><br>UNIPROT P30410, P30411, P30412, P30413,...<br>PDB0 3iu1"));
330     assertTrue(report
331             .endsWith("<br>PDB7 3iu1<br>PDB8,...<br>(Output Sequence Details to list all database references)</i>"));
332   }
333 }