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