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