More work on AAConWS not finished yet!
[jabaws.git] / runner / compbio / runner / conservation / AACon.java
1 /*\r
2  * Copyright (c) 2009 Peter Troshin JAva Bioinformatics Analysis Web Services\r
3  * (JABAWS) @version: 1.0 This library is free software; you can redistribute it\r
4  * and/or modify it under the terms of the Apache License version 2 as published\r
5  * by the Apache Software Foundation This library is distributed in the hope\r
6  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
7  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
8  * Apache License for more details. A copy of the license is in\r
9  * apache_license.txt. It is also available here:\r
10  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt Any republication or\r
11  * derived work distributed in source code form must include this copyright and\r
12  * license notice.\r
13  */\r
14 \r
15 package compbio.runner.conservation;\r
16 \r
17 import java.io.File;\r
18 import java.io.FileInputStream;\r
19 import java.io.FileNotFoundException;\r
20 import java.io.IOException;\r
21 import java.io.InputStream;\r
22 import java.util.Arrays;\r
23 import java.util.List;\r
24 import java.util.Map;\r
25 \r
26 import org.apache.log4j.Logger;\r
27 \r
28 import compbio.engine.client.CommandBuilder;\r
29 import compbio.engine.client.Executable;\r
30 import compbio.engine.client.SkeletalExecutable;\r
31 import compbio.metadata.Limit;\r
32 import compbio.metadata.LimitsManager;\r
33 import compbio.metadata.ResultNotAvailableException;\r
34 import compbio.runner.Util;\r
35 \r
36 /**\r
37  * Command line\r
38  * \r
39  * java -Xmx512 -jar jronn_v3.jar -i=test_seq.txt -n=1 -o=out.txt -s=stat.out\r
40  * \r
41  * @author pvtroshin\r
42  * \r
43  */\r
44 public class AACon extends SkeletalExecutable<AACon> {\r
45 \r
46         private static Logger log = Logger.getLogger(AACon.class);\r
47 \r
48         /**\r
49          * Number of cores to use, defaults to 1 for local execution or the value of\r
50          * "jronn.cluster.cpunum" property for cluster execution\r
51          */\r
52         private int ncoreNumber = 0;\r
53 \r
54         private final String ncorePrm = "-n=";\r
55 \r
56         // Cache for Limits information\r
57         private static LimitsManager<AACon> limits;\r
58 \r
59         public static final String KEY_VALUE_SEPARATOR = Util.SPACE;\r
60         public static final String STAT_FILE = "stat.txt";\r
61 \r
62         public AACon() {\r
63                 addParameters(Arrays.asList("-jar", getLibPath(), "-d=" + STAT_FILE,\r
64                                 "-f=RESULT_NO_ALIGNMENT"));\r
65         }\r
66 \r
67         @SuppressWarnings("unchecked")\r
68         @Override\r
69         public Map getResults(String workDirectory)\r
70                         throws ResultNotAvailableException {\r
71                 // MultiAnnotatedSequence<Method> annotations = null;\r
72                 try {\r
73                         InputStream inStream = new FileInputStream(new File(workDirectory,\r
74                                         getOutput()));\r
75                         // annotations = SequenceUtil.readResults(inStream);\r
76                         inStream.close();\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 (NullPointerException e) {\r
84                         log.error(e.getMessage(), e.getCause());\r
85                         throw new ResultNotAvailableException(e);\r
86                 }\r
87                 return null;\r
88         }\r
89 \r
90         private static String getLibPath() {\r
91 \r
92                 String settings = ph.getProperty("aacon.jar.file");\r
93                 if (compbio.util.Util.isEmpty(settings)) {\r
94                         throw new NullPointerException(\r
95                                         "Please define aacon.jar.file property in Executable.properties file"\r
96                                                         + "and initialize it with the location of jronn jar file");\r
97                 }\r
98                 if (new File(settings).isAbsolute()) {\r
99                         // Jronn jar can be found so no actions necessary\r
100                         // no further actions is necessary\r
101                         return settings;\r
102                 }\r
103                 return compbio.engine.client.Util.convertToAbsolute(settings);\r
104         }\r
105 \r
106         @Override\r
107         public List<String> getCreatedFiles() {\r
108                 return Arrays.asList(getOutput(), getError());\r
109         }\r
110 \r
111         @Override\r
112         public AACon setInput(String inFile) {\r
113                 super.setInput(inFile);\r
114                 cbuilder.setParam("-i=" + inFile);\r
115                 return this;\r
116         }\r
117 \r
118         @Override\r
119         public AACon setOutput(String outFile) {\r
120                 super.setOutput(outFile);\r
121                 cbuilder.setParam("-o=" + outFile);\r
122                 return this;\r
123         }\r
124 \r
125         @Override\r
126         public Limit<AACon> getLimit(String presetName) {\r
127                 if (limits == null) {\r
128                         limits = getLimits();\r
129                 }\r
130                 Limit<AACon> limit = null;\r
131                 if (limits != null) {\r
132                         // this returns default limit if preset is undefined!\r
133                         limit = limits.getLimitByName(presetName);\r
134                 }\r
135                 // If limit is not defined for a particular preset, then return default\r
136                 // limit\r
137                 if (limit == null) {\r
138                         log.debug("Limit for the preset " + presetName\r
139                                         + " is not found. Using default");\r
140                         limit = limits.getDefaultLimit();\r
141                 }\r
142                 return limit;\r
143         }\r
144 \r
145         @Override\r
146         public LimitsManager<AACon> getLimits() {\r
147                 // synchronise on static field\r
148                 synchronized (log) {\r
149                         if (limits == null) {\r
150                                 limits = Util.getLimits(this.getClass());\r
151                         }\r
152                 }\r
153                 return limits;\r
154         }\r
155 \r
156         @Override\r
157         public Class<? extends Executable<?>> getType() {\r
158                 return this.getClass();\r
159         }\r
160 \r
161         public static String getStatFile() {\r
162                 return STAT_FILE;\r
163         }\r
164 \r
165         public void setNCore(int ncoreNumber) {\r
166                 if (ncoreNumber < 1 || ncoreNumber > 100) {\r
167                         throw new IndexOutOfBoundsException(\r
168                                         "Number of cores must be within 1 and 100 ");\r
169                 }\r
170                 this.ncoreNumber = ncoreNumber;\r
171                 cbuilder.setParam(ncorePrm + Integer.toString(getNCore()));\r
172         }\r
173 \r
174         int getNCore() {\r
175                 return ncoreNumber;\r
176         }\r
177 \r
178         @Override\r
179         public CommandBuilder<AACon> getParameters(ExecProvider provider) {\r
180                 // If number of cores is provided, set it for the cluster execution\r
181                 // only!\r
182                 if (provider == Executable.ExecProvider.Cluster) {\r
183                         int cpunum = SkeletalExecutable.getClusterCpuNum(getType());\r
184                         cpunum = (cpunum == 0) ? 1 : cpunum;\r
185                         setNCore(cpunum);\r
186                 } else {\r
187                         // Limit number of cores to 1 for ANY execution which does not set\r
188                         // Ncores explicitly using setNCore method or is run on local VM\r
189                         if (ncoreNumber == 0) {\r
190                                 setNCore(1);\r
191                         }\r
192                 }\r
193                 return super.getParameters(provider);\r
194         }\r
195 \r
196 }\r