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