ce4d29304e6522ca14d0165ab451e60a9a055019
[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.HashSet;\r
24 import java.util.List;\r
25 \r
26 import org.apache.log4j.Logger;\r
27 \r
28 import compbio.data.sequence.Score;\r
29 import compbio.data.sequence.SequenceUtil;\r
30 import compbio.engine.client.CommandBuilder;\r
31 import compbio.engine.client.Executable;\r
32 import compbio.engine.client.SkeletalExecutable;\r
33 import compbio.metadata.Limit;\r
34 import compbio.metadata.LimitsManager;\r
35 import compbio.metadata.ResultNotAvailableException;\r
36 import compbio.runner.Util;\r
37 \r
38 /**\r
39  * Command line\r
40  * \r
41  * java -Xmx512 -jar jronn_v3.jar -i=test_seq.txt -n=1 -o=out.txt -s=stat.out\r
42  * \r
43  * @author pvtroshin\r
44  * \r
45  */\r
46 public class AACon extends SkeletalExecutable<AACon> {\r
47 \r
48         private static Logger log = Logger.getLogger(AACon.class);\r
49 \r
50         /**\r
51          * Number of cores to use, defaults to 1 for local execution or the value of\r
52          * "jronn.cluster.cpunum" property for cluster execution\r
53          */\r
54         private int ncoreNumber = 0;\r
55 \r
56         private final String ncorePrm = "-n=";\r
57 \r
58         // Cache for Limits information\r
59         private static LimitsManager<AACon> limits;\r
60 \r
61         public static final String KEY_VALUE_SEPARATOR = Util.SPACE;\r
62         public static final String STAT_FILE = "stat.txt";\r
63 \r
64         public AACon() {\r
65                 addParameters(Arrays.asList("-jar", getLibPath(), "-d=" + STAT_FILE,\r
66                                 "-f=RESULT_NO_ALIGNMENT"));\r
67         }\r
68         // HashMap<Method, float[]>\r
69         @Override\r
70         public HashSet<Score> getResults(String workDirectory)\r
71                         throws ResultNotAvailableException {\r
72                 HashSet<Score> annotations = null;\r
73                 try {\r
74                         InputStream inStream = new FileInputStream(new File(workDirectory,\r
75                                         getOutput()));\r
76                         annotations = SequenceUtil.readAAConResults(inStream);\r
77                         inStream.close();\r
78                 } catch (FileNotFoundException e) {\r
79                         log.error(e.getMessage(), e.getCause());\r
80                         throw new ResultNotAvailableException(e);\r
81                 } catch (IOException e) {\r
82                         log.error(e.getMessage(), e.getCause());\r
83                         throw new ResultNotAvailableException(e);\r
84                 } catch (NullPointerException e) {\r
85                         log.error(e.getMessage(), e.getCause());\r
86                         throw new ResultNotAvailableException(e);\r
87                 }\r
88                 return annotations;\r
89         }\r
90 \r
91         private static String getLibPath() {\r
92 \r
93                 String settings = ph.getProperty("aacon.jar.file");\r
94                 if (compbio.util.Util.isEmpty(settings)) {\r
95                         throw new NullPointerException(\r
96                                         "Please define aacon.jar.file property in Executable.properties file"\r
97                                                         + "and initialize it with the location of jronn jar file");\r
98                 }\r
99                 if (new File(settings).isAbsolute()) {\r
100                         // Jronn jar can be found so no actions necessary\r
101                         // no further actions is necessary\r
102                         return settings;\r
103                 }\r
104                 return compbio.engine.client.Util.convertToAbsolute(settings);\r
105         }\r
106 \r
107         @Override\r
108         public List<String> getCreatedFiles() {\r
109                 return Arrays.asList(getOutput(), getError());\r
110         }\r
111 \r
112         @Override\r
113         public AACon setInput(String inFile) {\r
114                 super.setInput(inFile);\r
115                 cbuilder.setParam("-i=" + inFile);\r
116                 return this;\r
117         }\r
118 \r
119         @Override\r
120         public AACon setOutput(String outFile) {\r
121                 super.setOutput(outFile);\r
122                 cbuilder.setParam("-o=" + outFile);\r
123                 return this;\r
124         }\r
125 \r
126         @Override\r
127         public Limit<AACon> getLimit(String presetName) {\r
128                 if (limits == null) {\r
129                         limits = getLimits();\r
130                 }\r
131                 Limit<AACon> limit = null;\r
132                 if (limits != null) {\r
133                         // this returns default limit if preset is undefined!\r
134                         limit = limits.getLimitByName(presetName);\r
135                 }\r
136                 // If limit is not defined for a particular preset, then return default\r
137                 // limit\r
138                 if (limit == null) {\r
139                         log.debug("Limit for the preset " + presetName\r
140                                         + " is not found. Using default");\r
141                         limit = limits.getDefaultLimit();\r
142                 }\r
143                 return limit;\r
144         }\r
145 \r
146         @Override\r
147         public LimitsManager<AACon> getLimits() {\r
148                 // synchronise on static field\r
149                 synchronized (log) {\r
150                         if (limits == null) {\r
151                                 limits = Util.getLimits(this.getClass());\r
152                         }\r
153                 }\r
154                 return limits;\r
155         }\r
156 \r
157         @Override\r
158         public Class<? extends Executable<?>> getType() {\r
159                 return this.getClass();\r
160         }\r
161 \r
162         public static String getStatFile() {\r
163                 return STAT_FILE;\r
164         }\r
165 \r
166         public void setNCore(int ncoreNumber) {\r
167                 if (ncoreNumber < 1 || ncoreNumber > 100) {\r
168                         throw new IndexOutOfBoundsException(\r
169                                         "Number of cores must be within 1 and 100 ");\r
170                 }\r
171                 this.ncoreNumber = ncoreNumber;\r
172                 cbuilder.setParam(ncorePrm + Integer.toString(getNCore()));\r
173         }\r
174 \r
175         int getNCore() {\r
176                 return ncoreNumber;\r
177         }\r
178 \r
179         @Override\r
180         public CommandBuilder<AACon> getParameters(ExecProvider provider) {\r
181                 // If number of cores is provided, set it for the cluster execution\r
182                 // only!\r
183                 if (provider == Executable.ExecProvider.Cluster) {\r
184                         int cpunum = SkeletalExecutable.getClusterCpuNum(getType());\r
185                         cpunum = (cpunum == 0) ? 1 : cpunum;\r
186                         setNCore(cpunum);\r
187                 } else {\r
188                         // Limit number of cores to 1 for ANY execution which does not set\r
189                         // Ncores explicitly using setNCore method or is run on local VM\r
190                         if (ncoreNumber == 0) {\r
191                                 setNCore(1);\r
192                         }\r
193                 }\r
194                 return super.getParameters(provider);\r
195         }\r
196 \r
197 }\r