88f63585324521c49795190e4757a95e83469939
[jabaws.git] / runner / compbio / runner / msa / ClustalW.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.Executable;\r
31 import compbio.engine.client.SkeletalExecutable;\r
32 import compbio.metadata.ResultNotAvailableException;\r
33 import compbio.runner.Util;\r
34 \r
35 public class ClustalW extends SkeletalExecutable<ClustalW> {\r
36 \r
37         private static Logger log = Logger.getLogger(ClustalW.class);\r
38         private static final String EXEC_STAT_FILE = "stat.log";\r
39         private static final String TREE_FILE_EXT = ".dnd";\r
40 \r
41         public static final String KEY_VALUE_SEPARATOR = "=";\r
42 \r
43         public ClustalW() {\r
44                 super(KEY_VALUE_SEPARATOR);\r
45                 addParameters(Arrays.asList("-OUTORDER=ALIGNED", "-QUIET", "-STATS="\r
46                                 + EXEC_STAT_FILE));\r
47                 // set default in, outs and err files\r
48                 this.setInput(super.inputFile);\r
49                 this.setOutput(super.outputFile);\r
50                 this.setError(super.errorFile);\r
51         }\r
52 \r
53         @Override\r
54         public ClustalW setOutput(String outFile) {\r
55                 super.setOutput(outFile);\r
56                 cbuilder.setParam("-OUTFILE=" + outFile);\r
57                 return this;\r
58         }\r
59 \r
60         @Override\r
61         public ClustalW setInput(String inFile) {\r
62                 super.setInput(inFile);\r
63                 cbuilder.setParam("-INFILE=" + inFile);\r
64                 return this;\r
65         }\r
66 \r
67         @SuppressWarnings("unchecked")\r
68         public Alignment getResults(String workDirectory)\r
69                         throws ResultNotAvailableException {\r
70                 try {\r
71                         return Util.readClustalFile(workDirectory, getOutput());\r
72                 } catch (FileNotFoundException e) {\r
73                         log.error(e.getMessage(), e.getCause());\r
74                         throw new ResultNotAvailableException(e);\r
75                 } catch (IOException e) {\r
76                         log.error(e.getMessage(), e.getCause());\r
77                         throw new ResultNotAvailableException(e);\r
78                 } catch (UnknownFileFormatException e) {\r
79                         log.error(e.getMessage(), e.getCause());\r
80                         throw new ResultNotAvailableException(e);\r
81                 } catch (NullPointerException e) {\r
82                         log.error(e.getMessage(), e.getCause());\r
83                         throw new ResultNotAvailableException(e);\r
84                 }\r
85         }\r
86 \r
87         @Override\r
88         public List<String> getCreatedFiles() {\r
89                 return Arrays.asList(getOutput(), EXEC_STAT_FILE,\r
90                                 convertInputNameToTreeName());\r
91         }\r
92 \r
93         /*\r
94          * Clustal output tree with same name as input file but .dnd extension e.g.\r
95          * this methods do similar conversion TO122.fasta -> TO122.dnd or\r
96          * TO122.fasta.in -> TO122.fasta.dnd It does not seems that there is any\r
97          * limits on the name length\r
98          * \r
99          * @return\r
100          */\r
101         private String convertInputNameToTreeName() {\r
102                 assert super.getInput() != null;\r
103                 int dotIdx = getInput().lastIndexOf(".");\r
104                 String treeFileName = "";\r
105                 if (dotIdx > 0) {\r
106                         treeFileName = getInput().substring(0, dotIdx);\r
107                 }\r
108                 return treeFileName + TREE_FILE_EXT;\r
109         }\r
110 \r
111         public static String getStatFile() {\r
112                 return EXEC_STAT_FILE;\r
113         }\r
114 \r
115         @SuppressWarnings("unchecked")\r
116         @Override\r
117         public Class<Executable<ClustalW>> getType() {\r
118                 return (Class<Executable<ClustalW>>) this.getClass();\r
119         }\r
120 \r
121 }\r