Fix probem with metadata
[jabaws.git] / webservices / compbio / ws / server / ClustalWS.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.ClustalW;\r
50 \r
51 @WebService(endpointInterface = "compbio.data.msa.MsaWS", targetNamespace = JABAService.SERVICE_NAMESPACE, serviceName = "ClustalWS")\r
52 public class ClustalWS implements MsaWS<ClustalW> {\r
53 \r
54         private static Logger log = Logger.getLogger(ClustalWS.class);\r
55 \r
56         private static final RunnerConfig<ClustalW> clustalOptions = Util.getSupportedOptions(ClustalW.class);\r
57 \r
58         private static final PresetManager<ClustalW> clustalPresets = Util.getPresets(ClustalW.class);\r
59 \r
60         private static final LimitsManager<ClustalW> limitMan = compbio.engine.client.Util.getLimits(new ClustalW().getType());\r
61 \r
62         @Override\r
63         public String align(List<FastaSequence> sequences)\r
64                         throws JobSubmissionException {\r
65 \r
66                 WSUtil.validateFastaInput(sequences);\r
67                 ConfiguredExecutable<ClustalW> confClust = init(sequences);\r
68                 return WSUtil.align(sequences, confClust, log, "align", getLimit(""));\r
69         }\r
70 \r
71         ConfiguredExecutable<ClustalW> init(List<FastaSequence> dataSet)\r
72                         throws JobSubmissionException {\r
73                 ClustalW clustal = new ClustalW();\r
74                 clustal.setInput(SkeletalExecutable.INPUT);\r
75                 clustal.setOutput(SkeletalExecutable.OUTPUT);\r
76                 clustal.setError(SkeletalExecutable.ERROR);\r
77                 ConfiguredExecutable<ClustalW> confClust = Configurator.configureExecutable(clustal, dataSet);\r
78                 return confClust;\r
79         }\r
80 \r
81         @Override\r
82         public String presetAlign(List<FastaSequence> sequences,\r
83                         Preset<ClustalW> preset) throws JobSubmissionException,\r
84                         WrongParameterException {\r
85                 WSUtil.validateFastaInput(sequences);\r
86                 if (preset == null) {\r
87                         throw new WrongParameterException("Preset must be provided!");\r
88                 }\r
89                 Limit<ClustalW> limit = getLimit(preset.getName());\r
90                 ConfiguredExecutable<ClustalW> confClust = init(sequences);\r
91                 confClust.addParameters(preset.getOptions());\r
92                 return WSUtil.align(sequences, confClust, log, "presetAlign", limit);\r
93         }\r
94 \r
95         @Override\r
96         public String customAlign(List<FastaSequence> sequences,\r
97                         List<Option<ClustalW>> options) throws JobSubmissionException,\r
98                         WrongParameterException {\r
99                 WSUtil.validateFastaInput(sequences);\r
100                 ConfiguredExecutable<ClustalW> confClust = init(sequences);\r
101                 List<String> params = WSUtil.getCommands(options, ClustalW.KEY_VALUE_SEPARATOR);\r
102                 confClust.addParameters(params);\r
103                 log.info("Setting parameters: " + params);\r
104                 return WSUtil.align(sequences, confClust, log, "customAlign", getLimit(""));\r
105         }\r
106 \r
107         @Override\r
108         public RunnerConfig<ClustalW> getRunnerOptions() {\r
109                 return clustalOptions;\r
110         }\r
111 \r
112         @SuppressWarnings("unchecked")\r
113         @Override\r
114         public Alignment getResult(String jobId) throws ResultNotAvailableException {\r
115                 WSUtil.validateJobId(jobId);\r
116                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
117                 ConfiguredExecutable<ClustalW> clustal = (ConfiguredExecutable<ClustalW>) asyncEngine.getResults(jobId);\r
118                 Alignment al = clustal.getResults();\r
119                 return new Alignment (al.getSequences(), Program.CLUSTAL, '-');\r
120         }\r
121 \r
122         @Override\r
123         public Limit<ClustalW> getLimit(String presetName) {\r
124                 if (limitMan == null) {\r
125                         // No limit is configured\r
126                         return null;\r
127                 }\r
128                 Limit<ClustalW> limit = limitMan.getLimitByName(presetName);\r
129                 return limit;\r
130         }\r
131 \r
132         @Override\r
133         public LimitsManager<ClustalW> getLimits() {\r
134                 return limitMan;\r
135         }\r
136 \r
137         @Override\r
138         public boolean cancelJob(String jobId) {\r
139                 WSUtil.validateJobId(jobId);\r
140                 boolean result = WSUtil.cancelJob(jobId);\r
141                 return result;\r
142         }\r
143 \r
144         @Override\r
145         public JobStatus getJobStatus(String jobId) {\r
146                 WSUtil.validateJobId(jobId);\r
147                 JobStatus status = WSUtil.getJobStatus(jobId);\r
148                 return status;\r
149         }\r
150 \r
151         @Override\r
152         public PresetManager<ClustalW> getPresets() {\r
153                 return clustalPresets;\r
154         }\r
155 \r
156         @Override\r
157         public ChunkHolder pullExecStatistics(String jobId, long position) {\r
158                 WSUtil.validateJobId(jobId);\r
159                 String file = Configurator.getWorkDirectory(jobId) + File.separator + ClustalW.getStatFile();\r
160                 ChunkHolder cholder = WSUtil.pullFile(file, position);\r
161                 return cholder;\r
162         }\r
163 \r
164 }\r