JAL-2311 suppress autocalc'd annotation to isolate the 'real' failure
[jalview.git] / test / jalview / ws / jabaws / JpredJabaStructExportImport.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.AlignmentI;
28 import jalview.gui.AlignFrame;
29 import jalview.gui.Jalview2XML;
30 import jalview.gui.JvOptionPane;
31 import jalview.io.AnnotationFile;
32 import jalview.io.DataSourceType;
33 import jalview.io.FileFormat;
34 import jalview.io.FileLoader;
35 import jalview.io.FormatAdapter;
36 import jalview.io.StockholmFileTest;
37 import jalview.ws.jws2.JPred301Client;
38 import jalview.ws.jws2.JabaParamStore;
39 import jalview.ws.jws2.SequenceAnnotationWSClient;
40 import jalview.ws.jws2.jabaws2.Jws2Instance;
41 import jalview.ws.params.AutoCalcSetting;
42
43 import java.awt.Component;
44 import java.net.ConnectException;
45 import java.util.ArrayList;
46 import java.util.List;
47
48 import javax.swing.JMenu;
49 import javax.swing.JMenuItem;
50 import javax.xml.ws.WebServiceException;
51
52 import org.testng.Assert;
53 import org.testng.annotations.AfterClass;
54 import org.testng.annotations.BeforeClass;
55 import org.testng.annotations.Test;
56
57 import compbio.data.msa.Category;
58 import compbio.data.msa.JABAService;
59 import compbio.metadata.Argument;
60 import compbio.metadata.WrongParameterException;
61 import compbio.ws.client.Jws2Client;
62 import compbio.ws.client.Services;
63
64 public class JpredJabaStructExportImport
65 {
66
67   @BeforeClass(alwaysRun = true)
68   public void setUpJvOptionPane()
69   {
70     JvOptionPane.setInteractiveMode(false);
71     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
72   }
73
74   private static String testseqs = "examples/uniref50.fa";
75
76   private static Jws2Instance jpredws;
77
78   private static AlignFrame af = null;
79
80   @BeforeClass(alwaysRun = true)
81   public static void setUpBeforeClass() throws ConnectException,
82           WebServiceException
83   {
84     Cache.loadProperties("test/jalview/io/testProps.jvprops");
85     Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS",
86             Boolean.TRUE.toString());
87     Cache.applicationProperties.setProperty("SHOW_QUALITY",
88             Boolean.FALSE.toString());
89     Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
90             Boolean.FALSE.toString());
91     Cache.applicationProperties.setProperty("SHOW_IDENTITY",
92             Boolean.FALSE.toString());
93     Cache.initLogger();
94     getJabaws();
95
96     FileLoader fl = new FileLoader(false);
97     af = fl.LoadFileWaitTillLoaded(testseqs, DataSourceType.FILE);
98     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
99   }
100
101   /**
102    * @throws WebServiceException
103    * @throws ConnectException
104    * 
105    */
106   static void getJabaws() throws ConnectException, WebServiceException
107   {
108     String jabaws = "http://www.compbio.dundee.ac.uk/jabaws";
109     JABAService service = null;
110     for (Category category : Category.getCategories())
111     {
112       for (Services srv : category.getServices())
113       {
114         if ("JpredWS".equals(srv.name()))
115         {
116           service = Jws2Client.connect(jabaws, srv);
117         }
118       }
119     }
120     jpredws = new Jws2Instance(jabaws, "JpredWs",
121             "Secondary Structure Prediction",
122             "SERVICE: JpredWS version 3.0.3", service);
123     Assert.assertNotNull(jpredws, "jpredws is null!");
124   }
125
126   @AfterClass(alwaysRun = true)
127   public static void tearDownAfterClass() throws Exception
128   {
129     if (af != null)
130     {
131       af.setVisible(false);
132       af.dispose();
133     }
134   }
135
136   @Test(groups = { "Network" })
137   public void testJPredStructOneSeqOnly()
138   {
139     af.selectAllSequenceMenuItem_actionPerformed(null);
140     af.getViewport()
141             .getSelectionGroup()
142             .addOrRemove(
143                     af.getViewport().getSelectionGroup().getSequenceAt(0),
144                     false);
145     af.hideSelSequences_actionPerformed(null);
146     JPred301Client jpredClient = new JPred301Client(jpredws, af, null, null);
147
148     assertTrue(
149             "Didn't find any default args to check for. Buggy implementation of hardwired arguments in client.",
150             jpredClient.selectDefaultArgs().size() > 0);
151
152     boolean success = false;
153     af.getViewport().getCalcManager().startWorker(jpredClient);
154     do
155     {
156       try
157       {
158         Thread.sleep(500);
159         List<Argument> args = JabaParamStore.getJabafromJwsArgs(af
160                 .getViewport()
161                 .getCalcIdSettingsFor(jpredClient.getCalcId())
162                 .getArgumentSet()), defargs = jpredClient
163                 .selectDefaultArgs();
164         for (Argument rg : args)
165         {
166           for (Argument defg : defargs)
167           {
168             if (defg.equals(rg))
169             {
170               success = true;
171             }
172           }
173         }
174         if (!success)
175         {
176           jpredClient.cancelCurrentJob();
177           Assert.fail("Jpred Client didn't run with hardwired default parameters.");
178         }
179
180       } catch (InterruptedException x)
181       {
182       }
183       ;
184     } while (af.getViewport().getCalcManager().isWorking());
185
186   }
187
188   /*
189    * test disabled pending completion of JAL-1601
190    * (JPred301Client is currently hidden in Jalview in
191    * 
192    */
193   @Test(groups = { "Network" })
194   public void testJPredStructExport() throws InterruptedException
195   {
196
197     JPred301Client jpredClient = new JPred301Client(jpredws, af, null, null);
198
199     af.getViewport().getCalcManager().startWorker(jpredClient);
200
201     do
202     {
203       try
204       {
205         Thread.sleep(50);
206       } catch (InterruptedException x)
207       {
208       }
209       ;
210     } while (af.getViewport().getCalcManager().isWorking());
211
212     synchronized (this)
213     {
214       wait(2000);
215     }
216     AlignmentI orig_alig = af.getViewport().getAlignment();
217     System.out.println("Alignment now has "
218             + orig_alig.getAlignmentAnnotation().length + " annotations");
219     verifyAnnotationFileIO("Testing JPredWS Annotation IO", orig_alig);
220
221   }
222
223   protected static void verifyAnnotationFileIO(String testname,
224           AlignmentI al)
225   {
226     try
227     {
228       // what format would be appropriate for RNAalifold annotations?
229       String aligfileout = FileFormat.Fasta.getWriter(null).print(
230               al.getSequencesArray(), true);
231
232       String anfileout = new AnnotationFile()
233               .printAnnotationsForAlignment(al);
234       assertTrue(
235               "Test "
236                       + testname
237                       + "\nAlignment annotation file was not regenerated. Null string",
238               anfileout != null);
239       assertTrue(
240               "Test "
241                       + testname
242                       + "\nAlignment annotation file was not regenerated. Empty string",
243               anfileout.length() > "JALVIEW_ANNOTATION".length());
244
245       System.out.println("Output annotation file:\n" + anfileout
246               + "\n<<EOF\n");
247
248       // again what format would be appropriate?
249       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
250               DataSourceType.PASTE, FileFormat.Fasta);
251       assertTrue(
252               "Test "
253                       + testname
254                       + "\nregenerated annotation file did not annotate alignment.",
255               new AnnotationFile().readAnnotationFile(al_new, anfileout,
256                       DataSourceType.PASTE));
257
258       // test for consistency in io
259       StockholmFileTest.testAlignmentEquivalence(al, al_new, false);
260       return;
261     } catch (Exception e)
262     {
263       e.printStackTrace();
264       Assert.fail("Test "
265               + testname
266               + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
267     }
268   }
269
270   @Test(groups = { "Network" }, enabled = false)
271   public void testJpredwsSettingsRecovery()
272   {
273     Assert.fail("not implemented");
274     List<compbio.metadata.Argument> opts = new ArrayList<compbio.metadata.Argument>();
275     for (compbio.metadata.Argument rg : (List<compbio.metadata.Argument>) jpredws
276             .getRunnerConfig().getArguments())
277     {
278       if (rg.getDescription().contains("emperature"))
279       {
280         try
281         {
282           rg.setValue("292");
283         } catch (WrongParameterException q)
284         {
285           Assert.fail("Couldn't set the temperature parameter "
286                   + q.getStackTrace());
287         }
288         opts.add(rg);
289       }
290       if (rg.getDescription().contains("max"))
291       {
292         opts.add(rg);
293       }
294     }
295     JPred301Client jpredClient = new JPred301Client(jpredws, af, null, opts);
296
297     af.getViewport().getCalcManager().startWorker(jpredClient);
298
299     do
300     {
301       try
302       {
303         Thread.sleep(50);
304       } catch (InterruptedException x)
305       {
306       }
307       ;
308     } while (af.getViewport().getCalcManager().isWorking());
309     AutoCalcSetting oldacs = af.getViewport().getCalcIdSettingsFor(
310             jpredClient.getCalcId());
311     String oldsettings = oldacs.getWsParamFile();
312     // write out parameters
313     jalview.gui.AlignFrame nalf = null;
314     assertTrue("Couldn't write out the Jar file",
315             new Jalview2XML(false).saveAlignment(af,
316                     "testJPredWS_param.jar", "trial parameter writeout"));
317     assertTrue("Couldn't read back the Jar file", (nalf = new Jalview2XML(
318             false).loadJalviewAlign("testJpredWS_param.jar")) != null);
319     if (nalf != null)
320     {
321       AutoCalcSetting acs = af.getViewport().getCalcIdSettingsFor(
322               jpredClient.getCalcId());
323       assertTrue("Calc ID settings not recovered from viewport stash",
324               acs.equals(oldacs));
325       assertTrue(
326               "Serialised Calc ID settings not identical to those recovered from viewport stash",
327               acs.getWsParamFile().equals(oldsettings));
328       JMenu nmenu = new JMenu();
329       new SequenceAnnotationWSClient()
330               .attachWSMenuEntry(nmenu, jpredws, af);
331       assertTrue("Couldn't get menu entry for service",
332               nmenu.getItemCount() > 0);
333       for (Component itm : nmenu.getMenuComponents())
334       {
335         if (itm instanceof JMenuItem)
336         {
337           JMenuItem i = (JMenuItem) itm;
338           if (i.getText().equals(
339                   jpredws.getAlignAnalysisUI().getAAconToggle()))
340           {
341             i.doClick();
342             break;
343           }
344         }
345       }
346       while (af.getViewport().isCalcInProgress())
347       {
348         try
349         {
350           Thread.sleep(200);
351         } catch (Exception x)
352         {
353         }
354         ;
355       }
356       AutoCalcSetting acs2 = af.getViewport().getCalcIdSettingsFor(
357               jpredClient.getCalcId());
358       assertTrue(
359               "Calc ID settings after recalculation has not been recovered.",
360               acs2.getWsParamFile().equals(oldsettings));
361     }
362   }
363 }