JAL-1683 replace year/version strings with tokens in source
[jalview.git] / test / jalview / ws / jabaws / RNAStructExportImport.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.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     if (rnaalifoldws == null)
81     {
82       System.exit(0);
83     }
84
85     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
86
87     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.FormatAdapter.FILE);
88
89     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
90
91   }
92
93   @AfterClass
94   public static void tearDownAfterClass() throws Exception
95   {
96     if (af != null)
97     {
98       af.setVisible(false);
99       af.dispose();
100     }
101   }
102
103   @Test
104   public void testRNAStructExport()
105   {
106
107     alifoldClient = new RNAalifoldClient(rnaalifoldws, af, null, null);
108
109     af.getViewport().getCalcManager().startWorker(alifoldClient);
110
111     do
112     {
113       try
114       {
115         Thread.sleep(50);
116       } catch (InterruptedException x)
117       {
118       }
119       ;
120     } while (af.getViewport().getCalcManager().isWorking());
121
122     AlignmentI orig_alig = af.getViewport().getAlignment();
123
124     testAnnotationFileIO("Testing RNAalifold Annotation IO", orig_alig);
125
126   }
127
128   public static void testAnnotationFileIO(String testname, AlignmentI al)
129   {
130     try
131     {
132       // what format would be appropriate for RNAalifold annotations?
133       String aligfileout = new FormatAdapter().formatSequences("PFAM",
134               al.getSequencesArray());
135
136       String anfileout = new AnnotationFile()
137               .printAnnotationsForAlignment(al);
138       assertTrue(
139               "Test "
140                       + testname
141                       + "\nAlignment annotation file was not regenerated. Null string",
142               anfileout != null);
143       assertTrue(
144               "Test "
145                       + testname
146                       + "\nAlignment annotation file was not regenerated. Empty string",
147               anfileout.length() > "JALVIEW_ANNOTATION".length());
148
149       System.out.println("Output annotation file:\n" + anfileout
150               + "\n<<EOF\n");
151
152       // again what format would be appropriate?
153       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
154               FormatAdapter.PASTE, "PFAM");
155       assertTrue(
156               "Test "
157                       + testname
158                       + "\nregenerated annotation file did not annotate alignment.",
159               new AnnotationFile().readAnnotationFile(al_new, anfileout,
160                       FormatAdapter.PASTE));
161
162       // test for consistency in io
163       StockholmFileTest.testAlignmentEquivalence(al, al_new);
164       return;
165     } catch (Exception e)
166     {
167       e.printStackTrace();
168     }
169     fail("Test "
170             + testname
171             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
172   }
173
174   @Test
175   public void testRnaalifoldSettingsRecovery()
176   {
177     List<compbio.metadata.Argument> opts = new ArrayList<compbio.metadata.Argument>();
178     for (compbio.metadata.Argument rg : (List<compbio.metadata.Argument>) rnaalifoldws
179             .getRunnerConfig().getArguments())
180     {
181       if (rg.getDescription().contains("emperature"))
182       {
183         try
184         {
185           rg.setValue("292");
186         } catch (WrongParameterException q)
187         {
188           fail("Couldn't set the temperature parameter "
189                   + q.getStackTrace());
190         }
191         opts.add(rg);
192       }
193       if (rg.getDescription().contains("max"))
194       {
195         opts.add(rg);
196       }
197     }
198     alifoldClient = new RNAalifoldClient(rnaalifoldws, af, null, opts);
199
200     af.getViewport().getCalcManager().startWorker(alifoldClient);
201
202     do
203     {
204       try
205       {
206         Thread.sleep(50);
207       } catch (InterruptedException x)
208       {
209       }
210       ;
211     } while (af.getViewport().getCalcManager().isWorking());
212     AutoCalcSetting oldacs = af.getViewport().getCalcIdSettingsFor(
213             alifoldClient.getCalcId());
214     String oldsettings = oldacs.getWsParamFile();
215     // write out parameters
216     jalview.gui.AlignFrame nalf = null;
217     assertTrue("Couldn't write out the Jar file",
218             new Jalview2XML(false).saveAlignment(af,
219                     "testRnalifold_param.jar", "trial parameter writeout"));
220     assertTrue("Couldn't read back the Jar file", (nalf = new Jalview2XML(
221             false).loadJalviewAlign("testRnalifold_param.jar")) != null);
222     if (nalf != null)
223     {
224       AutoCalcSetting acs = af.getViewport().getCalcIdSettingsFor(
225               alifoldClient.getCalcId());
226       assertTrue("Calc ID settings not recovered from viewport stash",
227               acs.equals(oldacs));
228       assertTrue(
229               "Serialised Calc ID settings not identical to those recovered from viewport stash",
230               acs.getWsParamFile().equals(oldsettings));
231       JMenu nmenu = new JMenu();
232       new SequenceAnnotationWSClient().attachWSMenuEntry(nmenu,
233               rnaalifoldws, af);
234       assertTrue("Couldn't get menu entry for service",
235               nmenu.getItemCount() > 0);
236       for (Component itm : nmenu.getMenuComponents())
237       {
238         if (itm instanceof JMenuItem)
239         {
240           JMenuItem i = (JMenuItem) itm;
241           if (i.getText().equals(
242                   rnaalifoldws.getAlignAnalysisUI().getAAconToggle()))
243           {
244             i.doClick();
245             break;
246           }
247         }
248       }
249       while (af.getViewport().isCalcInProgress())
250       {
251         try
252         {
253           Thread.sleep(200);
254         } catch (Exception x)
255         {
256         }
257         ;
258       }
259       AutoCalcSetting acs2 = af.getViewport().getCalcIdSettingsFor(
260               alifoldClient.getCalcId());
261       assertTrue(
262               "Calc ID settings after recalculation has not been recovered.",
263               acs2.getWsParamFile().equals(oldsettings));
264     }
265   }
266 }