JAL-674 fix test properties path and add properties file
[jalview.git] / test / jalview / io / Jalview2xmlTests.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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.junit.Assert.*;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.gui.AlignFrame;
28 import jalview.gui.Desktop;
29 import jalview.schemes.AnnotationColourGradient;
30 import jalview.schemes.ColourSchemeI;
31
32 import java.io.File;
33
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37
38 public class Jalview2xmlTests
39 {
40
41   /**
42    * @throws java.lang.Exception
43    */
44   @BeforeClass
45   public static void setUpBeforeClass() throws Exception
46   {
47     jalview.bin.Jalview.main(new String[]
48     { "-props", "test/jalview/io/testProps.jvprops" });
49   }
50
51   /**
52    * @throws java.lang.Exception
53    */
54   @AfterClass
55   public static void tearDownAfterClass() throws Exception
56   {
57     jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
58
59   }
60
61   public int countDsAnn(jalview.viewmodel.AlignmentViewport avp)
62   {
63     int numdsann = 0;
64     for (SequenceI sq : avp.getAlignment().getDataset().getSequences())
65     {
66       if (sq.getAnnotation() != null)
67       {
68         for (AlignmentAnnotation dssa : sq.getAnnotation())
69         {
70           if (dssa.isValidStruc())
71           {
72             numdsann++;
73           }
74         }
75       }
76     }
77     return numdsann;
78   }
79
80   @Test
81   public void testRNAStructureRecovery() throws Exception
82   {
83     String inFile = "examples/RF00031_folded.stk";
84     String tfile = File.createTempFile("JalviewTest", ".jvp")
85             .getAbsolutePath();
86     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
87             inFile, FormatAdapter.FILE);
88     assertTrue("Didn't read input file " + inFile, af != null);
89     int olddsann = countDsAnn(af.getViewport());
90     assertTrue("Didn't find any dataset annotations", olddsann > 0);
91     af.rnahelicesColour_actionPerformed(null);
92     assertTrue(
93             "Couldn't apply RNA helices colourscheme",
94             af.getViewport().getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour);
95     assertTrue("Failed to store as a project.",
96             af.saveAlignment(tfile, "Jalview"));
97     af.closeMenuItem_actionPerformed(true);
98     af = null;
99     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
100             FormatAdapter.FILE);
101     assertTrue("Failed to import new project", af != null);
102     int newdsann = countDsAnn(af.getViewport());
103     assertTrue(
104             "Differing numbers of dataset sequence annotation\nOriginally "
105                     + olddsann + " and now " + newdsann,
106             olddsann == newdsann);
107     System.out
108             .println("Read in same number of annotations as originally present ("
109                     + olddsann + ")");
110     assertTrue(
111             "RNA helices colourscheme was not applied on import.",
112             af.getViewport().getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour);
113   }
114
115   @Test
116   public void testTCoffeeScores() throws Exception
117   {
118     String inFile = "examples/uniref50.fa", inAnnot = "examples/uniref50.score_ascii";
119     String tfile = File.createTempFile("JalviewTest", ".jvp")
120             .getAbsolutePath();
121     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
122             inFile, FormatAdapter.FILE);
123     assertTrue("Didn't read input file " + inFile, af != null);
124     af.loadJalviewDataFile(inAnnot, FormatAdapter.FILE, null, null);
125     assertTrue(
126             "Didn't set T-coffee colourscheme",
127             af.getViewport().getGlobalColourScheme().getClass()
128                     .equals(jalview.schemes.TCoffeeColourScheme.class));
129     assertTrue(
130             "Recognise T-Coffee score from string",
131             jalview.schemes.ColourSchemeProperty.getColour(af.getViewport()
132                     .getAlignment(),
133                     jalview.schemes.ColourSchemeProperty.getColourName(af
134                             .getViewport().getGlobalColourScheme())) != null);
135
136     assertTrue("Failed to store as a project.",
137             af.saveAlignment(tfile, "Jalview"));
138     af.closeMenuItem_actionPerformed(true);
139     af = null;
140     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
141             FormatAdapter.FILE);
142     assertTrue("Failed to import new project", af != null);
143     assertTrue(
144             "Didn't set T-coffee colourscheme for imported project.",
145             af.getViewport().getGlobalColourScheme().getClass()
146                     .equals(jalview.schemes.TCoffeeColourScheme.class));
147     System.out
148             .println("T-Coffee score shading successfully recovered from project.");
149   }
150
151   @Test
152   public void testColourByAnnotScores() throws Exception
153   {
154     String inFile = "examples/uniref50.fa", inAnnot = "examples/testdata/uniref50_iupred.jva";
155     String tfile = File.createTempFile("JalviewTest", ".jvp")
156             .getAbsolutePath();
157     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
158             inFile, FormatAdapter.FILE);
159     assertTrue("Didn't read input file " + inFile, af != null);
160     af.loadJalviewDataFile(inAnnot, FormatAdapter.FILE, null, null);
161     AlignmentAnnotation[] aa = af.getViewport().getAlignment()
162             .getSequenceAt(0).getAnnotation("IUPredWS (Short)");
163     assertTrue(
164             "Didn't find any IUPred annotation to use to shade alignment.",
165             aa != null && aa.length > 0);
166     AnnotationColourGradient cs = new jalview.schemes.AnnotationColourGradient(
167             aa[0], null, AnnotationColourGradient.ABOVE_THRESHOLD);
168     AnnotationColourGradient gcs = new jalview.schemes.AnnotationColourGradient(
169             aa[0], null, AnnotationColourGradient.BELOW_THRESHOLD);
170     cs.setSeqAssociated(true);
171     gcs.setSeqAssociated(true);
172     af.changeColour(cs);
173     SequenceGroup sg = new SequenceGroup();
174     sg.setStartRes(57);
175     sg.setEndRes(92);
176     sg.cs = gcs;
177     af.getViewport().getAlignment().addGroup(sg);
178     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(1), false);
179     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(2), true);
180     af.alignPanel.alignmentChanged();
181     assertTrue("Failed to store as a project.",
182             af.saveAlignment(tfile, "Jalview"));
183     af.closeMenuItem_actionPerformed(true);
184     af = null;
185     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
186             FormatAdapter.FILE);
187     assertTrue("Failed to import new project", af != null);
188
189     // check for group and alignment colourschemes
190
191     ColourSchemeI _rcs = af.getViewport().getGlobalColourScheme();
192     ColourSchemeI _rgcs = af.getViewport().getAlignment().getGroups()
193             .get(0).cs;
194     assertTrue("Didn't recover global colourscheme", _rcs != null);
195     assertTrue("Didn't recover annotation colour global scheme",
196             _rcs instanceof AnnotationColourGradient);
197     AnnotationColourGradient __rcs = (AnnotationColourGradient) _rcs;
198     assertTrue("Annotation colourscheme wasn't sequence associated",
199             __rcs.isSeqAssociated());
200
201     boolean diffseqcols = false, diffgseqcols = false;
202     SequenceI[] sqs = af.getViewport().getAlignment().getSequencesArray();
203     for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
204             && (!diffseqcols || !diffgseqcols); p++)
205     {
206       if (_rcs.findColour(sqs[0].getCharAt(p), p, sqs[0]) != _rcs
207               .findColour(sqs[5].getCharAt(p), p, sqs[5]))
208       {
209         diffseqcols = true;
210       }
211     }
212     assertTrue("Got Different sequence colours", diffseqcols);
213     System.out
214             .println("Per sequence colourscheme (Background) successfully applied and recovered.");
215
216     assertTrue("Didn't recover group colourscheme", _rgcs != null);
217     assertTrue("Didn't recover annotation colour group colourscheme",
218             _rgcs instanceof AnnotationColourGradient);
219     __rcs = (AnnotationColourGradient) _rgcs;
220     assertTrue("Group Annotation colourscheme wasn't sequence associated",
221             __rcs.isSeqAssociated());
222
223     for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
224             && (!diffseqcols || !diffgseqcols); p++)
225     {
226       if (_rgcs.findColour(sqs[1].getCharAt(p), p, sqs[1]) != _rgcs
227               .findColour(sqs[2].getCharAt(p), p, sqs[2]))
228       {
229         diffgseqcols = true;
230       }
231     }
232     assertTrue("Got Different group sequence colours", diffgseqcols);
233     System.out
234             .println("Per sequence (Group) colourscheme successfully applied and recovered.");
235   }
236
237   @Test
238   public void gatherViewsHere() throws Exception
239   {
240     int origCount = Desktop.getAlignframes().length;
241     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
242             "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
243     assertTrue("Didn't read in the example file correctly.", af != null);
244     assertTrue("Didn't gather the views in the example file.",
245             Desktop.getAlignframes().length == 1 + origCount);
246
247   }
248 }