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