JAL-3848 - actually test the Slivka and JABAWS disorder services - nb still need...
[jalview.git] / test / jalview / ws / jabaws / DisorderAnnotExportImport.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.ws.api.ServiceWithParameters;
36 import jalview.ws.jws2.Jws2Discoverer;
37 import jalview.ws.jws2.SeqAnnotationServiceCalcWorker;
38 import jalview.ws.slivkaws.SlivkaWSDiscoverer;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import org.testng.Assert;
44 import org.testng.annotations.AfterClass;
45 import org.testng.annotations.BeforeClass;
46 import org.testng.annotations.DataProvider;
47 import org.testng.annotations.Test;
48
49 /*
50  * All methods in this class are set to the Network group because setUpBeforeClass will fail
51  * if there is no network.
52  */
53 @Test(singleThreaded = true)
54 public class DisorderAnnotExportImport
55 {
56
57   @BeforeClass(alwaysRun = true)
58   public void setUpJvOptionPane()
59   {
60     JvOptionPane.setInteractiveMode(false);
61     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
62   }
63
64   public static String testseqs = "examples/uniref50.fa";
65
66   public static Jws2Discoverer disc;
67
68   public static List<ServiceWithParameters> iupreds;
69
70   jalview.ws.jws2.SeqAnnotationServiceCalcWorker disorderClient;
71
72   public static jalview.gui.AlignFrame af = null;
73
74   @BeforeClass(alwaysRun = true)
75   public static void setUpBeforeClass() throws Exception
76   {
77     Cache.loadProperties("test/jalview/io/testProps.jvprops");
78     Cache.initLogger();
79     disc = JalviewJabawsTestUtils.getJabawsDiscoverer();
80
81     while (disc.isRunning())
82     {
83       // don't get services until discoverer has finished
84       Thread.sleep(100);
85     }
86     SlivkaWSDiscoverer disc2 = SlivkaWSDiscoverer.getInstance();
87     disc2.startDiscoverer();
88     while (disc2.isRunning())
89     {
90       Thread.sleep(100);
91     }
92     iupreds = new ArrayList<>();
93     for (ServiceWithParameters svc : disc2.getServices())
94     {
95       if (svc.getNameURI().toLowerCase().contains("iupred"))
96       {
97         iupreds.add(svc);
98       }
99     }
100     assertTrue("Couldn't discover any IUPred services to use to test.",
101             iupreds.size() > 0);
102     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
103     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.DataSourceType.FILE);
104     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
105   }
106
107   @AfterClass(alwaysRun = true)
108   public static void tearDownAfterClass() throws Exception
109   {
110     if (af != null)
111     {
112       af.setVisible(false);
113       af.dispose();
114       af = null;
115     }
116   }
117   
118   @DataProvider(name="getIuPreds",parallel = false)
119   public static ServiceWithParameters[][] getIuPreds()
120   {
121     ServiceWithParameters[][] services = new ServiceWithParameters[iupreds
122             .size()][1];
123
124     int i = 0;
125     for (ServiceWithParameters iupred : iupreds)
126     {
127       services[i++][0] = iupred;
128     }
129     return services;
130   }
131
132   /**
133    * test for patches to JAL-1294
134    */
135   @Test(groups = { "External", "Network" },dataProvider = "getIuPreds")
136   public void testDisorderAnnotExport(ServiceWithParameters iuPred)
137   {
138     disorderClient = new SeqAnnotationServiceCalcWorker(iuPred, af, null,
139             null);
140     af.getViewport().getCalcManager().startWorker(disorderClient);
141     do
142     {
143       try
144       {
145         Thread.sleep(50);
146       } catch (InterruptedException x)
147       {
148       }
149       ;
150     } while (af.getViewport().getCalcManager().isWorking());
151     AlignmentI orig_alig = af.getViewport().getAlignment();
152     // NOTE: Consensus annotation row cannot be exported and reimported
153     // faithfully - so we remove them
154     List<AlignmentAnnotation> toremove = new ArrayList<>();
155     for (AlignmentAnnotation aa : orig_alig.getAlignmentAnnotation())
156     {
157       if (aa.autoCalculated)
158       {
159         toremove.add(aa);
160       }
161     }
162     for (AlignmentAnnotation aa : toremove)
163     {
164       orig_alig.deleteAnnotation(aa);
165     }
166     checkAnnotationFileIO("Testing IUPred Annotation IO", orig_alig);
167
168   }
169
170   static void checkAnnotationFileIO(String testname, AlignmentI al)
171   {
172     try
173     {
174       String aligfileout = FileFormat.Pfam.getWriter(al).print(
175               al.getSequencesArray(), true);
176       String anfileout = new AnnotationFile()
177               .printAnnotationsForAlignment(al);
178       assertTrue(
179               "Test "
180                       + testname
181                       + "\nAlignment annotation file was not regenerated. Null string",
182               anfileout != null);
183       assertTrue(
184               "Test "
185                       + testname
186                       + "\nAlignment annotation file was not regenerated. Empty string",
187               anfileout.length() > "JALVIEW_ANNOTATION".length());
188
189       System.out.println("Output annotation file:\n" + anfileout
190               + "\n<<EOF\n");
191
192       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
193               DataSourceType.PASTE, FileFormat.Pfam);
194       assertTrue(
195               "Test "
196                       + testname
197                       + "\nregenerated annotation file did not annotate alignment.",
198               new AnnotationFile().readAnnotationFile(al_new, anfileout,
199                       DataSourceType.PASTE));
200
201       // test for consistency in io
202       StockholmFileTest.testAlignmentEquivalence(al, al_new, true, false,
203               false);
204       return;
205     } catch (Exception e)
206     {
207       e.printStackTrace();
208     }
209     Assert.fail("Test "
210             + testname
211             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
212   }
213
214 }