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.FeatureColourI;
30 import jalview.api.FeatureRenderer;
31 import jalview.datamodel.Alignment;
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.SequenceDummy;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceI;
36 import jalview.gui.AlignFrame;
38 import java.awt.Color;
40 import java.io.IOException;
43 import org.testng.annotations.Test;
45 public class FeaturesFileTest
48 private static String simpleGffFile = "examples/testdata/simpleGff3.gff";
50 @Test(groups = { "Functional" })
51 public void testParse() throws Exception
53 File f = new File("examples/uniref50.fa");
54 AlignmentI al = readAlignmentFile(f);
55 AlignFrame af = new AlignFrame(al, 500, 500);
56 Map<String, FeatureColourI> colours = af.getFeatureRenderer()
58 FeaturesFile featuresFile = new FeaturesFile(
59 "examples/exampleFeatures.txt", FormatAdapter.FILE);
60 assertTrue("Test " + "Features file test"
61 + "\nFailed to parse features file.",
62 featuresFile.parse(al.getDataset(), colours, true));
65 * Refetch the colour map from the FeatureRenderer (to confirm it has been
66 * updated - JAL-1904), and verify (some) feature group colours
68 colours = af.getFeatureRenderer().getFeatureColours();
69 assertEquals("26 feature group colours not found", 26, colours.size());
70 assertEquals(colours.get("Cath").getColour(), new Color(0x93b1d1));
71 assertEquals(colours.get("ASX-MOTIF").getColour(), new Color(0x6addbb));
74 * verify (some) features on sequences
76 SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
77 .getSequenceFeatures(); // FER_CAPAA
78 assertEquals(8, sfs.length);
79 SequenceFeature sf = sfs[0];
80 assertEquals("Pfam family%LINK%", sf.description);
81 assertEquals(0, sf.begin);
82 assertEquals(0, sf.end);
83 assertEquals("uniprot", sf.featureGroup);
84 assertEquals("Pfam", sf.type);
85 assertEquals(1, sf.links.size());
86 assertEquals("Pfam family|http://pfam.xfam.org/family/PF00111",
90 assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
91 assertEquals(39, sf.begin);
92 assertEquals(39, sf.end);
93 assertEquals("uniprot", sf.featureGroup);
94 assertEquals("METAL", sf.type);
96 assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
97 assertEquals(44, sf.begin);
98 assertEquals(44, sf.end);
99 assertEquals("uniprot", sf.featureGroup);
100 assertEquals("METAL", sf.type);
102 assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
103 assertEquals(47, sf.begin);
104 assertEquals(47, sf.end);
105 assertEquals("uniprot", sf.featureGroup);
106 assertEquals("METAL", sf.type);
108 assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
109 assertEquals(77, sf.begin);
110 assertEquals(77, sf.end);
111 assertEquals("uniprot", sf.featureGroup);
112 assertEquals("METAL", sf.type);
114 assertEquals("Fer2 Status: True Positive Pfam 8_8%LINK%",
116 assertEquals("Pfam 8_8|http://pfam.xfam.org/family/PF00111",
118 assertEquals(8, sf.begin);
119 assertEquals(83, sf.end);
120 assertEquals("uniprot", sf.featureGroup);
121 assertEquals("Pfam", sf.type);
123 assertEquals("Ferredoxin_fold Status: True Positive ", sf.description);
124 assertEquals(3, sf.begin);
125 assertEquals(93, sf.end);
126 assertEquals("uniprot", sf.featureGroup);
127 assertEquals("Cath", sf.type);
130 "High confidence server. Only hits with scores over 0.8 are reported. PHOSPHORYLATION (T) 89_8%LINK%",
133 "PHOSPHORYLATION (T) 89_8|http://www.cbs.dtu.dk/cgi-bin/proview/webface-link?seqid=P83527&service=NetPhos-2.0",
135 assertEquals(89, sf.begin);
136 assertEquals(89, sf.end);
137 assertEquals("netphos", sf.featureGroup);
138 assertEquals("PHOSPHORYLATION (T)", sf.type);
142 * Test parsing a features file with a mix of Jalview and GFF formatted
147 @Test(groups = { "Functional" })
148 public void testParse_mixedJalviewGff() throws Exception
150 File f = new File("examples/uniref50.fa");
151 AlignmentI al = readAlignmentFile(f);
152 AlignFrame af = new AlignFrame(al, 500, 500);
153 Map<String, FeatureColourI> colours = af.getFeatureRenderer()
154 .getFeatureColours();
155 // GFF2 uses space as name/value separator in column 9
156 String gffData = "METAL\tcc9900\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").getColour(), 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 parsing a features file with GFF formatted content only
213 @Test(groups = { "Functional" })
214 public void testParse_pureGff3() throws Exception
216 File f = new File("examples/uniref50.fa");
217 AlignmentI al = readAlignmentFile(f);
218 AlignFrame af = new AlignFrame(al, 500, 500);
219 Map<String, FeatureColourI> colours = af.getFeatureRenderer()
220 .getFeatureColours();
221 // GFF3 uses '=' separator for name/value pairs in colum 9
222 String gffData = "##gff-version 3\n"
223 + "FER_CAPAA\tuniprot\tMETAL\t39\t39\t0.0\t.\t.\t"
224 + "Note=Iron-sulfur (2Fe-2S);Note=another note;evidence=ECO:0000255|PROSITE-ProRule:PRU00465\n"
225 + "FER1_SOLLC\tuniprot\tPfam\t55\t130\t3.0\t.\t.\tID=$23";
226 FeaturesFile featuresFile = new FeaturesFile(gffData,
227 FormatAdapter.PASTE);
228 assertTrue("Failed to parse features file",
229 featuresFile.parse(al.getDataset(), colours, true));
231 // verify feature on FER_CAPAA
232 SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
233 .getSequenceFeatures();
234 assertEquals(1, sfs.length);
235 SequenceFeature sf = sfs[0];
236 // description parsed from Note attribute
237 assertEquals("Iron-sulfur (2Fe-2S),another note", sf.description);
238 assertEquals(39, sf.begin);
239 assertEquals(39, sf.end);
240 assertEquals("uniprot", sf.featureGroup);
241 assertEquals("METAL", sf.type);
243 "Note=Iron-sulfur (2Fe-2S);Note=another note;evidence=ECO:0000255|PROSITE-ProRule:PRU00465",
244 sf.getValue("ATTRIBUTES"));
246 // verify feature on FER1_SOLLC1
247 sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
248 assertEquals(1, sfs.length);
250 // ID used for description if available
251 assertEquals("$23", sf.description);
252 assertEquals(55, sf.begin);
253 assertEquals(130, sf.end);
254 assertEquals("uniprot", sf.featureGroup);
255 assertEquals("Pfam", sf.type);
256 assertEquals(3f, sf.getScore(), 0.001f);
260 * Test parsing a features file with Jalview format features (but no colour
261 * descriptors or startgroup to give the hint not to parse as GFF)
265 @Test(groups = { "Functional" })
266 public void testParse_jalviewFeaturesOnly() throws Exception
268 File f = new File("examples/uniref50.fa");
269 AlignmentI al = readAlignmentFile(f);
270 AlignFrame af = new AlignFrame(al, 500, 500);
271 Map<String, FeatureColourI> colours = af.getFeatureRenderer()
272 .getFeatureColours();
275 * one feature on FER_CAPAA and one on sequence 3 (index 2) FER1_SOLLC
277 String featureData = "Iron-sulfur (2Fe-2S)\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
278 + "Iron-phosphorus (2Fe-P)\tID_NOT_SPECIFIED\t2\t86\t87\tMETALLIC\n";
279 FeaturesFile featuresFile = new FeaturesFile(featureData,
280 FormatAdapter.PASTE);
281 assertTrue("Failed to parse features file",
282 featuresFile.parse(al.getDataset(), colours, true));
284 // verify FER_CAPAA feature
285 SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
286 .getSequenceFeatures();
287 assertEquals(1, sfs.length);
288 SequenceFeature sf = sfs[0];
289 assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
290 assertEquals(39, sf.begin);
291 assertEquals(39, sf.end);
292 assertEquals("METAL", sf.type);
294 // verify FER1_SOLLC feature
295 sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
296 assertEquals(1, sfs.length);
298 assertEquals("Iron-phosphorus (2Fe-P)", sf.description);
299 assertEquals(86, sf.begin);
300 assertEquals(87, sf.end);
301 assertEquals("METALLIC", sf.type);
304 private void checkDatasetfromSimpleGff3(AlignmentI dataset)
306 assertEquals("no sequences extracted from GFF3 file", 2,
307 dataset.getHeight());
309 SequenceI seq1 = dataset.findName("seq1");
310 SequenceI seq2 = dataset.findName("seq2");
314 "Failed to replace dummy seq1 with real sequence",
315 seq1 instanceof SequenceDummy
316 && ((SequenceDummy) seq1).isDummy());
318 "Failed to replace dummy seq2 with real sequence",
319 seq2 instanceof SequenceDummy
320 && ((SequenceDummy) seq2).isDummy());
321 String placeholderseq = new SequenceDummy("foo").getSequenceAsString();
322 assertFalse("dummy replacement buggy for seq1",
323 placeholderseq.equals(seq1.getSequenceAsString()));
324 assertFalse("dummy replacement buggy for seq2",
325 placeholderseq.equals(seq2.getSequenceAsString()));
326 assertNotNull("No features added to seq1", seq1.getSequenceFeatures());
327 assertEquals("Wrong number of features", 3,
328 seq1.getSequenceFeatures().length);
329 assertNull(seq2.getSequenceFeatures());
331 "Wrong number of features",
333 seq2.getSequenceFeatures() == null ? 0 : seq2
334 .getSequenceFeatures().length);
336 "Expected at least one CDNA/Protein mapping for seq1",
337 dataset.getCodonFrame(seq1) != null
338 && dataset.getCodonFrame(seq1).size() > 0);
342 @Test(groups = { "Functional" })
343 public void readGff3File() throws IOException
345 FeaturesFile gffreader = new FeaturesFile(true, simpleGffFile,
347 Alignment dataset = new Alignment(gffreader.getSeqsAsArray());
348 gffreader.addProperties(dataset);
349 checkDatasetfromSimpleGff3(dataset);
352 @Test(groups = { "Functional" })
353 public void simpleGff3FileClass() throws IOException
355 AlignmentI dataset = new Alignment(new SequenceI[] {});
356 FeaturesFile ffile = new FeaturesFile(simpleGffFile, FormatAdapter.FILE);
358 boolean parseResult = ffile.parse(dataset, null, false, false);
359 assertTrue("return result should be true", parseResult);
360 checkDatasetfromSimpleGff3(dataset);
363 @Test(groups = { "Functional" })
364 public void simpleGff3FileLoader() throws IOException
366 AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
367 simpleGffFile, FormatAdapter.FILE);
369 "Didn't read the alignment into an alignframe from Gff3 File",
371 checkDatasetfromSimpleGff3(af.getViewport().getAlignment());
374 @Test(groups = { "Functional" })
375 public void simpleGff3RelaxedIdMatching() throws IOException
377 AlignmentI dataset = new Alignment(new SequenceI[] {});
378 FeaturesFile ffile = new FeaturesFile(simpleGffFile, FormatAdapter.FILE);
380 boolean parseResult = ffile.parse(dataset, null, false, true);
381 assertTrue("return result (relaxedID matching) should be true",
383 checkDatasetfromSimpleGff3(dataset);
386 @Test(groups = { "Functional" })
387 public void testPrintJalviewFormat() throws Exception
389 File f = new File("examples/uniref50.fa");
390 AlignmentI al = readAlignmentFile(f);
391 AlignFrame af = new AlignFrame(al, 500, 500);
392 Map<String, FeatureColourI> colours = af.getFeatureRenderer()
393 .getFeatureColours();
394 String features = "METAL\tcc9900\n"
395 + "GAMMA-TURN\tred|0,255,255|20.0|95.0|below|66.0\n"
397 + "STARTGROUP\tuniprot\n"
398 + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
399 + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\n"
400 + "<html>Pfam domain<a href=\"http://pfam.xfam.org/family/PF00111\">Pfam_3_4</a></html>\tFER_CAPAA\t-1\t20\t20\tPfam\n"
401 + "ENDGROUP\tuniprot\n";
402 FeaturesFile featuresFile = new FeaturesFile(features,
403 FormatAdapter.PASTE);
404 featuresFile.parse(al.getDataset(), colours, false);
407 * first with no features displayed
409 FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
410 Map<String, FeatureColourI> visible = fr.getDisplayedFeatureCols();
411 String exported = featuresFile.printJalviewFormat(
412 al.getSequencesArray(), visible);
413 String expected = "No Features Visible";
414 assertEquals(expected, exported);
417 * set METAL (in uniprot group) and GAMMA-TURN visible, but not Pfam
419 fr.setVisible("METAL");
420 fr.setVisible("GAMMA-TURN");
421 visible = fr.getDisplayedFeatureCols();
422 exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
424 expected = "METAL\tcc9900\n"
425 + "GAMMA-TURN\tff0000|00ffff|20.0|95.0|below|66.0\n"
426 + "\nSTARTGROUP\tuniprot\n"
427 + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
428 + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
429 + "ENDGROUP\tuniprot\n";
430 assertEquals(expected, exported);
433 * now set Pfam visible
435 fr.setVisible("Pfam");
436 visible = fr.getDisplayedFeatureCols();
437 exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
440 * note the order of feature types is uncontrolled - derives from
441 * FeaturesDisplayed.featuresDisplayed which is a HashSet
443 expected = "METAL\tcc9900\n"
445 + "GAMMA-TURN\tff0000|00ffff|20.0|95.0|below|66.0\n"
446 + "\nSTARTGROUP\tuniprot\n"
447 + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
448 + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
449 + "<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"
450 + "ENDGROUP\tuniprot\n";
451 assertEquals(expected, exported);