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