3f668e32f31037582adf777e42274323b1c372a9
[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.ScoreManager;\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.ResultNotAvailableException;\r
38 import compbio.runner.Util;\r
39 \r
40 /**\r
41  * Command line\r
42  * \r
43  * java -Xmx512 -jar jronn_v3.jar -i=test_seq.txt -n=1 -o=out.txt -s=stat.out\r
44  * \r
45  * @author pvtroshin\r
46  * \r
47  */\r
48 public class Jronn extends SkeletalExecutable<Jronn> {\r
49 \r
50         private static Logger log = Logger.getLogger(Jronn.class);\r
51 \r
52         /**\r
53          * Number of cores to use, defaults to 1 for local execution or the value of\r
54          * "jronn.cluster.cpunum" property for cluster execution\r
55          */\r
56         private int ncoreNumber = 0;\r
57 \r
58         private final String ncorePrm = "-n=";\r
59 \r
60         public static final String KEY_VALUE_SEPARATOR = Util.SPACE;\r
61         public static final String STAT_FILE = "stat.txt";\r
62 \r
63         public Jronn() {\r
64                 addParameters(Arrays.asList("-jar", getLibPath(), "-s=" + STAT_FILE,\r
65                                 "-f=H"));\r
66         }\r
67 \r
68         @SuppressWarnings("unchecked")\r
69         @Override\r
70         public ScoreManager getResults(String workDirectory)\r
71                         throws ResultNotAvailableException {\r
72                 ScoreManager sequences = null;\r
73                 try {\r
74                         InputStream inStream = new FileInputStream(new File(workDirectory,\r
75                                         getOutput()));\r
76                         sequences = ScoreManager.newInstanceSingleScore(SequenceUtil\r
77                                         .readJRonn(inStream));\r
78                         inStream.close();\r
79                 } catch (FileNotFoundException e) {\r
80                         log.error(e.getMessage(), e.getCause());\r
81                         throw new ResultNotAvailableException(e);\r
82                 } catch (IOException e) {\r
83                         log.error(e.getMessage(), e.getCause());\r
84                         throw new ResultNotAvailableException(e);\r
85                 } catch (UnknownFileFormatException e) {\r
86                         log.error(e.getMessage(), e.getCause());\r
87                         throw new ResultNotAvailableException(e);\r
88                 } catch (NullPointerException e) {\r
89                         log.error(e.getMessage(), e.getCause());\r
90                         throw new ResultNotAvailableException(e);\r
91                 }\r
92                 return sequences;\r
93         }\r
94 \r
95         private static String getLibPath() {\r
96 \r
97                 String settings = ph.getProperty("jronn.jar.file");\r
98                 if (compbio.util.Util.isEmpty(settings)) {\r
99                         throw new NullPointerException(\r
100                                         "Please define jronn.jar.file property in Executable.properties file"\r
101                                                         + "and initialize it with the location of jronn jar file");\r
102                 }\r
103                 if (new File(settings).isAbsolute()) {\r
104                         // Jronn jar can be found so no actions necessary\r
105                         // no further actions is necessary\r
106                         return settings;\r
107                 }\r
108                 return compbio.engine.client.Util.convertToAbsolute(settings);\r
109         }\r
110 \r
111         @Override\r
112         public List<String> getCreatedFiles() {\r
113                 return Arrays.asList(getOutput(), getError());\r
114         }\r
115 \r
116         @Override\r
117         public Jronn setInput(String inFile) {\r
118                 super.setInput(inFile);\r
119                 cbuilder.setParam("-i=" + inFile);\r
120                 return this;\r
121         }\r
122 \r
123         @Override\r
124         public Jronn setOutput(String outFile) {\r
125                 super.setOutput(outFile);\r
126                 cbuilder.setParam("-o=" + outFile);\r
127                 return this;\r
128         }\r
129 \r
130         @SuppressWarnings("unchecked")\r
131         @Override\r
132         public Class<Executable<Jronn>> getType() {\r
133                 return (Class<Executable<Jronn>>) this.getClass();\r
134         }\r
135 \r
136         public static String getStatFile() {\r
137                 return STAT_FILE;\r
138         }\r
139 \r
140         public void setNCore(int ncoreNumber) {\r
141                 if (ncoreNumber < 1 || ncoreNumber > 100) {\r
142                         throw new IndexOutOfBoundsException(\r
143                                         "Number of cores must be within 1 and 100 ");\r
144                 }\r
145                 this.ncoreNumber = ncoreNumber;\r
146                 cbuilder.setParam(ncorePrm + Integer.toString(getNCore()));\r
147         }\r
148 \r
149         int getNCore() {\r
150                 return ncoreNumber;\r
151         }\r
152 \r
153         @Override\r
154         public CommandBuilder<Jronn> getParameters(ExecProvider provider) {\r
155                 // If number of cores is provided, set it for the cluster execution\r
156                 // only!\r
157                 if (provider == Executable.ExecProvider.Cluster) {\r
158                         int cpunum = SkeletalExecutable.getClusterCpuNum(getType());\r
159                         cpunum = (cpunum == 0) ? 1 : cpunum;\r
160                         setNCore(cpunum);\r
161                 } else {\r
162                         // Limit number of cores to 1 for ANY execution which does not set\r
163                         // Ncores explicitly using setNCore method or is run on local VM\r
164                         if (ncoreNumber == 0) {\r
165                                 setNCore(1);\r
166                         }\r
167                 }\r
168                 return super.getParameters(provider);\r
169         }\r
170 \r
171 }\r