2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
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;
37 import java.awt.Color;
38 import java.util.ArrayList;
39 import java.util.List;
42 import org.testng.annotations.BeforeClass;
43 import org.testng.annotations.Test;
45 import junit.extensions.PA;
47 public class SequenceAnnotationReportTest
50 @BeforeClass(alwaysRun = true)
51 public void setUpJvOptionPane()
53 JvOptionPane.setInteractiveMode(false);
54 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
57 @Test(groups = "Functional")
58 public void testAppendFeature_disulfideBond()
60 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
61 StringBuilder sb = new StringBuilder();
63 SequenceFeature sf = new SequenceFeature("disulfide bond", "desc", 1,
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());
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());
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",
82 @Test(groups = "Functional")
83 public void testAppendFeatures_longText()
85 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
86 StringBuilder sb = new StringBuilder();
87 String longString = // java11!"Abcd".repeat(50);
88 "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"
89 + "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"
90 + "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"
91 + "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"
92 + "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd";
94 SequenceFeature sf = new SequenceFeature("sequence", longString, 1, 3,
97 sar.appendFeature(sb, 1, null, sf, null);
98 assertTrue(sb.length() < 100);
100 List<SequenceFeature> sfl = new ArrayList<>();
112 int n = sar.appendFeaturesLengthLimit(sb, 1, sfl,
113 new FeatureRenderer(null), 200); // text should terminate before 200 characters
114 String s = sb.toString();
115 assertTrue(s.length() < 200);
116 assertEquals(n, 7); // should be 7 features left over
120 @Test(groups = "Functional")
121 public void testAppendFeature_status()
123 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
124 StringBuilder sb = new StringBuilder();
125 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
127 sf.setStatus("Confirmed");
129 sar.appendFeature(sb, 1, null, sf, null);
130 assertEquals("METAL 1 3; Fe2-S; (Confirmed)", sb.toString());
133 @Test(groups = "Functional")
134 public void testAppendFeature_withScore()
136 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
137 StringBuilder sb = new StringBuilder();
138 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
141 FeatureRendererModel fr = new FeatureRenderer(null);
142 Map<String, float[][]> minmax = fr.getMinMax();
143 sar.appendFeature(sb, 1, fr, sf, null);
145 * map has no entry for this feature type - score is not shown:
147 assertEquals("METAL 1 3; Fe2-S", sb.toString());
150 * map has entry for this feature type - score is shown:
152 minmax.put("METAL", new float[][] { { 0f, 1f }, null });
153 sar.appendFeature(sb, 1, fr, sf, null);
154 // <br/> is appended to a buffer > 6 in length
155 assertEquals("METAL 1 3; Fe2-S<br/>METAL 1 3; Fe2-S Score=1.3",
159 * map has min == max for this feature type - score is not shown:
161 minmax.put("METAL", new float[][] { { 2f, 2f }, null });
163 sar.appendFeature(sb, 1, fr, sf, null);
164 assertEquals("METAL 1 3; Fe2-S", sb.toString());
167 @Test(groups = "Functional")
168 public void testAppendFeature_noScore()
170 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
171 StringBuilder sb = new StringBuilder();
172 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
175 sar.appendFeature(sb, 1, null, sf, null);
176 assertEquals("METAL 1 3; Fe2-S", sb.toString());
180 * A specific attribute value is included if it is used to colour the feature
182 @Test(groups = "Functional")
183 public void testAppendFeature_colouredByAttribute()
185 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
186 StringBuilder sb = new StringBuilder();
187 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
189 sf.setValue("clinical_significance", "Benign");
192 * first with no colour by attribute
194 FeatureRendererModel fr = new FeatureRenderer(null);
195 sar.appendFeature(sb, 1, fr, sf, null);
196 assertEquals("METAL 1 3; Fe2-S", sb.toString());
199 * then with colour by an attribute the feature lacks
201 FeatureColourI fc = new FeatureColour(null, Color.white, Color.black,
203 fc.setAttributeName("Pfam");
204 fr.setColour("METAL", fc);
206 sar.appendFeature(sb, 1, fr, sf, null);
207 assertEquals("METAL 1 3; Fe2-S", sb.toString()); // no change
210 * then with colour by an attribute the feature has
212 fc.setAttributeName("clinical_significance");
214 sar.appendFeature(sb, 1, fr, sf, null);
215 assertEquals("METAL 1 3; Fe2-S; clinical_significance=Benign",
219 @Test(groups = "Functional")
220 public void testAppendFeature_withScoreStatusAttribute()
222 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
223 StringBuilder sb = new StringBuilder();
224 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
226 sf.setStatus("Confirmed");
227 sf.setValue("clinical_significance", "Benign");
229 FeatureRendererModel fr = new FeatureRenderer(null);
230 Map<String, float[][]> minmax = fr.getMinMax();
231 FeatureColourI fc = new FeatureColour(null, Color.white, Color.blue,
233 fc.setAttributeName("clinical_significance");
234 fr.setColour("METAL", fc);
235 minmax.put("METAL", new float[][] { { 0f, 1f }, null });
236 sar.appendFeature(sb, 1, fr, sf, null);
239 "METAL 1 3; Fe2-S Score=1.3; (Confirmed); clinical_significance=Benign",
243 @Test(groups = "Functional")
244 public void testAppendFeature_DescEqualsType()
246 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
247 StringBuilder sb = new StringBuilder();
248 SequenceFeature sf = new SequenceFeature("METAL", "METAL", 1, 3,
251 // description is not included if it duplicates type:
252 sar.appendFeature(sb, 1, null, sf, null);
253 assertEquals("METAL 1 3", sb.toString());
256 sf.setDescription("Metal");
257 // test is case-sensitive:
258 sar.appendFeature(sb, 1, null, sf, null);
259 assertEquals("METAL 1 3; Metal", sb.toString());
262 @Test(groups = "Functional")
263 public void testAppendFeature_stripHtml()
265 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
266 StringBuilder sb = new StringBuilder();
267 SequenceFeature sf = new SequenceFeature("METAL",
268 "<html><body>hello<em>world</em></body></html>", 1, 3,
271 sar.appendFeature(sb, 1, null, sf, null);
272 // !! strips off </body> but not <body> ??
273 assertEquals("METAL 1 3; <body>hello<em>world</em>", sb.toString());
276 sf.setDescription("<br>&kHD>6");
277 sar.appendFeature(sb, 1, null, sf, null);
278 // if no <html> tag, html-encodes > and < (only):
279 assertEquals("METAL 1 3; <br>&kHD>6", sb.toString());
282 @Test(groups = "Functional")
283 public void testCreateSequenceAnnotationReport()
285 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
286 StringBuilder sb = new StringBuilder();
288 SequenceI seq = new Sequence("s1", "MAKLKRFQSSTLL");
289 seq.setDescription("SeqDesc");
292 * positional features are ignored
294 seq.addSequenceFeature(new SequenceFeature("Domain", "Ferredoxin", 5,
296 sar.createSequenceAnnotationReport(sb, seq, true, true, null);
297 assertEquals("<i>SeqDesc</i>", sb.toString());
300 * non-positional feature
302 seq.addSequenceFeature(new SequenceFeature("Type1", "Nonpos", 0, 0, 1f,
305 sar.createSequenceAnnotationReport(sb, seq, true, true, null);
306 String expected = "<i>SeqDesc<br/>Type1 ; Nonpos Score=1.0</i>";
307 assertEquals(expected, sb.toString());
310 * non-positional features not wanted
313 sar.createSequenceAnnotationReport(sb, seq, true, false, null);
314 assertEquals("<i>SeqDesc</i>", sb.toString());
317 * add non-pos feature with score inside min-max range for feature type
318 * minmax holds { [positionalMin, positionalMax], [nonPosMin, nonPosMax] }
319 * score is only appended for positional features so ignored here!
320 * minMax are not recorded for non-positional features
322 seq.addSequenceFeature(new SequenceFeature("Metal", "Desc", 0, 0, 5f,
325 FeatureRendererModel fr = new FeatureRenderer(null);
326 Map<String, float[][]> minmax = fr.getMinMax();
327 minmax.put("Metal", new float[][] { null, new float[] { 2f, 5f } });
330 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
331 expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos</i>";
332 assertEquals(expected, sb.toString());
335 * 'linkonly' features are ignored; this is obsolete, as linkonly
336 * is only set by DasSequenceFetcher, and DAS is history
338 SequenceFeature sf = new SequenceFeature("Metal", "Desc", 0, 0, 5f,
340 sf.setValue("linkonly", Boolean.TRUE);
341 seq.addSequenceFeature(sf);
343 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
344 assertEquals(expected, sb.toString()); // unchanged!
347 * 'clinical_significance' attribute is only included in description
348 * when used for feature colouring
350 SequenceFeature sf2 = new SequenceFeature("Variant", "Havana", 0, 0,
352 sf2.setValue(GffConstants.CLINICAL_SIGNIFICANCE, "benign");
353 seq.addSequenceFeature(sf2);
355 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
356 expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos<br/>Variant ; Havana</i>";
357 assertEquals(expected, sb.toString());
362 seq.addDBRef(new DBRefEntry("PDB", "0", "3iu1"));
363 seq.addDBRef(new DBRefEntry("Uniprot", "1", "P30419"));
365 // with showDbRefs = false
367 sar.createSequenceAnnotationReport(sb, seq, false, true, fr);
368 assertEquals(expected, sb.toString()); // unchanged
370 // with showDbRefs = true, colour Variant features by clinical_significance
372 FeatureColourI fc = new FeatureColour(null, Color.green, Color.pink,
374 fc.setAttributeName("clinical_significance");
375 fr.setColour("Variant", fc);
376 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
377 expected = "<i>SeqDesc<br/>UNIPROT P30419<br/>PDB 3iu1<br/>Metal ; Desc<br/>"
378 + "Type1 ; Nonpos<br/>Variant ; Havana; clinical_significance=benign</i>";
379 assertEquals(expected, sb.toString());
380 // with showNonPositionalFeatures = false
382 sar.createSequenceAnnotationReport(sb, seq, true, false, fr);
383 expected = "<i>SeqDesc<br/>UNIPROT P30419<br/>PDB 3iu1</i>";
384 assertEquals(expected, sb.toString());
387 * long feature description is truncated with ellipsis
391 "This is a very long description which should be truncated");
392 sar.createSequenceAnnotationReport(sb, seq, false, true, fr);
393 expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos<br/>Variant ; This is a very long description which sh...; clinical_significance=benign</i>";
394 assertEquals(expected, sb.toString());
396 // see other tests for treatment of status and html
400 * Test that exercises an abbreviated sequence details report, with ellipsis
401 * where there are more than 40 different sources, or more than 4 dbrefs for a
404 @Test(groups = "Functional")
405 public void testCreateSequenceAnnotationReport_withEllipsis()
407 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
408 StringBuilder sb = new StringBuilder();
410 SequenceI seq = new Sequence("s1", "ABC");
412 int maxSources = (int) PA.getValue(sar, "MAX_SOURCES");
413 for (int i = 0; i <= maxSources; i++)
415 seq.addDBRef(new DBRefEntry("PDB" + i, "0", "3iu1"));
418 int maxRefs = (int) PA.getValue(sar, "MAX_REFS_PER_SOURCE");
419 for (int i = 0; i <= maxRefs; i++)
421 seq.addDBRef(new DBRefEntry("Uniprot", "0", "P3041" + i));
424 sar.createSequenceAnnotationReport(sb, seq, true, true, null, true);
425 String report = sb.toString();
428 "<i><br/>UNIPROT P30410, P30411, P30412, P30413,...<br/>PDB0 3iu1"));
431 "<br/>PDB7 3iu1<br/>PDB8,...<br/>(Output Sequence Details to list all database references)</i>"));