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