1 /* Copyright (c) 2011 Peter Troshin
\r
2 * Copyright (c) 2013 Alexander Sherstnev
\r
4 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\r
6 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
7 * Apache License version 2 as published by the Apache Software Foundation
\r
9 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
11 * License for more details.
\r
13 * A copy of the license is in apache_license.txt. It is also available here:
\r
14 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
16 * Any republication or derived work distributed in source code form
\r
17 * must include this copyright and license notice.
\r
20 package compbio.ws.client;
\r
22 import static compbio.ws.client.Constraints.hostkey;
\r
23 import static compbio.ws.client.Constraints.pseparator;
\r
24 import static compbio.ws.client.Constraints.servicekey;
\r
26 import java.io.ByteArrayInputStream;
\r
27 import java.io.Closeable;
\r
28 import java.io.IOException;
\r
29 import java.io.PrintWriter;
\r
30 import java.net.ConnectException;
\r
31 import java.util.Arrays;
\r
32 import java.util.List;
\r
34 import javax.xml.ws.WebServiceException;
\r
36 import compbio.data.msa.JABAService;
\r
37 import compbio.data.msa.Metadata;
\r
38 import compbio.data.msa.MsaWS;
\r
39 import compbio.data.msa.SequenceAnnotation;
\r
40 import compbio.data.sequence.Alignment;
\r
41 import compbio.data.sequence.FastaSequence;
\r
42 import compbio.data.sequence.ScoreManager;
\r
43 import compbio.data.sequence.SequenceUtil;
\r
44 import compbio.metadata.JobStatus;
\r
45 import compbio.metadata.Limit;
\r
46 import compbio.metadata.LimitsManager;
\r
47 import compbio.metadata.Option;
\r
48 import compbio.metadata.Preset;
\r
49 import compbio.metadata.PresetManager;
\r
50 import compbio.metadata.RunnerConfig;
\r
51 import compbio.metadata.UnsupportedRuntimeException;
\r
52 import compbio.util.FileUtil;
\r
53 import compbio.util.Util;
\r
56 * Class for testing web services
\r
60 * @version 1.5 February 2013
\r
62 public class WSTester {
\r
65 * Test sequences to be used as input for WS
\r
67 public static final String fastaInput2records =
\r
68 ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV\n"
\r
69 + ">Bar\nASDAAPEHPGIALWLHALEDAGQAEAAAAYTRAHQLLPEEPYITAQLLNAVA\n";
\r
70 public static final String fastaInput1record =
\r
71 ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV\n";
\r
72 public static final String fastaAlignment =
\r
73 ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV--------\n"
\r
74 + ">Bar\nASDAAPEH------------PGIALWLHALE-DAGQAEAAA---AYTRAHQLLPEEPYITAQLLNAVA\n";
\r
75 public static final String fastaRNAAlignment =
\r
76 ">Foo\nC-UUGCGUUAAUGAGAACAGAAACG-UAAA--CUAUAA-CCUAG-G-GGUUUCUGUUGGAUGGUUG----GCAAC\n"
\r
77 + ">Bar\nG-UGGCGCUUAUGACGCAGUUGUCU-UAAA-CUCGAAC--UCGA-GCGGGCAAUUGCUGAU-UACGAUUAACCAC\n";
\r
82 private static final String FAILED = "FAILED";
\r
83 private static final String OK = "OK";
\r
84 private static final String UNSUPPORTED = "UNSUPPORTED";
\r
87 * Converting input to a form accepted by WS
\r
89 * @return List of FastaSequence records
\r
91 private static List<FastaSequence> loadSeqs(int nLines) {
\r
94 return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput1record.getBytes()));
\r
96 return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput2records.getBytes()));
\r
97 } catch (IOException ignored) {
\r
98 // Should not happen as a source is not a external stream
\r
99 ignored.printStackTrace();
\r
105 * Converting input to a form accepted by WS
\r
107 * @return List of FastaSequence records
\r
109 private static List<FastaSequence> loadAlignment() {
\r
111 return SequenceUtil.readFasta(new ByteArrayInputStream(
\r
112 fastaAlignment.getBytes()));
\r
113 } catch (IOException ignored) {
\r
114 // Should not happen as a source is not a external stream
\r
115 ignored.printStackTrace();
\r
120 * Converting input to a form accepted by WS
\r
122 * @return List of FastaSequence records with aligned RNA sequences
\r
124 private static List<FastaSequence> loadRNAAlignment() {
\r
126 return SequenceUtil.readFasta(new ByteArrayInputStream(fastaRNAAlignment.getBytes()));
\r
127 } catch (IOException ignored) {
\r
128 // Should not happen as a source is not a external stream
\r
129 ignored.printStackTrace();
\r
134 private final PrintWriter writer;
\r
135 private final String hostname;
\r
138 * Construct an instance of JABAWS tester
\r
141 * - fully qualified host and context name of JABAWS e.g.
\r
142 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
144 * a PrintWriter instance to writer test log to.
\r
146 public WSTester(String hostname, PrintWriter writer) {
\r
147 if (Util.isEmpty(hostname)) {
\r
148 throw new NullPointerException("Hostname must be provided!");
\r
150 this.hostname = hostname;
\r
151 this.writer = writer;
\r
157 static void printUsage() {
\r
158 System.out.println("Usage: <Class or Jar file name> " + hostkey
\r
159 + pseparator + "host_and_context " + "<" + servicekey
\r
160 + pseparator + "serviceName>");
\r
161 System.out.println();
\r
162 System.out.println(hostkey
\r
164 + "<host_and_context> - a full URL to the JABAWS web server including context path e.g. http://10.31.1.159:8080/ws");
\r
165 System.out.println(servicekey
\r
167 + "<ServiceName> - optional if unspecified all services are tested otherwise one of "
\r
168 + Arrays.toString(Services.values()));
\r
169 System.out.println();
\r
174 * Calls alignment with preset
\r
179 * list of the Preset
\r
180 * @throws UnsupportedRuntimeException
\r
182 private <T> boolean presetAlign(MsaWS<T> msaws, List<Preset<T>> presets)
\r
183 throws UnsupportedRuntimeException {
\r
184 boolean succeed = false;
\r
185 for (Preset<T> preset : presets) {
\r
186 writer.print("Aligning with preset '" + preset.getName() + "'... ");
\r
187 Alignment al = null;
\r
189 String taskId = msaws.presetAlign(loadSeqs(2), preset);
\r
190 al = msaws.getResult(taskId);
\r
192 writer.println(OK);
\r
195 } catch (Exception e) {
\r
196 if (e instanceof UnsupportedRuntimeException) {
\r
197 // If executable is not supported than none of the presets
\r
200 throw (UnsupportedRuntimeException) e;
\r
202 reportException(e);
\r
210 private <T> boolean testMsaWS(MsaWS<T> msaws, Services service) throws Exception {
\r
211 assert msaws != null;
\r
213 boolean succeed = testDefaultAlignment(msaws, service);
\r
214 // If exception above is thrown than the tests below is not run
\r
216 PresetManager<T> pmanager = msaws.getPresets();
\r
217 if (pmanager != null && pmanager.getPresets().size() > 0) {
\r
218 writer.println("Testing alignment with presets:");
\r
219 List<Preset<T>> plist = pmanager.getPresets();
\r
220 succeed = !succeed ? presetAlign(msaws, plist) : succeed;
\r
222 testMetadata(msaws);
\r
226 * Call most of web services functions and check the output
\r
231 * @throws UnsupportedRuntimeException
\r
232 * is thrown if the connection to a web service was made, but
\r
233 * the web service is not functional. e.g. when native
\r
234 * executable does not exists for a server platform
\r
236 @SuppressWarnings("unchecked")
\r
237 private <T> boolean checkService(JABAService wservice, Services service) {
\r
239 if (wservice == null) {
\r
240 throw new NullPointerException("JABAService instance must be provided!");
\r
243 if (wservice instanceof MsaWS) {
\r
244 return testMsaWS((MsaWS<T>) wservice, service);
\r
245 } else if (wservice instanceof SequenceAnnotation) {
\r
246 return testSequenceAnnotationWS(
\r
247 (SequenceAnnotation<T>) wservice, service);
\r
249 throw new UnsupportedOperationException("The service: " + wservice.getClass() + " is not supported! ");
\r
251 } catch (Exception e) {
\r
252 reportException(e);
\r
257 private <T> boolean testSequenceAnnotationWS(
\r
258 SequenceAnnotation<T> wservice, Services service) throws Exception {
\r
259 writer.print("Calling analyse.........");
\r
261 List<FastaSequence> input = loadSeqs(2);
\r
262 if (service == Services.AAConWS ) {
\r
263 input = loadAlignment();
\r
264 } else if (service == Services.RNAalifoldWS) {
\r
265 input = loadRNAAlignment();
\r
267 boolean success = testDefaultAnalyse(input, wservice, null, null);
\r
269 PresetManager<T> presetman = wservice.getPresets();
\r
270 if (presetman != null) {
\r
271 List<Preset<T>> presets = presetman.getPresets();
\r
272 if (presets != null && !presets.isEmpty()) {
\r
273 Preset<T> preset = presets.get(0);
\r
274 writer.print("Calling analyse with Preset.........");
\r
275 success = testDefaultAnalyse(input, wservice, preset, null);
\r
278 testMetadata(wservice);
\r
282 private <T> boolean testDefaultAnalyse(List<FastaSequence> fastalist,
\r
283 SequenceAnnotation<T> wsproxy, Preset<T> preset,
\r
284 List<Option<T>> customOptions) throws Exception {
\r
286 ScoreManager scores = null;
\r
288 String jobId = null;
\r
289 if (customOptions != null) {
\r
290 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
291 } else if (preset != null) {
\r
292 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
294 jobId = wsproxy.analize(fastalist);
\r
296 Thread.sleep(1000);
\r
297 scores = wsproxy.getAnnotation(jobId);
\r
298 if (scores != null) {
\r
299 writer.println(OK);
\r
302 return scores != null;
\r
305 private void reportException(Exception e) {
\r
306 writer.println(FAILED);
\r
307 writer.println("Exception while waiting for results. "
\r
308 + "Exception details are below:");
\r
309 writer.println(e.getLocalizedMessage());
\r
310 e.printStackTrace(writer);
\r
313 private <T> void testMetadata(Metadata<T> msaws)
\r
314 throws UnsupportedRuntimeException {
\r
316 writer.print("Querying presets...");
\r
317 PresetManager<T> pmanager = msaws.getPresets();
\r
318 if (pmanager != null && pmanager.getPresets().size() > 0) {
\r
319 writer.println(OK);
\r
321 writer.println(UNSUPPORTED);
\r
324 writer.print("Querying Parameters...");
\r
325 RunnerConfig<T> options = msaws.getRunnerOptions();
\r
326 if (options != null && options.getArguments().size() > 0) {
\r
327 writer.println(OK);
\r
329 writer.println(UNSUPPORTED);
\r
332 writer.print("Querying Limits...");
\r
333 LimitsManager<T> limits = msaws.getLimits();
\r
334 if (limits != null && limits.getLimits().size() > 0) {
\r
335 writer.println(OK);
\r
336 // writer.println("Limits details: \n" + limits.toString());
\r
338 writer.println(UNSUPPORTED);
\r
341 writer.print("Querying Local Engine Limits...");
\r
342 Limit<T> localLimit = msaws
\r
343 .getLimit(PresetManager.LOCAL_ENGINE_LIMIT_PRESET);
\r
344 if (localLimit != null) {
\r
345 writer.println(OK);
\r
347 writer.println(UNSUPPORTED);
\r
352 * Align using default settings
\r
356 * @throws UnsupportedRuntimeException
\r
358 private <T> boolean testDefaultAlignment(MsaWS<T> msaws, Services service) throws Exception {
\r
359 writer.print("Testing alignment with default parameters:");
\r
360 Alignment al = null;
\r
361 boolean succeed = false;
\r
364 if (service == Services.JpredWS) {
\r
365 taskId = msaws.align(loadSeqs(1));
\r
367 taskId = msaws.align(loadSeqs(2));
\r
369 writer.print("\nQuerying job status...");
\r
370 JobStatus status = msaws.getJobStatus(taskId);
\r
371 while (status != JobStatus.FINISHED) {
\r
372 Thread.sleep(1000);
\r
373 status = msaws.getJobStatus(taskId);
\r
375 writer.println(OK);
\r
376 writer.print("Retrieving results...");
\r
377 al = msaws.getResult(taskId);
\r
380 writer.println(OK);
\r
385 * Test JWS2 web services
\r
390 * -h=<Your web application server host name, port and JWS2
\r
393 * -s=<ServiceName> which is optional. If service name is not
\r
394 * provided then all known JWS2 web services are tested
\r
395 * @throws IOException
\r
397 public static <T> void main(String[] args) throws IOException {
\r
399 if (args == null || args.length < 1) {
\r
400 WSTester.printUsage();
\r
403 String host = CmdHelper.getHost(args);
\r
404 String serviceName = CmdHelper.getServiceName(args);
\r
405 if (!Jws2Client.validURL(host)) {
\r
406 System.err.println("<host_and_context> parameter is not provided or is incorrect!");
\r
409 WSTester tester = new WSTester(host, new PrintWriter(System.out, true));
\r
411 if (serviceName != null) {
\r
412 Services service = Services.getService(serviceName);
\r
413 if (service == null) {
\r
414 tester.writer.println("Service '" + serviceName + "' is not supported. Valid values are: "
\r
415 + Arrays.toString(Services.values()));
\r
416 tester.writer.println();
\r
420 tester.checkService(service);
\r
424 tester.writer.println("<ServiceName> is not provided checking all known services...");
\r
426 for (Services serv : Services.values()) {
\r
427 tester.writer.println();
\r
428 tester.checkService(serv);
\r
434 * Test JABA web service
\r
437 * the service to test
\r
438 * @return true if the service works as expected, false otherwise
\r
439 * @throws WebServiceException
\r
440 * @throws ConnectException
\r
442 public boolean checkService(Services service) throws ConnectException,
\r
443 WebServiceException {
\r
444 JABAService ws = Jws2Client.connect(hostname, service);
\r
446 String line = "Cannot estabilish the connection to host " + hostname + " with service ";
\r
447 writer.println(line + service.toString());
\r
450 boolean succeed = false;
\r
452 writer.println("Checking service " + service.toString());
\r
453 succeed = checkService(ws, service);
\r
455 FileUtil.closeSilently(((Closeable) ws));
\r
457 reportResults(service, succeed);
\r
461 private void reportResults(Services serv, boolean succeed) {
\r
463 writer.println("Check is completed. The Service " + serv + " IS WORKING\n");
\r
465 writer.println("Check is aborted. The Service " + serv + " HAS SOME PROBLEMS\n");
\r