RNAalifoldClient updated to be more like AAConClient and now (attempts) to support...
[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     jalview.bin.Cache.initLogger();
44     disc = JalviewJabawsTestUtils.getJabawsDiscoverer();
45     
46     for (Jws2Instance svc : disc.getServices())
47     {
48         
49         System.out.println("Service type: " + svc.serviceType);
50         
51       if (svc.getServiceTypeURI().toLowerCase().contains("rnaalifoldws"))
52       {
53         rnaalifoldws = svc;
54       }
55     }
56     
57     System.out.println("State of rnaalifoldws: " + rnaalifoldws);
58     
59     if (rnaalifoldws == null) System.exit(0);
60     
61     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
62     
63     // Following this method a long way we find some (probably important!)
64     //  code that I have just commented out!
65     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.FormatAdapter.FILE);
66     
67     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
68     
69   }
70
71   @AfterClass
72   public static void tearDownAfterClass() throws Exception
73   {
74     if (af != null)
75     {
76       af.setVisible(false);
77       af.dispose();
78     }
79   }
80
81   /**
82    * test for patches to JAL-1294
83    */
84   @Test
85   public void testRNAStructExport()
86   {
87     alifoldClient = new RNAalifoldClient(rnaalifoldws, af, null, null);
88     
89     
90     System.out.println("START FOLDING");
91     af.getViewport().getCalcManager().startWorker(alifoldClient);
92     
93     
94     do
95     {
96       try
97       {
98         Thread.sleep(50);
99       } catch (InterruptedException x)
100       {
101       }
102       ;
103     } while (af.getViewport().getCalcManager().isWorking());
104     
105     System.out.println("END FOLDING");
106     
107     // ALL FOR TESTING
108     AlignCalcManagerI test = af.getViewport().getCalcManager();
109     RNAalifoldClient testWorker = ((RNAalifoldClient)test.getRegisteredWorkersOfClass(RNAalifoldClient.class).get(0));
110     testWorker.updateResultAnnotation(true);
111     System.out.println("Annotation from RNAalifoldclient");
112     for (Annotation ann : testWorker.ourAnnots.get(0).annotations) {
113         System.out.print(ann.toString()+"|");
114     }
115     System.out.println();
116     
117     
118     // Why are the AlignViewport.alignment and the RNAalifoldClient alignment
119     //  Annotations different
120     AlignmentI orig_alig = af.getViewport().getAlignment();
121     
122     System.out.println("orig_alig has class: " + orig_alig.getClass());
123     
124     // some time before here but after the RNAalifoldClient Update method
125     //  the alignment annotation is replaced....
126     
127     System.out.println("orig_alig annotation:\n");
128     for (AlignmentAnnotation an : orig_alig.getAlignmentAnnotation()) {
129         for (Annotation ann : an.annotations) {
130                 System.out.print(ann.toString()+"|");
131         }
132         System.out.println();
133     }
134     
135     
136     testAnnotationFileIO("Testing RNAalifold Annotation IO", orig_alig);
137
138   }
139
140   public static void testAnnotationFileIO(String testname, AlignmentI al)
141   {
142     try
143     {
144       String aligfileout = new FormatAdapter().formatSequences("CLUSTAL",
145               al.getSequencesArray());
146       
147       // test
148 //      System.out.println("aligfileout:\n" + aligfileout);
149       
150       String anfileout = new AnnotationFile().printAnnotations(
151               al.getAlignmentAnnotation(), al.getGroups(),
152               al.getProperties());
153       assertTrue(
154               "Test "
155                       + testname
156                       + "\nAlignment annotation file was not regenerated. Null string",
157               anfileout != null);
158       assertTrue(
159               "Test "
160                       + testname
161                       + "\nAlignment annotation file was not regenerated. Empty string",
162               anfileout.length() > "JALVIEW_ANNOTATION".length());
163
164       System.out.println("Output annotation file:\n" + anfileout
165               + "\n<<EOF\n");
166
167       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
168               FormatAdapter.PASTE, "CLUSTAL");
169       assertTrue(
170               "Test "
171                       + testname
172                       + "\nregenerated annotation file did not annotate alignment.",
173               new AnnotationFile().readAnnotationFile(al_new, anfileout,
174                       FormatAdapter.PASTE));
175
176       // test for consistency in io
177       StockholmFileTest.testAlignmentEquivalence(al, al_new);
178       return;
179     } catch (Exception e)
180     {
181       e.printStackTrace();
182     }
183     fail("Test "
184             + testname
185             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
186   }
187
188 }