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