JAL-1478 test to demonstrate bug
[jalview.git] / test / jalview / io / AnnotationFileIOTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.io;
20
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import jalview.datamodel.AlignmentI;
25
26 import java.io.File;
27
28 import org.junit.Test;
29
30 public class AnnotationFileIOTest
31 {
32
33   static String TestFiles[][] =
34   {
35       { "Test example annotation import/export", "examples/uniref50.fa",
36           "examples/testdata/example_annot_file.jva" },
37       { "Test multiple combine annotation statements import/export",
38           "examples/uniref50.fa",
39           "examples/testdata/test_combine_annot.jva" },
40       {
41           "Test multiple combine annotation statements with sequence_ref import/export",
42           "examples/uniref50.fa", "examples/testdata/uniref50_iupred.jva" },
43       {
44           "Test group only annotation file parsing results in parser indicating annotation was parsed",
45           "examples/uniref50.fa", "examples/testdata/test_grpannot.jva" } };
46
47   @Test
48   public void exampleAnnotationFileIO() throws Exception
49   {
50     for (String[] testPair:TestFiles)
51     {
52       testAnnotationFileIO(testPair[0], new File(testPair[1]), new File(testPair[2]));
53     }
54   }
55
56   public static AlignmentI readAlignmentFile(File f)
57   {
58     System.out.println("Reading file: " + f);
59     String ff = f.getPath();
60     try
61     {
62       FormatAdapter rf = new FormatAdapter();
63
64       AlignmentI al = rf.readFile(ff, AppletFormatAdapter.FILE,
65               new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
66       
67       // make sure dataset is initialised ? not sure about this
68       for (int i = 0; i < al.getSequencesArray().length; ++i)
69       {
70         al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i).createDatasetSequence());
71       }
72       assertNotNull("Couldn't read supplied alignment data.", al);
73       return al;
74     } catch (Exception e)
75     {
76       e.printStackTrace();
77     }
78     fail("Couln't read the alignment in file '"+f.toString()+"'");
79     return null;
80   }
81   /**
82    * test alignment data in given file can be imported, exported and reimported
83    * with no dataloss
84    * 
85    * @param f
86    *          - source datafile (IdentifyFile.identify() should work with it)
87    * @param ioformat
88    *          - label for IO class used to write and read back in the data from
89    *          f
90    */
91   public static void testAnnotationFileIO(String testname, File f, File annotFile)
92   {
93     System.out.println("Test: "+testname+"\nReading annotation file '"+annotFile+"' onto : " + f);
94     String af = annotFile.getPath();
95     try
96     {
97       AlignmentI al = readAlignmentFile(f);
98       
99       assertTrue("Test "+testname+"\nAlignment was not annotated - annotation file not imported.",new AnnotationFile().readAnnotationFile(al, af, FormatAdapter.FILE));
100       
101       String anfileout = new AnnotationFile().printAnnotations(al.getAlignmentAnnotation(), al.getGroups(), al.getProperties());
102       assertTrue("Test "+testname+"\nAlignment annotation file was not regenerated. Null string",anfileout!=null);
103       assertTrue("Test "+testname+"\nAlignment annotation file was not regenerated. Empty string",anfileout.length()>"JALVIEW_ANNOTATION".length());
104
105       System.out.println("Output annotation file:\n"+anfileout+"\n<<EOF\n");
106       
107       AlignmentI al_new = readAlignmentFile(f);
108       assertTrue("Test "+testname+"\nregenerated annotation file did not annotate alignment.",new AnnotationFile().readAnnotationFile(al_new, anfileout, FormatAdapter.PASTE));
109       
110       // test for consistency in io
111       StockholmFileTest.testAlignmentEquivalence(al, al_new);
112       return;
113     } catch (Exception e)
114     {
115       e.printStackTrace();
116     }
117     fail("Test "+testname+"\nCouldn't complete Annotation file roundtrip input/output/input test for '"+annotFile+"'.");
118   }
119 }