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