Merge branch 'develop' into features/JAL-1723_sequenceReport
[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
25 import jalview.datamodel.SequenceFeature;
26
27 import java.util.Hashtable;
28 import java.util.Map;
29
30 import org.testng.annotations.Test;
31
32 public class SequenceAnnotationReportTest
33 {
34   @Test(groups = "Functional")
35   public void testAppendFeature_disulfideBond()
36   {
37     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
38     StringBuilder sb = new StringBuilder();
39     sb.append("123456");
40     SequenceFeature sf = new SequenceFeature("disulfide bond", "desc", 1,
41             3, 1.2f, "group");
42
43     // residuePos == 2 does not match start or end of feature, nothing done:
44     sar.appendFeature(sb, 2, null, sf);
45     assertEquals("123456", sb.toString());
46
47     // residuePos == 1 matches start of feature, text appended (but no <br>)
48     // feature score is not included
49     sar.appendFeature(sb, 1, null, sf);
50     assertEquals("123456disulfide bond 1:3", sb.toString());
51
52     // residuePos == 3 matches end of feature, text appended
53     // <br> is prefixed once sb.length() > 6
54     sar.appendFeature(sb, 3, null, sf);
55     assertEquals("123456disulfide bond 1:3<br>disulfide bond 1:3",
56             sb.toString());
57   }
58
59   @Test(groups = "Functional")
60   public void testAppendFeature_status()
61   {
62     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
63     StringBuilder sb = new StringBuilder();
64     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
65             Float.NaN, "group");
66     sf.setStatus("Confirmed");
67
68     sar.appendFeature(sb, 1, null, sf);
69     assertEquals("METAL 1 3; Fe2-S; (Confirmed)", sb.toString());
70   }
71
72   @Test(groups = "Functional")
73   public void testAppendFeature_withScore()
74   {
75     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
76     StringBuilder sb = new StringBuilder();
77     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
78             "group");
79
80     Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
81     sar.appendFeature(sb, 1, minmax, sf);
82     /*
83      * map has no entry for this feature type - score is not shown:
84      */
85     assertEquals("METAL 1 3; Fe2-S", sb.toString());
86
87     /*
88      * map has entry for this feature type - score is shown:
89      */
90     minmax.put("METAL", new float[][] { { 0f, 1f }, null });
91     sar.appendFeature(sb, 1, minmax, sf);
92     // <br> is appended to a buffer > 6 in length
93     assertEquals("METAL 1 3; Fe2-S<br>METAL 1 3; Fe2-S Score=1.3",
94             sb.toString());
95
96     /*
97      * map has min == max for this feature type - score is not shown:
98      */
99     minmax.put("METAL", new float[][] { { 2f, 2f }, null });
100     sb.setLength(0);
101     sar.appendFeature(sb, 1, minmax, sf);
102     assertEquals("METAL 1 3; Fe2-S", sb.toString());
103   }
104
105   @Test(groups = "Functional")
106   public void testAppendFeature_noScore()
107   {
108     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
109     StringBuilder sb = new StringBuilder();
110     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
111             Float.NaN, "group");
112
113     sar.appendFeature(sb, 1, null, sf);
114     assertEquals("METAL 1 3; Fe2-S", sb.toString());
115   }
116
117   @Test(groups = "Functional")
118   public void testAppendFeature_clinicalSignificance()
119   {
120     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
121     StringBuilder sb = new StringBuilder();
122     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
123             Float.NaN, "group");
124     sf.setValue("clinical_significance", "Benign");
125
126     sar.appendFeature(sb, 1, null, sf);
127     assertEquals("METAL 1 3; Fe2-S; Benign", sb.toString());
128   }
129
130   @Test(groups = "Functional")
131   public void testAppendFeature_withScoreStatusClinicalSignificance()
132   {
133     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
134     StringBuilder sb = new StringBuilder();
135     SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
136             "group");
137     sf.setStatus("Confirmed");
138     sf.setValue("clinical_significance", "Benign");
139     Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
140     minmax.put("METAL", new float[][] { { 0f, 1f }, null });
141     sar.appendFeature(sb, 1, minmax, sf);
142
143     assertEquals("METAL 1 3; Fe2-S Score=1.3; (Confirmed); Benign",
144             sb.toString());
145   }
146
147   @Test(groups = "Functional")
148   public void testAppendFeature_DescEqualsType()
149   {
150     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
151     StringBuilder sb = new StringBuilder();
152     SequenceFeature sf = new SequenceFeature("METAL", "METAL", 1, 3,
153             Float.NaN, "group");
154
155     // description is not included if it duplicates type:
156     sar.appendFeature(sb, 1, null, sf);
157     assertEquals("METAL 1 3", sb.toString());
158
159     sb.setLength(0);
160     sf.setDescription("Metal");
161     // test is case-sensitive:
162     sar.appendFeature(sb, 1, null, sf);
163     assertEquals("METAL 1 3; Metal", sb.toString());
164   }
165
166   @Test(groups = "Functional")
167   public void testAppendFeature_stripHtml()
168   {
169     SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
170     StringBuilder sb = new StringBuilder();
171     SequenceFeature sf = new SequenceFeature("METAL",
172             "<html><body>hello<em>world</em></body></html>", 1, 3,
173             Float.NaN, "group");
174
175     sar.appendFeature(sb, 1, null, sf);
176     // !! strips off </body> but not <body> ??
177     assertEquals("METAL 1 3; <body>hello<em>world</em>", sb.toString());
178
179     sb.setLength(0);
180     sf.setDescription("<br>&kHD>6");
181     sar.appendFeature(sb, 1, null, sf);
182     // if no <html> tag, html-encodes > and < (only):
183     assertEquals("METAL 1 3; &lt;br&gt;&kHD&gt;6", sb.toString());
184   }
185 }