Clean up logging system
[jabaws.git] / runner / compbio / runner / msa / ClustalO.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.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 \r
40         private static final String EXEC_STAT_FILE = "stat.log";\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          * --threads=<n> Number of processors to use\r
57          * \r
58          * -l, --log=<file> Log all non-essential output to this file\r
59          */\r
60         public ClustalO() {\r
61                 super(KEY_VALUE_SEPARATOR);\r
62                 addParameters(Arrays.asList("--outfmt=clustal", "-v", "--log=" + EXEC_STAT_FILE));\r
63                 // set default in, outs and err files\r
64                 this.setInput(super.inputFile);\r
65                 this.setOutput(super.outputFile);\r
66                 this.setError(super.errorFile);\r
67         }\r
68 \r
69         @Override\r
70         public ClustalO setOutput(String outFile) {\r
71                 super.setOutput(outFile);\r
72                 cbuilder.setParam("--outfile=" + outFile);\r
73                 return this;\r
74         }\r
75 \r
76         @Override\r
77         public ClustalO setInput(String inFile) {\r
78                 super.setInput(inFile);\r
79                 cbuilder.setParam("--infile=" + inFile);\r
80                 return this;\r
81         }\r
82 \r
83         @SuppressWarnings("unchecked")\r
84         public Alignment getResults(String workDirectory)\r
85                         throws ResultNotAvailableException {\r
86                 try {\r
87                         return Util.readClustalFile(workDirectory, getOutput());\r
88                 } catch (FileNotFoundException e) {\r
89                         log.error(e.getMessage(), e.getCause());\r
90                         throw new ResultNotAvailableException(e);\r
91                 } catch (IOException e) {\r
92                         log.error(e.getMessage(), e.getCause());\r
93                         throw new ResultNotAvailableException(e);\r
94                 } catch (UnknownFileFormatException e) {\r
95                         log.error(e.getMessage(), e.getCause());\r
96                         throw new ResultNotAvailableException(e);\r
97                 } catch (NullPointerException e) {\r
98                         log.error(e.getMessage(), e.getCause());\r
99                         throw new ResultNotAvailableException(e);\r
100                 }\r
101         }\r
102 \r
103         public static String getStatFile() {\r
104                 return EXEC_STAT_FILE;\r
105         }\r
106 \r
107         @SuppressWarnings("unchecked")\r
108         @Override\r
109         public Class<ClustalO> getType() {\r
110                 return (Class<ClustalO>) this.getClass();\r
111         }\r
112         \r
113         @Override\r
114         public CommandBuilder<ClustalO> getParameters(ExecProvider provider) {\r
115                 // Limit number of cores to 1 for ANY execution which does not set\r
116                 // Ncores explicitly using setNCore method\r
117                 if (ncoreNumber == 0) {\r
118                         setNCore(1);\r
119                 }\r
120                 if (provider == Executable.ExecProvider.Cluster) {\r
121                         int cpunum = SkeletalExecutable.getClusterCpuNum(getType());\r
122                         if (cpunum != 0) {\r
123                                 setNCore(cpunum);\r
124                         } \r
125                 }\r
126                 return super.getParameters(provider);\r
127         }\r
128         \r
129         public void setNCore(int ncoreNumber) {\r
130                 if (ncoreNumber < 1 || ncoreNumber > 100) {\r
131                         throw new IndexOutOfBoundsException("Number of cores must be within 1 and 100 ");\r
132                 }\r
133                 this.ncoreNumber = ncoreNumber;\r
134                 cbuilder.setParam(ncorePrm, Integer.toString(getNCore()));\r
135         }\r
136 \r
137         int getNCore() {\r
138                 return ncoreNumber;\r
139         }\r
140 \r
141 }\r