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