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