cleaned up some comments and exception messages.
[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  * Standardise the exceptions (currently errors are output on stdout)
24  * Allow server configuration
25  * Throw away JembossParams and Jemboss dependence
26  * Generalise results to return collections of objects - alignments, trees, annotations, etc.
27  */
28
29 import java.net.*;
30 import java.security.*;
31 import java.util.*;
32
33 import ext.jemboss.*;
34 import ext.jemboss.soap.*;
35 import jalview.datamodel.*;
36 import jalview.io.*;
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     vamsas_server.setCurrentMode("interactive");
49     System.out.println("Jemboss Server Init\n"
50                        + vamsas_server.serverDescription()
51                        + "\nUser Authorisation ? "
52                        + vamsas_server.getUseAuth()
53                        +"\n");
54   }
55
56   /* public void updateServer(JembossParams props)
57   {
58     vamsas_server.updateJembossPropStrings (
59       props.getEmbossEnvironmentArray());
60   }
61   */
62     private Hashtable uniquify(SequenceI[] sequences) {
63         // Generate a safely named sequence set and a hash to recover the sequence names
64         Hashtable map = new Hashtable();
65         for (int i=0; i<sequences.length; i++) {
66             String safename = new String("Sequence"+i);
67             map.put(safename, sequences[i].getName());
68             sequences[i].setName(safename);
69         }
70         return map;
71     }
72
73     private boolean deuniquify(Hashtable map, SequenceI[] sequences) {
74         // recover unsafe sequence names for a sequence set
75         boolean allfound=true;
76         for (int i=0; i<sequences.length; i++) {
77             if (map.containsKey(sequences[i].getName())) {
78                 String unsafename = (String) map.get(sequences[i].getName());
79                 sequences[i].setName(unsafename);
80             } else {
81                 allfound=false;
82             }
83         }
84         return allfound;
85     }
86
87   public SequenceI[] clustalW(SequenceI[] sequences)
88   {
89
90     // Simplest client call - with gumph from jemboss.server.TestPrivateServer
91     if (vamsas_server.getPublicSoapURL().startsWith("https"))
92     {
93       //SSL settings
94       //    System.setProperty ("javax.net.debug", "all");
95       com.sun.net.ssl.internal.ssl.Provider p =
96           new com.sun.net.ssl.internal.ssl.Provider();
97       Security.addProvider(p);
98
99       //have to do it this way to work with JNLP
100       URL.setURLStreamHandlerFactory( new URLStreamHandlerFactory()
101       {
102         public URLStreamHandler createURLStreamHandler(final String protocol)
103         {
104           if(protocol != null && protocol.compareTo("https") == 0)
105           {
106             return new com.sun.net.ssl.internal.www.protocol.https.Handler();
107           }
108           return null;
109         }
110       });
111
112       //location of keystore
113       System.setProperty("javax.net.ssl.trustStore",
114                          "resources/client.keystore");
115
116       String jembossClientKeyStore = System.getProperty("user.home") +
117           "/.jembossClientKeystore";
118
119       try
120       {
121         new JembossJarUtil("resources/client.jar").writeByteFile(
122       "client.keystore",jembossClientKeyStore);
123         System.setProperty("javax.net.ssl.trustStore",
124                            jembossClientKeyStore);
125       }
126       catch(Exception exp){}
127
128     }
129
130     Hashtable filesToMove = new Hashtable();
131     String embossCommand = "emma -sequence jalseqs.fasta -auto";
132     // Duplicate
133     SequenceI[] myseq = new SequenceI[sequences.length];
134     for (int i=0; i<sequences.length; i++) {
135         myseq[i] = new Sequence(sequences[i]);
136     }
137     // Uniqueify, and 
138     Hashtable namemap = uniquify(myseq);
139     // Load sequence file into hash
140     
141     filesToMove.put("jalseqs.fasta",
142                     jalview.io.FastaFile.print(myseq,124,false).getBytes());
143
144     if(vamsas_server.getUseAuth() == true)
145       if(vamsas_server.getServiceUserName() == null)
146         System.out.println("jalview.Jemboss: OOPS! Authentication required!");
147
148     // submit to private server
149     try
150     {
151       JembossRun thisrun = new JembossRun(embossCommand,"",
152                                           filesToMove,vamsas_server);
153       Hashtable h = thisrun.hash();
154       Enumeration enum = h.keys();
155       // Find the alignment generated and parse it
156       while (enum.hasMoreElements())
157       {
158         String thiskey = (String)enum.nextElement().toString();
159         if (thiskey.endsWith(".aln"))
160         {
161           // FormatAdapter, and IdentifyFile use a protocol string
162           // to work out what 'file' is - file/url/Alignmentasastring
163           String alfile = h.get(thiskey).toString();
164           String format = IdentifyFile.Identify(alfile, "Paste");
165           SequenceI[] alignment = null;
166           
167           if (FormatProperties.contains(format))
168             alignment = FormatAdapter.read(alfile, "Paste", format);
169           else
170             System.out.println("jalview.Jemboss: Unknown format "+format+"\n");
171           if (alignment == null)
172             System.out.println("jalview.Jemboss: Couldn't read response:\n"
173                                + alfile + "\n---EOF\n");
174           else {
175             if (!deuniquify(namemap, alignment)) {
176                   System.out.println("jalview.Jemboss: Warning: Some of the "
177                                      +"original sequence names have not been recovered!\n");
178             }
179             return alignment;
180           }
181         }
182
183       }
184
185     }
186     catch (JembossSoapException eae)
187     {
188       System.out.println("jalview.Jemboss: SOAP ERROR :"+embossCommand
189                          + "\n" + eae);
190     }
191     catch (Exception e) {
192       System.out.println("jalview.Jemboss: Some other failure :\n"
193                          +e.getMessage()+"\n");
194     }
195
196     return null;
197   }
198 }
199