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