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