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