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.assertFalse;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertNull;
27 import static org.testng.AssertJUnit.assertTrue;
29 import jalview.api.FeatureRenderer;
30 import jalview.datamodel.Alignment;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.SequenceDummy;
33 import jalview.datamodel.SequenceFeature;
34 import jalview.datamodel.SequenceI;
35 import jalview.gui.AlignFrame;
36 import jalview.schemes.AnnotationColourGradient;
37 import jalview.schemes.GraduatedColor;
39 import java.awt.Color;
41 import java.io.IOException;
44 import org.testng.annotations.Test;
46 public class FeaturesFileTest
49 private static String simpleGffFile = "examples/testdata/simpleGff3.gff";
51 @Test(groups = { "Functional" })
52 public void testParse() throws Exception
54 File f = new File("examples/uniref50.fa");
55 AlignmentI al = readAlignmentFile(f);
56 AlignFrame af = new AlignFrame(al, 500, 500);
57 Map<String, Object> colours = af.getFeatureRenderer()
59 FeaturesFile featuresFile = new FeaturesFile(
60 "examples/exampleFeatures.txt", FormatAdapter.FILE);
61 assertTrue("Test " + "Features file test"
62 + "\nFailed to parse features file.",
63 featuresFile.parse(al.getDataset(), colours, true));
66 * Refetch the colour map from the FeatureRenderer (to confirm it has been
67 * updated - JAL-1904), and verify (some) feature group colours
69 colours = af.getFeatureRenderer().getFeatureColours();
70 assertEquals("26 feature group colours not found", 26, colours.size());
71 assertEquals(colours.get("Cath"), new Color(0x93b1d1));
72 assertEquals(colours.get("ASX-MOTIF"), new Color(0x6addbb));
75 * verify (some) features on sequences
77 SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
78 .getSequenceFeatures(); // FER_CAPAA
79 assertEquals(8, sfs.length);
80 SequenceFeature sf = sfs[0];
81 assertEquals("Pfam family%LINK%", sf.description);
82 assertEquals(0, sf.begin);
83 assertEquals(0, sf.end);
84 assertEquals("uniprot", sf.featureGroup);
85 assertEquals("Pfam", sf.type);
86 assertEquals(1, sf.links.size());
87 assertEquals("Pfam family|http://pfam.xfam.org/family/PF00111",
91 assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
92 assertEquals(39, sf.begin);
93 assertEquals(39, sf.end);
94 assertEquals("uniprot", sf.featureGroup);
95 assertEquals("METAL", sf.type);
97 assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
98 assertEquals(44, sf.begin);
99 assertEquals(44, sf.end);
100 assertEquals("uniprot", sf.featureGroup);
101 assertEquals("METAL", sf.type);
103 assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
104 assertEquals(47, sf.begin);
105 assertEquals(47, sf.end);
106 assertEquals("uniprot", sf.featureGroup);
107 assertEquals("METAL", sf.type);
109 assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
110 assertEquals(77, sf.begin);
111 assertEquals(77, sf.end);
112 assertEquals("uniprot", sf.featureGroup);
113 assertEquals("METAL", sf.type);
115 assertEquals("Fer2 Status: True Positive Pfam 8_8%LINK%",
117 assertEquals("Pfam 8_8|http://pfam.xfam.org/family/PF00111",
119 assertEquals(8, sf.begin);
120 assertEquals(83, sf.end);
121 assertEquals("uniprot", sf.featureGroup);
122 assertEquals("Pfam", sf.type);
124 assertEquals("Ferredoxin_fold Status: True Positive ", sf.description);
125 assertEquals(3, sf.begin);
126 assertEquals(93, sf.end);
127 assertEquals("uniprot", sf.featureGroup);
128 assertEquals("Cath", sf.type);
131 "High confidence server. Only hits with scores over 0.8 are reported. PHOSPHORYLATION (T) 89_8%LINK%",
134 "PHOSPHORYLATION (T) 89_8|http://www.cbs.dtu.dk/cgi-bin/proview/webface-link?seqid=P83527&service=NetPhos-2.0",
136 assertEquals(89, sf.begin);
137 assertEquals(89, sf.end);
138 assertEquals("netphos", sf.featureGroup);
139 assertEquals("PHOSPHORYLATION (T)", sf.type);
143 * Test parsing a features file with a mix of Jalview and GFF formatted
148 @Test(groups = { "Functional" })
149 public void testParse_mixedJalviewGff() throws Exception
151 File f = new File("examples/uniref50.fa");
152 AlignmentI al = readAlignmentFile(f);
153 AlignFrame af = new AlignFrame(al, 500, 500);
154 Map<String, Object> colours = af.getFeatureRenderer()
155 .getFeatureColours();
156 // GFF2 uses space as name/value separator in column 9
157 String gffData = "METAL\tcc9900\n" + "GFF\n"
158 + "FER_CAPAA\tuniprot\tMETAL\t44\t45\t4.0\t.\t.\tNote Iron-sulfur; Note 2Fe-2S\n"
159 + "FER1_SOLLC\tuniprot\tPfam\t55\t130\t2.0\t.\t.";
160 FeaturesFile featuresFile = new FeaturesFile(gffData,
161 FormatAdapter.PASTE);
162 assertTrue("Failed to parse features file",
163 featuresFile.parse(al.getDataset(), colours, true));
165 // verify colours read or synthesized
166 colours = af.getFeatureRenderer().getFeatureColours();
167 assertEquals("1 feature group colours not found", 1, colours.size());
168 assertEquals(colours.get("METAL"), new Color(0xcc9900));
170 // verify feature on FER_CAPAA
171 SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
172 .getSequenceFeatures();
173 assertEquals(1, sfs.length);
174 SequenceFeature sf = sfs[0];
175 assertEquals("Iron-sulfur,2Fe-2S", sf.description);
176 assertEquals(44, sf.begin);
177 assertEquals(45, sf.end);
178 assertEquals("uniprot", sf.featureGroup);
179 assertEquals("METAL", sf.type);
180 assertEquals(4f, sf.getScore(), 0.001f);
182 // verify feature on FER1_SOLLC
183 sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
184 assertEquals(1, sfs.length);
186 assertEquals("uniprot", sf.description);
187 assertEquals(55, sf.begin);
188 assertEquals(130, sf.end);
189 assertEquals("uniprot", sf.featureGroup);
190 assertEquals("Pfam", sf.type);
191 assertEquals(2f, sf.getScore(), 0.001f);
194 public static AlignmentI readAlignmentFile(File f) throws IOException
196 System.out.println("Reading file: " + f);
197 String ff = f.getPath();
198 FormatAdapter rf = new FormatAdapter();
200 AlignmentI al = rf.readFile(ff, FormatAdapter.FILE,
201 new IdentifyFile().identify(ff, FormatAdapter.FILE));
203 al.setDataset(null); // creates dataset sequences
204 assertNotNull("Couldn't read supplied alignment data.", al);
209 * Test various ways of describing a feature colour scheme
213 @Test(groups = { "Functional" })
214 public void testParseGraduatedColourScheme() throws Exception
216 FeaturesFile ff = new FeaturesFile();
219 GraduatedColor gc = ff.parseGraduatedColourScheme(
220 "BETA-TURN-IR\t9a6a94", "label");
221 assertTrue(gc.isColourByLabel());
222 assertEquals(Color.white, gc.getMinColor());
223 assertEquals(Color.black, gc.getMaxColor());
224 assertTrue(gc.isAutoScale());
226 // using colour name, rgb, etc:
227 String spec = "blue|255,0,255|absolute|20.0|95.0|below|66.0";
228 gc = ff.parseGraduatedColourScheme("BETA-TURN-IR\t" + spec, spec);
229 assertFalse(gc.isColourByLabel());
230 assertEquals(Color.blue, gc.getMinColor());
231 assertEquals(new Color(255, 0, 255), gc.getMaxColor());
232 assertFalse(gc.isAutoScale());
233 assertFalse(gc.getTolow());
234 assertEquals(20.0f, gc.getMin(), 0.001f);
235 assertEquals(95.0f, gc.getMax(), 0.001f);
236 assertEquals(AnnotationColourGradient.BELOW_THRESHOLD,
238 assertEquals(66.0f, gc.getThresh(), 0.001f);
240 // inverse gradient high to low:
241 spec = "blue|255,0,255|95.0|20.0|below|66.0";
242 gc = ff.parseGraduatedColourScheme("BETA-TURN-IR\t" + spec, spec);
243 assertTrue(gc.isAutoScale());
244 assertTrue(gc.getTolow());
248 * Test parsing a features file with GFF formatted content only
252 @Test(groups = { "Functional" })
253 public void testParse_pureGff3() throws Exception
255 File f = new File("examples/uniref50.fa");
256 AlignmentI al = readAlignmentFile(f);
257 AlignFrame af = new AlignFrame(al, 500, 500);
258 Map<String, Object> colours = af.getFeatureRenderer()
259 .getFeatureColours();
260 // GFF3 uses '=' separator for name/value pairs in colum 9
261 String gffData = "##gff-version 3\n"
262 + "FER_CAPAA\tuniprot\tMETAL\t39\t39\t0.0\t.\t.\t"
263 + "Note=Iron-sulfur (2Fe-2S);Note=another note;evidence=ECO:0000255|PROSITE-ProRule:PRU00465\n"
264 + "FER1_SOLLC\tuniprot\tPfam\t55\t130\t3.0\t.\t.\tID=$23";
265 FeaturesFile featuresFile = new FeaturesFile(gffData,
266 FormatAdapter.PASTE);
267 assertTrue("Failed to parse features file",
268 featuresFile.parse(al.getDataset(), colours, true));
270 // verify feature on FER_CAPAA
271 SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
272 .getSequenceFeatures();
273 assertEquals(1, sfs.length);
274 SequenceFeature sf = sfs[0];
275 // description parsed from Note attribute
276 assertEquals("Iron-sulfur (2Fe-2S),another note", sf.description);
277 assertEquals(39, sf.begin);
278 assertEquals(39, sf.end);
279 assertEquals("uniprot", sf.featureGroup);
280 assertEquals("METAL", sf.type);
282 "Note=Iron-sulfur (2Fe-2S);Note=another note;evidence=ECO:0000255|PROSITE-ProRule:PRU00465",
283 sf.getValue("ATTRIBUTES"));
285 // verify feature on FER1_SOLLC1
286 sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
287 assertEquals(1, sfs.length);
289 // ID used for description if available
290 assertEquals("$23", sf.description);
291 assertEquals(55, sf.begin);
292 assertEquals(130, sf.end);
293 assertEquals("uniprot", sf.featureGroup);
294 assertEquals("Pfam", sf.type);
295 assertEquals(3f, sf.getScore(), 0.001f);
299 * Test parsing a features file with Jalview format features (but no colour
300 * descriptors or startgroup to give the hint not to parse as GFF)
304 @Test(groups = { "Functional" })
305 public void testParse_jalviewFeaturesOnly() throws Exception
307 File f = new File("examples/uniref50.fa");
308 AlignmentI al = readAlignmentFile(f);
309 AlignFrame af = new AlignFrame(al, 500, 500);
310 Map<String, Object> colours = af.getFeatureRenderer()
311 .getFeatureColours();
314 * one feature on FER_CAPAA and one on sequence 3 (index 2) FER1_SOLLC
316 String featureData = "Iron-sulfur (2Fe-2S)\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
317 + "Iron-phosphorus (2Fe-P)\tID_NOT_SPECIFIED\t2\t86\t87\tMETALLIC\n";
318 FeaturesFile featuresFile = new FeaturesFile(featureData,
319 FormatAdapter.PASTE);
320 assertTrue("Failed to parse features file",
321 featuresFile.parse(al.getDataset(), colours, true));
323 // verify FER_CAPAA feature
324 SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
325 .getSequenceFeatures();
326 assertEquals(1, sfs.length);
327 SequenceFeature sf = sfs[0];
328 assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
329 assertEquals(39, sf.begin);
330 assertEquals(39, sf.end);
331 assertEquals("METAL", sf.type);
333 // verify FER1_SOLLC feature
334 sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
335 assertEquals(1, sfs.length);
337 assertEquals("Iron-phosphorus (2Fe-P)", sf.description);
338 assertEquals(86, sf.begin);
339 assertEquals(87, sf.end);
340 assertEquals("METALLIC", sf.type);
343 private void checkDatasetfromSimpleGff3(AlignmentI dataset)
345 assertEquals("no sequences extracted from GFF3 file", 2,
346 dataset.getHeight());
348 SequenceI seq1 = dataset.findName("seq1");
349 SequenceI seq2 = dataset.findName("seq2");
353 "Failed to replace dummy seq1 with real sequence",
354 seq1 instanceof SequenceDummy
355 && ((SequenceDummy) seq1).isDummy());
357 "Failed to replace dummy seq2 with real sequence",
358 seq2 instanceof SequenceDummy
359 && ((SequenceDummy) seq2).isDummy());
360 String placeholderseq = new SequenceDummy("foo").getSequenceAsString();
361 assertFalse("dummy replacement buggy for seq1",
362 placeholderseq.equals(seq1.getSequenceAsString()));
363 assertFalse("dummy replacement buggy for seq2",
364 placeholderseq.equals(seq2.getSequenceAsString()));
365 assertNotNull("No features added to seq1", seq1.getSequenceFeatures());
366 assertEquals("Wrong number of features", 3,
367 seq1.getSequenceFeatures().length);
368 assertNull(seq2.getSequenceFeatures());
370 "Wrong number of features",
372 seq2.getSequenceFeatures() == null ? 0 : seq2
373 .getSequenceFeatures().length);
375 "Expected at least one CDNA/Protein mapping for seq1",
376 dataset.getCodonFrame(seq1) != null
377 && dataset.getCodonFrame(seq1).size() > 0);
381 @Test(groups = { "Functional" })
382 public void readGff3File() throws IOException
384 FeaturesFile gffreader = new FeaturesFile(true, simpleGffFile,
386 Alignment dataset = new Alignment(gffreader.getSeqsAsArray());
387 gffreader.addProperties(dataset);
388 checkDatasetfromSimpleGff3(dataset);
391 @Test(groups = { "Functional" })
392 public void simpleGff3FileClass() throws IOException
394 AlignmentI dataset = new Alignment(new SequenceI[] {});
395 FeaturesFile ffile = new FeaturesFile(simpleGffFile,
398 boolean parseResult = ffile.parse(dataset, null, false, false);
399 assertTrue("return result should be true", parseResult);
400 checkDatasetfromSimpleGff3(dataset);
403 @Test(groups = { "Functional" })
404 public void simpleGff3FileLoader() throws IOException
406 AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
407 simpleGffFile, FormatAdapter.FILE);
409 "Didn't read the alignment into an alignframe from Gff3 File",
411 checkDatasetfromSimpleGff3(af.getViewport().getAlignment());
414 @Test(groups = { "Functional" })
415 public void simpleGff3RelaxedIdMatching() throws IOException
417 AlignmentI dataset = new Alignment(new SequenceI[] {});
418 FeaturesFile ffile = new FeaturesFile(simpleGffFile,
421 boolean parseResult = ffile.parse(dataset, null, false, true);
422 assertTrue("return result (relaxedID matching) should be true",
424 checkDatasetfromSimpleGff3(dataset);
427 @Test(groups = { "Functional" })
428 public void testPrintJalviewFormat() throws Exception
430 File f = new File("examples/uniref50.fa");
431 AlignmentI al = readAlignmentFile(f);
432 AlignFrame af = new AlignFrame(al, 500, 500);
433 Map<String, Object> colours = af.getFeatureRenderer()
434 .getFeatureColours();
435 String features = "METAL\tcc9900\n"
436 + "GAMMA-TURN\tred|0,255,255|20.0|95.0|below|66.0\n"
438 + "STARTGROUP\tuniprot\n"
439 + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
440 + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\n"
441 + "<html>Pfam domain<a href=\"http://pfam.xfam.org/family/PF00111\">Pfam_3_4</a></html>\tFER_CAPAA\t-1\t20\t20\tPfam\n"
442 + "ENDGROUP\tuniprot\n";
443 FeaturesFile featuresFile = new FeaturesFile(features,
444 FormatAdapter.PASTE);
445 featuresFile.parse(al.getDataset(), colours, false);
448 * first with no features displayed
450 FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
451 Map<String, Object> visible = fr
452 .getDisplayedFeatureCols();
453 String exported = featuresFile.printJalviewFormat(
454 al.getSequencesArray(), visible);
455 String expected = "No Features Visible";
456 assertEquals(expected, exported);
459 * set METAL (in uniprot group) and GAMMA-TURN visible, but not Pfam
461 fr.setVisible("METAL");
462 fr.setVisible("GAMMA-TURN");
463 visible = fr.getDisplayedFeatureCols();
464 exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
466 expected = "METAL\tcc9900\n"
467 + "GAMMA-TURN\tff0000|00ffff|20.0|95.0|below|66.0\n"
468 + "\nSTARTGROUP\tuniprot\n"
469 + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
470 + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
471 + "ENDGROUP\tuniprot\n";
472 assertEquals(expected, exported);
475 * now set Pfam visible
477 fr.setVisible("Pfam");
478 visible = fr.getDisplayedFeatureCols();
479 exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
482 * note the order of feature types is uncontrolled - derives from
483 * FeaturesDisplayed.featuresDisplayed which is a HashSet
485 expected = "METAL\tcc9900\n"
487 + "GAMMA-TURN\tff0000|00ffff|20.0|95.0|below|66.0\n"
488 + "\nSTARTGROUP\tuniprot\n"
489 + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
490 + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
491 + "<html>Pfam domain<a href=\"http://pfam.xfam.org/family/PF00111\">Pfam_3_4</a></html>\tFER_CAPAA\t-1\t20\t20\tPfam\t0.0\n"
492 + "ENDGROUP\tuniprot\n";
493 assertEquals(expected, exported);