JAL-2326 added setup method for JvOptionPane in all Jalveiw test classes to enable...
[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.FormatAdapter;
32 import jalview.io.StockholmFileTest;
33 import jalview.ws.jws2.AADisorderClient;
34 import jalview.ws.jws2.Jws2Discoverer;
35 import jalview.ws.jws2.jabaws2.Jws2Instance;
36
37 import java.util.ArrayList;
38 import java.util.List;
39
40 import org.testng.Assert;
41 import org.testng.annotations.AfterClass;
42 import org.testng.annotations.BeforeClass;
43 import org.testng.annotations.Test;
44
45 @Test(groups = { "External" })
46 public class DisorderAnnotExportImport
47 {
48
49   @BeforeClass(alwaysRun = true)
50   public void setUpJvOptionPane()
51   {
52     JvOptionPane.setInteractiveMode(false);
53     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
54   }
55
56   public static String testseqs = "examples/uniref50.fa";
57
58   public static Jws2Discoverer disc;
59
60   public static List<Jws2Instance> iupreds;
61
62   jalview.ws.jws2.AADisorderClient disorderClient;
63
64   public static jalview.gui.AlignFrame af = null;
65
66   @BeforeClass(inheritGroups = true)
67   public static void setUpBeforeClass() throws Exception
68   {
69     Cache.loadProperties("test/jalview/io/testProps.jvprops");
70     Cache.initLogger();
71     disc = JalviewJabawsTestUtils.getJabawsDiscoverer();
72     iupreds = new ArrayList<Jws2Instance>();
73     for (Jws2Instance svc : disc.getServices())
74     {
75       if (svc.getServiceTypeURI().toLowerCase().contains("iupredws"))
76       {
77         iupreds.add(svc);
78       }
79     }
80     assertTrue("Couldn't discover any IUPred services to use to test.",
81             iupreds.size() > 0);
82     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
83     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.FormatAdapter.FILE);
84     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
85   }
86
87   @AfterClass(alwaysRun = true)
88   public static void tearDownAfterClass() throws Exception
89   {
90     if (af != null)
91     {
92       af.setVisible(false);
93       af.dispose();
94       af = null;
95     }
96   }
97
98   /**
99    * test for patches to JAL-1294
100    */
101   @Test
102   public void testDisorderAnnotExport()
103   {
104     disorderClient = new AADisorderClient(iupreds.get(0), af, null, null);
105     af.getViewport().getCalcManager().startWorker(disorderClient);
106     do
107     {
108       try
109       {
110         Thread.sleep(50);
111       } catch (InterruptedException x)
112       {
113       }
114       ;
115     } while (af.getViewport().getCalcManager().isWorking());
116     AlignmentI orig_alig = af.getViewport().getAlignment();
117     // NOTE: Consensus annotation row cannot be exported and reimported
118     // faithfully - so we remove them
119     List<AlignmentAnnotation> toremove = new ArrayList<AlignmentAnnotation>();
120     for (AlignmentAnnotation aa : orig_alig.getAlignmentAnnotation())
121     {
122       if (aa.autoCalculated)
123       {
124         toremove.add(aa);
125       }
126     }
127     for (AlignmentAnnotation aa : toremove)
128     {
129       orig_alig.deleteAnnotation(aa);
130     }
131     checkAnnotationFileIO("Testing IUPred Annotation IO", orig_alig);
132
133   }
134
135   static void checkAnnotationFileIO(String testname, AlignmentI al)
136   {
137     try
138     {
139       String aligfileout = new FormatAdapter().formatSequences("PFAM",
140               al.getSequencesArray());
141       String anfileout = new AnnotationFile()
142               .printAnnotationsForAlignment(al);
143       assertTrue(
144               "Test "
145                       + testname
146                       + "\nAlignment annotation file was not regenerated. Null string",
147               anfileout != null);
148       assertTrue(
149               "Test "
150                       + testname
151                       + "\nAlignment annotation file was not regenerated. Empty string",
152               anfileout.length() > "JALVIEW_ANNOTATION".length());
153
154       System.out.println("Output annotation file:\n" + anfileout
155               + "\n<<EOF\n");
156
157       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
158               FormatAdapter.PASTE, "PFAM");
159       assertTrue(
160               "Test "
161                       + testname
162                       + "\nregenerated annotation file did not annotate alignment.",
163               new AnnotationFile().readAnnotationFile(al_new, anfileout,
164                       FormatAdapter.PASTE));
165
166       // test for consistency in io
167       StockholmFileTest.testAlignmentEquivalence(al, al_new, true);
168       return;
169     } catch (Exception e)
170     {
171       e.printStackTrace();
172     }
173     Assert.fail("Test "
174             + testname
175             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
176   }
177
178 }