JAL-1628 drop getShowAnnotation accessor in favour of isShowAnnotation in AlignViewpo...
[jalview.git] / test / jalview / ws / jabaws / RNAStructExportImport.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.ws.jabaws;
22
23 import static org.junit.Assert.*;
24
25 import java.awt.Component;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.Vector;
30
31 import javax.swing.JMenu;
32 import javax.swing.JMenuItem;
33
34 import jalview.api.AlignCalcManagerI;
35 import jalview.datamodel.AlignmentAnnotation;
36 import jalview.datamodel.AlignmentI;
37 import jalview.datamodel.Annotation;
38 import jalview.gui.Jalview2XML;
39 import jalview.io.AnnotationFile;
40 import jalview.io.FormatAdapter;
41 import jalview.io.StockholmFileTest;
42 import jalview.ws.jws2.AADisorderClient;
43 import jalview.ws.jws2.Jws2Discoverer;
44 import jalview.ws.jws2.RNAalifoldClient;
45 import jalview.ws.jws2.SequenceAnnotationWSClient;
46 import jalview.ws.jws2.dm.JabaOption;
47 import jalview.ws.jws2.jabaws2.Jws2Instance;
48 import jalview.ws.params.AutoCalcSetting;
49
50 import org.junit.AfterClass;
51 import org.junit.BeforeClass;
52 import org.junit.Test;
53
54 import compbio.metadata.WrongParameterException;
55
56 public class RNAStructExportImport
57 {
58   public static String testseqs = "examples/unfolded_RF00031.aln";
59
60   public static Jws2Discoverer disc;
61
62   public static Jws2Instance rnaalifoldws;
63
64   jalview.ws.jws2.RNAalifoldClient alifoldClient;
65
66   public static jalview.gui.AlignFrame af = null;
67
68   @BeforeClass
69   public static void setUpBeforeClass() throws Exception
70   {
71
72     jalview.bin.Cache.initLogger();
73     disc = JalviewJabawsTestUtils.getJabawsDiscoverer(false);
74
75     for (Jws2Instance svc : disc.getServices())
76     {
77
78       if (svc.getServiceTypeURI().toLowerCase().contains("rnaalifoldws"))
79       {
80         rnaalifoldws = svc;
81       }
82     }
83
84     System.out.println("State of rnaalifoldws: " + rnaalifoldws);
85
86     if (rnaalifoldws == null)
87       System.exit(0);
88
89     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
90
91     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.FormatAdapter.FILE);
92
93     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
94
95   }
96
97   @AfterClass
98   public static void tearDownAfterClass() throws Exception
99   {
100     if (af != null)
101     {
102       af.setVisible(false);
103       af.dispose();
104     }
105   }
106
107   @Test
108   public void testRNAStructExport()
109   {
110
111     alifoldClient = new RNAalifoldClient(rnaalifoldws, af, null, null);
112
113     af.getViewport().getCalcManager().startWorker(alifoldClient);
114
115     do
116     {
117       try
118       {
119         Thread.sleep(50);
120       } catch (InterruptedException x)
121       {
122       }
123       ;
124     } while (af.getViewport().getCalcManager().isWorking());
125
126     AlignmentI orig_alig = af.getViewport().getAlignment();
127
128     testAnnotationFileIO("Testing RNAalifold Annotation IO", orig_alig);
129
130   }
131
132   public static void testAnnotationFileIO(String testname, AlignmentI al)
133   {
134     try
135     {
136       // what format would be appropriate for RNAalifold annotations?
137       String aligfileout = new FormatAdapter().formatSequences("PFAM",
138               al.getSequencesArray());
139
140       String anfileout = new AnnotationFile()
141               .printAnnotationsForAlignment(al);
142       assertTrue(
143               "Test "
144                       + testname
145                       + "\nAlignment annotation file was not regenerated. Null string",
146               anfileout != null);
147       assertTrue(
148               "Test "
149                       + testname
150                       + "\nAlignment annotation file was not regenerated. Empty string",
151               anfileout.length() > "JALVIEW_ANNOTATION".length());
152
153       System.out.println("Output annotation file:\n" + anfileout
154               + "\n<<EOF\n");
155
156       // again what format would be appropriate?
157       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
158               FormatAdapter.PASTE, "PFAM");
159       assertTrue(
160               "Test "
161                       + testname
162                       + "\nregenerated annotation file did not annotate alignment.",
163               new AnnotationFile().readAnnotationFile(al_new, anfileout,
164                       FormatAdapter.PASTE));
165
166       // test for consistency in io
167       StockholmFileTest.testAlignmentEquivalence(al, al_new);
168       return;
169     } catch (Exception e)
170     {
171       e.printStackTrace();
172     }
173     fail("Test "
174             + testname
175             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
176   }
177
178   @Test
179   public void testRnaalifoldSettingsRecovery()
180   {
181     List<compbio.metadata.Argument> opts = new ArrayList<compbio.metadata.Argument>();
182     for (compbio.metadata.Argument rg : (List<compbio.metadata.Argument>) rnaalifoldws
183             .getRunnerConfig().getArguments())
184     {
185       if (rg.getDescription().contains("emperature"))
186       {
187         try
188         {
189           rg.setValue("292");
190         } catch (WrongParameterException q)
191         {
192           fail("Couldn't set the temperature parameter "
193                   + q.getStackTrace());
194         }
195         opts.add(rg);
196       }
197       if (rg.getDescription().contains("max"))
198       {
199         opts.add(rg);
200       }
201     }
202     alifoldClient = new RNAalifoldClient(rnaalifoldws, af, null, opts);
203
204     af.getViewport().getCalcManager().startWorker(alifoldClient);
205
206     do
207     {
208       try
209       {
210         Thread.sleep(50);
211       } catch (InterruptedException x)
212       {
213       }
214       ;
215     } while (af.getViewport().getCalcManager().isWorking());
216     AutoCalcSetting oldacs = af.getViewport().getCalcIdSettingsFor(
217             alifoldClient.getCalcId());
218     String oldsettings = oldacs.getWsParamFile();
219     // write out parameters
220     jalview.gui.AlignFrame nalf = null;
221     assertTrue("Couldn't write out the Jar file",
222             new Jalview2XML(false).SaveAlignment(af,
223                     "testRnalifold_param.jar", "trial parameter writeout"));
224     assertTrue("Couldn't read back the Jar file", (nalf = new Jalview2XML(
225             false).LoadJalviewAlign("testRnalifold_param.jar")) != null);
226     if (nalf != null)
227     {
228       AutoCalcSetting acs = af.getViewport().getCalcIdSettingsFor(
229               alifoldClient.getCalcId());
230       assertTrue("Calc ID settings not recovered from viewport stash",
231               acs.equals(oldacs));
232       assertTrue(
233               "Serialised Calc ID settings not identical to those recovered from viewport stash",
234               acs.getWsParamFile().equals(oldsettings));
235       JMenu nmenu = new JMenu();
236       new SequenceAnnotationWSClient().attachWSMenuEntry(nmenu,
237               rnaalifoldws, af);
238       assertTrue("Couldn't get menu entry for service",
239               nmenu.getItemCount() > 0);
240       for (Component itm : nmenu.getMenuComponents())
241       {
242         if (itm instanceof JMenuItem)
243         {
244           JMenuItem i = (JMenuItem) itm;
245           if (i.getText().equals(
246                   rnaalifoldws.getAlignAnalysisUI().getAAconToggle()))
247           {
248             i.doClick();
249             break;
250           }
251         }
252       }
253       while (af.getViewport().isCalcInProgress())
254       {
255         try
256         {
257           Thread.sleep(200);
258         } catch (Exception x)
259         {
260         }
261         ;
262       }
263       AutoCalcSetting acs2 = af.getViewport().getCalcIdSettingsFor(
264               alifoldClient.getCalcId());
265       assertTrue(
266               "Calc ID settings after recalculation has not been recovered.",
267               acs2.getWsParamFile().equals(oldsettings));
268     }
269   }
270 }