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