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