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