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