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