Finally, the new web services are working
[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.List;\r
24 \r
25 import org.apache.log4j.Logger;\r
26 \r
27 import compbio.data.sequence.SMERFSConstraints;\r
28 import compbio.data.sequence.ScoreManager;\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 = "-t=";\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 = "=";\r
62         public static final String STAT_FILE = "stat.txt";\r
63 \r
64         private final int windowWidth = SMERFSConstraints.DEFAULT_WINDOW_SIZE;\r
65         private final SMERFSConstraints colScoreMethod = SMERFSConstraints.MID_SCORE;\r
66         private final double gapTreshold = SMERFSConstraints.DEFAULT_GAP_THRESHOLD;\r
67 \r
68         public AACon() {\r
69                 addParameters(Arrays.asList("-jar", getLibPath(), "-d=" + STAT_FILE,\r
70                                 "-f=RESULT_NO_ALIGNMENT"));\r
71         }\r
72         // HashMap<Method, float[]>\r
73         @Override\r
74         public ScoreManager getResults(String workDirectory)\r
75                         throws ResultNotAvailableException {\r
76                 ScoreManager annotations = null;\r
77                 try {\r
78                         InputStream inStream = new FileInputStream(new File(workDirectory,\r
79                                         getOutput()));\r
80                         annotations = ScoreManager.newInstanceSingleSequence(SequenceUtil\r
81                                         .readAAConResults(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 (NullPointerException e) {\r
90                         log.error(e.getMessage(), e.getCause());\r
91                         throw new ResultNotAvailableException(e);\r
92                 }\r
93                 return annotations;\r
94         }\r
95 \r
96         private static String getLibPath() {\r
97 \r
98                 String settings = ph.getProperty("aacon.jar.file");\r
99                 if (compbio.util.Util.isEmpty(settings)) {\r
100                         throw new NullPointerException(\r
101                                         "Please define aacon.jar.file property in Executable.properties file"\r
102                                                         + "and initialize it with the location of jronn jar file");\r
103                 }\r
104                 if (new File(settings).isAbsolute()) {\r
105                         // the jar can be found so no actions necessary\r
106                         // no further actions is necessary\r
107                         return settings;\r
108                 }\r
109                 return compbio.engine.client.Util.convertToAbsolute(settings);\r
110         }\r
111 \r
112         @Override\r
113         public List<String> getCreatedFiles() {\r
114                 return Arrays.asList(getOutput(), getError());\r
115         }\r
116 \r
117         @Override\r
118         public AACon setInput(String inFile) {\r
119                 super.setInput(inFile);\r
120                 cbuilder.setParam("-i=" + inFile);\r
121                 return this;\r
122         }\r
123 \r
124         @Override\r
125         public AACon setOutput(String outFile) {\r
126                 super.setOutput(outFile);\r
127                 cbuilder.setParam("-o=" + outFile);\r
128                 return this;\r
129         }\r
130 \r
131         @Override\r
132         public Limit<AACon> getLimit(String presetName) {\r
133                 if (limits == null) {\r
134                         limits = getLimits();\r
135                 }\r
136                 Limit<AACon> limit = null;\r
137                 if (limits != null) {\r
138                         // this returns default limit if preset is undefined!\r
139                         limit = limits.getLimitByName(presetName);\r
140                 }\r
141                 // If limit is not defined for a particular preset, then return default\r
142                 // limit\r
143                 if (limit == null) {\r
144                         log.debug("Limit for the preset " + presetName\r
145                                         + " is not found. Using default");\r
146                         limit = limits.getDefaultLimit();\r
147                 }\r
148                 return limit;\r
149         }\r
150 \r
151         @Override\r
152         public LimitsManager<AACon> getLimits() {\r
153                 // synchronise on static field\r
154                 synchronized (log) {\r
155                         if (limits == null) {\r
156                                 limits = Util.getLimits(this.getClass());\r
157                         }\r
158                 }\r
159                 return limits;\r
160         }\r
161 \r
162         @Override\r
163         public Class<? extends Executable<?>> getType() {\r
164                 return this.getClass();\r
165         }\r
166 \r
167         public static String getStatFile() {\r
168                 return STAT_FILE;\r
169         }\r
170 \r
171         public void setNCore(int ncoreNumber) {\r
172                 if (ncoreNumber < 1 || ncoreNumber > 100) {\r
173                         throw new IndexOutOfBoundsException(\r
174                                         "Number of cores must be within 1 and 100 ");\r
175                 }\r
176                 this.ncoreNumber = ncoreNumber;\r
177                 cbuilder.setParam(ncorePrm + Integer.toString(getNCore()));\r
178         }\r
179 \r
180         int getNCore() {\r
181                 return ncoreNumber;\r
182         }\r
183 \r
184         @Override\r
185         public CommandBuilder<AACon> getParameters(ExecProvider provider) {\r
186                 // If number of cores is provided, set it for the cluster execution\r
187                 // only!\r
188                 if (provider == Executable.ExecProvider.Cluster) {\r
189                         int cpunum = SkeletalExecutable.getClusterCpuNum(getType());\r
190                         cpunum = (cpunum == 0) ? 1 : cpunum;\r
191                         setNCore(cpunum);\r
192                 } else {\r
193                         // Limit number of cores to 1 for ANY execution which does not set\r
194                         // Ncores explicitly using setNCore method or is run on local VM\r
195                         if (ncoreNumber == 0) {\r
196                                 setNCore(1);\r
197                         }\r
198                 }\r
199                 return super.getParameters(provider);\r
200         }\r
201 \r
202 }\r