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,
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,
109 new FeatureRenderer(null), 200); // text should terminate before 200 characters
110 String s = sb.toString();
111 assertTrue(s.length() < 200);
112 assertEquals(n, 7); // should be 7 features left over
116 @Test(groups = "Functional")
117 public void testAppendFeature_status()
119 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
120 StringBuilder sb = new StringBuilder();
121 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
123 sf.setStatus("Confirmed");
125 sar.appendFeature(sb, 1, null, sf, null, 0);
126 assertEquals("METAL 1 3; Fe2-S; (Confirmed)", sb.toString());
129 @Test(groups = "Functional")
130 public void testAppendFeature_withScore()
132 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
133 StringBuilder sb = new StringBuilder();
134 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
137 FeatureRendererModel fr = new FeatureRenderer(null);
138 Map<String, float[][]> minmax = fr.getMinMax();
139 sar.appendFeature(sb, 1, fr, sf, null, 0);
141 * map has no entry for this feature type - score is not shown:
143 assertEquals("METAL 1 3; Fe2-S", sb.toString());
146 * map has entry for this feature type - score is shown:
148 minmax.put("METAL", new float[][] { { 0f, 1f }, null });
149 sar.appendFeature(sb, 1, fr, sf, null, 0);
150 // <br/> is appended to a buffer > 6 in length
151 assertEquals("METAL 1 3; Fe2-S<br/>METAL 1 3; Fe2-S Score=1.3",
155 * map has min == max for this feature type - score is not shown:
157 minmax.put("METAL", new float[][] { { 2f, 2f }, null });
159 sar.appendFeature(sb, 1, fr, sf, null, 0);
160 assertEquals("METAL 1 3; Fe2-S", sb.toString());
163 @Test(groups = "Functional")
164 public void testAppendFeature_noScore()
166 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
167 StringBuilder sb = new StringBuilder();
168 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
171 sar.appendFeature(sb, 1, null, sf, null, 0);
172 assertEquals("METAL 1 3; Fe2-S", sb.toString());
176 * A specific attribute value is included if it is used to colour the feature
178 @Test(groups = "Functional")
179 public void testAppendFeature_colouredByAttribute()
181 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
182 StringBuilder sb = new StringBuilder();
183 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3,
185 sf.setValue("clinical_significance", "Benign");
188 * first with no colour by attribute
190 FeatureRendererModel fr = new FeatureRenderer(null);
191 sar.appendFeature(sb, 1, fr, sf, null, 0);
192 assertEquals("METAL 1 3; Fe2-S", sb.toString());
195 * then with colour by an attribute the feature lacks
197 FeatureColourI fc = new FeatureColour(null, Color.white, Color.black,
199 fc.setAttributeName("Pfam");
200 fr.setColour("METAL", fc);
202 sar.appendFeature(sb, 1, fr, sf, null, 0);
203 assertEquals("METAL 1 3; Fe2-S", sb.toString()); // no change
206 * then with colour by an attribute the feature has
208 fc.setAttributeName("clinical_significance");
210 sar.appendFeature(sb, 1, fr, sf, null, 0);
211 assertEquals("METAL 1 3; Fe2-S; clinical_significance=Benign",
215 @Test(groups = "Functional")
216 public void testAppendFeature_withScoreStatusAttribute()
218 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
219 StringBuilder sb = new StringBuilder();
220 SequenceFeature sf = new SequenceFeature("METAL", "Fe2-S", 1, 3, 1.3f,
222 sf.setStatus("Confirmed");
223 sf.setValue("clinical_significance", "Benign");
225 FeatureRendererModel fr = new FeatureRenderer(null);
226 Map<String, float[][]> minmax = fr.getMinMax();
227 FeatureColourI fc = new FeatureColour(null, Color.white, Color.blue,
229 fc.setAttributeName("clinical_significance");
230 fr.setColour("METAL", fc);
231 minmax.put("METAL", new float[][] { { 0f, 1f }, null });
232 sar.appendFeature(sb, 1, fr, sf, null, 0);
235 "METAL 1 3; Fe2-S Score=1.3; (Confirmed); clinical_significance=Benign",
239 @Test(groups = "Functional")
240 public void testAppendFeature_DescEqualsType()
242 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
243 StringBuilder sb = new StringBuilder();
244 SequenceFeature sf = new SequenceFeature("METAL", "METAL", 1, 3,
247 // description is not included if it duplicates type:
248 sar.appendFeature(sb, 1, null, sf, null, 0);
249 assertEquals("METAL 1 3", sb.toString());
252 sf.setDescription("Metal");
253 // test is case-sensitive:
254 sar.appendFeature(sb, 1, null, sf, null, 0);
255 assertEquals("METAL 1 3; Metal", sb.toString());
258 @Test(groups = "Functional")
259 public void testAppendFeature_stripHtml()
261 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
262 StringBuilder sb = new StringBuilder();
263 SequenceFeature sf = new SequenceFeature("METAL",
264 "<html><body>hello<em>world</em></body></html>", 1, 3,
267 sar.appendFeature(sb, 1, null, sf, null, 0);
268 // !! strips off </body> but not <body> ??
269 assertEquals("METAL 1 3; <body>hello<em>world</em>", sb.toString());
272 sf.setDescription("<br>&kHD>6");
273 sar.appendFeature(sb, 1, null, sf, null, 0);
274 // if no <html> tag, html-encodes > and < (only):
275 assertEquals("METAL 1 3; <br>&kHD>6", sb.toString());
278 @Test(groups = "Functional")
279 public void testCreateSequenceAnnotationReport()
281 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
282 StringBuilder sb = new StringBuilder();
284 SequenceI seq = new Sequence("s1", "MAKLKRFQSSTLL");
285 seq.setDescription("SeqDesc");
288 * positional features are ignored
290 seq.addSequenceFeature(new SequenceFeature("Domain", "Ferredoxin", 5,
292 sar.createSequenceAnnotationReport(sb, seq, true, true, null);
293 assertEquals("<i>SeqDesc</i>", sb.toString());
296 * non-positional feature
298 seq.addSequenceFeature(new SequenceFeature("Type1", "Nonpos", 0, 0, 1f,
301 sar.createSequenceAnnotationReport(sb, seq, true, true, null);
302 String expected = "<i>SeqDesc<br/>Type1 ; Nonpos Score=1.0</i>";
303 assertEquals(expected, sb.toString());
306 * non-positional features not wanted
309 sar.createSequenceAnnotationReport(sb, seq, true, false, null);
310 assertEquals("<i>SeqDesc</i>", sb.toString());
313 * add non-pos feature with score inside min-max range for feature type
314 * minmax holds { [positionalMin, positionalMax], [nonPosMin, nonPosMax] }
315 * score is only appended for positional features so ignored here!
316 * minMax are not recorded for non-positional features
318 seq.addSequenceFeature(new SequenceFeature("Metal", "Desc", 0, 0, 5f,
321 FeatureRendererModel fr = new FeatureRenderer(null);
322 Map<String, float[][]> minmax = fr.getMinMax();
323 minmax.put("Metal", new float[][] { null, new float[] { 2f, 5f } });
326 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
327 expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos</i>";
328 assertEquals(expected, sb.toString());
331 * 'linkonly' features are ignored; this is obsolete, as linkonly
332 * is only set by DasSequenceFetcher, and DAS is history
334 SequenceFeature sf = new SequenceFeature("Metal", "Desc", 0, 0, 5f,
336 sf.setValue("linkonly", Boolean.TRUE);
337 seq.addSequenceFeature(sf);
339 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
340 assertEquals(expected, sb.toString()); // unchanged!
343 * 'clinical_significance' attribute is only included in description
344 * when used for feature colouring
346 SequenceFeature sf2 = new SequenceFeature("Variant", "Havana", 0, 0,
348 sf2.setValue(GffConstants.CLINICAL_SIGNIFICANCE, "benign");
349 seq.addSequenceFeature(sf2);
351 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
352 expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos<br/>Variant ; Havana</i>";
353 assertEquals(expected, sb.toString());
358 seq.addDBRef(new DBRefEntry("PDB", "0", "3iu1"));
359 seq.addDBRef(new DBRefEntry("Uniprot", "1", "P30419"));
361 // with showDbRefs = false
363 sar.createSequenceAnnotationReport(sb, seq, false, true, fr);
364 assertEquals(expected, sb.toString()); // unchanged
366 // with showDbRefs = true, colour Variant features by clinical_significance
368 FeatureColourI fc = new FeatureColour(null, Color.green, Color.pink,
370 fc.setAttributeName("clinical_significance");
371 fr.setColour("Variant", fc);
372 sar.createSequenceAnnotationReport(sb, seq, true, true, fr);
373 expected = "<i>SeqDesc<br/>UNIPROT P30419<br/>PDB 3iu1<br/>Metal ; Desc<br/>"
374 + "Type1 ; Nonpos<br/>Variant ; Havana; clinical_significance=benign</i>";
375 assertEquals(expected, sb.toString());
376 // with showNonPositionalFeatures = false
378 sar.createSequenceAnnotationReport(sb, seq, true, false, fr);
379 expected = "<i>SeqDesc<br/>UNIPROT P30419<br/>PDB 3iu1</i>";
380 assertEquals(expected, sb.toString());
383 * long feature description is truncated with ellipsis
387 "This is a very long description which should be truncated");
388 sar.createSequenceAnnotationReport(sb, seq, false, true, fr);
389 expected = "<i>SeqDesc<br/>Metal ; Desc<br/>Type1 ; Nonpos<br/>Variant ; This is a very long description which sh...; clinical_significance=benign</i>";
390 assertEquals(expected, sb.toString());
392 // see other tests for treatment of status and html
396 * Test that exercises an abbreviated sequence details report, with ellipsis
397 * where there are more than 40 different sources, or more than 4 dbrefs for a
400 @Test(groups = "Functional")
401 public void testCreateSequenceAnnotationReport_withEllipsis()
403 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
404 StringBuilder sb = new StringBuilder();
406 SequenceI seq = new Sequence("s1", "ABC");
408 int maxSources = (int) PA.getValue(sar, "MAX_SOURCES");
409 for (int i = 0; i <= maxSources; i++)
411 seq.addDBRef(new DBRefEntry("PDB" + i, "0", "3iu1"));
414 int maxRefs = (int) PA.getValue(sar, "MAX_REFS_PER_SOURCE");
415 for (int i = 0; i <= maxRefs; i++)
417 seq.addDBRef(new DBRefEntry("Uniprot", "0", "P3041" + i));
420 sar.createSequenceAnnotationReport(sb, seq, true, true, null, true);
421 String report = sb.toString();
424 "<i><br/>UNIPROT P30410, P30411, P30412, P30413,...<br/>PDB0 3iu1"));
427 "<br/>PDB7 3iu1<br/>PDB8,...<br/>(Output Sequence Details to list all database references)</i>"));
431 * Test adding a linked feature to the tooltip
433 @Test(groups = "Functional")
434 public void testAppendFeature_virtualFeature()
437 * map CDS to peptide sequence
439 SequenceI cds = new Sequence("Cds/101-121", "CCTttgAGAtttCAAatgGAT");
440 SequenceI peptide = new Sequence("Peptide/8-14", "PLRFQMD");
441 MapList map = new MapList(new int[] { 101, 118 }, new int[] { 8, 13 },
443 Mapping mapping = new Mapping(peptide, map);
446 * assume variant feature found at CDS position 106 G>C
448 List<SequenceFeature> features = new ArrayList<>();
449 // vary ttg (Leu) to ttc (Phe)
450 SequenceFeature sf = new SequenceFeature("variant", "G,C", 106, 106,
453 MappedFeatures mf = new MappedFeatures(mapping, cds, 9, 'L', features);
455 StringBuilder sb = new StringBuilder();
456 SequenceAnnotationReport sar = new SequenceAnnotationReport(false);
457 sar.appendFeature(sb, 1, null, sf, mf, 0);
460 * linked feature shown in tooltip in protein coordinates
462 assertEquals("variant 9; G,C", sb.toString());
465 * adding "alleles" attribute to variant allows peptide consequence
466 * to be calculated and added to the tooltip
468 sf.setValue("alleles", "G,C");
469 sb = new StringBuilder();
470 sar.appendFeature(sb, 1, null, sf, mf, 0);
471 assertEquals("variant 9; G,C p.Leu9Phe", sb.toString());
474 * now a virtual peptide feature on CDS
475 * feature at 11-12 on peptide maps to 110-115 on CDS
476 * here we test for tooltip at 113 (t)
478 SequenceFeature sf2 = new SequenceFeature("metal", "Fe", 11, 12,
482 mapping = new Mapping(peptide, map);
483 mf = new MappedFeatures(mapping, peptide, 113, 't', features);
484 sb = new StringBuilder();
485 sar.appendFeature(sb, 1, null, sf2, mf, 0);
486 assertEquals("metal 110 115; Fe Score=2.3", sb.toString());