JAL-3949 - refactor logging from jalview.bin.Cache to jalview.bin.Console
[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
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertTrue;
27
28 import jalview.bin.Cache;
29 import jalview.bin.Console;
30 import jalview.datamodel.AlignmentAnnotation;
31 import jalview.datamodel.AlignmentI;
32 import jalview.gui.JvOptionPane;
33 import jalview.io.AnnotationFile;
34 import jalview.io.DataSourceType;
35 import jalview.io.FileFormat;
36 import jalview.io.FormatAdapter;
37 import jalview.io.StockholmFileTest;
38 import jalview.ws.jws2.AADisorderClient;
39 import jalview.ws.jws2.Jws2Discoverer;
40 import jalview.ws.jws2.jabaws2.Jws2Instance;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import org.testng.Assert;
46 import org.testng.annotations.AfterClass;
47 import org.testng.annotations.BeforeClass;
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<Jws2Instance> iupreds;
70
71   jalview.ws.jws2.AADisorderClient 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     Console.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     iupreds = new ArrayList<Jws2Instance>();
89     for (Jws2Instance svc : disc.getServices())
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 AADisorderClient(iupreds.get(0), af, null, null);
121     af.getViewport().getCalcManager().startWorker(disorderClient);
122     do
123     {
124       try
125       {
126         Thread.sleep(50);
127       } catch (InterruptedException x)
128       {
129       }
130       ;
131     } while (af.getViewport().getCalcManager().isWorking());
132     AlignmentI orig_alig = af.getViewport().getAlignment();
133     // NOTE: Consensus annotation row cannot be exported and reimported
134     // faithfully - so we remove them
135     List<AlignmentAnnotation> toremove = new ArrayList<AlignmentAnnotation>();
136     for (AlignmentAnnotation aa : orig_alig.getAlignmentAnnotation())
137     {
138       if (aa.autoCalculated)
139       {
140         toremove.add(aa);
141       }
142     }
143     for (AlignmentAnnotation aa : toremove)
144     {
145       orig_alig.deleteAnnotation(aa);
146     }
147     checkAnnotationFileIO("Testing IUPred Annotation IO", orig_alig);
148
149   }
150
151   static void checkAnnotationFileIO(String testname, AlignmentI al)
152   {
153     try
154     {
155       String aligfileout = FileFormat.Pfam.getWriter(al).print(
156               al.getSequencesArray(), true);
157       String anfileout = new AnnotationFile()
158               .printAnnotationsForAlignment(al);
159       assertTrue(
160               "Test "
161                       + testname
162                       + "\nAlignment annotation file was not regenerated. Null string",
163               anfileout != null);
164       assertTrue(
165               "Test "
166                       + testname
167                       + "\nAlignment annotation file was not regenerated. Empty string",
168               anfileout.length() > "JALVIEW_ANNOTATION".length());
169
170       System.out.println("Output annotation file:\n" + anfileout
171               + "\n<<EOF\n");
172
173       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
174               DataSourceType.PASTE, FileFormat.Pfam);
175       assertTrue(
176               "Test "
177                       + testname
178                       + "\nregenerated annotation file did not annotate alignment.",
179               new AnnotationFile().readAnnotationFile(al_new, anfileout,
180                       DataSourceType.PASTE));
181
182       // test for consistency in io
183       StockholmFileTest.testAlignmentEquivalence(al, al_new, true, false,
184               false);
185       return;
186     } catch (Exception e)
187     {
188       e.printStackTrace();
189     }
190     Assert.fail("Test "
191             + testname
192             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
193   }
194
195 }