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