JAL-1358 RNAStructExportImport test file mirrors DisorderAnnotExportImport file....
[jalview.git] / test / jalview / ws / jabaws / RNAStructExportImport.java
1 package jalview.ws.jabaws;
2
3 import static org.junit.Assert.*;
4
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.List;
8 import java.util.Vector;
9
10 import jalview.api.AlignCalcManagerI;
11 import jalview.datamodel.AlignmentAnnotation;
12 import jalview.datamodel.AlignmentI;
13 import jalview.datamodel.Annotation;
14 import jalview.io.AnnotationFile;
15 import jalview.io.FormatAdapter;
16 import jalview.io.StockholmFileTest;
17 import jalview.ws.jws2.AADisorderClient;
18 import jalview.ws.jws2.Jws2Discoverer;
19 import jalview.ws.jws2.RNAalifoldClient;
20 import jalview.ws.jws2.jabaws2.Jws2Instance;
21
22 import org.junit.AfterClass;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25
26 public class RNAStructExportImport 
27 {
28   public static String testseqs = "examples/unfolded_RF00031.aln";
29
30   public static Jws2Discoverer disc;
31
32   public static Jws2Instance rnaalifoldws;
33
34   jalview.ws.jws2.RNAalifoldClient alifoldClient;
35
36   public static jalview.gui.AlignFrame af = null;
37
38   @BeforeClass
39   public static void setUpBeforeClass() throws Exception
40   {
41         
42         
43         System.out.println("test1");
44
45     jalview.bin.Cache.initLogger();
46     disc = JalviewJabawsTestUtils.getJabawsDiscoverer();
47     
48     System.out.println("test2");
49     
50     for (Jws2Instance svc : disc.getServices())
51     {
52         
53         System.out.println("Service type: " + svc.serviceType);
54         
55       if (svc.getServiceTypeURI().toLowerCase().contains("rnaalifoldws"))
56       {
57         rnaalifoldws = svc;
58       }
59     }
60     
61     System.out.println("State of rnaalifoldws: " + rnaalifoldws);
62     
63     if (rnaalifoldws == null) System.exit(0);
64     
65     System.out.println("test3");
66     
67     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
68     
69     // Following this method a long way we find some (probably important!)
70     //  code that I have just commented out!
71     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.FormatAdapter.FILE);
72     
73     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
74     
75     System.out.println("test5");
76     
77   }
78
79   @AfterClass
80   public static void tearDownAfterClass() throws Exception
81   {
82     if (af != null)
83     {
84       af.setVisible(false);
85       af.dispose();
86     }
87   }
88
89   /**
90    * test for patches to JAL-1294
91    */
92   @Test
93   public void testRNAStructExport()
94   {
95     alifoldClient = new RNAalifoldClient(rnaalifoldws, af, null, null);
96     
97     System.out.println("Service action text:\n" + alifoldClient.getServiceActionText());
98     
99     
100     System.out.println("START FOLDING");
101     af.getViewport().getCalcManager().startWorker(alifoldClient);
102     
103     
104     do
105     {
106       try
107       {
108         Thread.sleep(50);
109       } catch (InterruptedException x)
110       {
111       }
112       ;
113     } while (af.getViewport().getCalcManager().isWorking());
114     
115     System.out.println("END FOLDING");
116     
117     // ALL FOR TESTING
118     AlignCalcManagerI test = af.getViewport().getCalcManager();
119     RNAalifoldClient testWorker = ((RNAalifoldClient)test.getRegisteredWorkersOfClass(RNAalifoldClient.class).get(0));
120     testWorker.updateResultAnnotation(true);
121     System.out.println("Annotation from RNAalifoldclient");
122     for (Annotation ann : testWorker.ourAnnots.get(0).annotations) {
123         System.out.print(ann.toString()+"|");
124     }
125     System.out.println();
126     
127     
128     // Why are the AlignViewport.alignment and the RNAalifoldClient alignment
129     //  Annotations different
130     AlignmentI orig_alig = af.getViewport().getAlignment();
131     
132     System.out.println("orig_alig has class: " + orig_alig.getClass());
133     
134     // some time before here but after the RNAalifoldClient Update method
135     //  the alignment annotation is replaced....
136     
137     System.out.println("orig_alig annotation:\n");
138     for (AlignmentAnnotation an : orig_alig.getAlignmentAnnotation()) {
139         for (Annotation ann : an.annotations) {
140                 System.out.print(ann.toString()+"|");
141         }
142         System.out.println();
143     }
144     
145     
146     testAnnotationFileIO("Testing RNAalifold Annotation IO", orig_alig);
147
148   }
149
150   public static void testAnnotationFileIO(String testname, AlignmentI al)
151   {
152     try
153     {
154       String aligfileout = new FormatAdapter().formatSequences("CLUSTAL",
155               al.getSequencesArray());
156       
157       // test
158 //      System.out.println("aligfileout:\n" + aligfileout);
159       
160       String anfileout = new AnnotationFile().printAnnotations(
161               al.getAlignmentAnnotation(), al.getGroups(),
162               al.getProperties());
163       assertTrue(
164               "Test "
165                       + testname
166                       + "\nAlignment annotation file was not regenerated. Null string",
167               anfileout != null);
168       assertTrue(
169               "Test "
170                       + testname
171                       + "\nAlignment annotation file was not regenerated. Empty string",
172               anfileout.length() > "JALVIEW_ANNOTATION".length());
173
174       System.out.println("Output annotation file:\n" + anfileout
175               + "\n<<EOF\n");
176
177       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
178               FormatAdapter.PASTE, "CLUSTAL");
179       assertTrue(
180               "Test "
181                       + testname
182                       + "\nregenerated annotation file did not annotate alignment.",
183               new AnnotationFile().readAnnotationFile(al_new, anfileout,
184                       FormatAdapter.PASTE));
185
186       // test for consistency in io
187       StockholmFileTest.testAlignmentEquivalence(al, al_new);
188       return;
189     } catch (Exception e)
190     {
191       e.printStackTrace();
192     }
193     fail("Test "
194             + testname
195             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
196   }
197
198 }