ea30e4f80299bd0e8ef8c87706c9090151bced1a
[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.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
87     SlivkaWSDiscoverer disc2 = SlivkaWSDiscoverer.getInstance();
88     disc2.startDiscoverer();
89     while (disc2.isRunning())
90     {
91       if (svc.getServiceTypeURI().toLowerCase(Locale.ROOT).contains("iupredws"))
92       {
93         iupreds.add(svc);
94       }
95     }
96     assertTrue("Couldn't discover any IUPred services to use to test.",
97             iupreds.size() > 0);
98     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
99     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.DataSourceType.FILE);
100     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
101   }
102
103   @AfterClass(alwaysRun = true)
104   public static void tearDownAfterClass() throws Exception
105   {
106     if (af != null)
107     {
108       af.setVisible(false);
109       af.dispose();
110       af = null;
111     }
112   }
113
114   /**
115    * test for patches to JAL-1294
116    */
117   @Test(groups = { "External", "Network" })
118   public void testDisorderAnnotExport()
119   {
120     disorderClient = new SeqAnnotationServiceCalcWorker(iupreds.get(0), af, null,
121             null);
122     af.getViewport().getCalcManager().startWorker(disorderClient);
123     do
124     {
125       try
126       {
127         Thread.sleep(50);
128       } catch (InterruptedException x)
129       {
130       }
131       ;
132     } while (af.getViewport().getCalcManager().isWorking());
133     AlignmentI orig_alig = af.getViewport().getAlignment();
134     // NOTE: Consensus annotation row cannot be exported and reimported
135     // faithfully - so we remove them
136     List<AlignmentAnnotation> toremove = new ArrayList<>();
137     for (AlignmentAnnotation aa : orig_alig.getAlignmentAnnotation())
138     {
139       if (aa.autoCalculated)
140       {
141         toremove.add(aa);
142       }
143     }
144     for (AlignmentAnnotation aa : toremove)
145     {
146       orig_alig.deleteAnnotation(aa);
147     }
148     checkAnnotationFileIO("Testing IUPred Annotation IO", orig_alig);
149
150   }
151
152   static void checkAnnotationFileIO(String testname, AlignmentI al)
153   {
154     try
155     {
156       String aligfileout = FileFormat.Pfam.getWriter(al).print(
157               al.getSequencesArray(), true);
158       String anfileout = new AnnotationFile()
159               .printAnnotationsForAlignment(al);
160       assertTrue(
161               "Test "
162                       + testname
163                       + "\nAlignment annotation file was not regenerated. Null string",
164               anfileout != null);
165       assertTrue(
166               "Test "
167                       + testname
168                       + "\nAlignment annotation file was not regenerated. Empty string",
169               anfileout.length() > "JALVIEW_ANNOTATION".length());
170
171       System.out.println("Output annotation file:\n" + anfileout
172               + "\n<<EOF\n");
173
174       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
175               DataSourceType.PASTE, FileFormat.Pfam);
176       assertTrue(
177               "Test "
178                       + testname
179                       + "\nregenerated annotation file did not annotate alignment.",
180               new AnnotationFile().readAnnotationFile(al_new, anfileout,
181                       DataSourceType.PASTE));
182
183       // test for consistency in io
184       StockholmFileTest.testAlignmentEquivalence(al, al_new, true, false,
185               false);
186       return;
187     } catch (Exception e)
188     {
189       e.printStackTrace();
190     }
191     Assert.fail("Test "
192             + testname
193             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
194   }
195
196 }