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