4a5257d86804656a57ab5a37c8fd2ba960295ab5
[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 java.net.*;
29 import java.security.*;
30 import java.util.*;
31
32 import ext.jemboss.*;
33 import ext.jemboss.soap.*;
34 import jalview.datamodel.*;
35 import jalview.io.*;
36
37
38 public class Jemboss
39 {
40   private static ext.jemboss.JembossParams vamsas_server;
41
42   public Jemboss()
43   {
44     // Set up default jalview-jemboss server properties
45     //
46     vamsas_server = new JembossParams();
47     System.out.println("Jemboss Server Init\n"
48                        + vamsas_server.serverDescription()
49                        + "\nUser Authorisation ? "
50                        + vamsas_server.getUseAuth()
51                        +"\n");
52   }
53
54   /* public void updateServer(JembossParams props)
55   {
56     vamsas_server.updateJembossPropStrings (
57       props.getEmbossEnvironmentArray());
58   }
59   */
60
61   public SequenceI[] clustalW(SequenceI[] sequences)
62   {
63
64     // Simplest client call - with gumph from jemboss.server.TestPrivateServer
65     if (vamsas_server.getPublicSoapURL().startsWith("https"))
66     {
67       //SSL settings
68       //    System.setProperty ("javax.net.debug", "all");
69       com.sun.net.ssl.internal.ssl.Provider p =
70           new com.sun.net.ssl.internal.ssl.Provider();
71       Security.addProvider(p);
72
73       //have to do it this way to work with JNLP
74       URL.setURLStreamHandlerFactory( new URLStreamHandlerFactory()
75       {
76         public URLStreamHandler createURLStreamHandler(final String protocol)
77         {
78           if(protocol != null && protocol.compareTo("https") == 0)
79           {
80             return new com.sun.net.ssl.internal.www.protocol.https.Handler();
81           }
82           return null;
83         }
84       });
85
86       //location of keystore
87       System.setProperty("javax.net.ssl.trustStore",
88                          "resources/client.keystore");
89
90       String jembossClientKeyStore = System.getProperty("user.home") +
91           "/.jembossClientKeystore";
92
93       try
94       {
95         new JembossJarUtil("resources/client.jar").writeByteFile(
96       "client.keystore",jembossClientKeyStore);
97         System.setProperty("javax.net.ssl.trustStore",
98                            jembossClientKeyStore);
99       }
100       catch(Exception exp){}
101
102     }
103
104     Hashtable filesToMove = new Hashtable();
105     String embossCommand = "emma -sequence jalseqs.fasta -auto";
106     // Load sequence file into hash
107     filesToMove.put("jalseqs.fasta",
108                     jalview.io.FastaFile.print(sequences,124,false).getBytes());
109
110     if(vamsas_server.getUseAuth() == true)
111       if(vamsas_server.getServiceUserName() == null)
112         System.out.println("jalview.Jemboss: OOPS! Authentication required!");
113
114     // submit to private server
115     try
116     {
117       JembossRun thisrun = new JembossRun(embossCommand,"",
118                                           filesToMove,vamsas_server);
119       Hashtable h = thisrun.hash();
120       Enumeration enum = h.keys();
121       // Find the alignment generated and parse it
122       while (enum.hasMoreElements())
123       {
124         String thiskey = (String)enum.nextElement().toString();
125         if (thiskey.endsWith(".aln"))
126         {
127           // FormatAdapter, and IdentifyFile use a protocol string
128           // to work out what 'file' is - file/url/Alignmentasastring
129           String alfile = h.get(thiskey).toString();
130           String format = IdentifyFile.Identify(alfile, "Paste");
131           SequenceI[] alignment = null;
132
133           if (FormatProperties.contains(format))
134             alignment = FormatAdapter.read(alfile, "Paste", format);
135           else
136             System.out.println("jalview.Jemboss: Unknown format "+format+"\n");
137           if (alignment == null)
138             System.out.println("jalview.Jemboss: Couldn't read response:\n"
139                                + alfile + "\n---EOF\n");
140           else
141             return alignment;
142         }
143
144       }
145
146     }
147     catch (JembossSoapException eae)
148     {
149       System.out.println("jalview.Jemboss: SOAP ERROR :"+embossCommand
150                          + "\n" + eae);
151     }
152     catch (Exception e) {
153       System.out.println("jalview.Jemboss: Some other failure :\n"
154                          +e.getMessage()+"\n");
155     }
156
157     return null;
158   }
159 }
160