087afc61681865383fb1ae4cd65ff2e6c79a9db0
[jabaws.git] / webservices / compbio / ws / server / ProbconsWS.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.server;\r
20 \r
21 import java.io.File;\r
22 import java.util.List;\r
23 \r
24 import javax.jws.WebService;\r
25 \r
26 import org.apache.log4j.Logger;\r
27 \r
28 import compbio.data.msa.JABAService;\r
29 import compbio.data.msa.MsaWS;\r
30 import compbio.data.sequence.Alignment;\r
31 import compbio.data.sequence.FastaSequence;\r
32 import compbio.data.sequence.Program;\r
33 import compbio.engine.AsyncExecutor;\r
34 import compbio.engine.Configurator;\r
35 import compbio.engine.client.ConfiguredExecutable;\r
36 import compbio.engine.client.SkeletalExecutable;\r
37 import compbio.metadata.ChunkHolder;\r
38 import compbio.metadata.JobStatus;\r
39 import compbio.metadata.JobSubmissionException;\r
40 import compbio.metadata.Limit;\r
41 import compbio.metadata.LimitsManager;\r
42 import compbio.metadata.Option;\r
43 import compbio.metadata.Preset;\r
44 import compbio.metadata.PresetManager;\r
45 import compbio.metadata.ResultNotAvailableException;\r
46 import compbio.metadata.RunnerConfig;\r
47 import compbio.metadata.WrongParameterException;\r
48 import compbio.runner.Util;\r
49 import compbio.runner.msa.Probcons;\r
50 \r
51 @WebService(endpointInterface = "compbio.data.msa.MsaWS", targetNamespace = JABAService.SERVICE_NAMESPACE, serviceName = "ProbconsWS")\r
52 public class ProbconsWS implements MsaWS<Probcons> {\r
53 \r
54         private static Logger log = Logger.getLogger(ProbconsWS.class);\r
55 \r
56         private static final RunnerConfig<Probcons> probconsOptions = Util.getSupportedOptions(Probcons.class);\r
57         private static final LimitsManager<Probcons> limitMan = compbio.engine.client.Util.getLimits(new Probcons().getType());\r
58 \r
59         @Override\r
60         public String align(List<FastaSequence> sequences)\r
61                         throws JobSubmissionException {\r
62                 WSUtil.validateFastaInput(sequences);\r
63                 ConfiguredExecutable<Probcons> confProbcons = init(sequences);\r
64                 return WSUtil.align(sequences, confProbcons, log, "align", getLimit(""));\r
65         }\r
66 \r
67         ConfiguredExecutable<Probcons> init(List<FastaSequence> dataSet)\r
68                         throws JobSubmissionException {\r
69                 Probcons probcons = new Probcons();\r
70                 probcons.setInput(SkeletalExecutable.INPUT);\r
71                 probcons.setOutput(SkeletalExecutable.OUTPUT);\r
72                 probcons.setError(SkeletalExecutable.ERROR);\r
73                 return Configurator.configureExecutable(probcons, dataSet);\r
74         }\r
75 \r
76         @Override\r
77         public String customAlign(List<FastaSequence> sequences,\r
78                         List<Option<Probcons>> options) throws JobSubmissionException,\r
79                         WrongParameterException {\r
80                 WSUtil.validateFastaInput(sequences);\r
81                 ConfiguredExecutable<Probcons> confProbcons = init(sequences);\r
82                 List<String> params = WSUtil.getCommands(options, Probcons.KEY_VALUE_SEPARATOR);\r
83                 log.info("Setting parameters:" + params);\r
84                 confProbcons.addParameters(params);\r
85                 return WSUtil.align(sequences, confProbcons, log, "customAlign",getLimit(""));\r
86         }\r
87 \r
88         @Override\r
89         public String presetAlign(List<FastaSequence> sequences,\r
90                         Preset<Probcons> preset) throws JobSubmissionException,\r
91                         WrongParameterException {\r
92                 WSUtil.validateFastaInput(sequences);\r
93                 if (preset == null) {\r
94                         throw new WrongParameterException("Preset must be provided!");\r
95                 }\r
96                 ConfiguredExecutable<Probcons> confProbcons = init(sequences);\r
97                 confProbcons.addParameters(preset.getOptions());\r
98                 Limit<Probcons> limit = getLimit(preset.getName());\r
99                 return WSUtil.align(sequences, confProbcons, log, "presetAlign", limit);\r
100         }\r
101 \r
102         @SuppressWarnings("unchecked")\r
103         @Override\r
104         public Alignment getResult(String jobId) throws ResultNotAvailableException {\r
105                 WSUtil.validateJobId(jobId);\r
106                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
107                 ConfiguredExecutable<Probcons> probcons = (ConfiguredExecutable<Probcons>) asyncEngine.getResults(jobId);\r
108                 Alignment al = probcons.getResults();\r
109                 return new Alignment (al.getSequences(), Program.Probcons, '-');\r
110         }\r
111 \r
112         @Override\r
113         public Limit<Probcons> getLimit(String presetName) {\r
114                 if (limitMan == null) {\r
115                         // Limit is not defined\r
116                         return null;\r
117                 }\r
118                 return limitMan.getLimitByName(presetName);\r
119         }\r
120 \r
121         @Override\r
122         public LimitsManager<Probcons> getLimits() {\r
123                 return limitMan;\r
124         }\r
125 \r
126         @Override\r
127         public ChunkHolder pullExecStatistics(String jobId, long position) {\r
128                 WSUtil.validateJobId(jobId);\r
129                 // TODO check if output is the one to return\r
130                 String file = Configurator.getWorkDirectory(jobId) + File.separator + new Probcons().getError();\r
131                 return WSUtil.pullFile(file, position);\r
132         }\r
133 \r
134         @Override\r
135         public boolean cancelJob(String jobId) {\r
136                 WSUtil.validateJobId(jobId);\r
137                 return WSUtil.cancelJob(jobId);\r
138         }\r
139 \r
140         @Override\r
141         public JobStatus getJobStatus(String jobId) {\r
142                 WSUtil.validateJobId(jobId);\r
143                 return WSUtil.getJobStatus(jobId);\r
144         }\r
145 \r
146         @Override\r
147         public PresetManager<Probcons> getPresets() {\r
148                 return null;\r
149         }\r
150 \r
151         @Override\r
152         public RunnerConfig<Probcons> getRunnerOptions() {\r
153                 return probconsOptions;\r
154         }\r
155 \r
156 }\r