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