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