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