Finally, the new web services are working
[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.Limit;\r
33 import compbio.metadata.LimitsManager;\r
34 import compbio.metadata.ResultNotAvailableException;\r
35 import compbio.runner.Util;\r
36 \r
37 public class ClustalW extends SkeletalExecutable<ClustalW> {\r
38 \r
39     private static Logger log = Logger.getLogger(ClustalW.class);\r
40     private static final String EXEC_STAT_FILE = "stat.log";\r
41     private static final String TREE_FILE_EXT = ".dnd";\r
42 \r
43     public static final String KEY_VALUE_SEPARATOR = "=";\r
44 \r
45     // Cache for Limits information\r
46     private static LimitsManager<ClustalW> limits;\r
47 \r
48     public ClustalW() {\r
49         super(KEY_VALUE_SEPARATOR);\r
50         addParameters(Arrays.asList("-OUTORDER=ALIGNED", "-QUIET", "-STATS="\r
51                 + EXEC_STAT_FILE));\r
52         // set default in, outs and err files\r
53         this.setInput(super.inputFile);\r
54         this.setOutput(super.outputFile);\r
55         this.setError(super.errorFile);\r
56     }\r
57 \r
58     @Override\r
59     public ClustalW setOutput(String outFile) {\r
60         super.setOutput(outFile);\r
61         cbuilder.setParam("-OUTFILE=" + outFile);\r
62         return this;\r
63     }\r
64 \r
65     @Override\r
66     public ClustalW setInput(String inFile) {\r
67         super.setInput(inFile);\r
68         cbuilder.setParam("-INFILE=" + inFile);\r
69         return this;\r
70     }\r
71 \r
72     @SuppressWarnings("unchecked")\r
73     public Alignment getResults(String workDirectory)\r
74             throws ResultNotAvailableException {\r
75         try {\r
76             return Util.readClustalFile(workDirectory, getOutput());\r
77         } catch (FileNotFoundException e) {\r
78             log.error(e.getMessage(), e.getCause());\r
79             throw new ResultNotAvailableException(e);\r
80         } catch (IOException e) {\r
81             log.error(e.getMessage(), e.getCause());\r
82             throw new ResultNotAvailableException(e);\r
83         } catch (UnknownFileFormatException e) {\r
84             log.error(e.getMessage(), e.getCause());\r
85             throw new ResultNotAvailableException(e);\r
86         } catch (NullPointerException e) {\r
87             log.error(e.getMessage(), e.getCause());\r
88             throw new ResultNotAvailableException(e);\r
89         }\r
90     }\r
91 \r
92     @Override\r
93     public List<String> getCreatedFiles() {\r
94         return Arrays.asList(getOutput(), EXEC_STAT_FILE,\r
95                 convertInputNameToTreeName());\r
96     }\r
97 \r
98     /*\r
99      * Clustal output tree with same name as input file but .dnd extension e.g.\r
100      * this methods do similar conversion TO122.fasta -> TO122.dnd or\r
101      * TO122.fasta.in -> TO122.fasta.dnd It does not seems that there is any\r
102      * limits on the name length\r
103      * \r
104      * @return\r
105      */\r
106     private String convertInputNameToTreeName() {\r
107         assert super.getInput() != null;\r
108         int dotIdx = getInput().lastIndexOf(".");\r
109         String treeFileName = "";\r
110         if (dotIdx > 0) {\r
111             treeFileName = getInput().substring(0, dotIdx);\r
112         }\r
113         return treeFileName + TREE_FILE_EXT;\r
114     }\r
115 \r
116     public static String getStatFile() {\r
117         return EXEC_STAT_FILE;\r
118     }\r
119 \r
120     @Override\r
121     public Limit<ClustalW> getLimit(String presetName) {\r
122 \r
123         if (limits == null) {\r
124             limits = getLimits();\r
125         }\r
126 \r
127         Limit<ClustalW> limit = null;\r
128         if (limits != null) {\r
129             // this returns default limit if preset is undefined!\r
130             limit = limits.getLimitByName(presetName);\r
131         }\r
132         // If limit is not defined for a particular preset, then return default\r
133         // limit\r
134         if (limit == null) {\r
135             log.debug("Limit for the preset " + presetName\r
136                     + " is not found. Using default");\r
137             limit = limits.getDefaultLimit();\r
138         }\r
139         return limit;\r
140     }\r
141 \r
142     @Override\r
143     public LimitsManager<ClustalW> getLimits() {\r
144         // synchronise on static field\r
145         synchronized (log) {\r
146             if (limits == null) {\r
147                 limits = Util.getLimits(this.getClass());\r
148             }\r
149         }\r
150         return limits;\r
151     }\r
152 \r
153     @Override\r
154     public Class<? extends Executable<?>> getType() {\r
155         return this.getClass();\r
156     }\r
157 \r
158 }\r