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