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.MappedFeatures;
37 import jalview.datamodel.Mapping;
38 import jalview.datamodel.Sequence;
39 import jalview.datamodel.SequenceFeature;
40 import jalview.datamodel.SequenceI;
41 import jalview.gui.JvOptionPane;
42 import jalview.io.gff.GffConstants;
43 import jalview.renderer.seqfeatures.FeatureRenderer;
44 import jalview.schemes.FeatureColour;
45 import jalview.util.MapList;
46 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
47 import junit.extensions.PA;
49 public class SequenceAnnotationReportTest
52 @BeforeClass(alwaysRun = true)
53 public void setUpJvOptionPane()
55 JvOptionPane.setInteractiveMode(false);
56 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
59 @Test(groups = "Functional")
60 public void testAppendFeature_disulfideBond()
62 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
63 StringBuilder sb = new StringBuilder();
65 SequenceFeature sf = new SequenceFeature("disulfide bond", "desc", 1, 3,
68 // residuePos == 2 does not match start or end of feature, nothing done:
69 sar.appendFeature(sb, 2, null, sf, null, 0);
70 assertEquals("123456", sb.toString());
72 // residuePos == 1 matches start of feature, text appended (but no <br/>)
73 // feature score is not included
74 sar.appendFeature(sb, 1, null, sf, null, 0);
75 assertEquals("123456disulfide bond 1:3", sb.toString());
77 // residuePos == 3 matches end of feature, text appended
78 // <br/> is prefixed once sb.length() > 6
79 sar.appendFeature(sb, 3, null, sf, null, 0);
80 assertEquals("123456disulfide bond 1:3<br/>disulfide bond 1:3",
84 @Test(groups = "Functional")
85 public void testAppendFeatures_longText()
87 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
88 StringBuilder sb = new StringBuilder();
89 String longString = "Abcd".repeat(50);
90 SequenceFeature sf = new SequenceFeature("sequence", longString, 1, 3,
93 sar.appendFeature(sb, 1, null, sf, null, 0);
94 assertTrue(sb.length() < 100);
96 List<SequenceFeature> sfl = new ArrayList<>();
108 int n = sar.appendFeatures(sb, 1, sfl, new FeatureRenderer(null), 200); // text
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(false);
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, 0);
130 assertEquals("METAL 1 3; Fe2-S; (Confirmed)", sb.toString());
133 @Test(groups = "Functional")
134 public void testAppendFeature_withScore()
136 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
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, 0);
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, 0);
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, 0);
164 assertEquals("METAL 1 3; Fe2-S", sb.toString());
167 @Test(groups = "Functional")
168 public void testAppendFeature_noScore()
170 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
171 StringBuilder sb = new StringBuilder();
172 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
175 sar.appendFeature(sb, 1, null, sf, null, 0);
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(false);
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, 0);
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, 0);
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, 0);
215 assertEquals("METAL 1 3; Fe2-S; clinical_significance=Benign",
219 @Test(groups = "Functional")
220 public void testAppendFeature_withScoreStatusAttribute()
222 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
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, 0);
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(false);
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, 0);
253 assertEquals("METAL 1 3", sb.toString());
256 sf.setDescription("Metal");
257 // test is case-sensitive:
258 sar.appendFeature(sb, 1, null, sf, null, 0);
259 assertEquals("METAL 1 3; Metal", sb.toString());
262 @Test(groups = "Functional")
263 public void testAppendFeature_stripHtml()
265 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
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, 0);
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, 0);
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(false);
286 StringBuilder sb = new StringBuilder();
288 SequenceI seq = new Sequence("s1", "MAKLKRFQSSTLL");
289 seq.setDescription("SeqDesc");
292 * positional features are ignored
294 seq.addSequenceFeature(
295 new SequenceFeature("Domain", "Ferredoxin", 5, 10, 1f, null));
296 sar.createSequenceAnnotationReport(sb, seq, true, true, null);
297 assertEquals("<i>SeqDesc\n" + "\n" + "</i>", sb.toString());
300 * non-positional feature
302 seq.addSequenceFeature(
303 new SequenceFeature("Type1", "Nonpos", 0, 0, 1f, null));
305 sar.createSequenceAnnotationReport(sb, seq, true, true, null);
306 String expected = "<i>SeqDesc\n" + "\n"
307 + "<br/>Type1 ; Nonpos Score=1.0</i>";
308 assertEquals(expected, sb.toString());
311 * non-positional features not wanted
314 sar.createSequenceAnnotationReport(sb, seq, true, false, null);
315 assertEquals("<i>SeqDesc\n\n</i>", sb.toString());
318 * add non-pos feature with score inside min-max range for feature type
319 * minmax holds { [positionalMin, positionalMax], [nonPosMin, nonPosMax] }
320 * score is only appended for positional features so ignored here!
321 * minMax are not recorded for non-positional features
323 seq.addSequenceFeature(
324 new SequenceFeature("Metal", "Desc", 0, 0, 5f, null));
326 FeatureRendererModel fr = new FeatureRenderer(null);
327 Map<String, float[][]> minmax = fr.getMinMax();
328 minmax.put("Metal", new float[][] { null, new float[] { 2f, 5f } });
331 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
332 expected = "<i>SeqDesc\n" + "\n"
333 + "<br/>Metal ; Desc<br/>Type1 ; Nonpos</i>";
334 assertEquals(expected, sb.toString());
337 * 'linkonly' features are ignored; this is obsolete, as linkonly
338 * is only set by DasSequenceFetcher, and DAS is history
340 SequenceFeature sf = new SequenceFeature("Metal", "Desc", 0, 0, 5f,
342 sf.setValue("linkonly", Boolean.TRUE);
343 seq.addSequenceFeature(sf);
345 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
346 assertEquals(expected, sb.toString()); // unchanged!
349 * 'clinical_significance' attribute is only included in description
350 * when used for feature colouring
352 SequenceFeature sf2 = new SequenceFeature("Variant", "Havana", 0, 0, 5f,
354 sf2.setValue(GffConstants.CLINICAL_SIGNIFICANCE, "benign");
355 seq.addSequenceFeature(sf2);
357 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
358 expected = "<i>SeqDesc\n" + "\n"
359 + "<br/>Metal ; Desc<br/>Type1 ; Nonpos<br/>Variant ; Havana</i>";
360 assertEquals(expected, sb.toString());
365 seq.addDBRef(new DBRefEntry("PDB", "0", "3iu1"));
366 seq.addDBRef(new DBRefEntry("Uniprot", "1", "P30419"));
368 // with showDbRefs = false
370 sar.createSequenceAnnotationReport(sb, seq, false, true, fr);
371 assertEquals(expected, sb.toString()); // unchanged
373 // with showDbRefs = true, colour Variant features by clinical_significance
375 FeatureColourI fc = new FeatureColour(null, Color.green, Color.pink,
377 fc.setAttributeName("clinical_significance");
378 fr.setColour("Variant", fc);
379 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
380 expected = "<i>SeqDesc\n" + "<br/>\n" + "UNIPROT P30419<br/>\n"
382 + "<br/>Metal ; Desc<br/>Type1 ; Nonpos<br/>Variant ; Havana; clinical_significance=benign</i>";
383 assertEquals(expected, sb.toString());
384 // with showNonPositionalFeatures = false
386 sar.createSequenceAnnotationReport(sb, seq, true, false, fr);
387 expected = "<i>SeqDesc\n" + "<br/>\n" + "UNIPROT P30419<br/>\n"
388 + "PDB 3iu1\n" + "</i>";
389 assertEquals(expected, sb.toString());
392 * long feature description is truncated with ellipsis
396 "This is a very long description which should be truncated");
397 sar.createSequenceAnnotationReport(sb, seq, false, true, fr);
398 expected = "<i>SeqDesc\n" + "\n"
399 + "<br/>Metal ; Desc<br/>Type1 ; Nonpos<br/>Variant ; This is a very long description which sh...; clinical_significance=benign</i>";
400 assertEquals(expected, sb.toString());
402 // see other tests for treatment of status and html
406 * Test that exercises an abbreviated sequence details report, with ellipsis
407 * where there are more than 40 different sources, or more than 4 dbrefs for a
410 @Test(groups = "Functional")
411 public void testCreateSequenceAnnotationReport_withEllipsis()
413 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
414 StringBuilder sb = new StringBuilder();
416 SequenceI seq = new Sequence("s1", "ABC");
418 int maxSources = (int) PA.getValue(sar, "MAX_SOURCES");
419 for (int i = 0; i <= maxSources; i++)
421 seq.addDBRef(new DBRefEntry("PDB" + i, "0", "3iu1"));
424 int maxRefs = (int) PA.getValue(sar, "MAX_REFS_PER_SOURCE");
425 for (int i = 0; i <= maxRefs; i++)
427 seq.addDBRef(new DBRefEntry("Uniprot", "0", "P3041" + i));
430 sar.createSequenceAnnotationReport(sb, seq, true, true, null, true);
431 String report = sb.toString();
432 assertTrue(report.startsWith("<i>\n" + "<br/>\n" + "UNIPROT P30410,\n"
433 + " P30411,\n" + " P30412,\n" + " P30413,...<br/>\n"
434 + "PDB0 3iu1<br/>\n" + "PDB1 3iu1<br/>"));
435 assertTrue(report.endsWith("PDB3 3iu1<br/>\n" + "PDB4,...<br/>\n"
436 + "(Output Sequence Details to list all database references)\n"
441 * Test adding a linked feature to the tooltip
443 @Test(groups = "Functional")
444 public void testAppendFeature_virtualFeature()
447 * map CDS to peptide sequence
449 SequenceI cds = new Sequence("Cds/101-121", "CCTttgAGAtttCAAatgGAT");
450 SequenceI peptide = new Sequence("Peptide/8-14", "PLRFQMD");
451 MapList map = new MapList(new int[] { 101, 118 }, new int[] { 8, 13 },
453 Mapping mapping = new Mapping(peptide, map);
456 * assume variant feature found at CDS position 106 G>C
458 List<SequenceFeature> features = new ArrayList<>();
459 // vary ttg (Leu) to ttc (Phe)
460 SequenceFeature sf = new SequenceFeature("variant", "G,C", 106, 106,
463 MappedFeatures mf = new MappedFeatures(mapping, cds, 9, 'L', features);
465 StringBuilder sb = new StringBuilder();
466 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
467 sar.appendFeature(sb, 1, null, sf, mf, 0);
470 * linked feature shown in tooltip in protein coordinates
472 assertEquals("variant 9; G,C", sb.toString());
475 * adding "alleles" attribute to variant allows peptide consequence
476 * to be calculated and added to the tooltip
478 sf.setValue("alleles", "G,C");
479 sb = new StringBuilder();
480 sar.appendFeature(sb, 1, null, sf, mf, 0);
481 assertEquals("variant 9; G,C p.Leu9Phe", sb.toString());
484 * now a virtual peptide feature on CDS
485 * feature at 11-12 on peptide maps to 110-115 on CDS
486 * here we test for tooltip at 113 (t)
488 SequenceFeature sf2 = new SequenceFeature("metal", "Fe", 11, 12, 2.3f,
492 mapping = new Mapping(peptide, map);
493 mf = new MappedFeatures(mapping, peptide, 113, 't', features);
494 sb = new StringBuilder();
495 sar.appendFeature(sb, 1, null, sf2, mf, 0);
496 assertEquals("metal 110 115; Fe Score=2.3", sb.toString());