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 = "Abcd".repeat(50);
88 SequenceFeature sf = new SequenceFeature("sequence", longString, 1, 3,
91 sar.appendFeature(sb, 1, null, sf, null);
92 assertTrue(sb.length() < 100);
94 List<SequenceFeature> sfl = new ArrayList<>();
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
114 @Test(groups = "Functional")
115 public void testAppendFeature_status()
117 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
118 StringBuilder sb = new StringBuilder();
119 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
121 sf.setStatus("Confirmed");
123 sar.appendFeature(sb, 1, null, sf, null);
124 assertEquals("METAL 1 3; Fe2-S; (Confirmed)", sb.toString());
127 @Test(groups = "Functional")
128 public void testAppendFeature_withScore()
130 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
131 StringBuilder sb = new StringBuilder();
132 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
135 FeatureRendererModel fr = new FeatureRenderer(null);
136 Map<String, float[][]> minmax = fr.getMinMax();
137 sar.appendFeature(sb, 1, fr, sf, null);
139 * map has no entry for this feature type - score is not shown:
141 assertEquals("METAL 1 3; Fe2-S", sb.toString());
144 * map has entry for this feature type - score is shown:
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",
153 * map has min == max for this feature type - score is not shown:
155 minmax.put("METAL", new float[][] { { 2f, 2f }, null });
157 sar.appendFeature(sb, 1, fr, sf, null);
158 assertEquals("METAL 1 3; Fe2-S", sb.toString());
161 @Test(groups = "Functional")
162 public void testAppendFeature_noScore()
164 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
165 StringBuilder sb = new StringBuilder();
166 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
169 sar.appendFeature(sb, 1, null, sf, null);
170 assertEquals("METAL 1 3; Fe2-S", sb.toString());
174 * A specific attribute value is included if it is used to colour the feature
176 @Test(groups = "Functional")
177 public void testAppendFeature_colouredByAttribute()
179 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
180 StringBuilder sb = new StringBuilder();
181 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
183 sf.setValue("clinical_significance", "Benign");
186 * first with no colour by attribute
188 FeatureRendererModel fr = new FeatureRenderer(null);
189 sar.appendFeature(sb, 1, fr, sf, null);
190 assertEquals("METAL 1 3; Fe2-S", sb.toString());
193 * then with colour by an attribute the feature lacks
195 FeatureColourI fc = new FeatureColour(null, Color.white, Color.black,
197 fc.setAttributeName("Pfam");
198 fr.setColour("METAL", fc);
200 sar.appendFeature(sb, 1, fr, sf, null);
201 assertEquals("METAL 1 3; Fe2-S", sb.toString()); // no change
204 * then with colour by an attribute the feature has
206 fc.setAttributeName("clinical_significance");
208 sar.appendFeature(sb, 1, fr, sf, null);
209 assertEquals("METAL 1 3; Fe2-S; clinical_significance=Benign",
213 @Test(groups = "Functional")
214 public void testAppendFeature_withScoreStatusAttribute()
216 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
217 StringBuilder sb = new StringBuilder();
218 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
220 sf.setStatus("Confirmed");
221 sf.setValue("clinical_significance", "Benign");
223 FeatureRendererModel fr = new FeatureRenderer(null);
224 Map<String, float[][]> minmax = fr.getMinMax();
225 FeatureColourI fc = new FeatureColour(null, Color.white, Color.blue,
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);
233 "METAL 1 3; Fe2-S Score=1.3; (Confirmed); clinical_significance=Benign",
237 @Test(groups = "Functional")
238 public void testAppendFeature_DescEqualsType()
240 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
241 StringBuilder sb = new StringBuilder();
242 SequenceFeature sf = new SequenceFeature("METAL", "METAL", 1, 3,
245 // description is not included if it duplicates type:
246 sar.appendFeature(sb, 1, null, sf, null);
247 assertEquals("METAL 1 3", sb.toString());
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());
256 @Test(groups = "Functional")
257 public void testAppendFeature_stripHtml()
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,
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());
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; <br>&kHD>6", sb.toString());
276 @Test(groups = "Functional")
277 public void testCreateSequenceAnnotationReport()
279 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
280 StringBuilder sb = new StringBuilder();
282 SequenceI seq = new Sequence("s1", "MAKLKRFQSSTLL");
283 seq.setDescription("SeqDesc");
286 * positional features are ignored
288 seq.addSequenceFeature(new SequenceFeature("Domain", "Ferredoxin", 5,
290 sar.createSequenceAnnotationReport(sb, seq, true, true, null);
291 assertEquals("<i>SeqDesc</i>", sb.toString());
294 * non-positional feature
296 seq.addSequenceFeature(new SequenceFeature("Type1", "Nonpos", 0, 0, 1f,
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());
304 * non-positional features not wanted
307 sar.createSequenceAnnotationReport(sb, seq, true, false, null);
308 assertEquals("<i>SeqDesc</i>", sb.toString());
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
316 seq.addSequenceFeature(new SequenceFeature("Metal", "Desc", 0, 0, 5f,
319 FeatureRendererModel fr = new FeatureRenderer(null);
320 Map<String, float[][]> minmax = fr.getMinMax();
321 minmax.put("Metal", new float[][] { null, new float[] { 2f, 5f } });
324 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
325 expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos</i>";
326 assertEquals(expected, sb.toString());
329 * 'linkonly' features are ignored; this is obsolete, as linkonly
330 * is only set by DasSequenceFetcher, and DAS is history
332 SequenceFeature sf = new SequenceFeature("Metal", "Desc", 0, 0, 5f,
334 sf.setValue("linkonly", Boolean.TRUE);
335 seq.addSequenceFeature(sf);
337 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
338 assertEquals(expected, sb.toString()); // unchanged!
341 * 'clinical_significance' attribute is only included in description
342 * when used for feature colouring
344 SequenceFeature sf2 = new SequenceFeature("Variant", "Havana", 0, 0,
346 sf2.setValue(GffConstants.CLINICAL_SIGNIFICANCE, "benign");
347 seq.addSequenceFeature(sf2);
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());
356 seq.addDBRef(new DBRefEntry("PDB", "0", "3iu1"));
357 seq.addDBRef(new DBRefEntry("Uniprot", "1", "P30419"));
359 // with showDbRefs = false
361 sar.createSequenceAnnotationReport(sb, seq, false, true, fr);
362 assertEquals(expected, sb.toString()); // unchanged
364 // with showDbRefs = true, colour Variant features by clinical_significance
366 FeatureColourI fc = new FeatureColour(null, Color.green, Color.pink,
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
376 sar.createSequenceAnnotationReport(sb, seq, true, false, fr);
377 expected = "<i>SeqDesc<br/>UNIPROT P30419<br/>PDB 3iu1</i>";
378 assertEquals(expected, sb.toString());
381 * long feature description is truncated with ellipsis
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());
390 // see other tests for treatment of status and html
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
398 @Test(groups = "Functional")
399 public void testCreateSequenceAnnotationReport_withEllipsis()
401 SequenceAnnotationReport sar = new SequenceAnnotationReport(null);
402 StringBuilder sb = new StringBuilder();
404 SequenceI seq = new Sequence("s1", "ABC");
406 int maxSources = (int) PA.getValue(sar, "MAX_SOURCES");
407 for (int i = 0; i <= maxSources; i++)
409 seq.addDBRef(new DBRefEntry("PDB" + i, "0", "3iu1"));
412 int maxRefs = (int) PA.getValue(sar, "MAX_REFS_PER_SOURCE");
413 for (int i = 0; i <= maxRefs; i++)
415 seq.addDBRef(new DBRefEntry("Uniprot", "0", "P3041" + i));
418 sar.createSequenceAnnotationReport(sb, seq, true, true, null, true);
419 String report = sb.toString();
422 "<i><br/>UNIPROT P30410, P30411, P30412, P30413,...<br/>PDB0 3iu1"));
425 "<br/>PDB7 3iu1<br/>PDB8,...<br/>(Output Sequence Details to list all database references)</i>"));