dbe999a464c67ee2455d7408c5217d3c5dd90ac1
[jabaws.git] / webservices / compbio / ws / client / Jws2Client.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.ws.client;\r
20 \r
21 import static compbio.ws.client.Constraints.inputkey;\r
22 import static compbio.ws.client.Constraints.outputkey;\r
23 import static compbio.ws.client.Constraints.paramFile;\r
24 \r
25 import java.io.Closeable;\r
26 import java.io.File;\r
27 import java.io.FileInputStream;\r
28 import java.io.FileOutputStream;\r
29 import java.io.IOException;\r
30 import java.io.PrintWriter;\r
31 import java.io.Writer;\r
32 import java.net.ConnectException;\r
33 import java.net.MalformedURLException;\r
34 import java.net.URL;\r
35 import java.util.Arrays;\r
36 import java.util.Collections;\r
37 import java.util.List;\r
38 import java.util.Set;\r
39 import java.util.logging.Level;\r
40 import java.util.logging.Logger;\r
41 \r
42 import javax.xml.namespace.QName;\r
43 import javax.xml.ws.Service;\r
44 import javax.xml.ws.WebServiceException;\r
45 \r
46 import compbio.data.msa.JABAService;\r
47 import compbio.data.msa.Metadata;\r
48 import compbio.data.msa.MsaWS;\r
49 import compbio.data.msa.RegistryWS;\r
50 import compbio.data.msa.SequenceAnnotation;\r
51 import compbio.data.sequence.Alignment;\r
52 import compbio.data.sequence.AlignmentMetadata;\r
53 import compbio.data.sequence.JpredAlignment;\r
54 import compbio.data.sequence.FastaSequence;\r
55 import compbio.data.sequence.ScoreManager;\r
56 import compbio.data.sequence.SequenceUtil;\r
57 import compbio.data.sequence.UnknownFileFormatException;\r
58 import compbio.metadata.JobSubmissionException;\r
59 import compbio.metadata.Option;\r
60 import compbio.metadata.Limit;\r
61 import compbio.metadata.Preset;\r
62 import compbio.metadata.PresetManager;\r
63 import compbio.metadata.ResultNotAvailableException;\r
64 import compbio.metadata.WrongParameterException;\r
65 import compbio.util.FileUtil;\r
66 \r
67 /**\r
68  * A command line client for JAva Bioinformatics Analysis Web Services\r
69  * \r
70  * @author pvtroshin\r
71  * @version 1.0\r
72  */\r
73 public class Jws2Client {\r
74 \r
75         /**\r
76          * Use java.util.Logger instead of log4j logger to reduce the size of the client package\r
77          */\r
78         private static final Logger log = Logger.getLogger(Jws2Client.class.getCanonicalName());\r
79 \r
80         /**\r
81          * Attempt to construct the URL object from the string\r
82          * \r
83          * @param urlstr\r
84          * @return true if it succeed false otherwise\r
85          */\r
86         public static boolean validURL(String urlstr) {\r
87                 try {\r
88                         if (urlstr == null || urlstr.trim().length() == 0) {\r
89                                 return false;\r
90                         }\r
91                         new URL(urlstr);\r
92                 } catch (MalformedURLException e) {\r
93                         return false;\r
94                 }\r
95                 return true;\r
96         }\r
97 \r
98         /**\r
99          * Connects to the service and do the job as requested, if something goes\r
100          * wrong reports or/and prints usage help.\r
101          * \r
102          * @param <T>\r
103          *            web service type\r
104          * @param cmd\r
105          *            command line options\r
106          * @throws IOException\r
107          */\r
108         @SuppressWarnings("unchecked")\r
109         <T> Jws2Client(String[] cmd) throws IOException {\r
110                 \r
111 \r
112                 String hostname = CmdHelper.getHost(cmd);\r
113                 if (hostname == null) {\r
114                         System.err.println("Host name is not provided!");\r
115                         System.out.println(Constraints.help_text);\r
116                         System.exit(1);\r
117                 }\r
118 \r
119                 if (!validURL(hostname)) {\r
120                         System.err.println("Host name is not valid!");\r
121                         System.out.println(Constraints.help_text);\r
122                         System.exit(1);\r
123                 }\r
124 \r
125                 boolean listServices = CmdHelper.listServices(cmd);\r
126                 if (listServices) {\r
127                         listAllServices(hostname);\r
128                         System.exit(0);\r
129                 }\r
130 \r
131                 String serviceName = CmdHelper.getServiceName(cmd);\r
132                 if (serviceName == null) {\r
133                         System.err.println("Service name is no provided!");\r
134                         System.out.println(Constraints.help_text);\r
135                         System.exit(1);\r
136                 }\r
137 \r
138                 Services service = Services.getService(serviceName);\r
139                 if (service == null) {\r
140                         String mess = "Service " + serviceName + " is no available! Valid values are: ";\r
141                         System.err.println(mess + Arrays.toString(Services.values()));\r
142                         System.out.println(Constraints.help_text);\r
143                         System.exit(1);\r
144                 }\r
145 \r
146                 if (CmdHelper.testService(cmd)) {\r
147                         testService(hostname, service, new PrintWriter(System.out, true));\r
148                         System.exit(0);\r
149                 }\r
150 \r
151                 Metadata<T> thews = (Metadata<T>) connect(hostname, service);\r
152                 Preset<T> preset = null;\r
153                 if (null != CmdHelper.getPresetName(cmd)) {\r
154                         preset = MetadataHelper.getPreset(thews, CmdHelper.getPresetName(cmd));\r
155                 }\r
156 \r
157                 List<Option<T>> customOptions = null;\r
158                 if (null != IOHelper.getFile(cmd, paramFile, true)) {\r
159                         List<String> prms = IOHelper.loadParameters(IOHelper.getFile(cmd, paramFile, true));\r
160                         customOptions = MetadataHelper.processParameters(prms, thews.getRunnerOptions());\r
161                 }\r
162 \r
163                 if (null != IOHelper.getFile(cmd, inputkey, true)) {\r
164                         File infile = IOHelper.getFile(cmd, inputkey, true);\r
165                         File outfile = IOHelper.getFile(cmd, outputkey, false);\r
166                         Writer writer = null;\r
167                         if (outfile != null) {\r
168                                 writer = IOHelper.getWriter(outfile);\r
169                         } else {\r
170                                 writer = new PrintWriter(System.out, true);\r
171                         }\r
172                         if (service.getServiceType() == SequenceAnnotation.class) {\r
173                                 ScoreManager result = analize(infile, ((SequenceAnnotation<T>) thews), preset, customOptions);\r
174                                 IOHelper.writeOut(writer, result);\r
175                                 writer.close();\r
176                         } else if (service.getServiceType() == MsaWS.class) {\r
177                                 Alignment alignment = align(infile, (MsaWS<T>) thews, preset, customOptions);\r
178                                 if (serviceName.equalsIgnoreCase("JpredWS")) {\r
179                                         writer.close();\r
180                                         JpredAlignment jpred = (JpredAlignment)alignment;\r
181                                         if (outfile != null) {\r
182                                                 FileOutputStream fout = new FileOutputStream(outfile);\r
183                                                 SequenceUtil.writeFastaKeepTheStream(fout, jpred.getJpredSequences(), 60);\r
184                                                 fout.close();\r
185                                         } else {\r
186                                                 SequenceUtil.writeFasta(System.out, jpred.getJpredSequences());\r
187                                         }\r
188                                 } else {\r
189                                         IOHelper.writeOut(writer, alignment);\r
190                                         writer.close();\r
191                                 }\r
192                                 AlignmentMetadata md = alignment.getMetadata();\r
193                                 System.out.println("Output has been prepared with " + md.getProgram());\r
194                         }\r
195                 }\r
196 \r
197                 if (CmdHelper.listParameters(cmd)) {\r
198                         List<Option<T>> opts = MetadataHelper.getParametersList(thews, hostname);\r
199                         for (Option<T> o : opts) {\r
200                                 System.out.println("##############################################################################\n" + o.toString());\r
201                         }\r
202                 }\r
203                 if (CmdHelper.listPresets(cmd)) {\r
204                         PresetManager<T> psm = MetadataHelper.getPresetList(thews);\r
205                         if (null != psm) {\r
206                                 System.out.print(psm);\r
207                         } else {\r
208                                 System.out.println("No presets are defined for the Web service");\r
209                         }\r
210                 }\r
211                 if (CmdHelper.listLimits(cmd)) {\r
212                         List<Limit<T>> lims = MetadataHelper.getLimits(thews);\r
213                         for (Limit<T> l : lims) {\r
214                                 System.out.println("##############################################################################\n" + l.toString());\r
215                         }\r
216                 }\r
217                 log.fine("Disconnecting...");\r
218                 ((Closeable) thews).close();\r
219                 log.fine("Disconnected successfully!");\r
220         }\r
221 \r
222         /**\r
223          * Connects to a web service by the host and the service name web service type\r
224          * \r
225          * @param host\r
226          *            the fully qualified name of JABAWS server including JABAWS\r
227          *            context name e.g\r
228          *            http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba\r
229          * @param service\r
230          *            the name of the JABAWS service to connect to\r
231          * @return JABAService<T>\r
232          * @throws WebServiceException\r
233          * @throws ConnectException\r
234          *             if fails to connect to the service on the host\r
235          */\r
236         public static JABAService connect(String host, Services service)\r
237                         throws WebServiceException, ConnectException {\r
238                 URL url = null;\r
239                 log.log(Level.FINE, "Attempting to connect with " + service.toString() + "...");\r
240                 System.out.println ("Attempting to connect with " + service.toString() + "...");\r
241                 try {\r
242                         url = new URL(host + "/" + service.toString() + "?wsdl");\r
243                 } catch (MalformedURLException e) {\r
244                         e.printStackTrace();\r
245                         // ignore as the host name is already verified\r
246                 }\r
247                 Service serv = null;\r
248                 try {\r
249                         serv = service.getService(url, service.getServiceNamespace());\r
250                 } catch (WebServiceException wse) {\r
251                         wse.printStackTrace();\r
252                 }\r
253                 if (serv == null) {\r
254                         throw new ConnectException("Could not connect to " + url + ". Is the server down?");\r
255                 }\r
256                 JABAService serviceIF = service.getInterface(serv);\r
257                 log.log(Level.INFO, "Connected successfully!");\r
258 \r
259                 return serviceIF;\r
260         }\r
261 \r
262         /**\r
263          * Get a connection of JABAWS registry\r
264          * \r
265          * @param host\r
266          *            the fully qualified name of JABAWS server including JABAWS\r
267          *            context name e.g\r
268          *            http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba\r
269          * @return compbio.data.msa.RegistryWS - instance of a RegistryWS web\r
270          *         service\r
271          * @throws WebServiceException\r
272          * @throws ConnectException\r
273          */\r
274         public static compbio.data.msa.RegistryWS connectToRegistry(String host)\r
275                         throws WebServiceException, ConnectException {\r
276                 URL url = null;\r
277                 String service = "RegistryWS";\r
278                 log.log(Level.FINE, "Attempting to connect...");\r
279 \r
280                 try {\r
281                         url = new URL(host + "/" + service + "?wsdl");\r
282                 } catch (MalformedURLException e) {\r
283                         e.printStackTrace();\r
284                         // ignore as the host name is already verified\r
285                 }\r
286                 QName qname = new QName(JABAService.V2_SERVICE_NAMESPACE, service);\r
287                 Service serv = Service.create(url, qname);\r
288 \r
289                 if (serv == null) {\r
290                         throw new ConnectException("Could not connect to " + url + ". Is the server down?");\r
291                 }\r
292 \r
293                 QName portName = new QName(serv.getServiceName().getNamespaceURI(), service + "Port");\r
294                 compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName, compbio.data.msa.RegistryWS.class);\r
295                 log.log(Level.INFO, "Connected to " + service + " successfully!");\r
296 \r
297                 return serviceIF;\r
298         }\r
299 \r
300         /**\r
301          * Asks registry to test the service on the host hostname\r
302          * \r
303          * @param hostname\r
304          * @param service\r
305          * @param writer\r
306          * @throws ConnectException\r
307          * @throws WebServiceException\r
308          */\r
309         public static void testService(String hostname, Services service, PrintWriter writer)\r
310                         throws ConnectException, WebServiceException {\r
311                 RegistryWS registry = connectToRegistry(hostname);\r
312                 if (registry != null) {\r
313                         String message = registry.testService(service);\r
314                         writer.println("Service " + service + " testing results: ");\r
315                         writer.println(message);\r
316                         FileUtil.closeSilently(((Closeable) registry));\r
317                 }\r
318                 writer.flush();\r
319         }\r
320 \r
321         private static void listAllServices(String hostname) throws WebServiceException, IOException {\r
322                 RegistryWS registry = connectToRegistry(hostname);\r
323                 Set<Services> func_services = Collections.emptySet();\r
324                 Set<Services> nonfunc_services = Collections.emptySet();\r
325                 if (registry != null) {\r
326                         func_services = registry.getSupportedServices();\r
327                         nonfunc_services = registry.getNonoperatedServices();\r
328                         FileUtil.closeSilently(((Closeable) registry));\r
329                 } else {\r
330                         System.out.println("Failed to connect to the registry! ");\r
331                         return;\r
332                 }\r
333                 if (!func_services.isEmpty()) {\r
334                         System.out.println("There are " + func_services.size() + " services at " + hostname + ":");\r
335                         String mess = "\n\rThe list:\n";\r
336                         System.out.println(mess + Services.toString(func_services));\r
337                 }\r
338                 if (!nonfunc_services.isEmpty()) {\r
339                         System.out.println("There are " + nonfunc_services.size() + " non-available services at " + hostname + ":");\r
340                         String mess = "The list (internal tests failed): ";\r
341                         System.out.println(mess + Services.toString(nonfunc_services));\r
342                 }\r
343         }\r
344 \r
345         /**\r
346          * Calculate conservation for sequences loaded from a FASTA record list structure \r
347          * \r
348          * @param fastalist\r
349          *            the list of FASTA records\r
350          * @param wsproxy\r
351          *            a web service proxy\r
352          * @param preset\r
353          *            Preset to use optional\r
354          * @param customOptions\r
355          *            the list of options\r
356          * @return Set<Score> the conservation scores\r
357          * @throws UnknownFileFormatException\r
358          */\r
359         static <T> ScoreManager analize(List<FastaSequence> fastalist, SequenceAnnotation<T> wsproxy, Preset<T> preset, List<Option<T>> customOptions) {\r
360                 ScoreManager scores = null;\r
361                 try {\r
362                         String jobId = null;\r
363                         if (customOptions != null && preset != null) {\r
364                                 System.out.println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");\r
365                         }\r
366                         if (customOptions != null) {\r
367                                 jobId = wsproxy.customAnalize(fastalist, customOptions);\r
368                         } else if (preset != null) {\r
369                                 jobId = wsproxy.presetAnalize(fastalist, preset);\r
370                         } else {\r
371                                 jobId = wsproxy.analize(fastalist);\r
372                         }\r
373                         System.out.println("\n\rcalling predictor.........");\r
374                         Thread.sleep(100);\r
375                         scores = wsproxy.getAnnotation(jobId);\r
376                 } catch (JobSubmissionException e) {\r
377                         System.err.println("Exception while submitting job to a web server. Exception details are below:");\r
378                         e.printStackTrace();\r
379                 } catch (ResultNotAvailableException e) {\r
380                         System.err.println("Exception while waiting for results. Exception details are below:");\r
381                         e.printStackTrace();\r
382                 } catch (InterruptedException e) {\r
383                         Thread.currentThread().interrupt();\r
384                         System.err.println("Exception while waiting for results. Exception details are below:");\r
385                         e.printStackTrace();\r
386                 } catch (WrongParameterException e) {\r
387                         String mess = "Parsing the web method input parameters failed Exception details are below:";\r
388                         System.err.println(mess);\r
389                         e.printStackTrace();\r
390                 }\r
391                 return scores;\r
392 \r
393         }\r
394 \r
395         /**\r
396          * Calculate conservation for sequences loaded from the file\r
397          * \r
398          * @param wsproxy\r
399          *            a web service proxy\r
400          * @param file\r
401          *            the file to read the results from\r
402          * @param preset\r
403          *            Preset to use optional\r
404          * @param customOptions\r
405          *            the list of options\r
406          * @return Set<Score> the conservation scores\r
407          * @throws IOException\r
408          * @throws UnknownFileFormatException\r
409          */\r
410         static <T> ScoreManager analize(File file, SequenceAnnotation<T> wsproxy, Preset<T> preset, List<Option<T>> customOptions) {\r
411                 List<FastaSequence> fastalist = null;\r
412                 try {\r
413                         fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());\r
414                         assert !fastalist.isEmpty() : "Input is empty!";\r
415                 } catch (IOException e) {\r
416                         String mess = "Reading the input file failed. Check that the file contains a list of FASTA records!\n";\r
417                         System.err.println(mess + "Exception details are below:");\r
418                         e.printStackTrace();\r
419                 } catch (UnknownFileFormatException e) {\r
420                         String mess = "Reading the input file failed. Exception details are below:";\r
421                         System.err.println(mess);\r
422                         System.out.println(e.getMessage());\r
423                         e.printStackTrace();\r
424                 }\r
425                 return analize(fastalist, wsproxy, preset, customOptions);\r
426         }\r
427 \r
428         /**\r
429          * Align sequences from the file using MsaWS\r
430          * \r
431          * @param <T>\r
432          *            web service type e.g. Clustal\r
433          * @param file\r
434          *            to write the resulting alignment to\r
435          * @param msaws\r
436          *            MsaWS required\r
437          * @param preset\r
438          *            Preset to use optional\r
439          * @param customOptions\r
440          *            file which contains new line separated list of options\r
441          * @return Alignment\r
442          */\r
443         static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,\r
444                         List<Option<T>> customOptions) {\r
445                 FileInputStream instream = null;\r
446                 Alignment alignment = null;\r
447                 try {\r
448                         instream = new FileInputStream(file);\r
449                         List<FastaSequence> fastalist = SequenceUtil.readFasta(instream);\r
450                         instream.close();\r
451                         String jobId = null;\r
452                         if (customOptions != null && preset != null) {\r
453                                 System.out.println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");\r
454                         }\r
455                         if (customOptions != null) {\r
456                                 jobId = msaws.customAlign(fastalist, customOptions);\r
457                         } else if (preset != null) {\r
458                                 jobId = msaws.presetAlign(fastalist, preset);\r
459                         } else {\r
460                                 jobId = msaws.align(fastalist);\r
461                         }\r
462                         System.out.println("\ncalling program.........");\r
463                         Thread.sleep(100);\r
464                         alignment = msaws.getResult(jobId);\r
465                 } catch (IOException e) {\r
466                         System.err.println("Exception while reading the input file. Check that the input file is a FASTA file! "\r
467                                                 + "Exception details are below:");\r
468                         e.printStackTrace();\r
469                 } catch (JobSubmissionException e) {\r
470                         System.err.println("Exception while submitting job to a web server. Exception details are below:");\r
471                         e.printStackTrace();\r
472                 } catch (ResultNotAvailableException e) {\r
473                         System.err.println("Exception while waiting for results. Exception details are below:");\r
474                         e.printStackTrace();\r
475                 } catch (InterruptedException ignored) {\r
476                         // ignore and propagate an interruption\r
477                         Thread.currentThread().interrupt();\r
478                 } catch (WrongParameterException e) {\r
479                         e.printStackTrace();\r
480                 } finally {\r
481                         if (instream != null) {\r
482                                 try {\r
483                                         instream.close();\r
484                                 } catch (IOException ignored) {\r
485                                         // ignore\r
486                                 }\r
487                         }\r
488                 }\r
489                 return alignment;\r
490         }\r
491         \r
492         /**\r
493          * Starts command line client, if no parameter are supported print help. Two\r
494          * parameters are required for successful call the JWS2 host name and a\r
495          * service name.\r
496          * \r
497          * @param args\r
498          *            Usage: <Class or Jar file name> -h=host_and_context\r
499          *            -s=serviceName ACTION [OPTIONS]\r
500          * \r
501          *            -h=<host_and_context> - a full URL to the JWS2 web server\r
502          *            including context path e.g. http://10.31.1.159:8080/ws\r
503          * \r
504          *            -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,\r
505          *            TcoffeeWS, ProbconsWS] ACTIONS:\r
506          * \r
507          *            -i=<inputFile> - full path to fasta formatted sequence file,\r
508          *            from which to align sequences\r
509          * \r
510          *            -parameters - lists parameters supported by web service\r
511          * \r
512          *            -presets - lists presets supported by web service\r
513          * \r
514          *            -limits - lists web services limits Please note that if input\r
515          *            file is specified other actions are ignored\r
516          * \r
517          *            OPTIONS: (only for use with -i action):\r
518          * \r
519          *            -r=<presetName> - name of the preset to use\r
520          * \r
521          *            -o=<outputFile> - full path to the file where to write an\r
522          *            alignment -f=<parameterInputFile> - the name of the file with\r
523          *            the list of parameters to use. Please note that -r and -f\r
524          *            options cannot be used together. Alignment is done with either\r
525          *            preset or a parameters from the file, but not both!\r
526          * \r
527          */\r
528         public static void main(String[] args) {\r
529                 if (args == null) {\r
530                         System.out.println(Constraints.help_text);\r
531                         System.exit(1);\r
532                 }\r
533                 if (args.length < 2) {\r
534                         System.err.println("Host (-h=<host>, e.g. -h=http://www.compbio.dundee.ac.uk/jabaws) and service (-s=<ServiceName>, e.g. -s=Jpred) are required!");\r
535                         System.out.println(Constraints.help_text);\r
536                         System.exit(1);\r
537                 }\r
538                 try {\r
539                         new Jws2Client(args);\r
540                 } catch (IOException e) {\r
541                         log.log(Level.SEVERE, "IOException in client! " + e.getMessage(), e.getCause());\r
542                         System.err.println("Cannot write output file! Stack trace: ");\r
543                         e.printStackTrace();\r
544                 }\r
545         }\r
546 }\r