Next version of JABA
[jabaws.git] / runner / compbio / runner / msa / Muscle.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 javax.xml.bind.annotation.XmlTransient;\r
27 \r
28 import org.apache.log4j.Logger;\r
29 \r
30 import compbio.data.sequence.Alignment;\r
31 import compbio.data.sequence.UnknownFileFormatException;\r
32 import compbio.engine.client.Executable;\r
33 import compbio.engine.client.SkeletalExecutable;\r
34 import compbio.metadata.Limit;\r
35 import compbio.metadata.LimitsManager;\r
36 import compbio.metadata.ResultNotAvailableException;\r
37 import compbio.runner.Util;\r
38 \r
39 public class Muscle extends SkeletalExecutable<Muscle> {\r
40 \r
41     /*\r
42      * Tell JAXB to ignore this while marshalling\r
43      */\r
44     @XmlTransient\r
45     private static Logger log = Logger.getLogger(Muscle.class);\r
46 \r
47     // Cache for Limits information\r
48     private static LimitsManager<Muscle> limits;\r
49 \r
50     private static final String EXEC_STAT_FILE = "stat.log";\r
51 \r
52     public static final String KEY_VALUE_SEPARATOR = Util.SPACE;\r
53 \r
54     /**\r
55      * Default options are\r
56      * \r
57      * -clwstrict - write output in clustal format\r
58      * \r
59      * @param workDirectory\r
60      */\r
61     public Muscle() {\r
62         /*\r
63          * The –quiet command-line option disables writing progress messages to\r
64          * standard error. If the –verbose command-line option is specified, a\r
65          * progress message will be written to the log file when each iteration\r
66          * completes. So –quiet and –verbose are not contradictory."-quiet",\r
67          * "-verbose"\r
68          */\r
69         addParameters(Arrays.asList("-clwstrict", "-quiet", "-verbose"));\r
70         cbuilder.setParam("-log", EXEC_STAT_FILE);\r
71     }\r
72 \r
73     @Override\r
74     public Muscle setOutput(String outFile) {\r
75         super.setOutput(outFile);\r
76         cbuilder.setParam("-out", outFile);\r
77         return this;\r
78     }\r
79 \r
80     @Override\r
81     public Muscle setInput(String inFile) {\r
82         super.setInput(inFile);\r
83         cbuilder.setParam("-in", inFile);\r
84         return this;\r
85     }\r
86 \r
87     @SuppressWarnings("unchecked")\r
88     @Override\r
89     public Alignment getResults(String workDirectory)\r
90             throws ResultNotAvailableException {\r
91         try {\r
92             return Util.readClustalFile(workDirectory, getOutput());\r
93         } catch (FileNotFoundException e) {\r
94             log.error(e.getMessage(), e.getCause());\r
95             throw new ResultNotAvailableException(e);\r
96         } catch (IOException e) {\r
97             log.error(e.getMessage(), e.getCause());\r
98             throw new ResultNotAvailableException(e);\r
99         } catch (UnknownFileFormatException e) {\r
100             log.error(e.getMessage(), e.getCause());\r
101             throw new ResultNotAvailableException(e);\r
102         } catch (NullPointerException e) {\r
103             log.error(e.getMessage(), e.getCause());\r
104             throw new ResultNotAvailableException(e);\r
105         }\r
106     }\r
107 \r
108     @Override\r
109     public List<String> getCreatedFiles() {\r
110         return Arrays.asList(getOutput(), EXEC_STAT_FILE);\r
111     }\r
112 \r
113     public static String getStatFile() {\r
114         return EXEC_STAT_FILE;\r
115     }\r
116 \r
117     @Override\r
118     public Limit<Muscle> getLimit(String presetName) {\r
119 \r
120         if (limits == null) {\r
121             limits = getLimits();\r
122         }\r
123 \r
124         Limit<Muscle> limit = null;\r
125         if (limits != null) {\r
126             // this returns default limit if preset is undefined!\r
127             limit = limits.getLimitByName(presetName);\r
128         }\r
129         // If limit is not defined for a particular preset, then return default\r
130         // limit\r
131         if (limit == null) {\r
132             log.debug("Limit for the preset " + presetName\r
133                     + " is not found. Using default");\r
134             limit = limits.getDefaultLimit();\r
135         }\r
136         return limit;\r
137     }\r
138 \r
139     @Override\r
140     public LimitsManager<Muscle> getLimits() {\r
141         // synchronise on static field\r
142         synchronized (log) {\r
143             if (limits == null) {\r
144                 limits = Util.getLimits(this.getClass());\r
145             }\r
146         }\r
147         return limits;\r
148     }\r
149 \r
150     @Override\r
151     public Class<? extends Executable<?>> getType() {\r
152         return this.getClass();\r
153     }\r
154 \r
155 }\r