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