JAL-1270 JUnit to TestNG refactoring
[jalview.git] / test / jalview / io / Jalview2xmlTests.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.assertTrue;
24
25 import java.io.File;
26
27 import org.testng.Assert;
28 import org.testng.AssertJUnit;
29 import org.testng.annotations.AfterClass;
30 import org.testng.annotations.BeforeClass;
31 import org.testng.annotations.Test;
32
33 import jalview.api.AlignmentViewPanel;
34 import jalview.api.ViewStyleI;
35 import jalview.bin.Cache;
36 import jalview.datamodel.AlignmentAnnotation;
37 import jalview.datamodel.SequenceGroup;
38 import jalview.datamodel.SequenceI;
39 import jalview.gui.AlignFrame;
40 import jalview.gui.Desktop;
41 import jalview.schemes.AnnotationColourGradient;
42 import jalview.schemes.ColourSchemeI;
43
44 public class Jalview2xmlTests
45 {
46
47   /**
48    * @throws java.lang.Exception
49    */
50   @BeforeClass
51   public static void setUpBeforeClass() throws Exception
52   {
53     jalview.bin.Jalview.main(new String[]
54     { "-props", "test/jalview/io/testProps.jvprops" });
55   }
56
57   /**
58    * @throws java.lang.Exception
59    */
60   @AfterClass
61   public static void tearDownAfterClass() throws Exception
62   {
63     jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
64
65   }
66
67   public int countDsAnn(jalview.viewmodel.AlignmentViewport avp)
68   {
69     int numdsann = 0;
70     for (SequenceI sq : avp.getAlignment().getDataset().getSequences())
71     {
72       if (sq.getAnnotation() != null)
73       {
74         for (AlignmentAnnotation dssa : sq.getAnnotation())
75         {
76           if (dssa.isValidStruc())
77           {
78             numdsann++;
79           }
80         }
81       }
82     }
83     return numdsann;
84   }
85
86   @Test
87   public void testRNAStructureRecovery() throws Exception
88   {
89     String inFile = "examples/RF00031_folded.stk";
90     String tfile = File.createTempFile("JalviewTest", ".jvp")
91             .getAbsolutePath();
92     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
93             inFile, FormatAdapter.FILE);
94     assertTrue("Didn't read input file " + inFile, af != null);
95     int olddsann = countDsAnn(af.getViewport());
96     assertTrue("Didn't find any dataset annotations", olddsann > 0);
97     af.rnahelicesColour_actionPerformed(null);
98     assertTrue(
99             "Couldn't apply RNA helices colourscheme",
100             af.getViewport().getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour);
101     assertTrue("Failed to store as a project.",
102             af.saveAlignment(tfile, "Jalview"));
103     af.closeMenuItem_actionPerformed(true);
104     af = null;
105     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
106             FormatAdapter.FILE);
107     assertTrue("Failed to import new project", af != null);
108     int newdsann = countDsAnn(af.getViewport());
109     assertTrue(
110             "Differing numbers of dataset sequence annotation\nOriginally "
111                     + olddsann + " and now " + newdsann,
112             olddsann == newdsann);
113     System.out
114             .println("Read in same number of annotations as originally present ("
115                     + olddsann + ")");
116     assertTrue(
117             "RNA helices colourscheme was not applied on import.",
118             af.getViewport().getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour);
119   }
120
121   @Test
122   public void testTCoffeeScores() throws Exception
123   {
124     String inFile = "examples/uniref50.fa", inAnnot = "examples/uniref50.score_ascii";
125     String tfile = File.createTempFile("JalviewTest", ".jvp")
126             .getAbsolutePath();
127     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
128             inFile, FormatAdapter.FILE);
129     assertTrue("Didn't read input file " + inFile, af != null);
130     af.loadJalviewDataFile(inAnnot, FormatAdapter.FILE, null, null);
131     assertTrue(
132             "Didn't set T-coffee colourscheme",
133             af.getViewport().getGlobalColourScheme().getClass()
134                     .equals(jalview.schemes.TCoffeeColourScheme.class));
135     assertTrue(
136             "Recognise T-Coffee score from string",
137             jalview.schemes.ColourSchemeProperty.getColour(af.getViewport()
138                     .getAlignment(),
139                     jalview.schemes.ColourSchemeProperty.getColourName(af
140                             .getViewport().getGlobalColourScheme())) != null);
141
142     assertTrue("Failed to store as a project.",
143             af.saveAlignment(tfile, "Jalview"));
144     af.closeMenuItem_actionPerformed(true);
145     af = null;
146     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
147             FormatAdapter.FILE);
148     assertTrue("Failed to import new project", af != null);
149     assertTrue(
150             "Didn't set T-coffee colourscheme for imported project.",
151             af.getViewport().getGlobalColourScheme().getClass()
152                     .equals(jalview.schemes.TCoffeeColourScheme.class));
153     System.out
154             .println("T-Coffee score shading successfully recovered from project.");
155   }
156
157   @Test
158   public void testColourByAnnotScores() throws Exception
159   {
160     String inFile = "examples/uniref50.fa", inAnnot = "examples/testdata/uniref50_iupred.jva";
161     String tfile = File.createTempFile("JalviewTest", ".jvp")
162             .getAbsolutePath();
163     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
164             inFile, FormatAdapter.FILE);
165     assertTrue("Didn't read input file " + inFile, af != null);
166     af.loadJalviewDataFile(inAnnot, FormatAdapter.FILE, null, null);
167     AlignmentAnnotation[] aa = af.getViewport().getAlignment()
168             .getSequenceAt(0).getAnnotation("IUPredWS (Short)");
169     assertTrue(
170             "Didn't find any IUPred annotation to use to shade alignment.",
171             aa != null && aa.length > 0);
172     AnnotationColourGradient cs = new jalview.schemes.AnnotationColourGradient(
173             aa[0], null, AnnotationColourGradient.ABOVE_THRESHOLD);
174     AnnotationColourGradient gcs = new jalview.schemes.AnnotationColourGradient(
175             aa[0], null, AnnotationColourGradient.BELOW_THRESHOLD);
176     cs.setSeqAssociated(true);
177     gcs.setSeqAssociated(true);
178     af.changeColour(cs);
179     SequenceGroup sg = new SequenceGroup();
180     sg.setStartRes(57);
181     sg.setEndRes(92);
182     sg.cs = gcs;
183     af.getViewport().getAlignment().addGroup(sg);
184     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(1), false);
185     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(2), true);
186     af.alignPanel.alignmentChanged();
187     assertTrue("Failed to store as a project.",
188             af.saveAlignment(tfile, "Jalview"));
189     af.closeMenuItem_actionPerformed(true);
190     af = null;
191     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
192             FormatAdapter.FILE);
193     assertTrue("Failed to import new project", af != null);
194
195     // check for group and alignment colourschemes
196
197     ColourSchemeI _rcs = af.getViewport().getGlobalColourScheme();
198     ColourSchemeI _rgcs = af.getViewport().getAlignment().getGroups()
199             .get(0).cs;
200     assertTrue("Didn't recover global colourscheme", _rcs != null);
201     assertTrue("Didn't recover annotation colour global scheme",
202             _rcs instanceof AnnotationColourGradient);
203     AnnotationColourGradient __rcs = (AnnotationColourGradient) _rcs;
204     assertTrue("Annotation colourscheme wasn't sequence associated",
205             __rcs.isSeqAssociated());
206
207     boolean diffseqcols = false, diffgseqcols = false;
208     SequenceI[] sqs = af.getViewport().getAlignment().getSequencesArray();
209     for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
210             && (!diffseqcols || !diffgseqcols); p++)
211     {
212       if (_rcs.findColour(sqs[0].getCharAt(p), p, sqs[0]) != _rcs
213               .findColour(sqs[5].getCharAt(p), p, sqs[5]))
214       {
215         diffseqcols = true;
216       }
217     }
218     assertTrue("Got Different sequence colours", diffseqcols);
219     System.out
220             .println("Per sequence colourscheme (Background) successfully applied and recovered.");
221
222     assertTrue("Didn't recover group colourscheme", _rgcs != null);
223     assertTrue("Didn't recover annotation colour group colourscheme",
224             _rgcs instanceof AnnotationColourGradient);
225     __rcs = (AnnotationColourGradient) _rgcs;
226     assertTrue("Group Annotation colourscheme wasn't sequence associated",
227             __rcs.isSeqAssociated());
228
229     for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
230             && (!diffseqcols || !diffgseqcols); p++)
231     {
232       if (_rgcs.findColour(sqs[1].getCharAt(p), p, sqs[1]) != _rgcs
233               .findColour(sqs[2].getCharAt(p), p, sqs[2]))
234       {
235         diffgseqcols = true;
236       }
237     }
238     assertTrue("Got Different group sequence colours", diffgseqcols);
239     System.out
240             .println("Per sequence (Group) colourscheme successfully applied and recovered.");
241   }
242
243   @Test
244   public void gatherViewsHere() throws Exception
245   {
246     int origCount = Desktop.getAlignFrames() == null ? 0 : Desktop
247             .getAlignFrames().length;
248     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
249             "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
250     assertTrue("Didn't read in the example file correctly.", af != null);
251     assertTrue("Didn't gather the views in the example file.",
252             Desktop.getAlignFrames().length == 1 + origCount);
253
254   }
255
256   @Test
257   public void viewRefPdbAnnotation() throws Exception
258   {
259     Cache.applicationProperties.setProperty("STRUCT_FROM_PDB",
260             Boolean.TRUE.toString());
261     Cache.applicationProperties.setProperty("ADD_SS_ANN",
262             Boolean.TRUE.toString());
263     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
264             "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
265     assertTrue("Didn't read in the example file correctly.", af != null);
266     AlignmentViewPanel sps = null;
267     for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
268     {
269       if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
270       {
271         sps = ap;
272         break;
273       }
274     }
275     assertTrue("Couldn't find the structure view", sps != null);
276     SequenceI sq = sps.getAlignment().findName("1A70|");
277     AlignmentAnnotation refan = null;
278     for (AlignmentAnnotation ra:sps.getAlignment().getAlignmentAnnotation())
279     {
280       if (ra.graph != 0)
281       {
282         refan = ra;
283         break;
284       }
285     }
286     assertTrue("Annotation secondary structure not found.",refan!=null);
287     assertTrue("Couldn't find 1a70 null chain", sq != null);
288     // compare the manually added temperature factor annotation
289     // to the track automatically transferred from the pdb structure on load
290     for (AlignmentAnnotation ala : sq.getDatasetSequence().getAnnotation())
291     {
292       AlignmentAnnotation alaa;
293       sq.addAlignmentAnnotation(alaa = new AlignmentAnnotation(ala));
294       alaa.adjustForAlignment();
295       if (ala.graph == refan.graph)
296       {
297         for (int p = 0; p < ala.annotations.length; p++)
298         {
299           sq.findPosition(p);
300           try {
301             assertTrue(
302                     "Mismatch at alignment position " + p,
303                   (alaa.annotations[p] == null && refan.annotations[p] == null)
304                             || alaa.annotations[p].value == refan.annotations[p].value);
305           }
306           catch (NullPointerException q)
307           {
308             Assert.fail("Mismatch of alignment annotations at position " + p
309                     + " Ref seq ann: " + refan.annotations[p]
310                     + " alignment " + alaa.annotations[p]);
311           }
312           }
313       }
314     }
315     
316   }
317
318   @Test
319   public void testCopyViewSettings() throws Exception
320   {
321     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
322             "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
323     assertTrue("Didn't read in the example file correctly.", af != null);
324     AlignmentViewPanel sps = null, groups = null;
325     for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
326     {
327       if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
328       {
329         sps = ap;
330       }
331       if (ap.getViewName().contains("MAFFT"))
332       {
333         groups = ap;
334       }
335     }
336     assertTrue("Couldn't find the structure view", sps != null);
337     assertTrue("Couldn't find the MAFFT view", groups != null);
338
339     ViewStyleI structureStyle = sps.getAlignViewport().getViewStyle();
340     ViewStyleI groupStyle = groups.getAlignViewport().getViewStyle();
341     AssertJUnit.assertFalse(structureStyle.sameStyle(groupStyle));
342
343     groups.getAlignViewport().setViewStyle(structureStyle);
344     AssertJUnit.assertFalse(groupStyle.sameStyle(groups.getAlignViewport()
345             .getViewStyle()));
346     Assert.assertTrue(structureStyle.sameStyle(groups.getAlignViewport()
347             .getViewStyle()));
348
349   }
350 }