JAL-2326 added setup method for JvOptionPane in all Jalveiw test classes to enable...
[jalview.git] / test / jalview / io / AnnotationFileIOTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.io;
22
23 import static org.testng.AssertJUnit.assertNotNull;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.gui.JvOptionPane;
29 import jalview.io.AnnotationFile.ViewDef;
30
31 import java.io.File;
32 import java.util.Hashtable;
33
34 import org.testng.Assert;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.Test;
37
38 public class AnnotationFileIOTest
39 {
40
41   @BeforeClass(alwaysRun = true)
42   public void setUpJvOptionPane()
43   {
44     JvOptionPane.setInteractiveMode(false);
45     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
46   }
47
48   static String TestFiles[][] = {
49       { "Test example annotation import/export", "examples/uniref50.fa",
50           "examples/testdata/example_annot_file.jva" },
51       { "Test multiple combine annotation statements import/export",
52           "examples/uniref50.fa",
53           "examples/testdata/test_combine_annot.jva" },
54       {
55           "Test multiple combine annotation statements with sequence_ref import/export",
56           "examples/uniref50.fa", "examples/testdata/uniref50_iupred.jva" },
57       {
58           "Test group only annotation file parsing results in parser indicating annotation was parsed",
59           "examples/uniref50.fa", "examples/testdata/test_grpannot.jva" },
60       { "Test hiding/showing of insertions on sequence_ref",
61           "examples/uniref50.fa", "examples/testdata/uniref50_seqref.jva" } };
62
63   @Test(groups = { "Functional" })
64   public void exampleAnnotationFileIO() throws Exception
65   {
66     for (String[] testPair : TestFiles)
67     {
68       testAnnotationFileIO(testPair[0], new File(testPair[1]), new File(
69               testPair[2]));
70     }
71   }
72
73   public static AlignmentI readAlignmentFile(File f)
74   {
75     System.out.println("Reading file: " + f);
76     String ff = f.getPath();
77     try
78     {
79       FormatAdapter rf = new FormatAdapter();
80
81       AlignmentI al = rf.readFile(ff, AppletFormatAdapter.FILE,
82               new IdentifyFile().identify(ff, AppletFormatAdapter.FILE));
83
84       // make sure dataset is initialised ? not sure about this
85       for (int i = 0; i < al.getSequencesArray().length; ++i)
86       {
87         al.getSequenceAt(i).createDatasetSequence();
88       }
89       assertNotNull("Couldn't read supplied alignment data.", al);
90       return al;
91     } catch (Exception e)
92     {
93       e.printStackTrace();
94     }
95     Assert.fail("Couln't read the alignment in file '" + f.toString() + "'");
96     return null;
97   }
98
99   /**
100    * test alignment data in given file can be imported, exported and reimported
101    * with no dataloss
102    * 
103    * @param f
104    *          - source datafile (IdentifyFile.identify() should work with it)
105    * @param ioformat
106    *          - label for IO class used to write and read back in the data from
107    *          f
108    */
109
110   // @Test(groups ={ "Functional" })
111   public static void testAnnotationFileIO(String testname, File f,
112           File annotFile)
113   {
114     System.out.println("Test: " + testname + "\nReading annotation file '"
115             + annotFile + "' onto : " + f);
116     String af = annotFile.getPath();
117     try
118     {
119       AlignmentI al = readAlignmentFile(f);
120       ColumnSelection cs = new ColumnSelection();
121       assertTrue(
122               "Test "
123                       + testname
124                       + "\nAlignment was not annotated - annotation file not imported.",
125               new AnnotationFile().readAnnotationFile(al, cs, af,
126                       FormatAdapter.FILE));
127
128       AnnotationFile aff = new AnnotationFile();
129       ViewDef v = aff.new ViewDef(null, al.getHiddenSequences(), cs,
130               new Hashtable());
131       String anfileout = new AnnotationFile().printAnnotations(
132               al.getAlignmentAnnotation(), al.getGroups(),
133               al.getProperties(), null, al, v);
134       assertTrue(
135               "Test "
136                       + testname
137                       + "\nAlignment annotation file was not regenerated. Null string",
138               anfileout != null);
139       assertTrue(
140               "Test "
141                       + testname
142                       + "\nAlignment annotation file was not regenerated. Empty string",
143               anfileout.length() > "JALVIEW_ANNOTATION".length());
144
145       System.out.println("Output annotation file:\n" + anfileout
146               + "\n<<EOF\n");
147
148       AlignmentI al_new = readAlignmentFile(f);
149       assertTrue(
150               "Test "
151                       + testname
152                       + "\nregenerated annotation file did not annotate alignment.",
153               new AnnotationFile().readAnnotationFile(al_new, anfileout,
154                       FormatAdapter.PASTE));
155
156       // test for consistency in io
157       StockholmFileTest.testAlignmentEquivalence(al, al_new, false);
158       return;
159     } catch (Exception e)
160     {
161       e.printStackTrace();
162     }
163     Assert.fail("Test "
164             + testname
165             + "\nCouldn't complete Annotation file roundtrip input/output/input test for '"
166             + annotFile + "'.");
167   }
168 }