JAL-3468 Fixed tests and added test for max-length of feature set tooltip
[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.api.FeatureColourI;
27 import jalview.datamodel.DBRefEntry;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceI;
31 import jalview.gui.JvOptionPane;
32 import jalview.io.gff.GffConstants;
33 import jalview.renderer.seqfeatures.FeatureRenderer;
34 import jalview.schemes.FeatureColour;
35 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
36
37 import java.awt.Color;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Map;
41
42 import org.testng.annotations.BeforeClass;
43 import org.testng.annotations.Test;
44
45 import junit.extensions.PA;
46
47 public class SequenceAnnotationReportTest
48 {
49
50   @BeforeClass(alwaysRun = true)
51   public void setUpJvOptionPane()
52   {
53     JvOptionPane.setInteractiveMode(false);
54     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
55   }
56
57   @Test(groups = "Functional")
58   public void testAppendFeature_disulfideBond()
59   {
60     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
61     StringBuilder sb = new StringBuilder();
62     sb.append("123456");
63     SequenceFeature sf = new SequenceFeature("disulfide bond", "desc", 1,
64             3, 1.2f, "group");
65
66     // residuePos == 2 does not match start or end of feature, nothing done:
67     sar.appendFeature(sb, 2, null, sf, null);
68     assertEquals("123456", sb.toString());
69
70     // residuePos == 1 matches start of feature, text appended (but no <br/>)
71     // feature score is not included
72     sar.appendFeature(sb, 1, null, sf, null);
73     assertEquals("123456disulfide bond 1:3", sb.toString());
74
75     // residuePos == 3 matches end of feature, text appended
76     // <br/> is prefixed once sb.length() > 6
77     sar.appendFeature(sb, 3, null, sf, null);
78     assertEquals("123456disulfide bond 1:3<br/>disulfide bond 1:3",
79             sb.toString());
80   }
81
82   @Test(groups = "Functional")
83   public void testAppendFeatures_longText()
84   {
85     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
86     StringBuilder sb = new StringBuilder();
87     String longString = "Abcd".repeat(50);
88     SequenceFeature sf = new SequenceFeature("sequence", longString, 1, 3,
89             "group");
90
91     sar.appendFeature(sb, 1, null, sf, null);
92     assertTrue(sb.length() < 100);
93
94     List<SequenceFeature> sfl = new ArrayList<>();
95     sb.setLength(0);
96     sfl.add(sf);
97     sfl.add(sf);
98     sfl.add(sf);
99     sfl.add(sf);
100     sfl.add(sf);
101     sfl.add(sf);
102     sfl.add(sf);
103     sfl.add(sf);
104     sfl.add(sf);
105     sfl.add(sf);
106     int n = sar.appendFeaturesLengthLimit(sb, 1, sfl,
107             new FeatureRenderer(null), 200); // text should terminate before 200 characters
108     String s = sb.toString();
109     assertTrue(s.length() < 200);
110     assertEquals(n, 7); // should be 7 features left over
111
112   }
113
114   @Test(groups = "Functional")
115   public void testAppendFeature_status()
116   {
117     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
118     StringBuilder sb = new StringBuilder();
119     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
120             Float.NaN, "group");
121     sf.setStatus("Confirmed");
122
123     sar.appendFeature(sb, 1, null, sf, null);
124     assertEquals("METAL 1 3; Fe2-S; (Confirmed)", sb.toString());
125   }
126
127   @Test(groups = "Functional")
128   public void testAppendFeature_withScore()
129   {
130     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
131     StringBuilder sb = new StringBuilder();
132     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
133             "group");
134
135     FeatureRendererModel fr = new FeatureRenderer(null);
136     Map<String, float[][]> minmax = fr.getMinMax();
137     sar.appendFeature(sb, 1, fr, sf, null);
138     /*
139      * map has no entry for this feature type - score is not shown:
140      */
141     assertEquals("METAL 1 3; Fe2-S", sb.toString());
142
143     /*
144      * map has entry for this feature type - score is shown:
145      */
146     minmax.put("METAL", new float[][] { { 0f, 1f }, null });
147     sar.appendFeature(sb, 1, fr, sf, null);
148     // <br/> is appended to a buffer > 6 in length
149     assertEquals("METAL 1 3; Fe2-S<br/>METAL 1 3; Fe2-S Score=1.3",
150             sb.toString());
151
152     /*
153      * map has min == max for this feature type - score is not shown:
154      */
155     minmax.put("METAL", new float[][] { { 2f, 2f }, null });
156     sb.setLength(0);
157     sar.appendFeature(sb, 1, fr, sf, null);
158     assertEquals("METAL 1 3; Fe2-S", sb.toString());
159   }
160
161   @Test(groups = "Functional")
162   public void testAppendFeature_noScore()
163   {
164     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
165     StringBuilder sb = new StringBuilder();
166     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
167             Float.NaN, "group");
168
169     sar.appendFeature(sb, 1, null, sf, null);
170     assertEquals("METAL 1 3; Fe2-S", sb.toString());
171   }
172
173   /**
174    * A specific attribute value is included if it is used to colour the feature
175    */
176   @Test(groups = "Functional")
177   public void testAppendFeature_colouredByAttribute()
178   {
179     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
180     StringBuilder sb = new StringBuilder();
181     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
182             Float.NaN, "group");
183     sf.setValue("clinical_significance", "Benign");
184
185     /*
186      * first with no colour by attribute
187      */
188     FeatureRendererModel fr = new FeatureRenderer(null);
189     sar.appendFeature(sb, 1, fr, sf, null);
190     assertEquals("METAL 1 3; Fe2-S", sb.toString());
191
192     /*
193      * then with colour by an attribute the feature lacks
194      */
195     FeatureColourI fc = new FeatureColour(null, Color.white, Color.black,
196             null, 5, 10);
197     fc.setAttributeName("Pfam");
198     fr.setColour("METAL", fc);
199     sb.setLength(0);
200     sar.appendFeature(sb, 1, fr, sf, null);
201     assertEquals("METAL 1 3; Fe2-S", sb.toString()); // no change
202
203     /*
204      * then with colour by an attribute the feature has
205      */
206     fc.setAttributeName("clinical_significance");
207     sb.setLength(0);
208     sar.appendFeature(sb, 1, fr, sf, null);
209     assertEquals("METAL 1 3; Fe2-S; clinical_significance=Benign",
210             sb.toString());
211   }
212
213   @Test(groups = "Functional")
214   public void testAppendFeature_withScoreStatusAttribute()
215   {
216     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
217     StringBuilder sb = new StringBuilder();
218     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
219             "group");
220     sf.setStatus("Confirmed");
221     sf.setValue("clinical_significance", "Benign");
222
223     FeatureRendererModel fr = new FeatureRenderer(null);
224     Map<String, float[][]> minmax = fr.getMinMax();
225     FeatureColourI fc = new FeatureColour(null, Color.white, Color.blue,
226             null, 12, 22);
227     fc.setAttributeName("clinical_significance");
228     fr.setColour("METAL", fc);
229     minmax.put("METAL", new float[][] { { 0f, 1f }, null });
230     sar.appendFeature(sb, 1, fr, sf, null);
231
232     assertEquals(
233             "METAL 1 3; Fe2-S Score=1.3; (Confirmed); clinical_significance=Benign",
234             sb.toString());
235   }
236
237   @Test(groups = "Functional")
238   public void testAppendFeature_DescEqualsType()
239   {
240     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
241     StringBuilder sb = new StringBuilder();
242     SequenceFeature sf = new SequenceFeature("METAL", "METAL", 1, 3,
243             Float.NaN, "group");
244
245     // description is not included if it duplicates type:
246     sar.appendFeature(sb, 1, null, sf, null);
247     assertEquals("METAL 1 3", sb.toString());
248
249     sb.setLength(0);
250     sf.setDescription("Metal");
251     // test is case-sensitive:
252     sar.appendFeature(sb, 1, null, sf, null);
253     assertEquals("METAL 1 3; Metal", sb.toString());
254   }
255
256   @Test(groups = "Functional")
257   public void testAppendFeature_stripHtml()
258   {
259     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
260     StringBuilder sb = new StringBuilder();
261     SequenceFeature sf = new SequenceFeature("METAL",
262             "<html><body>hello<em>world</em></body></html>", 1, 3,
263             Float.NaN, "group");
264
265     sar.appendFeature(sb, 1, null, sf, null);
266     // !! strips off </body> but not <body> ??
267     assertEquals("METAL 1 3; <body>hello<em>world</em>", sb.toString());
268
269     sb.setLength(0);
270     sf.setDescription("<br>&kHD>6");
271     sar.appendFeature(sb, 1, null, sf, null);
272     // if no <html> tag, html-encodes > and < (only):
273     assertEquals("METAL 1 3; &lt;br&gt;&kHD&gt;6", sb.toString());
274   }
275
276   @Test(groups = "Functional")
277   public void testCreateSequenceAnnotationReport()
278   {
279     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
280     StringBuilder sb = new StringBuilder();
281
282     SequenceI seq = new Sequence("s1", "MAKLKRFQSSTLL");
283     seq.setDescription("SeqDesc");
284
285     /*
286      * positional features are ignored
287      */
288     seq.addSequenceFeature(new SequenceFeature("Domain", "Ferredoxin", 5,
289             10, 1f, null));
290     sar.createSequenceAnnotationReport(sb, seq, true, true, null);
291     assertEquals("<i>SeqDesc</i>", sb.toString());
292
293     /*
294      * non-positional feature
295      */
296     seq.addSequenceFeature(new SequenceFeature("Type1", "Nonpos", 0, 0, 1f,
297             null));
298     sb.setLength(0);
299     sar.createSequenceAnnotationReport(sb, seq, true, true, null);
300     String expected = "<i>SeqDesc<br/>Type1 ; Nonpos Score=1.0</i>";
301     assertEquals(expected, sb.toString());
302
303     /*
304      * non-positional features not wanted
305      */
306     sb.setLength(0);
307     sar.createSequenceAnnotationReport(sb, seq, true, false, null);
308     assertEquals("<i>SeqDesc</i>", sb.toString());
309
310     /*
311      * add non-pos feature with score inside min-max range for feature type
312      * minmax holds { [positionalMin, positionalMax], [nonPosMin, nonPosMax] }
313      * score is only appended for positional features so ignored here!
314      * minMax are not recorded for non-positional features
315      */
316     seq.addSequenceFeature(new SequenceFeature("Metal", "Desc", 0, 0, 5f,
317             null));
318
319     FeatureRendererModel fr = new FeatureRenderer(null);
320     Map<String, float[][]> minmax = fr.getMinMax();
321     minmax.put("Metal", new float[][] { null, new float[] { 2f, 5f } });
322
323     sb.setLength(0);
324     sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
325     expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos</i>";
326     assertEquals(expected, sb.toString());
327     
328     /*
329      * 'linkonly' features are ignored; this is obsolete, as linkonly
330      * is only set by DasSequenceFetcher, and DAS is history
331      */
332     SequenceFeature sf = new SequenceFeature("Metal", "Desc", 0, 0, 5f,
333             null);
334     sf.setValue("linkonly", Boolean.TRUE);
335     seq.addSequenceFeature(sf);
336     sb.setLength(0);
337     sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
338     assertEquals(expected, sb.toString()); // unchanged!
339
340     /*
341      * 'clinical_significance' attribute is only included in description 
342      * when used for feature colouring
343      */
344     SequenceFeature sf2 = new SequenceFeature("Variant", "Havana", 0, 0,
345             5f, null);
346     sf2.setValue(GffConstants.CLINICAL_SIGNIFICANCE, "benign");
347     seq.addSequenceFeature(sf2);
348     sb.setLength(0);
349     sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
350     expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos<br/>Variant ; Havana</i>";
351     assertEquals(expected, sb.toString());
352
353     /*
354      * add dbrefs
355      */
356     seq.addDBRef(new DBRefEntry("PDB", "0", "3iu1"));
357     seq.addDBRef(new DBRefEntry("Uniprot", "1", "P30419"));
358
359     // with showDbRefs = false
360     sb.setLength(0);
361     sar.createSequenceAnnotationReport(sb, seq, false, true, fr);
362     assertEquals(expected, sb.toString()); // unchanged
363
364     // with showDbRefs = true, colour Variant features by clinical_significance
365     sb.setLength(0);
366     FeatureColourI fc = new FeatureColour(null, Color.green, Color.pink,
367             null, 2, 3);
368     fc.setAttributeName("clinical_significance");
369     fr.setColour("Variant", fc);
370     sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
371     expected = "<i>SeqDesc<br/>UNIPROT P30419<br/>PDB 3iu1<br/>Metal ; Desc<br/>"
372             + "Type1 ; Nonpos<br/>Variant ; Havana; clinical_significance=benign</i>";
373     assertEquals(expected, sb.toString());
374     // with showNonPositionalFeatures = false
375     sb.setLength(0);
376     sar.createSequenceAnnotationReport(sb, seq, true, false, fr);
377     expected = "<i>SeqDesc<br/>UNIPROT P30419<br/>PDB 3iu1</i>";
378     assertEquals(expected, sb.toString());
379
380     /*
381      * long feature description is truncated with ellipsis
382      */
383     sb.setLength(0);
384     sf2.setDescription(
385             "This is a very long description which should be truncated");
386     sar.createSequenceAnnotationReport(sb, seq, false, true, fr);
387     expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos<br/>Variant ; This is a very long description which sh...; clinical_significance=benign</i>";
388     assertEquals(expected, sb.toString());
389
390     // see other tests for treatment of status and html
391   }
392
393   /**
394    * Test that exercises an abbreviated sequence details report, with ellipsis
395    * where there are more than 40 different sources, or more than 4 dbrefs for a
396    * single source
397    */
398   @Test(groups = "Functional")
399   public void testCreateSequenceAnnotationReport_withEllipsis()
400   {
401     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
402     StringBuilder sb = new StringBuilder();
403   
404     SequenceI seq = new Sequence("s1", "ABC");
405
406     int maxSources = (int) PA.getValue(sar, "MAX_SOURCES");
407     for (int i = 0; i <= maxSources; i++)
408     {
409       seq.addDBRef(new DBRefEntry("PDB" + i, "0", "3iu1"));
410     }
411     
412     int maxRefs = (int) PA.getValue(sar, "MAX_REFS_PER_SOURCE");
413     for (int i = 0; i <= maxRefs; i++)
414     {
415       seq.addDBRef(new DBRefEntry("Uniprot", "0", "P3041" + i));
416     }
417   
418     sar.createSequenceAnnotationReport(sb, seq, true, true, null, true);
419     String report = sb.toString();
420     assertTrue(report
421             .startsWith(
422                     "<i><br/>UNIPROT P30410, P30411, P30412, P30413,...<br/>PDB0 3iu1"));
423     assertTrue(report
424             .endsWith(
425                     "<br/>PDB7 3iu1<br/>PDB8,...<br/>(Output Sequence Details to list all database references)</i>"));
426   }
427 }