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