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