JAL-1358 Cleaned up RNAalifoldClient.java. RNA helices colouring bug and parameters...
[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       if (svc.getServiceTypeURI().toLowerCase().contains("rnaalifoldws"))
50       {
51         rnaalifoldws = svc;
52       }
53     }
54     
55     System.out.println("State of rnaalifoldws: " + rnaalifoldws);
56     
57     if (rnaalifoldws == null) System.exit(0);
58     
59     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
60     
61     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.FormatAdapter.FILE);
62     
63     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
64     
65   }
66
67   @AfterClass
68   public static void tearDownAfterClass() throws Exception
69   {
70     if (af != null)
71     {
72       af.setVisible(false);
73       af.dispose();
74     }
75   }
76
77   @Test
78   public void testRNAStructExport()
79   {
80         
81         
82     alifoldClient = new RNAalifoldClient(rnaalifoldws, af, null, null);
83     
84     af.getViewport().getCalcManager().startWorker(alifoldClient);
85     
86     
87     do
88     {
89       try
90       {
91         Thread.sleep(50);
92       } catch (InterruptedException x)
93       {
94       }
95       ;
96     } while (af.getViewport().getCalcManager().isWorking());
97     
98     
99     AlignmentI orig_alig = af.getViewport().getAlignment();
100     
101     testAnnotationFileIO("Testing RNAalifold Annotation IO", orig_alig);
102
103   }
104
105   public static void testAnnotationFileIO(String testname, AlignmentI al)
106   {
107     try
108     {
109         // what format would be appropriate for RNAalifold annotations?
110       String aligfileout = new FormatAdapter().formatSequences("PFAM",
111               al.getSequencesArray());
112       
113       
114       String anfileout = new AnnotationFile().printAnnotations(
115               al.getAlignmentAnnotation(), al.getGroups(),
116               al.getProperties());
117       assertTrue(
118               "Test "
119                       + testname
120                       + "\nAlignment annotation file was not regenerated. Null string",
121               anfileout != null);
122       assertTrue(
123               "Test "
124                       + testname
125                       + "\nAlignment annotation file was not regenerated. Empty string",
126               anfileout.length() > "JALVIEW_ANNOTATION".length());
127
128       System.out.println("Output annotation file:\n" + anfileout
129               + "\n<<EOF\n");
130
131       // again what format would be appropriate?
132       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
133               FormatAdapter.PASTE, "PFAM");
134       assertTrue(
135               "Test "
136                       + testname
137                       + "\nregenerated annotation file did not annotate alignment.",
138               new AnnotationFile().readAnnotationFile(al_new, anfileout,
139                       FormatAdapter.PASTE));
140
141       // test for consistency in io
142       StockholmFileTest.testAlignmentEquivalence(al, al_new);
143       return;
144     } catch (Exception e)
145     {
146       e.printStackTrace();
147     }
148     fail("Test "
149             + testname
150             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
151   }
152
153 }