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