Change the way to deal with Limits to simplify wrapper writing and enable couping...
[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.util.Arrays;\r
24 import java.util.List;\r
25 \r
26 import org.apache.log4j.Logger;\r
27 \r
28 import compbio.data.sequence.Alignment;\r
29 import compbio.data.sequence.UnknownFileFormatException;\r
30 import compbio.engine.client.ClusterNativeSpecExecutable;\r
31 import compbio.engine.client.CommandBuilder;\r
32 import compbio.engine.client.Executable;\r
33 import compbio.engine.client.PipedExecutable;\r
34 import compbio.engine.client.SkeletalExecutable;\r
35 import compbio.engine.conf.PropertyHelperManager;\r
36 import compbio.metadata.ResultNotAvailableException;\r
37 import compbio.runner.Util;\r
38 import compbio.util.PropertyHelper;\r
39 \r
40 public class Tcoffee extends SkeletalExecutable<Tcoffee>\r
41                 implements\r
42                         PipedExecutable<Tcoffee>,\r
43                         ClusterNativeSpecExecutable<Tcoffee> {\r
44 \r
45         private static Logger log = Logger.getLogger(Tcoffee.class);\r
46 \r
47         private static PropertyHelper ph = PropertyHelperManager\r
48                         .getPropertyHelper();\r
49 \r
50         public static final String KEY_VALUE_SEPARATOR = "=";\r
51 \r
52         /**\r
53          * Number of cores to use, defaults to 1 for local execution or the value of\r
54          * "tcoffee.cluster.cpunum" property for cluster execution\r
55          */\r
56         private int ncoreNumber = 0;\r
57 \r
58         /*\r
59          * Number of cores parameter name\r
60          */\r
61         private final static String ncorePrm = "-n_core";\r
62 \r
63         /**\r
64          * \r
65          * @param workDirectory\r
66          */\r
67         public Tcoffee() {\r
68                 super(KEY_VALUE_SEPARATOR);\r
69                 /*\r
70                  * Use "-quiet" to disable sdtout and progress to stderr inorder=input -\r
71                  * prevent t-coffee from sorting sequences\r
72                  */\r
73                 addParameters(Arrays.asList("-output=clustalw"));\r
74                 setInput(super.inputFile);\r
75         }\r
76 \r
77         @Override\r
78         public Tcoffee setInput(String inFile) {\r
79                 super.setInput(inFile);\r
80                 cbuilder.setParam("-seq", inFile);\r
81                 return this;\r
82         }\r
83 \r
84         @SuppressWarnings("unchecked")\r
85         @Override\r
86         public Alignment getResults(String workDirectory)\r
87                         throws ResultNotAvailableException {\r
88                 try {\r
89                         return Util.readClustalFile(workDirectory, getOutput());\r
90                 } catch (FileNotFoundException e) {\r
91                         log.error(e.getMessage(), e.getCause());\r
92                         throw new ResultNotAvailableException(e);\r
93                 } catch (IOException e) {\r
94                         log.error(e.getMessage(), e.getCause());\r
95                         throw new ResultNotAvailableException(e);\r
96                 } catch (UnknownFileFormatException e) {\r
97                         log.error(e.getMessage(), e.getCause());\r
98                         throw new ResultNotAvailableException(e);\r
99                 } catch (NullPointerException e) {\r
100                         log.error(e.getMessage(), e.getCause());\r
101                         throw new ResultNotAvailableException(e);\r
102                 }\r
103         }\r
104 \r
105         @Override\r
106         public List<String> getCreatedFiles() {\r
107                 return Arrays.asList(getOutput());\r
108         }\r
109 \r
110         public void setNCore(int ncoreNumber) {\r
111                 if (ncoreNumber < 1 || ncoreNumber > 100) {\r
112                         throw new IndexOutOfBoundsException(\r
113                                         "Number of cores must be within 1 and 100 ");\r
114                 }\r
115                 this.ncoreNumber = ncoreNumber;\r
116                 cbuilder.setParam(ncorePrm, Integer.toString(getNCore()));\r
117         }\r
118 \r
119         int getNCore() {\r
120                 return ncoreNumber;\r
121         }\r
122 \r
123         @Override\r
124         public CommandBuilder<Tcoffee> getParameters(ExecProvider provider) {\r
125                 // Limit number of cores to 1 for ANY execution which does not set\r
126                 // Ncores explicitly using setNCore method\r
127                 if (ncoreNumber == 0) {\r
128                         setNCore(1);\r
129                 }\r
130                 if (provider == Executable.ExecProvider.Cluster) {\r
131                         int cpunum = SkeletalExecutable.getClusterCpuNum(getType());\r
132                         if (cpunum != 0) {\r
133                                 setNCore(cpunum);\r
134                         }\r
135                 }\r
136                 return super.getParameters(provider);\r
137         }\r
138 \r
139         @Override\r
140         public String getNativeSpecs() {\r
141                 return getClusterSettings();\r
142         }\r
143 \r
144         @SuppressWarnings("unchecked")\r
145         @Override\r
146         public Class<Executable<Tcoffee>> getType() {\r
147                 return (Class<Executable<Tcoffee>>) this.getClass();\r
148         }\r
149 }\r