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