Gramma improved
[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     /*\r
70      * Initialise the logger. This cannot be done in the constructor as the\r
71      * WebServiceContext is not available at the object construction time\r
72      */\r
73     private WSLogger getLogger() {\r
74         if (logger == null) {\r
75             synchronized (ClustalWS.class) {\r
76                 if (logger == null) {\r
77                     logger = WSLogger.getStatLogger(Services.ClustalWS,\r
78                             wsContext);\r
79                 }\r
80             }\r
81         }\r
82         return logger;\r
83     }\r
84 \r
85     @Override\r
86     public String align(List<FastaSequence> sequences)\r
87             throws JobSubmissionException {\r
88         WSUtil.validateFastaInput(sequences);\r
89         ConfiguredExecutable<ClustalW> confClust = init(sequences);\r
90         return WSUtil.align(sequences, confClust, getLogger(), "align",\r
91                 getLimit(""));\r
92     }\r
93 \r
94     ConfiguredExecutable<ClustalW> init(List<FastaSequence> dataSet)\r
95             throws JobSubmissionException {\r
96         ClustalW clustal = new ClustalW();\r
97         ConfiguredExecutable<ClustalW> confClust = Configurator\r
98                 .configureExecutable(clustal, dataSet);\r
99         return confClust;\r
100     }\r
101 \r
102     @Override\r
103     public String presetAlign(List<FastaSequence> sequences,\r
104             Preset<ClustalW> preset) throws JobSubmissionException,\r
105             WrongParameterException {\r
106         WSUtil.validateFastaInput(sequences);\r
107         if (preset == null) {\r
108             throw new WrongParameterException("Preset must be provided!");\r
109         }\r
110         Limit<ClustalW> limit = getLimit(preset.getName());\r
111         ConfiguredExecutable<ClustalW> confClust = init(sequences);\r
112         confClust.addParameters(preset.getOptions());\r
113         return WSUtil.align(sequences, confClust, getLogger(), "presetAlign",\r
114                 limit);\r
115     }\r
116 \r
117     @Override\r
118     public String customAlign(List<FastaSequence> sequences,\r
119             List<Option<ClustalW>> options) throws JobSubmissionException,\r
120             WrongParameterException {\r
121         WSUtil.validateFastaInput(sequences);\r
122         ConfiguredExecutable<ClustalW> confClust = init(sequences);\r
123         List<String> params = WSUtil.getCommands(options,\r
124                 ClustalW.KEY_VALUE_SEPARATOR);\r
125         confClust.addParameters(params);\r
126         log.info("Setting parameters: " + params);\r
127         return WSUtil.align(sequences, confClust, getLogger(), "customAlign",\r
128                 getLimit(""));\r
129     }\r
130 \r
131     @Override\r
132     public RunnerConfig<ClustalW> getRunnerOptions() {\r
133         Timer timer = Timer.getMilliSecondsTimer();\r
134         getLogger().logAll(timer, "getRunnerOptions");\r
135         return clustalOptions;\r
136     }\r
137 \r
138     @SuppressWarnings("unchecked")\r
139     @Override\r
140     public Alignment getResult(String jobId) throws ResultNotAvailableException {\r
141         Timer timer = Timer.getMilliSecondsTimer();\r
142         WSUtil.validateJobId(jobId);\r
143         AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
144         ConfiguredExecutable<ClustalW> clustal = (ConfiguredExecutable<ClustalW>) asyncEngine\r
145                 .getResults(jobId);\r
146         Alignment al = clustal.getResults();\r
147         getLogger().log(timer, "getResults", jobId);\r
148         return al;\r
149     }\r
150 \r
151     @Override\r
152     public Limit<ClustalW> getLimit(String presetName) {\r
153         Timer timer = Timer.getMilliSecondsTimer();\r
154         Limit<ClustalW> limit = new ClustalW().getLimit(presetName);\r
155         getLogger().logAll(timer, "getLimit");\r
156         return limit;\r
157     }\r
158 \r
159     @Override\r
160     public LimitsManager<ClustalW> getLimits() {\r
161         Timer timer = Timer.getMilliSecondsTimer();\r
162         LimitsManager<ClustalW> limits = new ClustalW().getLimits();\r
163         getLogger().logAll(timer, "getLimits");\r
164         return limits;\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