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