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