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