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