/* Copyright (c) 2009 Peter Troshin * * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0 * * This library is free software; you can redistribute it and/or modify it under the terms of the * Apache License version 2 as published by the Apache Software Foundation * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache * License for more details. * * A copy of the license is in apache_license.txt. It is also available here: * @see: http://www.apache.org/licenses/LICENSE-2.0.txt * * Any republication or derived work distributed in source code form * must include this copyright and license notice. */ package compbio.ws.client; import static compbio.ws.client.Constraints.hostkey; import static compbio.ws.client.Constraints.inputkey; import static compbio.ws.client.Constraints.limitList; import static compbio.ws.client.Constraints.outputkey; import static compbio.ws.client.Constraints.paramFile; import static compbio.ws.client.Constraints.paramList; import static compbio.ws.client.Constraints.presetList; import static compbio.ws.client.Constraints.presetkey; import static compbio.ws.client.Constraints.pseparator; import static compbio.ws.client.Constraints.servicekey; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.ws.Service; import javax.xml.ws.WebServiceException; import compbio.data.msa.JABAService; import compbio.data.msa.Metadata; import compbio.data.msa.MsaWS; import compbio.data.msa.SequenceAnnotation; import compbio.data.sequence.Alignment; import compbio.data.sequence.FastaSequence; import compbio.data.sequence.Score; import compbio.data.sequence.SequenceUtil; import compbio.data.sequence.UnknownFileFormatException; import compbio.metadata.JobSubmissionException; import compbio.metadata.Option; import compbio.metadata.Preset; import compbio.metadata.ResultNotAvailableException; import compbio.metadata.WrongParameterException; /** * A command line client for JAva Bioinformatics Analysis Web Services * * @author pvtroshin * @version 1.0 */ public class Jws2Client { /* * Use java.util.Logger instead of log4j logger to reduce the size of the * client package */ private static final Logger log = Logger.getLogger(Jws2Client.class .getCanonicalName()); // JABAWS version 1.0 service name static final String QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/01/2010/"; // JABAWS version 2.0 service name static final String V2_QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/12/2010/"; /** * Attempt to construct the URL object from the string * * @param urlstr * @return true if it succeed false otherwise */ public static boolean validURL(String urlstr) { try { if (urlstr == null || urlstr.trim().length() == 0) { return false; } new URL(urlstr); } catch (MalformedURLException e) { return false; } return true; } /** * Connects to the service and do the job as requested, if something goes * wrong reports or/and prints usage help. * * @param * web service type * @param cmd * command line options * @throws IOException */ Jws2Client(String[] cmd) throws IOException { String hostname = CmdHelper.getHost(cmd); if (hostname == null) { System.out.println("Host name is not provided!"); printUsage(1); } if (!validURL(hostname)) { System.out.println("Host name is not valid!"); printUsage(1); } String serviceName = CmdHelper.getServiceName(cmd); if (serviceName == null) { System.out.println("Service name is no provided!"); printUsage(1); } Services service = Services.getService(serviceName); if (service == null) { System.out.println("Service " + serviceName + " is no supported! Valid values are: " + Arrays.toString(Services.values())); printUsage(1); } File inputFile = IOHelper.getFile(cmd, inputkey, true); File outFile = IOHelper.getFile(cmd, outputkey, false); File parametersFile = IOHelper.getFile(cmd, paramFile, true); String presetName = CmdHelper.getPresetName(cmd); Metadata msaws = (Metadata) connect(hostname, service); Preset preset = null; if (presetName != null) { preset = MetadataHelper.getPreset(msaws, presetName); } List> customOptions = null; if (parametersFile != null) { List prms = IOHelper.loadParameters(parametersFile); customOptions = MetadataHelper.processParameters(prms, msaws.getRunnerOptions()); } Alignment alignment = null; if (inputFile != null) { OutputStream outStream = null; if (outFile != null) { outStream = IOHelper.getOutStream(outFile); } else { // this stream is going to be closed later which is fine as // std.out will not be outStream = System.out; } if (service == Services.AAConWS) { Map> result = analize(inputFile, ((SequenceAnnotation) msaws), preset, customOptions); IOHelper.writeOut(outStream, result); } else { alignment = align(inputFile, (MsaWS) msaws, preset, customOptions); IOHelper.writeOut(outStream, alignment); } // stream is closed in the method no need to close it here } boolean listParameters = CmdHelper.listParameters(cmd); if (listParameters) { System.out.println(MetadataHelper.getParametersList(msaws)); } boolean listPreset = CmdHelper.listPresets(cmd); if (listPreset) { System.out.println(MetadataHelper.getPresetList(msaws)); } boolean listLimits = CmdHelper.listLimits(cmd); if (listLimits) { System.out.println(MetadataHelper.getLimits(msaws)); } log.fine("Disconnecting..."); ((Closeable) msaws).close(); log.fine("Disconnected successfully!"); } /** * Calculate conservation for sequences loaded from the file * * @param wsproxy * a web service proxy * @param file * the file to read the results from * @param preset * Preset to use optional * @param customOptions * the list of options * @return Set the conservation scores * @throws UnknownFileFormatException */ Map> analize(File file, SequenceAnnotation wsproxy, Preset preset, List> customOptions) { List fastalist = null; Map> scores = null; try { fastalist = SequenceUtil.openInputStream(file.getAbsolutePath()); String jobId = null; if (customOptions != null && preset != null) { System.out .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!"); } if (customOptions != null) { jobId = wsproxy.customAnalize(fastalist, customOptions); } else if (preset != null) { jobId = wsproxy.presetAnalize(fastalist, preset); } else { jobId = wsproxy.analize(fastalist); } Thread.sleep(1000); scores = wsproxy.getAnnotation(jobId); } catch (IOException e) { System.err .println("Exception while reading the input file. " + "Check that the input file contains a list of fasta formatted sequences! " + "Exception details are below:"); e.printStackTrace(); } catch (JobSubmissionException e) { System.err .println("Exception while submitting job to a web server. " + "Exception details are below:"); e.printStackTrace(); } catch (ResultNotAvailableException e) { System.err.println("Exception while waiting for results. " + "Exception details are below:"); e.printStackTrace(); } catch (InterruptedException ignored) { // ignore and propagate an interruption Thread.currentThread().interrupt(); } catch (WrongParameterException e) { System.err .println("Exception while parsing the web method input parameters. " + "Exception details are below:"); e.printStackTrace(); } catch (UnknownFileFormatException e) { System.err .println("Exception while attempting to read the input file " + "Exception details are below:"); System.out.println(e.getMessage()); e.printStackTrace(); } return scores; } /** * Connects to a web service by the host and the service name * * @param T * web service type * @param host * @param service * @return MsaWS * @throws WebServiceException */ public static JABAService connect(String host, Services service) throws WebServiceException { URL url = null; log.log(Level.FINE, "Attempting to connect..."); try { url = new URL(host + "/" + service.toString() + "?wsdl"); } catch (MalformedURLException e) { e.printStackTrace(); // ignore as the host name is already verified } Service serv = null; try { serv = service.getService(url, QUALIFIED_SERVICE_NAME); } catch (WebServiceException wse) { System.out.println("Conecting to JABAWS version 2 service"); if (isV2service(wse)) { serv = service.getService(url, V2_QUALIFIED_SERVICE_NAME); } } JABAService serviceIF = service.getInterface(serv); log.log(Level.FINE, "Connected successfully!"); return serviceIF; } static boolean isV2service(WebServiceException wse) { String message = wse.getMessage(); int idx = message.indexOf("not a valid service"); if (idx > 0) { if (message.substring(idx).contains(V2_QUALIFIED_SERVICE_NAME)) { return true; } } return false; } /** * Align sequences from the file using MsaWS * * @param * web service type e.g. Clustal * @param file * to write the resulting alignment to * @param msaws * MsaWS required * @param preset * Preset to use optional * @param customOptions * file which contains new line separated list of options * @return Alignment */ static Alignment align(File file, MsaWS msaws, Preset preset, List> customOptions) { FileInputStream instream = null; List fastalist = null; Alignment alignment = null; try { instream = new FileInputStream(file); fastalist = SequenceUtil.readFasta(instream); instream.close(); String jobId = null; if (customOptions != null && preset != null) { System.out .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!"); } if (customOptions != null) { jobId = msaws.customAlign(fastalist, customOptions); } else if (preset != null) { jobId = msaws.presetAlign(fastalist, preset); } else { jobId = msaws.align(fastalist); } Thread.sleep(1000); alignment = msaws.getResult(jobId); } catch (IOException e) { System.err .println("Exception while reading the input file. " + "Check that the input file contains a list of fasta formatted sequences! " + "Exception details are below:"); e.printStackTrace(); } catch (JobSubmissionException e) { System.err .println("Exception while submitting job to a web server. " + "Exception details are below:"); e.printStackTrace(); } catch (ResultNotAvailableException e) { System.err.println("Exception while waiting for results. " + "Exception details are below:"); e.printStackTrace(); } catch (InterruptedException ignored) { // ignore and propagate an interruption Thread.currentThread().interrupt(); } catch (WrongParameterException e) { e.printStackTrace(); } finally { if (instream != null) { try { instream.close(); } catch (IOException ignored) { // ignore } } } return alignment; } /** * Prints Jws2Client usage information to standard out * * @param exitStatus */ static void printUsage(int exitStatus) { System.out.println(); System.out.println("Usage: " + hostkey + pseparator + "host_and_context " + servicekey + pseparator + "serviceName ACTION [OPTIONS] "); System.out.println(); System.out .println(hostkey + pseparator + " - a full URL to the JWS2 web server including context path e.g. http://10.31.1.159:8080/ws"); System.out.println(servicekey + pseparator + " - one of " + Arrays.toString(Services.values())); System.out.println(); System.out.println("ACTIONS: "); System.out .println(inputkey + pseparator + " - full path to fasta formatted sequence file, from which to align sequences"); System.out.println(paramList + " - lists parameters supported by web service"); System.out.println(presetList + " - lists presets supported by web service"); System.out.println(limitList + " - lists web services limits"); System.out .println("Please note that if input file is specified other actions are ignored"); System.out.println(); System.out.println("OPTIONS (only for use with -i action):"); System.out.println(presetkey + pseparator + " - name of the preset to use"); System.out .println(outputkey + pseparator + " - full path to the file where to write an alignment"); System.out .println("-f= - the name of the file with the list of parameters to use."); System.out .println("Please note that -r and -f options cannot be used together. " + "Alignment is done with either preset or a parameters from the file, but not both!"); System.exit(exitStatus); } /** * Starts command line client, if no parameter are supported print help. Two * parameters are required for successfull call the JWS2 host name and a * service name. * * @param args * Usage: -h=host_and_context * -s=serviceName ACTION [OPTIONS] * * -h= - a full URL to the JWS2 web server * including context path e.g. http://10.31.1.159:8080/ws * * -s= - one of [MafftWS, MuscleWS, ClustalWS, * TcoffeeWS, ProbconsWS] ACTIONS: * * -i= - full path to fasta formatted sequence file, * from which to align sequences * * -parameters - lists parameters supported by web service * * -presets - lists presets supported by web service * * -limits - lists web services limits Please note that if input * file is specified other actions are ignored * * OPTIONS: (only for use with -i action): * * -r= - name of the preset to use * * -o= - full path to the file where to write an * alignment -f= - the name of the file with * the list of parameters to use. Please note that -r and -f * options cannot be used together. Alignment is done with either * preset or a parameters from the file, but not both! * */ public static void main(String[] args) { if (args == null) { printUsage(1); } if (args.length < 2) { System.out.println("Host and service names are required!"); printUsage(1); } try { new Jws2Client(args); } catch (IOException e) { log.log(Level.SEVERE, "IOException in client! " + e.getMessage(), e.getCause()); System.err.println("Cannot write output file! Stack trace: "); e.printStackTrace(); } } }