9c4be2dcfa7b21e2a443a084b86913a55ab4ea77
[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.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import java.awt.Color;
28 import java.io.File;
29 import java.util.Hashtable;
30 import java.util.List;
31
32 import org.testng.Assert;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
35
36 import jalview.datamodel.AlignmentI;
37 import jalview.datamodel.HiddenColumns;
38 import jalview.datamodel.SequenceGroup;
39 import jalview.gui.AlignFrame;
40 import jalview.gui.JvOptionPane;
41 import jalview.io.AnnotationFile.ViewDef;
42
43 public class AnnotationFileIOTest
44 {
45
46   @BeforeClass(alwaysRun = true)
47   public void setUpJvOptionPane()
48   {
49     JvOptionPane.setInteractiveMode(false);
50     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
51   }
52
53   static String TestFiles[][] = {
54       { "Test example annotation import/export", "examples/uniref50.fa",
55           "examples/testdata/example_annot_file.jva" },
56       { "Test multiple combine annotation statements import/export",
57           "examples/uniref50.fa",
58           "examples/testdata/test_combine_annot.jva" },
59       {
60           "Test multiple combine annotation statements with sequence_ref import/export",
61           "examples/uniref50.fa", "examples/testdata/uniref50_iupred.jva" },
62       {
63           "Test group only annotation file parsing results in parser indicating annotation was parsed",
64           "examples/uniref50.fa", "examples/testdata/test_grpannot.jva" },
65       { "Test hiding/showing of insertions on sequence_ref",
66           "examples/uniref50.fa", "examples/testdata/uniref50_seqref.jva" } };
67
68   @Test(groups = { "Functional" })
69   public void exampleAnnotationFileIO() throws Exception
70   {
71     for (String[] testPair : TestFiles)
72     {
73       testAnnotationFileIO(testPair[0], new File(testPair[1]), new File(
74               testPair[2]));
75     }
76   }
77
78   protected AlignmentI readAlignmentFile(File f)
79   {
80     System.out.println("Reading file: " + f);
81     String ff = f.getPath();
82     try
83     {
84       FormatAdapter rf = new FormatAdapter();
85
86       AlignmentI al = rf.readFile(ff, DataSourceType.FILE,
87               new IdentifyFile().identify(ff, DataSourceType.FILE));
88
89       // make sure dataset is initialised ? not sure about this
90       for (int i = 0; i < al.getSequencesArray().length; ++i)
91       {
92         al.getSequenceAt(i).createDatasetSequence();
93       }
94       assertNotNull("Couldn't read supplied alignment data.", al);
95       return al;
96     } catch (Exception e)
97     {
98       e.printStackTrace();
99     }
100     Assert.fail("Couln't read the alignment in file '" + f.toString() + "'");
101     return null;
102   }
103
104   /**
105    * test alignment data in given file can be imported, exported and reimported
106    * with no dataloss
107    * 
108    * @param f
109    *          - source datafile (IdentifyFile.identify() should work with it)
110    * @param ioformat
111    *          - label for IO class used to write and read back in the data from
112    *          f
113    */
114   void testAnnotationFileIO(String testname, File f,
115           File annotFile)
116   {
117     System.out.println("Test: " + testname + "\nReading annotation file '"
118             + annotFile + "' onto : " + f);
119     String af = annotFile.getPath();
120     try
121     {
122       AlignmentI al = readAlignmentFile(f);
123       HiddenColumns cs = new HiddenColumns();
124       assertTrue(
125               "Test "
126                       + testname
127                       + "\nAlignment was not annotated - annotation file not imported.",
128               new AnnotationFile().readAnnotationFile(al, cs, af,
129                       DataSourceType.FILE));
130
131       AnnotationFile aff = new AnnotationFile();
132       // ViewDef is not used by Jalview
133       ViewDef v = aff.new ViewDef(null, al.getHiddenSequences(), cs,
134               new Hashtable());
135       String anfileout = new AnnotationFile().printAnnotations(
136               al.getAlignmentAnnotation(), al.getGroups(),
137               al.getProperties(), null, al, v);
138       assertTrue(
139               "Test "
140                       + testname
141                       + "\nAlignment annotation file was not regenerated. Null string",
142               anfileout != null);
143       assertTrue(
144               "Test "
145                       + testname
146                       + "\nAlignment annotation file was not regenerated. Empty string",
147               anfileout.length() > "JALVIEW_ANNOTATION".length());
148
149       System.out.println("Output annotation file:\n" + anfileout
150               + "\n<<EOF\n");
151
152       AlignmentI al_new = readAlignmentFile(f);
153       assertTrue(
154               "Test "
155                       + testname
156                       + "\nregenerated annotation file did not annotate alignment.",
157               new AnnotationFile().readAnnotationFile(al_new, anfileout,
158                       DataSourceType.PASTE));
159
160       // test for consistency in io
161       StockholmFileTest.testAlignmentEquivalence(al, al_new, false, false,
162               false);
163       return;
164     } catch (Exception e)
165     {
166       e.printStackTrace();
167     }
168     Assert.fail("Test "
169             + testname
170             + "\nCouldn't complete Annotation file roundtrip input/output/input test for '"
171             + annotFile + "'.");
172   }
173   
174   @Test(groups="Functional")
175   public void testAnnotateAlignmentView()
176   {
177     long t1 = System.currentTimeMillis();
178     /*
179      * JAL-3779 test multiple groups of the same name get annotated
180      */
181     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
182             ">Seq1\nQRSIL\n>Seq2\nFTHND\n>Seq3\nRPVSL\n",
183             DataSourceType.PASTE);
184     long t2 = System.currentTimeMillis();
185     System.err.println("t0: " + (t2 - t1));
186     // seq1 and seq3 are in distinct groups both named Group1
187     String annotationFile = "JALVIEW_ANNOTATION\nSEQUENCE_GROUP\tGroup1\t*\t*\t1\n"
188             + "SEQUENCE_GROUP\tGroup2\t*\t*\t2\n"
189             + "SEQUENCE_GROUP\tGroup1\t*\t*\t3\n"
190             + "PROPERTIES\tGroup1\toutlineColour=blue\tidColour=red\n";
191     new AnnotationFile().annotateAlignmentView(af.getViewport(), annotationFile, DataSourceType.PASTE);
192     
193     AlignmentI al = af.getViewport().getAlignment();
194     List<SequenceGroup> groups = al.getGroups();
195     assertEquals(3, groups.size());
196     SequenceGroup sg = groups.get(0);
197     assertEquals("Group1", sg.getName());
198     assertTrue(sg.contains(al.getSequenceAt(0)));
199     assertEquals(Color.BLUE, sg.getOutlineColour());
200     assertEquals(Color.RED, sg.getIdColour());
201     sg = groups.get(1);
202     assertEquals("Group2", sg.getName());
203     assertTrue(sg.contains(al.getSequenceAt(1)));
204     
205     /*
206      * the bug fix: a second group of the same name is also given properties
207      */
208     sg = groups.get(2);
209     assertEquals("Group1", sg.getName());
210     assertTrue(sg.contains(al.getSequenceAt(2)));
211     assertEquals(Color.BLUE, sg.getOutlineColour());
212     assertEquals(Color.RED, sg.getIdColour());
213   }
214 }