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