Change the way to deal with Limits to simplify wrapper writing and enable couping...
[jabaws.git] / webservices / compbio / ws / server / ClustalWS.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.ClustalW;\r
49 import compbio.util.Timer;\r
50 import compbio.ws.client.Services;\r
51 \r
52 @WebService(endpointInterface = "compbio.data.msa.MsaWS", targetNamespace = "http://msa.data.compbio/01/01/2010/", serviceName = "ClustalWS")\r
53 public class ClustalWS implements MsaWS<ClustalW> {\r
54 \r
55         // Ask for resource injection\r
56         @Resource\r
57         private WebServiceContext wsContext;\r
58 \r
59         private static Logger log = Logger.getLogger(ClustalWS.class);\r
60 \r
61         private static volatile WSLogger logger;\r
62 \r
63         private static final RunnerConfig<ClustalW> clustalOptions = Util\r
64                         .getSupportedOptions(ClustalW.class);\r
65 \r
66         private static final PresetManager<ClustalW> clustalPresets = Util\r
67                         .getPresets(ClustalW.class);\r
68 \r
69         private static final LimitsManager<ClustalW> limitMan = compbio.runner.Util\r
70                         .getLimits(new ClustalW().getType());\r
71 \r
72         /*\r
73          * Initialise the logger. This cannot be done in the constructor as the\r
74          * WebServiceContext is not available at the object construction time\r
75          */\r
76         private WSLogger getLogger() {\r
77                 if (logger == null) {\r
78                         synchronized (ClustalWS.class) {\r
79                                 if (logger == null) {\r
80                                         logger = WSLogger.getStatLogger(Services.ClustalWS,\r
81                                                         wsContext);\r
82                                 }\r
83                         }\r
84                 }\r
85                 return logger;\r
86         }\r
87 \r
88         @Override\r
89         public String align(List<FastaSequence> sequences)\r
90                         throws JobSubmissionException {\r
91 \r
92                 WSUtil.validateFastaInput(sequences);\r
93                 ConfiguredExecutable<ClustalW> confClust = init(sequences);\r
94                 return WSUtil.align(sequences, confClust, getLogger(), "align",\r
95                                 getLimit(""));\r
96         }\r
97 \r
98         ConfiguredExecutable<ClustalW> init(List<FastaSequence> dataSet)\r
99                         throws JobSubmissionException {\r
100                 ClustalW clustal = new ClustalW();\r
101                 ConfiguredExecutable<ClustalW> confClust = Configurator\r
102                                 .configureExecutable(clustal, dataSet);\r
103                 return confClust;\r
104         }\r
105 \r
106         @Override\r
107         public String presetAlign(List<FastaSequence> sequences,\r
108                         Preset<ClustalW> preset) throws JobSubmissionException,\r
109                         WrongParameterException {\r
110                 WSUtil.validateFastaInput(sequences);\r
111                 if (preset == null) {\r
112                         throw new WrongParameterException("Preset must be provided!");\r
113                 }\r
114                 Limit<ClustalW> limit = getLimit(preset.getName());\r
115                 ConfiguredExecutable<ClustalW> confClust = init(sequences);\r
116                 confClust.addParameters(preset.getOptions());\r
117                 return WSUtil.align(sequences, confClust, getLogger(), "presetAlign",\r
118                                 limit);\r
119         }\r
120 \r
121         @Override\r
122         public String customAlign(List<FastaSequence> sequences,\r
123                         List<Option<ClustalW>> options) throws JobSubmissionException,\r
124                         WrongParameterException {\r
125                 WSUtil.validateFastaInput(sequences);\r
126                 ConfiguredExecutable<ClustalW> confClust = init(sequences);\r
127                 List<String> params = WSUtil.getCommands(options,\r
128                                 ClustalW.KEY_VALUE_SEPARATOR);\r
129                 confClust.addParameters(params);\r
130                 log.info("Setting parameters: " + params);\r
131                 return WSUtil.align(sequences, confClust, getLogger(), "customAlign",\r
132                                 getLimit(""));\r
133         }\r
134 \r
135         @Override\r
136         public RunnerConfig<ClustalW> getRunnerOptions() {\r
137                 Timer timer = Timer.getMilliSecondsTimer();\r
138                 getLogger().logAll(timer, "getRunnerOptions");\r
139                 return clustalOptions;\r
140         }\r
141 \r
142         @SuppressWarnings("unchecked")\r
143         @Override\r
144         public Alignment getResult(String jobId) throws ResultNotAvailableException {\r
145                 Timer timer = Timer.getMilliSecondsTimer();\r
146                 WSUtil.validateJobId(jobId);\r
147                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
148                 ConfiguredExecutable<ClustalW> clustal = (ConfiguredExecutable<ClustalW>) asyncEngine\r
149                                 .getResults(jobId);\r
150                 Alignment al = clustal.getResults();\r
151                 getLogger().log(timer, "getResults", jobId);\r
152                 return al;\r
153         }\r
154 \r
155         @Override\r
156         public Limit<ClustalW> getLimit(String presetName) {\r
157                 Timer timer = Timer.getMilliSecondsTimer();\r
158                 Limit<ClustalW> limit = limitMan.getLimitByName(presetName);\r
159                 getLogger().logAll(timer, "getLimit");\r
160                 return limit;\r
161         }\r
162         @Override\r
163         public LimitsManager<ClustalW> getLimits() {\r
164                 return limitMan;\r
165         }\r
166 \r
167         @Override\r
168         public boolean cancelJob(String jobId) {\r
169                 Timer timer = Timer.getMilliSecondsTimer();\r
170                 WSUtil.validateJobId(jobId);\r
171                 boolean result = WSUtil.cancelJob(jobId);\r
172                 getLogger().logFine(timer, "Cancel");\r
173                 return result;\r
174         }\r
175 \r
176         @Override\r
177         public JobStatus getJobStatus(String jobId) {\r
178                 Timer timer = Timer.getMilliSecondsTimer();\r
179                 WSUtil.validateJobId(jobId);\r
180                 JobStatus status = WSUtil.getJobStatus(jobId);\r
181                 getLogger().logFine(timer, "getJobStatus");\r
182                 return status;\r
183         }\r
184 \r
185         @Override\r
186         public PresetManager<ClustalW> getPresets() {\r
187                 Timer timer = Timer.getMilliSecondsTimer();\r
188                 getLogger().logAll(timer, "pullExecStatistics");\r
189                 return clustalPresets;\r
190         }\r
191 \r
192         @Override\r
193         public ChunkHolder pullExecStatistics(String jobId, long position) {\r
194                 Timer timer = Timer.getMilliSecondsTimer();\r
195                 WSUtil.validateJobId(jobId);\r
196                 String file = Configurator.getWorkDirectory(jobId) + File.separator\r
197                                 + ClustalW.getStatFile();\r
198                 ChunkHolder cholder = WSUtil.pullFile(file, position);\r
199                 getLogger().logFine(timer, "pullExecStatistics");\r
200                 return cholder;\r
201         }\r
202 \r
203 }\r