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