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