JAL-1432 updated copyright notices
[jalview.git] / test / jalview / ws / jabaws / DisorderAnnotExportImport.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.ws.jabaws;
20
21 import static org.junit.Assert.*;
22
23 import java.util.ArrayList;
24 import java.util.List;
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 org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38
39 public class DisorderAnnotExportImport
40 {
41   public static String testseqs = "examples/uniref50.fa";
42
43   public static Jws2Discoverer disc;
44
45   public static List<Jws2Instance> iupreds;
46
47   jalview.ws.jws2.AADisorderClient disorderClient;
48
49   public static jalview.gui.AlignFrame af = null;
50
51   @BeforeClass
52   public static void setUpBeforeClass() throws Exception
53   {
54
55     jalview.bin.Cache.initLogger();
56     disc = JalviewJabawsTestUtils.getJabawsDiscoverer();
57     iupreds = new ArrayList<Jws2Instance>();
58     for (Jws2Instance svc : disc.getServices())
59     {
60       if (svc.getServiceTypeURI().toLowerCase().contains("iupredws"))
61       {
62         iupreds.add(svc);
63       }
64     }
65     assertTrue("Couldn't discover any IUPred services to use to test.",
66             iupreds.size() > 0);
67     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
68     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.FormatAdapter.FILE);
69     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
70   }
71
72   @AfterClass
73   public static void tearDownAfterClass() throws Exception
74   {
75     if (af != null)
76     {
77       af.setVisible(false);
78       af.dispose();
79     }
80   }
81
82   /**
83    * test for patches to JAL-1294
84    */
85   @Test
86   public void testDisorderAnnotExport()
87   {
88     disorderClient = new AADisorderClient(iupreds.get(0), af, null, null);
89     af.getViewport().getCalcManager().startWorker(disorderClient);
90     do
91     {
92       try
93       {
94         Thread.sleep(50);
95       } catch (InterruptedException x)
96       {
97       }
98       ;
99     } while (af.getViewport().getCalcManager().isWorking());
100     AlignmentI orig_alig = af.getViewport().getAlignment();
101     // NOTE: Consensus annotation row cannot be exported and reimported faithfully - so we remove them
102     List<AlignmentAnnotation> toremove = new ArrayList<AlignmentAnnotation>();
103     for (AlignmentAnnotation aa:orig_alig.getAlignmentAnnotation())
104     {
105       if (aa.autoCalculated)
106       {
107         toremove.add(aa);
108       }
109     }
110     for (AlignmentAnnotation aa:toremove)
111     {
112       orig_alig.deleteAnnotation(aa);
113     }
114     testAnnotationFileIO("Testing IUPred Annotation IO", orig_alig);
115
116   }
117
118   public static void testAnnotationFileIO(String testname, AlignmentI al)
119   {
120     try
121     {
122       String aligfileout = new FormatAdapter().formatSequences("PFAM",
123               al.getSequencesArray());
124       String anfileout = new AnnotationFile().printAnnotations(
125               al.getAlignmentAnnotation(), al.getGroups(),
126               al.getProperties());
127       assertTrue(
128               "Test "
129                       + testname
130                       + "\nAlignment annotation file was not regenerated. Null string",
131               anfileout != null);
132       assertTrue(
133               "Test "
134                       + testname
135                       + "\nAlignment annotation file was not regenerated. Empty string",
136               anfileout.length() > "JALVIEW_ANNOTATION".length());
137
138       System.out.println("Output annotation file:\n" + anfileout
139               + "\n<<EOF\n");
140
141       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
142               FormatAdapter.PASTE, "PFAM");
143       assertTrue(
144               "Test "
145                       + testname
146                       + "\nregenerated annotation file did not annotate alignment.",
147               new AnnotationFile().readAnnotationFile(al_new, anfileout,
148                       FormatAdapter.PASTE));
149
150       // test for consistency in io
151       StockholmFileTest.testAlignmentEquivalence(al, al_new);
152       return;
153     } catch (Exception e)
154     {
155       e.printStackTrace();
156     }
157     fail("Test "
158             + testname
159             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
160   }
161
162 }