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