first jemboss clustalw client for jalview.
[jalview.git] / src / jalview / ws / Jemboss.java
1 package jalview.ws;
2
3 /**
4  * <p>Title: JalviewX</p>
5  * <p>Description: Jalview Re-engineered</p>
6  * <p>Copyright: Copyright (c) 2004</p>
7  * <p>Company: Dundee University</p>
8  * @author not attributable
9  * @version 1.0
10  * Commenced 14th January 2005, Jim Procter
11  * Contains and depends on GPL-2 jemboss classes
12  * see http://www.rfcgr.mrc.ac.uk/Software/EMBOSS/Jemboss/index.html
13  */
14
15 /**
16  * Philosophy
17  * Jemboss webservices are implemented here as re-entrant synchronous SOAP
18  * exchanges. It is up to the user of the class whether these exchanges
19  * are executed in their own thread to effect background processing
20  * functionality.
21  *
22  * Things to do
23  * Test threadability
24  * Standardise the exceptions (currently errors are output on stdout)
25  * Allow server configuration
26  */
27
28 import jalview.io.*;
29 import jalview.datamodel.*;
30 import ext.jemboss.*;
31 import ext.jemboss.soap.*;
32 import java.io.*;
33 import java.net.*;
34 import java.util.Hashtable;
35 import java.util.Enumeration;
36 import java.security.Security;
37
38
39 public class Jemboss
40 {
41   private static ext.jemboss.JembossParams vamsas_server;
42
43   public Jemboss()
44   {
45     // Set up default jalview-jemboss server properties
46     //
47     vamsas_server = new JembossParams();
48     System.out.println("Jemboss Server Init\n"
49                        + vamsas_server.serverDescription()
50                        + "\nUser Authorisation ? "
51                        + vamsas_server.getUseAuth()
52                        +"\n");
53   }
54
55   /* public void updateServer(JembossParams props)
56   {
57     vamsas_server.updateJembossPropStrings (
58       props.getEmbossEnvironmentArray());
59   }
60   */
61
62   public SequenceI[] clustalW(SequenceI[] sequences)
63   {
64
65     // Simplest client call - with gumph from jemboss.server.TestPrivateServer
66     if (vamsas_server.getPublicSoapURL().startsWith("https"))
67     {
68       //SSL settings
69       //    System.setProperty ("javax.net.debug", "all");
70       com.sun.net.ssl.internal.ssl.Provider p =
71           new com.sun.net.ssl.internal.ssl.Provider();
72       Security.addProvider(p);
73
74       //have to do it this way to work with JNLP
75       URL.setURLStreamHandlerFactory( new URLStreamHandlerFactory()
76       {
77         public URLStreamHandler createURLStreamHandler(final String protocol)
78         {
79           if(protocol != null && protocol.compareTo("https") == 0)
80           {
81             return new com.sun.net.ssl.internal.www.protocol.https.Handler();
82           }
83           return null;
84         }
85       });
86
87       //location of keystore
88       System.setProperty("javax.net.ssl.trustStore",
89                          "resources/client.keystore");
90
91       String jembossClientKeyStore = System.getProperty("user.home") +
92           "/.jembossClientKeystore";
93
94       try
95       {
96         new JembossJarUtil("resources/client.jar").writeByteFile(
97       "client.keystore",jembossClientKeyStore);
98         System.setProperty("javax.net.ssl.trustStore",
99                            jembossClientKeyStore);
100       }
101       catch(Exception exp){}
102
103     }
104
105     Hashtable filesToMove = new Hashtable();
106     String embossCommand = "emma -sequence jalseqs.fasta -auto";
107     // Load sequence file into hash
108     filesToMove.put("jalseqs.fasta",
109                     jalview.io.FastaFile.print(sequences,124,false).getBytes());
110
111     if(vamsas_server.getUseAuth() == true)
112       if(vamsas_server.getServiceUserName() == null)
113         System.out.println("jalview.Jemboss: OOPS! Authentication required!");
114
115     // submit to private server
116     try
117     {
118       JembossRun thisrun = new JembossRun(embossCommand,"",
119                                           filesToMove,vamsas_server);
120       Hashtable h = thisrun.hash();
121       Enumeration enum = h.keys();
122       // Find the alignment generated and parse it
123       while (enum.hasMoreElements())
124       {
125         String thiskey = (String)enum.nextElement().toString();
126         if (thiskey.endsWith(".aln"))
127         {
128           // FormatAdapter, and IdentifyFile use a protocol string
129           // to work out what 'file' is - file/url/Alignmentasastring
130           String alfile = h.get(thiskey).toString();
131           String format = IdentifyFile.Identify(alfile, "Paste");
132           SequenceI[] alignment = null;
133
134           if (FormatProperties.contains(format))
135             alignment = FormatAdapter.read(alfile, "Paste", format);
136           else
137             System.out.println("jalview.Jemboss: Unknown format "+format+"\n");
138           if (alignment == null)
139             System.out.println("jalview.Jemboss: Couldn't read response:\n"
140                                + alfile + "\n---EOF\n");
141           else
142             return alignment;
143         }
144
145       }
146
147     }
148     catch (JembossSoapException eae)
149     {
150       System.out.println("jalview.Jemboss: SOAP ERROR :"+embossCommand
151                          + "\n" + eae);
152     }
153     catch (Exception e) {
154       System.out.println("jalview.Jemboss: Some other failure :\n"
155                          +e.getMessage()+"\n");
156     }
157
158     return null;
159   }
160 }
161