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