JAL-1328 System.exit() removed as it aborts an entire JUnit suite
[jalview.git] / test / jalview / ws / jabaws / RNAStructExportImport.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.Jws2Discoverer;
32 import jalview.ws.jws2.RNAalifoldClient;
33 import jalview.ws.jws2.SequenceAnnotationWSClient;
34 import jalview.ws.jws2.jabaws2.Jws2Instance;
35 import jalview.ws.params.AutoCalcSetting;
36
37 import java.awt.Component;
38 import java.util.ArrayList;
39 import java.util.List;
40
41 import javax.swing.JMenu;
42 import javax.swing.JMenuItem;
43
44 import org.junit.AfterClass;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47
48 import compbio.metadata.WrongParameterException;
49
50 public class RNAStructExportImport
51 {
52   public static String testseqs = "examples/unfolded_RF00031.aln";
53
54   public static Jws2Discoverer disc;
55
56   public static Jws2Instance rnaalifoldws;
57
58   jalview.ws.jws2.RNAalifoldClient alifoldClient;
59
60   public static jalview.gui.AlignFrame af = null;
61
62   @BeforeClass
63   public static void setUpBeforeClass() throws Exception
64   {
65
66     jalview.bin.Cache.initLogger();
67     disc = JalviewJabawsTestUtils.getJabawsDiscoverer(false);
68
69     for (Jws2Instance svc : disc.getServices())
70     {
71
72       if (svc.getServiceTypeURI().toLowerCase().contains("rnaalifoldws"))
73       {
74         rnaalifoldws = svc;
75       }
76     }
77
78     System.out.println("State of rnaalifoldws: " + rnaalifoldws);
79
80     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
81
82     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.FormatAdapter.FILE);
83
84     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
85
86   }
87
88   @AfterClass
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
99   public void testRNAStructExport()
100   {
101
102     alifoldClient = new RNAalifoldClient(rnaalifoldws, af, null, null);
103
104     af.getViewport().getCalcManager().startWorker(alifoldClient);
105
106     do
107     {
108       try
109       {
110         Thread.sleep(50);
111       } catch (InterruptedException x)
112       {
113       }
114       ;
115     } while (af.getViewport().getCalcManager().isWorking());
116
117     AlignmentI orig_alig = af.getViewport().getAlignment();
118
119     testAnnotationFileIO("Testing RNAalifold Annotation IO", orig_alig);
120
121   }
122
123   public static void testAnnotationFileIO(String testname, AlignmentI al)
124   {
125     try
126     {
127       // what format would be appropriate for RNAalifold annotations?
128       String aligfileout = new FormatAdapter().formatSequences("PFAM",
129               al.getSequencesArray());
130
131       String anfileout = new AnnotationFile().printAnnotations(
132               al.getAlignmentAnnotation(), al.getGroups(),
133               al.getProperties());
134       assertTrue(
135               "Test "
136                       + testname
137                       + "\nAlignment annotation file was not regenerated. Null string",
138               anfileout != null);
139       assertTrue(
140               "Test "
141                       + testname
142                       + "\nAlignment annotation file was not regenerated. Empty string",
143               anfileout.length() > "JALVIEW_ANNOTATION".length());
144
145       System.out.println("Output annotation file:\n" + anfileout
146               + "\n<<EOF\n");
147
148       // again what format would be appropriate?
149       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
150               FormatAdapter.PASTE, "PFAM");
151       assertTrue(
152               "Test "
153                       + testname
154                       + "\nregenerated annotation file did not annotate alignment.",
155               new AnnotationFile().readAnnotationFile(al_new, anfileout,
156                       FormatAdapter.PASTE));
157
158       // test for consistency in io
159       StockholmFileTest.testAlignmentEquivalence(al, al_new);
160       return;
161     } catch (Exception e)
162     {
163       e.printStackTrace();
164     }
165     fail("Test "
166             + testname
167             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
168   }
169
170   @Test
171   public void testRnaalifoldSettingsRecovery()
172   {
173     List<compbio.metadata.Argument> opts = new ArrayList<compbio.metadata.Argument>();
174     for (compbio.metadata.Argument rg : (List<compbio.metadata.Argument>) rnaalifoldws
175             .getRunnerConfig().getArguments())
176     {
177       if (rg.getDescription().contains("emperature"))
178       {
179         try
180         {
181           rg.setValue("292");
182         } catch (WrongParameterException q)
183         {
184           fail("Couldn't set the temperature parameter "
185                   + q.getStackTrace());
186         }
187         opts.add(rg);
188       }
189       if (rg.getDescription().contains("max"))
190       {
191         opts.add(rg);
192       }
193     }
194
195     assertNotNull(rnaalifoldws);
196     alifoldClient = new RNAalifoldClient(rnaalifoldws, af, null, opts);
197
198     af.getViewport().getCalcManager().startWorker(alifoldClient);
199
200     do
201     {
202       try
203       {
204         Thread.sleep(50);
205       } catch (InterruptedException x)
206       {
207       }
208       ;
209     } while (af.getViewport().getCalcManager().isWorking());
210     AutoCalcSetting oldacs = af.getViewport().getCalcIdSettingsFor(
211             alifoldClient.getCalcId());
212     String oldsettings = oldacs.getWsParamFile();
213     // write out parameters
214     jalview.gui.AlignFrame nalf = null;
215     assertTrue("Couldn't write out the Jar file",
216             new Jalview2XML(false).SaveAlignment(af,
217                     "testRnalifold_param.jar", "trial parameter writeout"));
218     assertTrue("Couldn't read back the Jar file", (nalf = new Jalview2XML(
219             false).LoadJalviewAlign("testRnalifold_param.jar")) != null);
220     if (nalf != null)
221     {
222       AutoCalcSetting acs = af.getViewport().getCalcIdSettingsFor(
223               alifoldClient.getCalcId());
224       assertTrue("Calc ID settings not recovered from viewport stash",
225               acs.equals(oldacs));
226       assertTrue(
227               "Serialised Calc ID settings not identical to those recovered from viewport stash",
228               acs.getWsParamFile().equals(oldsettings));
229       JMenu nmenu = new JMenu();
230       new SequenceAnnotationWSClient().attachWSMenuEntry(nmenu,
231               rnaalifoldws, af);
232       assertTrue("Couldn't get menu entry for service",
233               nmenu.getItemCount() > 0);
234       for (Component itm : nmenu.getMenuComponents())
235       {
236         if (itm instanceof JMenuItem)
237         {
238           JMenuItem i = (JMenuItem) itm;
239           if (i.getText().equals(
240                   rnaalifoldws.getAlignAnalysisUI().getAAconToggle()))
241           {
242             i.doClick();
243             break;
244           }
245         }
246       }
247       while (af.getViewport().isCalcInProgress())
248       {
249         try
250         {
251           Thread.sleep(200);
252         } catch (Exception x)
253         {
254         }
255         ;
256       }
257       AutoCalcSetting acs2 = af.getViewport().getCalcIdSettingsFor(
258               alifoldClient.getCalcId());
259       assertTrue(
260               "Calc ID settings after recalculation has not been recovered.",
261               acs2.getWsParamFile().equals(oldsettings));
262     }
263   }
264 }