Merge branch 'Jalview-JS/develop' into merge_js_develop
[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.testng.AssertJUnit.assertNotNull;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import jalview.bin.Cache;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
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.project.Jalview2XML;
36 import jalview.ws.api.ServiceWithParameters;
37 import jalview.ws.jws2.JabaParamStore;
38 import jalview.ws.jws2.Jws2Discoverer;
39 import jalview.ws.jws2.SeqAnnotationServiceCalcWorker;
40 import jalview.ws.jws2.SequenceAnnotationWSClient;
41 import jalview.ws.jws2.jabaws2.Jws2Instance;
42 import jalview.ws.params.AutoCalcSetting;
43
44 import java.awt.Component;
45 import java.io.File;
46 import java.util.ArrayList;
47 import java.util.List;
48
49 import javax.swing.JMenu;
50 import javax.swing.JMenuItem;
51
52 import org.testng.Assert;
53 import org.testng.annotations.AfterClass;
54 import org.testng.annotations.BeforeClass;
55 import org.testng.annotations.Test;
56
57 import compbio.metadata.Argument;
58 import compbio.metadata.WrongParameterException;
59
60 /*
61  * All methods in this class are set to the Network group because setUpBeforeClass will fail
62  * if there is no network.
63  */
64 @Test(singleThreaded = true)
65 public class RNAStructExportImport
66 {
67
68   @BeforeClass(alwaysRun = true)
69   public void setUpJvOptionPane()
70   {
71     JvOptionPane.setInteractiveMode(false);
72     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
73   }
74
75   private static final String JAR_FILE_NAME = "testRnalifold_param.jar";
76
77   public static String testseqs = "examples/RF00031_folded.stk";
78
79   public static Jws2Discoverer disc;
80
81   public static Jws2Instance rnaalifoldws;
82
83   SeqAnnotationServiceCalcWorker alifoldClient;
84
85   public static jalview.gui.AlignFrame af = null;
86
87   @BeforeClass(alwaysRun = true)
88   public static void setUpBeforeClass() throws Exception
89   {
90     Cache.loadProperties("test/jalview/io/testProps.jvprops");
91     Cache.initLogger();
92     disc = JalviewJabawsTestUtils.getJabawsDiscoverer(false);
93
94     while (disc.isRunning())
95     {
96       // don't get services until discoverer has finished
97       Thread.sleep(100);
98     }
99
100     for (ServiceWithParameters svc : disc.getServices())
101     {
102
103       if (svc.getNameURI().toLowerCase().contains("rnaalifoldws"))
104       {
105         rnaalifoldws = (Jws2Instance) svc;
106       }
107     }
108
109     System.out.println("State of rnaalifoldws: " + rnaalifoldws);
110
111     if (rnaalifoldws == null)
112     {
113       Assert.fail("no web service");
114     }
115
116     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
117
118     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.DataSourceType.FILE);
119
120     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
121
122     // remove any existing annotation
123     List<AlignmentAnnotation> aal = new ArrayList<>();
124     for (AlignmentAnnotation rna : af.getViewport().getAlignment()
125             .getAlignmentAnnotation())
126     {
127       if (rna.isRNA())
128       {
129         aal.add(rna);
130       }
131     }
132     for (AlignmentAnnotation rna : aal)
133     {
134       af.getViewport().getAlignment().deleteAnnotation(rna);
135     }
136     af.getViewport().alignmentChanged(af.alignPanel); // why is af.alignPanel
137                                                       // public?
138   }
139
140   @AfterClass(alwaysRun = true)
141   public static void tearDownAfterClass() throws Exception
142   {
143     if (af != null)
144     {
145       af.setVisible(false);
146       af.dispose();
147       File f = new File(JAR_FILE_NAME);
148       if (f.exists())
149       {
150         f.delete();
151       }
152     }
153   }
154
155   @Test(groups = { "Network" })
156   public void testRNAAliFoldValidStructure()
157   {
158
159     alifoldClient = new SeqAnnotationServiceCalcWorker(rnaalifoldws, af, null,
160             null);
161
162     af.getViewport().getCalcManager().startWorker(alifoldClient);
163
164     do
165     {
166       try
167       {
168         Thread.sleep(50);
169       } catch (InterruptedException x)
170       {
171       }
172     } while (af.getViewport().getCalcManager().isWorking());
173
174     AlignmentI orig_alig = af.getViewport().getAlignment();
175     for (AlignmentAnnotation aa : orig_alig.getAlignmentAnnotation())
176     {
177       if (alifoldClient.involves(aa))
178       {
179         if (aa.isRNA())
180         {
181           assertTrue(
182                   "Did not create valid structure from RNAALiFold prediction",
183                   aa.isValidStruc());
184         }
185       }
186     }
187   }
188
189   @Test(groups = { "Network" })
190   public void testRNAStructExport()
191   {
192
193     alifoldClient = new SeqAnnotationServiceCalcWorker(rnaalifoldws, af, null,
194             null);
195
196     af.getViewport().getCalcManager().startWorker(alifoldClient);
197
198     do
199     {
200       try
201       {
202         Thread.sleep(50);
203       } catch (InterruptedException x)
204       {
205       }
206     } while (af.getViewport().getCalcManager().isWorking());
207
208     AlignmentI orig_alig = af.getViewport().getAlignment();
209     // JBPNote: this assert fails (2.10.2) because the 'Reference Positions'
210     // annotation is mistakenly recognised as an RNA annotation row when read in
211     // as an annotation file. bug is JAL-3122
212     verifyAnnotationFileIO("Testing RNAalifold Annotation IO", orig_alig);
213
214   }
215
216   static void verifyAnnotationFileIO(String testname, AlignmentI al)
217   {
218     try
219     {
220       // what format would be appropriate for RNAalifold annotations?
221       String aligfileout = FileFormat.Pfam.getWriter(null).print(
222               al.getSequencesArray(), true);
223
224       String anfileout = new AnnotationFile()
225               .printAnnotationsForAlignment(al);
226       assertNotNull(
227               "Test "
228                       + testname
229                       + "\nAlignment annotation file was not regenerated. Null string",
230               anfileout);
231       assertTrue(
232               "Test "
233                       + testname
234                       + "\nAlignment annotation file was not regenerated. Empty string",
235               anfileout.length() > "JALVIEW_ANNOTATION".length());
236
237       System.out.println("Output annotation file:\n" + anfileout
238               + "\n<<EOF\n");
239
240       // again what format would be appropriate?
241       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
242               DataSourceType.PASTE, FileFormat.Pfam);
243       assertTrue(
244               "Test "
245                       + testname
246                       + "\nregenerated annotation file did not annotate alignment.",
247               new AnnotationFile().readAnnotationFile(al_new, anfileout,
248                       DataSourceType.PASTE));
249
250       // test for consistency in io
251       StockholmFileTest.testAlignmentEquivalence(al, al_new, false, false,
252               false);
253       return;
254     } catch (Exception e)
255     {
256       e.printStackTrace();
257     }
258     Assert.fail("Test "
259             + testname
260             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
261   }
262
263   @Test(groups = { "Network" })
264   public void testRnaalifoldSettingsRecovery()
265   {
266     List<Argument> opts = new ArrayList<>();
267     for (Argument rg : (List<Argument>) rnaalifoldws.getRunnerConfig()
268             .getArguments())
269     {
270       if (rg.getDescription().contains("emperature"))
271       {
272         try
273         {
274           rg.setValue("292");
275         } catch (WrongParameterException q)
276         {
277           Assert.fail("Couldn't set the temperature parameter "
278                   + q.getStackTrace());
279         }
280         opts.add(rg);
281       }
282       if (rg.getDescription().contains("max"))
283       {
284         opts.add(rg);
285       }
286     }
287     alifoldClient = new SeqAnnotationServiceCalcWorker(rnaalifoldws, af, null,
288             JabaParamStore.getJwsArgsfromJaba(opts));
289
290     af.getViewport().getCalcManager().startWorker(alifoldClient);
291
292     do
293     {
294       try
295       {
296         Thread.sleep(50);
297       } catch (InterruptedException x)
298       {
299       }
300       ;
301     } while (af.getViewport().getCalcManager().isWorking());
302     AutoCalcSetting oldacs = af.getViewport().getCalcIdSettingsFor(
303             alifoldClient.getCalcId());
304     String oldsettings = oldacs.getWsParamFile();
305     // write out parameters
306     jalview.gui.AlignFrame nalf = null;
307     assertTrue("Couldn't write out the Jar file",
308             new Jalview2XML(false).saveAlignment(af, JAR_FILE_NAME,
309                     "trial parameter writeout"));
310     assertTrue("Couldn't read back the Jar file", (nalf = new Jalview2XML(
311             false).loadJalviewAlign(JAR_FILE_NAME)) != null);
312     if (nalf != null)
313     {
314       AutoCalcSetting acs = af.getViewport().getCalcIdSettingsFor(
315               alifoldClient.getCalcId());
316       assertTrue("Calc ID settings not recovered from viewport stash",
317               acs.equals(oldacs));
318       assertTrue(
319               "Serialised Calc ID settings not identical to those recovered from viewport stash",
320               acs.getWsParamFile().equals(oldsettings));
321       JMenu nmenu = new JMenu();
322       new SequenceAnnotationWSClient().attachWSMenuEntry(nmenu,
323               rnaalifoldws, af);
324       assertTrue("Couldn't get menu entry for service",
325               nmenu.getItemCount() > 0);
326       for (Component itm : nmenu.getMenuComponents())
327       {
328         if (itm instanceof JMenuItem)
329         {
330           JMenuItem i = (JMenuItem) itm;
331           if (i.getText().equals(
332                   rnaalifoldws.getAlignAnalysisUI().getAAconToggle()))
333           {
334             i.doClick();
335             break;
336           }
337         }
338       }
339       while (af.getViewport().isCalcInProgress())
340       {
341         try
342         {
343           Thread.sleep(200);
344         } catch (Exception x)
345         {
346         }
347         ;
348       }
349       AutoCalcSetting acs2 = af.getViewport().getCalcIdSettingsFor(
350               alifoldClient.getCalcId());
351       assertTrue(
352               "Calc ID settings after recalculation has not been recovered.",
353               acs2.getWsParamFile().equals(oldsettings));
354     }
355   }
356 }