Clean up logging system
[jabaws.git] / runner / compbio / runner / conservation / AACon.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.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.conservation;\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.engine.client.CommandBuilder;\r
34 import compbio.engine.client.Executable;\r
35 import compbio.engine.client.SkeletalExecutable;\r
36 import compbio.metadata.ResultNotAvailableException;\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 = "-t=";\r
57 \r
58         public static final String KEY_VALUE_SEPARATOR = "=";\r
59         public static final String STAT_FILE = "stat.txt";\r
60 \r
61         public AACon() {\r
62                 addParameters(Arrays.asList("-jar", getLibPath(), "-d=" + STAT_FILE,\r
63                                 "-f=RESULT_NO_ALIGNMENT"));\r
64         }\r
65         // HashMap<Method, float[]>\r
66         @Override\r
67         public ScoreManager getResults(String workDirectory)\r
68                         throws ResultNotAvailableException {\r
69                 ScoreManager annotations = null;\r
70                 try {\r
71                         InputStream inStream = new FileInputStream(new File(workDirectory,\r
72                                         getOutput()));\r
73                         annotations = ScoreManager.newInstanceSingleSequence(SequenceUtil\r
74                                         .readAAConResults(inStream));\r
75                         inStream.close();\r
76                 } catch (FileNotFoundException e) {\r
77                         log.error(e.getMessage(), e.getCause());\r
78                         throw new ResultNotAvailableException(e);\r
79                 } catch (IOException e) {\r
80                         log.error(e.getMessage(), e.getCause());\r
81                         throw new ResultNotAvailableException(e);\r
82                 } catch (NullPointerException e) {\r
83                         log.error(e.getMessage(), e.getCause());\r
84                         throw new ResultNotAvailableException(e);\r
85                 }\r
86                 return annotations;\r
87         }\r
88 \r
89         private static String getLibPath() {\r
90 \r
91                 String settings = ph.getProperty("aacon.jar.file");\r
92                 if (compbio.util.Util.isEmpty(settings)) {\r
93                         throw new NullPointerException(\r
94                                         "Please define aacon.jar.file property in Executable.properties file"\r
95                                                         + "and initialize it with the location of jronn jar file");\r
96                 }\r
97                 if (new File(settings).isAbsolute()) {\r
98                         // the jar can be found so no actions necessary\r
99                         // no further actions is necessary\r
100                         return settings;\r
101                 }\r
102                 return compbio.engine.client.Util.convertToAbsolute(settings);\r
103         }\r
104 \r
105         @Override\r
106         public List<String> getCreatedFiles() {\r
107                 return Arrays.asList(getOutput(), getError());\r
108         }\r
109 \r
110         @Override\r
111         public AACon setInput(String inFile) {\r
112                 super.setInput(inFile);\r
113                 cbuilder.setParam("-i=" + inFile);\r
114                 return this;\r
115         }\r
116 \r
117         @Override\r
118         public AACon setOutput(String outFile) {\r
119                 super.setOutput(outFile);\r
120                 cbuilder.setParam("-o=" + outFile);\r
121                 return this;\r
122         }\r
123 \r
124         @SuppressWarnings("unchecked")\r
125         @Override\r
126         public Class<AACon> getType() {\r
127                 return (Class<AACon>) this.getClass();\r
128         }\r
129 \r
130         public static String getStatFile() {\r
131                 return STAT_FILE;\r
132         }\r
133 \r
134         public void setNCore(int ncoreNumber) {\r
135                 if (ncoreNumber < 1 || ncoreNumber > 100) {\r
136                         throw new IndexOutOfBoundsException(\r
137                                         "Number of cores must be within 1 and 100 ");\r
138                 }\r
139                 this.ncoreNumber = ncoreNumber;\r
140                 cbuilder.setParam(ncorePrm + Integer.toString(getNCore()));\r
141         }\r
142 \r
143         int getNCore() {\r
144                 return ncoreNumber;\r
145         }\r
146 \r
147         @Override\r
148         public CommandBuilder<AACon> getParameters(ExecProvider provider) {\r
149                 // If number of cores is provided, set it for the cluster execution\r
150                 // only!\r
151                 if (provider == Executable.ExecProvider.Cluster) {\r
152                         int cpunum = SkeletalExecutable.getClusterCpuNum(getType());\r
153                         cpunum = (cpunum == 0) ? 1 : cpunum;\r
154                         setNCore(cpunum);\r
155                 } else {\r
156                         // Limit number of cores to 1 for ANY execution which does not set\r
157                         // Ncores explicitly using setNCore method or is run on local VM\r
158                         if (ncoreNumber == 0) {\r
159                                 setNCore(1);\r
160                         }\r
161                 }\r
162                 return super.getParameters(provider);\r
163         }\r
164 \r
165 }\r