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 java.awt.Color;
27 import java.util.ArrayList;
28 import java.util.List;
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.Test;
34 import jalview.api.FeatureColourI;
35 import jalview.datamodel.DBRefEntry;
36 import jalview.datamodel.Sequence;
37 import jalview.datamodel.SequenceFeature;
38 import jalview.datamodel.SequenceI;
39 import jalview.gui.JvOptionPane;
40 import jalview.io.gff.GffConstants;
41 import jalview.renderer.seqfeatures.FeatureRenderer;
42 import jalview.schemes.FeatureColour;
43 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
44 import junit.extensions.PA;
46 public class SequenceAnnotationReportTest
49 @BeforeClass(alwaysRun = true)
50 public void setUpJvOptionPane()
52 JvOptionPane.setInteractiveMode(false);
53 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
56 @Test(groups = "Functional")
57 public void testAppendFeature_disulfideBond()
59 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
60 StringBuilder sb = new StringBuilder();
62 SequenceFeature sf = new SequenceFeature("disulfide bond", "desc", 1,
65 // residuePos == 2 does not match start or end of feature, nothing done:
66 sar.appendFeature(sb, 2, null, sf, null, 0);
67 assertEquals("123456", sb.toString());
69 // residuePos == 1 matches start of feature, text appended (but no <br/>)
70 // feature score is not included
71 sar.appendFeature(sb, 1, null, sf, null, 0);
72 assertEquals("123456disulfide bond 1:3", sb.toString());
74 // residuePos == 3 matches end of feature, text appended
75 // <br/> is prefixed once sb.length() > 6
76 sar.appendFeature(sb, 3, null, sf, null, 0);
77 assertEquals("123456disulfide bond 1:3<br/>disulfide bond 1:3",
81 @Test(groups = "Functional")
82 public void testAppendFeatures_longText()
84 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
85 StringBuilder sb = new StringBuilder();
86 String longString = "Abcd".repeat(50);
87 SequenceFeature sf = new SequenceFeature("sequence", longString, 1, 3,
90 sar.appendFeature(sb, 1, null, sf, null, 0);
91 assertTrue(sb.length() < 100);
93 List<SequenceFeature> sfl = new ArrayList<>();
105 int n = sar.appendFeatures(sb, 1, sfl,
106 new FeatureRenderer(null), 200); // text should terminate before 200 characters
107 String s = sb.toString();
108 assertTrue(s.length() < 200);
109 assertEquals(n, 7); // should be 7 features left over
113 @Test(groups = "Functional")
114 public void testAppendFeature_status()
116 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
117 StringBuilder sb = new StringBuilder();
118 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
120 sf.setStatus("Confirmed");
122 sar.appendFeature(sb, 1, null, sf, null, 0);
123 assertEquals("METAL 1 3; Fe2-S; (Confirmed)", sb.toString());
126 @Test(groups = "Functional")
127 public void testAppendFeature_withScore()
129 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
130 StringBuilder sb = new StringBuilder();
131 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
134 FeatureRendererModel fr = new FeatureRenderer(null);
135 Map<String, float[][]> minmax = fr.getMinMax();
136 sar.appendFeature(sb, 1, fr, sf, null, 0);
138 * map has no entry for this feature type - score is not shown:
140 assertEquals("METAL 1 3; Fe2-S", sb.toString());
143 * map has entry for this feature type - score is shown:
145 minmax.put("METAL", new float[][] { { 0f, 1f }, null });
146 sar.appendFeature(sb, 1, fr, sf, null, 0);
147 // <br/> is appended to a buffer > 6 in length
148 assertEquals("METAL 1 3; Fe2-S<br/>METAL 1 3; Fe2-S Score=1.3",
152 * map has min == max for this feature type - score is not shown:
154 minmax.put("METAL", new float[][] { { 2f, 2f }, null });
156 sar.appendFeature(sb, 1, fr, sf, null, 0);
157 assertEquals("METAL 1 3; Fe2-S", sb.toString());
160 @Test(groups = "Functional")
161 public void testAppendFeature_noScore()
163 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
164 StringBuilder sb = new StringBuilder();
165 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
168 sar.appendFeature(sb, 1, null, sf, null, 0);
169 assertEquals("METAL 1 3; Fe2-S", sb.toString());
173 * A specific attribute value is included if it is used to colour the feature
175 @Test(groups = "Functional")
176 public void testAppendFeature_colouredByAttribute()
178 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
179 StringBuilder sb = new StringBuilder();
180 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
182 sf.setValue("clinical_significance", "Benign");
185 * first with no colour by attribute
187 FeatureRendererModel fr = new FeatureRenderer(null);
188 sar.appendFeature(sb, 1, fr, sf, null, 0);
189 assertEquals("METAL 1 3; Fe2-S", sb.toString());
192 * then with colour by an attribute the feature lacks
194 FeatureColourI fc = new FeatureColour(null, Color.white, Color.black,
196 fc.setAttributeName("Pfam");
197 fr.setColour("METAL", fc);
199 sar.appendFeature(sb, 1, fr, sf, null, 0);
200 assertEquals("METAL 1 3; Fe2-S", sb.toString()); // no change
203 * then with colour by an attribute the feature has
205 fc.setAttributeName("clinical_significance");
207 sar.appendFeature(sb, 1, fr, sf, null, 0);
208 assertEquals("METAL 1 3; Fe2-S; clinical_significance=Benign",
212 @Test(groups = "Functional")
213 public void testAppendFeature_withScoreStatusAttribute()
215 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
216 StringBuilder sb = new StringBuilder();
217 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
219 sf.setStatus("Confirmed");
220 sf.setValue("clinical_significance", "Benign");
222 FeatureRendererModel fr = new FeatureRenderer(null);
223 Map<String, float[][]> minmax = fr.getMinMax();
224 FeatureColourI fc = new FeatureColour(null, Color.white, Color.blue,
226 fc.setAttributeName("clinical_significance");
227 fr.setColour("METAL", fc);
228 minmax.put("METAL", new float[][] { { 0f, 1f }, null });
229 sar.appendFeature(sb, 1, fr, sf, null, 0);
232 "METAL 1 3; Fe2-S Score=1.3; (Confirmed); clinical_significance=Benign",
236 @Test(groups = "Functional")
237 public void testAppendFeature_DescEqualsType()
239 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
240 StringBuilder sb = new StringBuilder();
241 SequenceFeature sf = new SequenceFeature("METAL", "METAL", 1, 3,
244 // description is not included if it duplicates type:
245 sar.appendFeature(sb, 1, null, sf, null, 0);
246 assertEquals("METAL 1 3", sb.toString());
249 sf.setDescription("Metal");
250 // test is case-sensitive:
251 sar.appendFeature(sb, 1, null, sf, null, 0);
252 assertEquals("METAL 1 3; Metal", sb.toString());
255 @Test(groups = "Functional")
256 public void testAppendFeature_stripHtml()
258 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
259 StringBuilder sb = new StringBuilder();
260 SequenceFeature sf = new SequenceFeature("METAL",
261 "<html><body>hello<em>world</em></body></html>", 1, 3,
264 sar.appendFeature(sb, 1, null, sf, null, 0);
265 // !! strips off </body> but not <body> ??
266 assertEquals("METAL 1 3; <body>hello<em>world</em>", sb.toString());
269 sf.setDescription("<br>&kHD>6");
270 sar.appendFeature(sb, 1, null, sf, null, 0);
271 // if no <html> tag, html-encodes > and < (only):
272 assertEquals("METAL 1 3; <br>&kHD>6", sb.toString());
275 @Test(groups = "Functional")
276 public void testCreateSequenceAnnotationReport()
278 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
279 StringBuilder sb = new StringBuilder();
281 SequenceI seq = new Sequence("s1", "MAKLKRFQSSTLL");
282 seq.setDescription("SeqDesc");
285 * positional features are ignored
287 seq.addSequenceFeature(new SequenceFeature("Domain", "Ferredoxin", 5,
289 sar.createSequenceAnnotationReport(sb, seq, true, true, null);
290 assertEquals("<i>SeqDesc</i>", sb.toString());
293 * non-positional feature
295 seq.addSequenceFeature(new SequenceFeature("Type1", "Nonpos", 0, 0, 1f,
298 sar.createSequenceAnnotationReport(sb, seq, true, true, null);
299 String expected = "<i>SeqDesc<br/>Type1 ; Nonpos Score=1.0</i>";
300 assertEquals(expected, sb.toString());
303 * non-positional features not wanted
306 sar.createSequenceAnnotationReport(sb, seq, true, false, null);
307 assertEquals("<i>SeqDesc</i>", sb.toString());
310 * add non-pos feature with score inside min-max range for feature type
311 * minmax holds { [positionalMin, positionalMax], [nonPosMin, nonPosMax] }
312 * score is only appended for positional features so ignored here!
313 * minMax are not recorded for non-positional features
315 seq.addSequenceFeature(new SequenceFeature("Metal", "Desc", 0, 0, 5f,
318 FeatureRendererModel fr = new FeatureRenderer(null);
319 Map<String, float[][]> minmax = fr.getMinMax();
320 minmax.put("Metal", new float[][] { null, new float[] { 2f, 5f } });
323 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
324 expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos</i>";
325 assertEquals(expected, sb.toString());
328 * 'linkonly' features are ignored; this is obsolete, as linkonly
329 * is only set by DasSequenceFetcher, and DAS is history
331 SequenceFeature sf = new SequenceFeature("Metal", "Desc", 0, 0, 5f,
333 sf.setValue("linkonly", Boolean.TRUE);
334 seq.addSequenceFeature(sf);
336 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
337 assertEquals(expected, sb.toString()); // unchanged!
340 * 'clinical_significance' attribute is only included in description
341 * when used for feature colouring
343 SequenceFeature sf2 = new SequenceFeature("Variant", "Havana", 0, 0,
345 sf2.setValue(GffConstants.CLINICAL_SIGNIFICANCE, "benign");
346 seq.addSequenceFeature(sf2);
348 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
349 expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos<br/>Variant ; Havana</i>";
350 assertEquals(expected, sb.toString());
355 seq.addDBRef(new DBRefEntry("PDB", "0", "3iu1"));
356 seq.addDBRef(new DBRefEntry("Uniprot", "1", "P30419"));
358 // with showDbRefs = false
360 sar.createSequenceAnnotationReport(sb, seq, false, true, fr);
361 assertEquals(expected, sb.toString()); // unchanged
363 // with showDbRefs = true, colour Variant features by clinical_significance
365 FeatureColourI fc = new FeatureColour(null, Color.green, Color.pink,
367 fc.setAttributeName("clinical_significance");
368 fr.setColour("Variant", fc);
369 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
370 expected = "<i>SeqDesc<br/>UNIPROT P30419<br/>PDB 3iu1<br/>Metal ; Desc<br/>"
371 + "Type1 ; Nonpos<br/>Variant ; Havana; clinical_significance=benign</i>";
372 assertEquals(expected, sb.toString());
373 // with showNonPositionalFeatures = false
375 sar.createSequenceAnnotationReport(sb, seq, true, false, fr);
376 expected = "<i>SeqDesc<br/>UNIPROT P30419<br/>PDB 3iu1</i>";
377 assertEquals(expected, sb.toString());
380 * long feature description is truncated with ellipsis
384 "This is a very long description which should be truncated");
385 sar.createSequenceAnnotationReport(sb, seq, false, true, fr);
386 expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos<br/>Variant ; This is a very long description which sh...; clinical_significance=benign</i>";
387 assertEquals(expected, sb.toString());
389 // see other tests for treatment of status and html
393 * Test that exercises an abbreviated sequence details report, with ellipsis
394 * where there are more than 40 different sources, or more than 4 dbrefs for a
397 @Test(groups = "Functional")
398 public void testCreateSequenceAnnotationReport_withEllipsis()
400 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
401 StringBuilder sb = new StringBuilder();
403 SequenceI seq = new Sequence("s1", "ABC");
405 int maxSources = (int) PA.getValue(sar, "MAX_SOURCES");
406 for (int i = 0; i <= maxSources; i++)
408 seq.addDBRef(new DBRefEntry("PDB" + i, "0", "3iu1"));
411 int maxRefs = (int) PA.getValue(sar, "MAX_REFS_PER_SOURCE");
412 for (int i = 0; i <= maxRefs; i++)
414 seq.addDBRef(new DBRefEntry("Uniprot", "0", "P3041" + i));
417 sar.createSequenceAnnotationReport(sb, seq, true, true, null, true);
418 String report = sb.toString();
421 "<i><br/>UNIPROT P30410, P30411, P30412, P30413,...<br/>PDB0 3iu1"));
424 "<br/>PDB7 3iu1<br/>PDB8,...<br/>(Output Sequence Details to list all database references)</i>"));