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