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