30f61d7572f83ab945b5c5b677a34723bcf168cb
[jalview.git] / test / jalview / io / AnnotationFileIOTest.java
1 package jalview.io;
2
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
5 import static org.junit.Assert.fail;
6 import jalview.datamodel.AlignmentI;
7
8 import java.io.File;
9
10 import org.junit.Test;
11
12 public class AnnotationFileIOTest
13 {
14
15   static String TestFiles[][] = {{ "Test example annotation import/export","examples/uniref50.fa", "examples/testdata/example_annot_file.jva" }
16           ,{"Test multiple combine annotation statements import/export","examples/uniref50.fa", "examples/testdata/test_combine_annot.jva"}
17           ,{"Test multiple combine annotation statements with sequence_ref import/export","examples/uniref50.fa", "examples/testdata/uniref50_iupred.jva"}};
18
19   @Test
20   public void exampleAnnotationFileIO() throws Exception
21   {
22     for (String[] testPair:TestFiles)
23     {
24       testAnnotationFileIO(testPair[0], new File(testPair[1]), new File(testPair[2]));
25     }
26   }
27
28   public static AlignmentI readAlignmentFile(File f)
29   {
30     System.out.println("Reading file: " + f);
31     String ff = f.getPath();
32     try
33     {
34       FormatAdapter rf = new FormatAdapter();
35
36       AlignmentI al = rf.readFile(ff, AppletFormatAdapter.FILE,
37               new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
38       
39       // make sure dataset is initialised ? not sure about this
40       for (int i = 0; i < al.getSequencesArray().length; ++i)
41       {
42         al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i).createDatasetSequence());
43       }
44       assertNotNull("Couldn't read supplied alignment data.", al);
45       return al;
46     } catch (Exception e)
47     {
48       e.printStackTrace();
49     }
50     fail("Couln't read the alignment in file '"+f.toString()+"'");
51     return null;
52   }
53   /**
54    * test alignment data in given file can be imported, exported and reimported
55    * with no dataloss
56    * 
57    * @param f
58    *          - source datafile (IdentifyFile.identify() should work with it)
59    * @param ioformat
60    *          - label for IO class used to write and read back in the data from
61    *          f
62    */
63   public static void testAnnotationFileIO(String testname, File f, File annotFile)
64   {
65     System.out.println("Test: "+testname+"\nReading annotation file '"+annotFile+"' onto : " + f);
66     String af = annotFile.getPath();
67     try
68     {
69       AlignmentI al = readAlignmentFile(f);
70       
71       assertTrue("Test "+testname+"\nAlignment was not annotated - annotation file not imported.",new AnnotationFile().readAnnotationFile(al, af, FormatAdapter.FILE));
72       
73       String anfileout = new AnnotationFile().printAnnotations(al.getAlignmentAnnotation(), al.getGroups(), al.getProperties());
74       assertTrue("Test "+testname+"\nAlignment annotation file was not regenerated. Null string",anfileout!=null);
75       assertTrue("Test "+testname+"\nAlignment annotation file was not regenerated. Empty string",anfileout.length()>"JALVIEW_ANNOTATION".length());
76
77       System.out.println("Output annotation file:\n"+anfileout+"\n<<EOF\n");
78       
79       AlignmentI al_new = readAlignmentFile(f);
80       assertTrue("Test "+testname+"\nregenerated annotation file did not annotate alignment.",new AnnotationFile().readAnnotationFile(al_new, anfileout, FormatAdapter.PASTE));
81       
82       // test for consistency in io
83       StockholmFileTest.testAlignmentEquivalence(al, al_new);
84       return;
85     } catch (Exception e)
86     {
87       e.printStackTrace();
88     }
89     fail("Test "+testname+"\nCouldn't complete Annotation file roundtrip input/output/input test for '"+annotFile+"'.");
90   }
91 }