Finally, the new web services are working
[jabaws.git] / runner / compbio / runner / msa / Tcoffee.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.runner.msa;\r
20 \r
21 import java.io.FileNotFoundException;\r
22 import java.io.IOException;\r
23 import java.security.InvalidParameterException;\r
24 import java.util.Arrays;\r
25 import java.util.List;\r
26 \r
27 import org.apache.log4j.Logger;\r
28 \r
29 import compbio.data.sequence.Alignment;\r
30 import compbio.data.sequence.UnknownFileFormatException;\r
31 import compbio.engine.client.ClusterNativeSpecExecutable;\r
32 import compbio.engine.client.CommandBuilder;\r
33 import compbio.engine.client.Executable;\r
34 import compbio.engine.client.PipedExecutable;\r
35 import compbio.engine.client.SkeletalExecutable;\r
36 import compbio.engine.conf.PropertyHelperManager;\r
37 import compbio.metadata.Limit;\r
38 import compbio.metadata.LimitsManager;\r
39 import compbio.metadata.ResultNotAvailableException;\r
40 import compbio.runner.Util;\r
41 import compbio.util.PropertyHelper;\r
42 \r
43 public class Tcoffee extends SkeletalExecutable<Tcoffee> implements\r
44 PipedExecutable<Tcoffee>, ClusterNativeSpecExecutable<Tcoffee> {\r
45 \r
46         private static Logger log = Logger.getLogger(Tcoffee.class);\r
47 \r
48         private static PropertyHelper ph = PropertyHelperManager\r
49         .getPropertyHelper();\r
50 \r
51         // Cache for Limits information\r
52         private static LimitsManager<Tcoffee> limits;\r
53 \r
54         public static final String KEY_VALUE_SEPARATOR = "=";\r
55 \r
56         /**\r
57          * Number of cores to use, defaults to 1 for local execution or the value of\r
58          * "tcoffee.cluster.cpunum" property for cluster execution\r
59          */\r
60         private int ncoreNumber = 0;\r
61 \r
62         /*\r
63          * Number of cores parameter name\r
64          */\r
65         private final static String ncorePrm = "-n_core";\r
66 \r
67         /**\r
68          * \r
69          * @param workDirectory\r
70          */\r
71         public Tcoffee() {\r
72                 super(KEY_VALUE_SEPARATOR);\r
73                 /*\r
74                  * Use "-quiet" to disable sdtout and progress to stderr inorder=input -\r
75                  * prevent t-coffee from sorting sequences\r
76                  */\r
77                 addParameters(Arrays.asList("-output=clustalw"));\r
78                 setInput(super.inputFile);\r
79         }\r
80 \r
81         @Override\r
82         public Tcoffee setInput(String inFile) {\r
83                 super.setInput(inFile);\r
84                 cbuilder.setParam("-seq", inFile);\r
85                 return this;\r
86         }\r
87 \r
88         @SuppressWarnings("unchecked")\r
89         @Override\r
90         public Alignment getResults(String workDirectory)\r
91         throws ResultNotAvailableException {\r
92                 try {\r
93                         return Util.readClustalFile(workDirectory, getOutput());\r
94                 } catch (FileNotFoundException e) {\r
95                         log.error(e.getMessage(), e.getCause());\r
96                         throw new ResultNotAvailableException(e);\r
97                 } catch (IOException e) {\r
98                         log.error(e.getMessage(), e.getCause());\r
99                         throw new ResultNotAvailableException(e);\r
100                 } catch (UnknownFileFormatException e) {\r
101                         log.error(e.getMessage(), e.getCause());\r
102                         throw new ResultNotAvailableException(e);\r
103                 } catch (NullPointerException e) {\r
104                         log.error(e.getMessage(), e.getCause());\r
105                         throw new ResultNotAvailableException(e);\r
106                 }\r
107         }\r
108 \r
109         @Override\r
110         public List<String> getCreatedFiles() {\r
111                 return Arrays.asList(getOutput());\r
112         }\r
113 \r
114         public void setNCore(int ncoreNumber) {\r
115                 if (ncoreNumber < 1 || ncoreNumber > 100) {\r
116                         throw new IndexOutOfBoundsException(\r
117                                         "Number of cores must be within 1 and 100 ");\r
118                 }\r
119                 this.ncoreNumber = ncoreNumber;\r
120                 cbuilder.setParam(ncorePrm, Integer.toString(getNCore()));\r
121         }\r
122 \r
123         int getNCore() {\r
124                 return ncoreNumber;\r
125         }\r
126 \r
127         @Override\r
128         public CommandBuilder<Tcoffee> getParameters(ExecProvider provider) {\r
129                 // Limit number of cores to 1 for ANY execution which does not set\r
130                 // Ncores explicitly using setNCore method\r
131                 if (ncoreNumber == 0) {\r
132                         setNCore(1);\r
133                 }\r
134                 if (provider == Executable.ExecProvider.Cluster) {\r
135                         int cpunum = SkeletalExecutable.getClusterCpuNum(getType());\r
136                         if (cpunum != 0) {\r
137                                 setNCore(cpunum);\r
138                         }\r
139                 }\r
140                 return super.getParameters(provider);\r
141         }\r
142 \r
143         @Override\r
144         public Limit<Tcoffee> getLimit(String presetName) {\r
145                 if (limits == null) {\r
146                         limits = getLimits();\r
147                 }\r
148 \r
149                 Limit<Tcoffee> limit = null;\r
150                 if (limits != null) {\r
151                         // this returns default limit if preset is undefined!\r
152                         limit = limits.getLimitByName(presetName);\r
153                 }\r
154                 // If limit is not defined for a particular preset, then return default\r
155                 // limit\r
156                 if (limit == null) {\r
157                         log.debug("Limit for the preset " + presetName\r
158                                         + " is not found. Using default");\r
159                         limit = limits.getDefaultLimit();\r
160                 }\r
161                 return limit;\r
162         }\r
163 \r
164         @Override\r
165         public LimitsManager<Tcoffee> getLimits() {\r
166                 // synchronise on static field\r
167                 synchronized (log) {\r
168                         if (limits == null) {\r
169                                 limits = Util.getLimits(this.getClass());\r
170                         }\r
171                 }\r
172                 return limits;\r
173         }\r
174 \r
175         @Override\r
176         public String getNativeSpecs() {\r
177                 return getClusterSettings();\r
178         }\r
179 \r
180 \r
181         @Override\r
182         public Class<? extends Executable<?>> getType() {\r
183                 return this.getClass();\r
184         }\r
185 }\r