fa19d00a57587efa2d4505ccc9ae5f47964bcb63
[jalview.git] / test / jalview / ws / jabaws / DisorderAnnotExportImport.java
1 package jalview.ws.jabaws;
2
3 import static org.junit.Assert.*;
4
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import jalview.datamodel.AlignmentAnnotation;
9 import jalview.datamodel.AlignmentI;
10 import jalview.io.AnnotationFile;
11 import jalview.io.FormatAdapter;
12 import jalview.io.StockholmFileTest;
13 import jalview.ws.jws2.AADisorderClient;
14 import jalview.ws.jws2.Jws2Discoverer;
15 import jalview.ws.jws2.jabaws2.Jws2Instance;
16
17 import org.junit.AfterClass;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20
21 public class DisorderAnnotExportImport
22 {
23   public static String testseqs = "examples/uniref50.fa";
24
25   public static Jws2Discoverer disc;
26
27   public static List<Jws2Instance> iupreds;
28
29   jalview.ws.jws2.AADisorderClient disorderClient;
30
31   public static jalview.gui.AlignFrame af = null;
32
33   @BeforeClass
34   public static void setUpBeforeClass() throws Exception
35   {
36
37     jalview.bin.Cache.initLogger();
38     disc = JalviewJabawsTestUtils.getJabawsDiscoverer();
39     iupreds = new ArrayList<Jws2Instance>();
40     for (Jws2Instance svc : disc.getServices())
41     {
42       if (svc.getServiceTypeURI().toLowerCase().contains("iupredws"))
43       {
44         iupreds.add(svc);
45       }
46     }
47     assertTrue("Couldn't discover any IUPred services to use to test.",
48             iupreds.size() > 0);
49     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
50     af = fl.LoadFileWaitTillLoaded(testseqs, jalview.io.FormatAdapter.FILE);
51     assertNotNull("Couldn't load test data ('" + testseqs + "')", af);
52   }
53
54   @AfterClass
55   public static void tearDownAfterClass() throws Exception
56   {
57     if (af != null)
58     {
59       af.setVisible(false);
60       af.dispose();
61     }
62   }
63
64   /**
65    * test for patches to JAL-1294
66    */
67   @Test
68   public void testDisorderAnnotExport()
69   {
70     disorderClient = new AADisorderClient(iupreds.get(0), af, null, null);
71     af.getViewport().getCalcManager().startWorker(disorderClient);
72     do
73     {
74       try
75       {
76         Thread.sleep(50);
77       } catch (InterruptedException x)
78       {
79       }
80       ;
81     } while (af.getViewport().getCalcManager().isWorking());
82     AlignmentI orig_alig = af.getViewport().getAlignment();
83     // NOTE: Consensus annotation row cannot be exported and reimported faithfully - so we remove them
84     List<AlignmentAnnotation> toremove = new ArrayList<AlignmentAnnotation>();
85     for (AlignmentAnnotation aa:orig_alig.getAlignmentAnnotation())
86     {
87       if (aa.autoCalculated)
88       {
89         toremove.add(aa);
90       }
91     }
92     for (AlignmentAnnotation aa:toremove)
93     {
94       orig_alig.deleteAnnotation(aa);
95     }
96     testAnnotationFileIO("Testing IUPred Annotation IO", orig_alig);
97
98   }
99
100   public static void testAnnotationFileIO(String testname, AlignmentI al)
101   {
102     try
103     {
104       String aligfileout = new FormatAdapter().formatSequences("PFAM",
105               al.getSequencesArray());
106       String anfileout = new AnnotationFile().printAnnotations(
107               al.getAlignmentAnnotation(), al.getGroups(),
108               al.getProperties());
109       assertTrue(
110               "Test "
111                       + testname
112                       + "\nAlignment annotation file was not regenerated. Null string",
113               anfileout != null);
114       assertTrue(
115               "Test "
116                       + testname
117                       + "\nAlignment annotation file was not regenerated. Empty string",
118               anfileout.length() > "JALVIEW_ANNOTATION".length());
119
120       System.out.println("Output annotation file:\n" + anfileout
121               + "\n<<EOF\n");
122
123       AlignmentI al_new = new FormatAdapter().readFile(aligfileout,
124               FormatAdapter.PASTE, "PFAM");
125       assertTrue(
126               "Test "
127                       + testname
128                       + "\nregenerated annotation file did not annotate alignment.",
129               new AnnotationFile().readAnnotationFile(al_new, anfileout,
130                       FormatAdapter.PASTE));
131
132       // test for consistency in io
133       StockholmFileTest.testAlignmentEquivalence(al, al_new);
134       return;
135     } catch (Exception e)
136     {
137       e.printStackTrace();
138     }
139     fail("Test "
140             + testname
141             + "\nCouldn't complete Annotation file roundtrip input/output/input test.");
142   }
143
144 }