JAL-3070 slivka service discovery as a background thread - still need to unify Jabaws...
[jalview.git] / test / jalview / ws / jabaws / AAConAnnotAndSettingsIO.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.AlignFrame;
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.ws.api.ServiceWithParameters;
37 import jalview.ws.jws2.Jws2Discoverer;
38 import jalview.ws.jws2.SeqAnnotationServiceCalcWorker;
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> aacon;
68
69   jalview.ws.jws2.SeqAnnotationServiceCalcWorker aaconClient;
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
86     aacon = new ArrayList<>();
87     for (ServiceWithParameters svc : disc.getServices())
88     {
89       if (svc.getNameURI().toLowerCase().contains("aacon"))
90       {
91         aacon.add(svc);
92       }
93     }
94     assertTrue("Couldn't discover any AACon services to use to test.",
95             aacon.size() > 0);
96     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
97     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.DataSourceType.FILE);
98     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
99   }
100
101   @AfterClass(alwaysRun = true)
102   public static void tearDownAfterClass() throws Exception
103   {
104     if (af != null)
105     {
106       af.setVisible(false);
107       af.dispose();
108       af = null;
109     }
110   }
111
112   /**
113    * Run AACon on an alignment with defaults and verify Just Shenkin annotation
114    * appears
115    */
116   @Test(groups = { "External", "Network" })
117   public void testAAConAnnotAndRecovery()
118   {
119     testAAConClient(af, aacon.get(0));
120   }
121
122   static void testAAConClient(AlignFrame af, ServiceWithParameters aacon)
123   {
124     SeqAnnotationServiceCalcWorker aaconClient = new SeqAnnotationServiceCalcWorker(
125             aacon, af, null,
126             null);
127     af.getViewport().getCalcManager().startWorker(aaconClient);
128     do
129     {
130       try
131       {
132         Thread.sleep(50);
133       } catch (InterruptedException x)
134       {
135       }
136       ;
137     } while (af.getViewport().getCalcManager().isWorking());
138     AlignmentI orig_alig = af.getViewport().getAlignment();
139     boolean foundShenkin = false;
140     for (AlignmentAnnotation aa : orig_alig
141             .findAnnotation(aacon.getAlignAnalysisUI().getCalcId()))
142     {
143       assertTrue("AACon annotation not marked as autocalculated!",
144               aa.autoCalculated);
145       if ("shenkin".equals(aa.label.toLowerCase()))
146       {
147         foundShenkin = true;
148         break;
149       }
150     }
151     assertTrue("Failed to locate 'SHENKIN' annotation row.", foundShenkin);
152   }
153
154   static void checkAnnotationFileIO(String testname, AlignmentI al)
155   {
156     try
157     {
158       String aligfileout = FileFormat.Pfam.getWriter(al).print(
159               al.getSequencesArray(), true);
160       String anfileout = new AnnotationFile()
161               .printAnnotationsForAlignment(al);
162       assertTrue(
163               "Test "
164                       + testname
165                       + "\nAlignment annotation file was not regenerated. Null string",
166               anfileout != null);
167       assertTrue(
168               "Test "
169                       + testname
170                       + "\nAlignment annotation file was not regenerated. Empty string",
171               anfileout.length() > "JALVIEW_ANNOTATION".length());
172
173       System.out.println("Output annotation file:\n" + anfileout
174               + "\n<<EOF\n");
175
176       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
177               DataSourceType.PASTE, FileFormat.Pfam);
178       assertTrue(
179               "Test "
180                       + testname
181                       + "\nregenerated annotation file did not annotate alignment.",
182               new AnnotationFile().readAnnotationFile(al_new, anfileout,
183                       DataSourceType.PASTE));
184
185       // test for consistency in io
186       StockholmFileTest.testAlignmentEquivalence(al, al_new, true, false,
187               false);
188       return;
189     } catch (Exception e)
190     {
191       e.printStackTrace();
192     }
193     Assert.fail("Test "
194             + testname
195             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
196   }
197
198 }