Get rind of NativeClusterJob interface as pretty much every jobs best to use some...
[jabaws.git] / runner / compbio / runner / msa / Mafft.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 \r
31 import compbio.engine.client.PipedExecutable;\r
32 import compbio.engine.client.SkeletalExecutable;\r
33 import compbio.metadata.ResultNotAvailableException;\r
34 import compbio.runner.Util;\r
35 \r
36 /**\r
37  * \r
38  * @author pvtroshin\r
39  *\r
40  */\r
41 public class Mafft extends SkeletalExecutable<Mafft>\r
42                 implements\r
43                         PipedExecutable<Mafft> {\r
44         /*\r
45          * TODO get rid of piping: Mafft now supports --out option for output file. \r
46      * TODO enable multithreading support\r
47          */\r
48         \r
49         /**\r
50          * Number of cores to use, not used if "mafft.cluster.cpunum" property \r
51          * is not defined and in case of local execution \r
52          */\r
53         private int ncoreNumber = 0;\r
54         \r
55         /*\r
56          * Number of cores parameter name\r
57          */\r
58         private final static String ncorePrm = "--thread";\r
59         \r
60         private static Logger log = Logger.getLogger(Mafft.class);\r
61 \r
62         private static String autoOption = "--auto";\r
63 \r
64         private final String MATRIX_PAR_NAME = "--aamatrix";\r
65 \r
66         public static final String KEY_VALUE_SEPARATOR = Util.SPACE;\r
67 \r
68         public Mafft() {\r
69                 // remove default input to prevent it to appear in the parameters list\r
70                 // that could happen if the parameters are set first\r
71                 // super.setInput("");\r
72                 addParameters(Arrays.asList("--clustalout", autoOption));\r
73         }\r
74 \r
75         @SuppressWarnings("unchecked")\r
76         public Alignment getResults(String workDirectory)\r
77                         throws ResultNotAvailableException {\r
78                 try {\r
79                         return Util.readClustalFile(workDirectory, getOutput());\r
80                 } catch (FileNotFoundException e) {\r
81                         log.error(e.getMessage(), e.getCause());\r
82                         throw new ResultNotAvailableException(e);\r
83                 } catch (IOException e) {\r
84                         log.error(e.getMessage(), e.getCause());\r
85                         throw new ResultNotAvailableException(e);\r
86                 } catch (UnknownFileFormatException e) {\r
87                         log.error(e.getMessage(), e.getCause());\r
88                         throw new ResultNotAvailableException(e);\r
89                 } catch (NullPointerException e) {\r
90                         log.error(e.getMessage(), e.getCause());\r
91                         throw new ResultNotAvailableException(e);\r
92                 }\r
93         }\r
94 \r
95         @Override\r
96         public Mafft setInput(String inFile) {\r
97                 super.setInput(inFile);\r
98                 cbuilder.setLast(inFile);\r
99                 return this;\r
100         }\r
101 \r
102         /**\r
103          * Mafft input must always be the last parameter!\r
104          */\r
105         @Override\r
106         public Mafft addParameters(List<String> parameters) {\r
107                 cbuilder.addParams(parameters);\r
108                 cbuilder.removeParam(autoOption);\r
109                 return this;\r
110         }\r
111 \r
112 \r
113         public void setNCore(int ncoreNumber) {\r
114                 if (ncoreNumber < 1 || ncoreNumber > 100) {\r
115                         throw new IndexOutOfBoundsException(\r
116                                         "Number of cores must be within 1 and 100 ");\r
117                 }\r
118                 this.ncoreNumber = ncoreNumber;\r
119                 cbuilder.setParam(ncorePrm, Integer.toString(getNCore()));\r
120         }\r
121 \r
122         int getNCore() {\r
123                 return ncoreNumber;\r
124         }\r
125         \r
126         \r
127         @SuppressWarnings("unchecked")\r
128         @Override\r
129         public Class<Mafft> getType() {\r
130                 return (Class<Mafft>) this.getClass();\r
131         }\r
132 \r
133         /*\r
134          * @Override public List<String> getParameters(\r
135          * compbio.runner.Executable.ExecProvider provider) { for (int i = 0; i <\r
136          * param.size(); i++) { String par = param.get(i); if\r
137          * (isMatrixParameter(par)) { String matrixName = getValue(i); if (new\r
138          * File(matrixName).isAbsolute()) { // Matrix can be found so no actions\r
139          * necessary // This method has been called already and the matrix name //\r
140          * is modified to contain full path // no further actions is necessary\r
141          * break; } String matrixPath = Util.getExecProperty("matrix.path", this);\r
142          * String absMatrixPath = Util.convertToAbsolute(matrixPath); param.remove(i\r
143          * + 1); param.remove(i); super.addParameter(MATRIX_PAR_NAME);\r
144          * super.addParameter(absMatrixPath + File.separator + matrixName); break; }\r
145          * } return super.getParameters(provider); }\r
146          * \r
147          * boolean isMatrixParameter(String parameter) { assert\r
148          * !compbio.util.Util.isEmpty(parameter); if\r
149          * (parameter.toUpperCase().startsWith(MATRIX_PAR_NAME)) { return true; }\r
150          * return false; }\r
151          * \r
152          * String getValue(int i) { return param.get(i + 1); }\r
153          */\r
154 }\r