ae0d8ec1033b93c6a7b9e5557bbc0531f015604d
[jabaws.git] / runner / compbio / runner / msa / ClustalO.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 \r
25 import org.apache.log4j.Logger;\r
26 \r
27 import compbio.data.sequence.Alignment;\r
28 import compbio.data.sequence.UnknownFileFormatException;\r
29 import compbio.engine.client.CommandBuilder;\r
30 import compbio.engine.client.Executable;\r
31 import compbio.engine.client.SkeletalExecutable;\r
32 import compbio.engine.client.Executable.ExecProvider;\r
33 import compbio.metadata.ResultNotAvailableException;\r
34 import compbio.runner.Util;\r
35 \r
36 public class ClustalO extends SkeletalExecutable<ClustalO> {\r
37 \r
38         private static Logger log = Logger.getLogger(ClustalO.class);\r
39         private static final String EXEC_STAT_FILE = "stat.log";\r
40         \r
41 \r
42         public static final String KEY_VALUE_SEPARATOR = "=";\r
43         \r
44         /*\r
45          * Number of cores parameter name\r
46          */\r
47         private final static String ncorePrm = "--threads";\r
48 \r
49         /**\r
50          * Number of cores to use, defaults to 1 for local execution or the value of\r
51          * "tcoffee.cluster.cpunum" property for cluster execution\r
52          */\r
53         private int ncoreNumber = 0;\r
54 \r
55         \r
56         /**\r
57          * --threads=<n> Number of processors to use\r
58          * \r
59          * -l, --log=<file> Log all non-essential output to this file\r
60          */\r
61         public ClustalO() {\r
62                 super(KEY_VALUE_SEPARATOR);\r
63                 addParameters(Arrays.asList("--outfmt=clustal", "-v", "--log="\r
64                                 + EXEC_STAT_FILE));\r
65                 // set default in, outs and err files\r
66                 this.setInput(super.inputFile);\r
67                 this.setOutput(super.outputFile);\r
68                 this.setError(super.errorFile);\r
69         }\r
70 \r
71         @Override\r
72         public ClustalO setOutput(String outFile) {\r
73                 super.setOutput(outFile);\r
74                 cbuilder.setParam("--outfile=" + outFile);\r
75                 return this;\r
76         }\r
77 \r
78         @Override\r
79         public ClustalO setInput(String inFile) {\r
80                 super.setInput(inFile);\r
81                 cbuilder.setParam("--infile=" + inFile);\r
82                 return this;\r
83         }\r
84 \r
85         @SuppressWarnings("unchecked")\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         public static String getStatFile() {\r
106                 return EXEC_STAT_FILE;\r
107         }\r
108 \r
109         @SuppressWarnings("unchecked")\r
110         @Override\r
111         public Class<ClustalO> getType() {\r
112                 return (Class<ClustalO>) this.getClass();\r
113         }\r
114         \r
115         @Override\r
116         public CommandBuilder<ClustalO> getParameters(ExecProvider provider) {\r
117                 // Limit number of cores to 1 for ANY execution which does not set\r
118                 // Ncores explicitly using setNCore method\r
119                 if (ncoreNumber == 0) {\r
120                         setNCore(1);\r
121                 }\r
122                 if (provider == Executable.ExecProvider.Cluster) {\r
123                         int cpunum = SkeletalExecutable.getClusterCpuNum(getType());\r
124                         if (cpunum != 0) {\r
125                                 setNCore(cpunum);\r
126                         } \r
127                 }\r
128                 return super.getParameters(provider);\r
129         }\r
130         \r
131         public void setNCore(int ncoreNumber) {\r
132                 if (ncoreNumber < 1 || ncoreNumber > 100) {\r
133                         throw new IndexOutOfBoundsException(\r
134                                         "Number of cores must be within 1 and 100 ");\r
135                 }\r
136                 this.ncoreNumber = ncoreNumber;\r
137                 cbuilder.setParam(ncorePrm, Integer.toString(getNCore()));\r
138         }\r
139 \r
140         int getNCore() {\r
141                 return ncoreNumber;\r
142         }\r
143 \r
144 \r
145 }\r