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