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