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