fileFormat enum wip changes
[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.DataSourceType;
31 import jalview.io.FileFormat;
32 import jalview.io.FormatAdapter;
33 import jalview.io.StockholmFileTest;
34 import jalview.ws.jws2.JPred301Client;
35 import jalview.ws.jws2.JabaParamStore;
36 import jalview.ws.jws2.Jws2Discoverer;
37 import jalview.ws.jws2.SequenceAnnotationWSClient;
38 import jalview.ws.jws2.jabaws2.Jws2Instance;
39 import jalview.ws.params.AutoCalcSetting;
40
41 import java.awt.Component;
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import javax.swing.JMenu;
46 import javax.swing.JMenuItem;
47
48 import org.testng.Assert;
49 import org.testng.annotations.AfterClass;
50 import org.testng.annotations.BeforeClass;
51 import org.testng.annotations.Test;
52
53 import compbio.metadata.Argument;
54 import compbio.metadata.WrongParameterException;
55
56 public class JpredJabaStructExportImport
57 {
58   public static String testseqs = "examples/uniref50.fa";
59
60   public static Jws2Discoverer disc;
61
62   public static Jws2Instance jpredws;
63
64   jalview.ws.jws2.JPred301Client jpredClient;
65
66   public static jalview.gui.AlignFrame af = null;
67
68   @BeforeClass(alwaysRun = true)
69   public static void setUpBeforeClass() throws Exception
70   {
71     Cache.loadProperties("test/jalview/io/testProps.jvprops");
72     Cache.initLogger();
73     disc = JalviewJabawsTestUtils.getJabawsDiscoverer(false);
74
75     for (Jws2Instance svc : disc.getServices())
76     {
77       if (svc.getServiceTypeURI().toLowerCase().contains("jpred"))
78       {
79         jpredws = svc;
80       }
81     }
82
83     System.out.println("State of jpredws: " + jpredws);
84     Assert.assertNotNull(jpredws, "jpredws is null!");
85     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
86     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.DataSourceType.FILE);
87     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
88   }
89
90   @AfterClass(alwaysRun = true)
91   public static void tearDownAfterClass() throws Exception
92   {
93     if (af != null)
94     {
95       af.setVisible(false);
96       af.dispose();
97     }
98   }
99
100   @Test(groups = { "Functional" })
101   public void testJPredStructOneSeqOnly()
102   {
103     af.selectAllSequenceMenuItem_actionPerformed(null);
104     af.getViewport()
105             .getSelectionGroup()
106             .addOrRemove(
107                     af.getViewport().getSelectionGroup().getSequenceAt(0),
108                     false);
109     af.hideSelSequences_actionPerformed(null);
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           Assert.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(groups = { "Functional" })
153   public void testJPredStructExport()
154   {
155
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 = FileFormat.Pfam.getAlignmentFile().print(
183               al.getSequencesArray(), true);
184
185       String anfileout = new AnnotationFile()
186               .printAnnotationsForAlignment(al);
187       assertTrue(
188               "Test "
189                       + testname
190                       + "\nAlignment annotation file was not regenerated. Null string",
191               anfileout != null);
192       assertTrue(
193               "Test "
194                       + testname
195                       + "\nAlignment annotation file was not regenerated. Empty string",
196               anfileout.length() > "JALVIEW_ANNOTATION".length());
197
198       System.out.println("Output annotation file:\n" + anfileout
199               + "\n<<EOF\n");
200
201       // again what format would be appropriate?
202       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
203               DataSourceType.PASTE, FileFormat.Fasta);
204       assertTrue(
205               "Test "
206                       + testname
207                       + "\nregenerated annotation file did not annotate alignment.",
208               new AnnotationFile().readAnnotationFile(al_new, anfileout,
209                       DataSourceType.PASTE));
210
211       // test for consistency in io
212       StockholmFileTest.testAlignmentEquivalence(al, al_new, false);
213       return;
214     } catch (Exception e)
215     {
216       e.printStackTrace();
217     }
218     Assert.fail("Test "
219             + testname
220             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
221   }
222
223   @Test(groups = { "Functional" })
224   public void testJpredwsSettingsRecovery()
225   {
226     Assert.fail("not implemnented");
227     List<compbio.metadata.Argument> opts = new ArrayList<compbio.metadata.Argument>();
228     for (compbio.metadata.Argument rg : (List<compbio.metadata.Argument>) jpredws
229             .getRunnerConfig().getArguments())
230     {
231       if (rg.getDescription().contains("emperature"))
232       {
233         try
234         {
235           rg.setValue("292");
236         } catch (WrongParameterException q)
237         {
238           Assert.fail("Couldn't set the temperature parameter "
239                   + q.getStackTrace());
240         }
241         opts.add(rg);
242       }
243       if (rg.getDescription().contains("max"))
244       {
245         opts.add(rg);
246       }
247     }
248     jpredClient = new JPred301Client(jpredws, af, null, opts);
249
250     af.getViewport().getCalcManager().startWorker(jpredClient);
251
252     do
253     {
254       try
255       {
256         Thread.sleep(50);
257       } catch (InterruptedException x)
258       {
259       }
260       ;
261     } while (af.getViewport().getCalcManager().isWorking());
262     AutoCalcSetting oldacs = af.getViewport().getCalcIdSettingsFor(
263             jpredClient.getCalcId());
264     String oldsettings = oldacs.getWsParamFile();
265     // write out parameters
266     jalview.gui.AlignFrame nalf = null;
267     assertTrue("Couldn't write out the Jar file",
268             new Jalview2XML(false).saveAlignment(af,
269                     "testJPredWS_param.jar", "trial parameter writeout"));
270     assertTrue("Couldn't read back the Jar file", (nalf = new Jalview2XML(
271             false).loadJalviewAlign("testJpredWS_param.jar")) != null);
272     if (nalf != null)
273     {
274       AutoCalcSetting acs = af.getViewport().getCalcIdSettingsFor(
275               jpredClient.getCalcId());
276       assertTrue("Calc ID settings not recovered from viewport stash",
277               acs.equals(oldacs));
278       assertTrue(
279               "Serialised Calc ID settings not identical to those recovered from viewport stash",
280               acs.getWsParamFile().equals(oldsettings));
281       JMenu nmenu = new JMenu();
282       new SequenceAnnotationWSClient()
283               .attachWSMenuEntry(nmenu, jpredws, af);
284       assertTrue("Couldn't get menu entry for service",
285               nmenu.getItemCount() > 0);
286       for (Component itm : nmenu.getMenuComponents())
287       {
288         if (itm instanceof JMenuItem)
289         {
290           JMenuItem i = (JMenuItem) itm;
291           if (i.getText().equals(
292                   jpredws.getAlignAnalysisUI().getAAconToggle()))
293           {
294             i.doClick();
295             break;
296           }
297         }
298       }
299       while (af.getViewport().isCalcInProgress())
300       {
301         try
302         {
303           Thread.sleep(200);
304         } catch (Exception x)
305         {
306         }
307         ;
308       }
309       AutoCalcSetting acs2 = af.getViewport().getCalcIdSettingsFor(
310               jpredClient.getCalcId());
311       assertTrue(
312               "Calc ID settings after recalculation has not been recovered.",
313               acs2.getWsParamFile().equals(oldsettings));
314     }
315   }
316 }