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