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