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