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