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