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.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
27 import java.awt.Color;
29 import java.util.Hashtable;
30 import java.util.List;
32 import org.testng.Assert;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
36 import jalview.datamodel.AlignmentI;
37 import jalview.datamodel.HiddenColumns;
38 import jalview.datamodel.SequenceGroup;
39 import jalview.gui.AlignFrame;
40 import jalview.gui.JvOptionPane;
41 import jalview.io.AnnotationFile.ViewDef;
43 public class AnnotationFileIOTest
46 @BeforeClass(alwaysRun = true)
47 public void setUpJvOptionPane()
49 JvOptionPane.setInteractiveMode(false);
50 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
53 static String TestFiles[][] = { { "Test example annotation import/export",
54 "examples/uniref50.fa", "examples/testdata/example_annot_file.jva" },
55 { "Test multiple combine annotation statements import/export",
56 "examples/uniref50.fa",
57 "examples/testdata/test_combine_annot.jva" },
58 { "Test multiple combine annotation statements with sequence_ref import/export",
59 "examples/uniref50.fa", "examples/testdata/uniref50_iupred.jva" },
60 { "Test group only annotation file parsing results in parser indicating annotation was parsed",
61 "examples/uniref50.fa", "examples/testdata/test_grpannot.jva" },
62 { "Test hiding/showing of insertions on sequence_ref",
63 "examples/uniref50.fa",
64 "examples/testdata/uniref50_seqref.jva" } };
66 @Test(groups = { "Functional" })
67 public void exampleAnnotationFileIO() throws Exception
69 for (String[] testPair : TestFiles)
71 testAnnotationFileIO(testPair[0], new File(testPair[1]),
72 new File(testPair[2]));
76 protected AlignmentI readAlignmentFile(File f)
78 System.out.println("Reading file: " + f);
79 String ff = f.getPath();
82 FormatAdapter rf = new FormatAdapter();
84 AlignmentI al = rf.readFile(ff, DataSourceType.FILE,
85 new IdentifyFile().identify(ff, DataSourceType.FILE));
87 // make sure dataset is initialised ? not sure about this
88 for (int i = 0; i < al.getSequencesArray().length; ++i)
90 al.getSequenceAt(i).createDatasetSequence();
92 assertNotNull("Couldn't read supplied alignment data.", al);
99 "Couln't read the alignment in file '" + f.toString() + "'");
104 * test alignment data in given file can be imported, exported and reimported
108 * - source datafile (IdentifyFile.identify() should work with it)
110 * - label for IO class used to write and read back in the data from
113 void testAnnotationFileIO(String testname, File f, File annotFile)
115 System.out.println("Test: " + testname + "\nReading annotation file '"
116 + annotFile + "' onto : " + f);
117 String af = annotFile.getPath();
120 AlignmentI al = readAlignmentFile(f);
121 HiddenColumns cs = new HiddenColumns();
122 assertTrue("Test " + testname
123 + "\nAlignment was not annotated - annotation file not imported.",
124 new AnnotationFile().readAnnotationFile(al, cs, af,
125 DataSourceType.FILE));
127 AnnotationFile aff = new AnnotationFile();
128 // ViewDef is not used by Jalview
129 ViewDef v = aff.new ViewDef(null, al.getHiddenSequences(), cs,
131 String anfileout = new AnnotationFile().printAnnotations(
132 al.getAlignmentAnnotation(), al.getGroups(),
133 al.getProperties(), null, al, v);
134 assertTrue("Test " + testname
135 + "\nAlignment annotation file was not regenerated. Null string",
137 assertTrue("Test " + testname
138 + "\nAlignment annotation file was not regenerated. Empty string",
139 anfileout.length() > "JALVIEW_ANNOTATION".length());
142 "Output annotation file:\n" + anfileout + "\n<<EOF\n");
144 AlignmentI al_new = readAlignmentFile(f);
145 assertTrue("Test " + testname
146 + "\nregenerated annotation file did not annotate alignment.",
147 new AnnotationFile().readAnnotationFile(al_new, anfileout,
148 DataSourceType.PASTE));
150 // test for consistency in io
151 StockholmFileTest.testAlignmentEquivalence(al, al_new, false, false,
154 } catch (Exception e)
158 Assert.fail("Test " + testname
159 + "\nCouldn't complete Annotation file roundtrip input/output/input test for '"
163 @Test(groups = "Functional")
164 public void testAnnotateAlignmentView()
166 long t1 = System.currentTimeMillis();
168 * JAL-3779 test multiple groups of the same name get annotated
170 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
171 ">Seq1\nQRSIL\n>Seq2\nFTHND\n>Seq3\nRPVSL\n",
172 DataSourceType.PASTE);
173 long t2 = System.currentTimeMillis();
174 System.err.println("t0: " + (t2 - t1));
175 // seq1 and seq3 are in distinct groups both named Group1
176 String annotationFile = "JALVIEW_ANNOTATION\nSEQUENCE_GROUP\tGroup1\t*\t*\t1\n"
177 + "SEQUENCE_GROUP\tGroup2\t*\t*\t2\n"
178 + "SEQUENCE_GROUP\tGroup1\t*\t*\t3\n"
179 + "PROPERTIES\tGroup1\toutlineColour=blue\tidColour=red\n";
180 new AnnotationFile().annotateAlignmentView(af.getViewport(),
181 annotationFile, DataSourceType.PASTE);
183 AlignmentI al = af.getViewport().getAlignment();
184 List<SequenceGroup> groups = al.getGroups();
185 assertEquals(3, groups.size());
186 SequenceGroup sg = groups.get(0);
187 assertEquals("Group1", sg.getName());
188 assertTrue(sg.contains(al.getSequenceAt(0)));
189 assertEquals(Color.BLUE, sg.getOutlineColour());
190 assertEquals(Color.RED, sg.getIdColour());
192 assertEquals("Group2", sg.getName());
193 assertTrue(sg.contains(al.getSequenceAt(1)));
196 * the bug fix: a second group of the same name is also given properties
199 assertEquals("Group1", sg.getName());
200 assertTrue(sg.contains(al.getSequenceAt(2)));
201 assertEquals(Color.BLUE, sg.getOutlineColour());
202 assertEquals(Color.RED, sg.getIdColour());