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