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