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