f967f610e094099a5d98d441ab487577f23a397e
[jabaws.git] / webservices / compbio / ws / server / _MsaService.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 package compbio.ws.server;\r
19 \r
20 import java.io.File;\r
21 import java.util.List;\r
22 \r
23 import org.apache.log4j.Logger;\r
24 \r
25 import compbio.data.sequence.Alignment;\r
26 import compbio.data.sequence.FastaSequence;\r
27 import compbio.engine.AsyncExecutor;\r
28 import compbio.engine.Configurator;\r
29 import compbio.engine.client.ConfiguredExecutable;\r
30 import compbio.engine.client.SkeletalExecutable;\r
31 import compbio.metadata.ChunkHolder;\r
32 import compbio.metadata.JobSubmissionException;\r
33 import compbio.metadata.Limit;\r
34 import compbio.metadata.Option;\r
35 import compbio.metadata.Preset;\r
36 import compbio.metadata.ResultNotAvailableException;\r
37 import compbio.metadata.WrongParameterException;\r
38 import compbio.runner.msa.ClustalW;\r
39 \r
40 /**\r
41  * \r
42  * TODO to complete after the approach is tested with SequenceAnnotation!\r
43  * \r
44  * Common methods for all SequenceAnnotation web services\r
45  * \r
46  * @author pvtroshin\r
47  * \r
48  * @param <T>\r
49  * \r
50  * @version 1.0 June 2011\r
51  * @since 2.0\r
52  */\r
53 public abstract class _MsaService<T> extends GenericMetadataService {\r
54 \r
55         /*\r
56          * FIXME - instances of the Runner (?) and their types should be defined in\r
57          * Executable IF\r
58          */\r
59         _MsaService(SkeletalExecutable<T> exec, Logger log) {\r
60                 super(exec, log);\r
61         }\r
62 \r
63         public String align(List<FastaSequence> sequences)\r
64                         throws JobSubmissionException {\r
65 \r
66                 WSUtil.validateFastaInput(sequences);\r
67                 ConfiguredExecutable<T> confClust = init(sequences);\r
68                 return WSUtil.align(sequences, confClust, log, "align", getLimit(""));\r
69         }\r
70 \r
71         public String presetAlign(List<FastaSequence> sequences, Preset<T> preset)\r
72                         throws JobSubmissionException, WrongParameterException {\r
73                 WSUtil.validateFastaInput(sequences);\r
74                 if (preset == null) {\r
75                         throw new WrongParameterException("Preset must be provided!");\r
76                 }\r
77                 Limit<T> limit = getLimit(preset.getName());\r
78                 ConfiguredExecutable<T> confClust = init(sequences);\r
79                 confClust.addParameters(preset.getOptions());\r
80                 return WSUtil.align(sequences, confClust, log, "presetAlign", limit);\r
81         }\r
82 \r
83         public String customAlign(List<FastaSequence> sequences,\r
84                         List<Option<T>> options) throws JobSubmissionException,\r
85                         WrongParameterException {\r
86                 WSUtil.validateFastaInput(sequences);\r
87                 ConfiguredExecutable<T> confClust = init(sequences);\r
88                 List<String> params = WSUtil.getCommands(options,\r
89                                 ClustalW.KEY_VALUE_SEPARATOR);\r
90                 confClust.addParameters(params);\r
91                 log.info("Setting parameters: " + params);\r
92                 return WSUtil.align(sequences, confClust, log, "customAlign",\r
93                                 getLimit(""));\r
94         }\r
95 \r
96         @SuppressWarnings("unchecked")\r
97         public Alignment getResult(String jobId) throws ResultNotAvailableException {\r
98 \r
99                 WSUtil.validateJobId(jobId);\r
100                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
101                 ConfiguredExecutable<T> clustal = (ConfiguredExecutable<T>) asyncEngine\r
102                                 .getResults(jobId);\r
103                 Alignment al = clustal.getResults();\r
104                 return al;\r
105         }\r
106 \r
107         @Override\r
108         public ChunkHolder pullExecStatistics(String jobId, long position) {\r
109                 WSUtil.validateJobId(jobId);\r
110                 String file = Configurator.getWorkDirectory(jobId) + File.separator\r
111                                 + ClustalW.getStatFile();\r
112                 ChunkHolder cholder = WSUtil.pullFile(file, position);\r
113                 return cholder;\r
114         }\r
115 \r
116 }\r