Add Java wrapper class for Jpred
[jabaws.git] / runner / compbio / runner / predictors / Jpred.java
1 /* Copyright (c) 2013 Alexander Sherstnev\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 3.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.predictors;\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  * jpred.pl -in d16vpa_.fas -out res_d16vpa_ -dbname ported_db -dbpath /data/UNIREFdb -ncpu 4\r
42  * \r
43  * @author asherstnev\r
44  * \r
45  */\r
46 public class Jpred extends SkeletalExecutable<Jpred> {\r
47 \r
48         private static Logger log = Logger.getLogger(Jpred.class);\r
49 \r
50         /**\r
51          * Number of cores to use, defaults to 1 for local execution or the value of\r
52          * "jpred.cluster.cpunum" property for cluster execution\r
53          */\r
54         private int ncoreNumber = 0;\r
55 \r
56         public static final String KEY_VALUE_SEPARATOR = " ";\r
57         public static final String STAT_FILE = "stat.txt";\r
58 \r
59         public Jpred() {\r
60 //              addParameters(Arrays.asList());\r
61         }\r
62         // HashMap<Method, float[]>\r
63         @Override\r
64         public ScoreManager getResults(String workDirectory)\r
65                         throws ResultNotAvailableException {\r
66                 ScoreManager annotations = null;\r
67                 try {\r
68                         InputStream inStream = new FileInputStream(new File(workDirectory, getOutput()));\r
69                         annotations = ScoreManager.newInstanceSingleSequence(SequenceUtil.readAAConResults(inStream));\r
70                         inStream.close();\r
71                 } catch (FileNotFoundException e) {\r
72                         log.error(e.getMessage(), e.getCause());\r
73                         throw new ResultNotAvailableException(e);\r
74                 } catch (IOException e) {\r
75                         log.error(e.getMessage(), e.getCause());\r
76                         throw new ResultNotAvailableException(e);\r
77                 } catch (NullPointerException e) {\r
78                         log.error(e.getMessage(), e.getCause());\r
79                         throw new ResultNotAvailableException(e);\r
80                 }\r
81                 return annotations;\r
82         }\r
83 \r
84         @Override\r
85         public List<String> getCreatedFiles() {\r
86                 return Arrays.asList(getOutput(), getError());\r
87         }\r
88 \r
89         @Override\r
90         public Jpred setInput(String inFile) {\r
91                 super.setInput(inFile);\r
92                 cbuilder.setParam("-in " + inFile);\r
93                 return this;\r
94         }\r
95 \r
96         @Override\r
97         public Jpred setOutput(String outFile) {\r
98                 super.setOutput(outFile);\r
99                 cbuilder.setParam("-out " + outFile);\r
100                 return this;\r
101         }\r
102 \r
103         @SuppressWarnings("unchecked")\r
104         @Override\r
105         public Class<Jpred> getType() {\r
106                 return (Class<Jpred>) this.getClass();\r
107         }\r
108 \r
109         public static String getStatFile() {\r
110                 return STAT_FILE;\r
111         }\r
112 \r
113         public void setNCore(int ncoreNumber) {\r
114                 if (0 < ncoreNumber && ncoreNumber < 9) {\r
115                         this.ncoreNumber = ncoreNumber;\r
116                         cbuilder.setParam("-ncpu " + Integer.toString(getNCore()));\r
117                 } else {\r
118                         throw new IndexOutOfBoundsException("Number of cores must be between 1 and 8 ");\r
119                 }\r
120         }\r
121 \r
122         int getNCore() {\r
123                 return ncoreNumber;\r
124         }\r
125 \r
126         @Override\r
127         public CommandBuilder<Jpred> getParameters(ExecProvider provider) {\r
128                 // If number of cores is provided, set it for the cluster execution only!\r
129                 if (provider == Executable.ExecProvider.Cluster) {\r
130                         int cpunum = SkeletalExecutable.getClusterCpuNum(getType());\r
131                         cpunum = (cpunum == 0) ? 1 : cpunum;\r
132                         setNCore(cpunum);\r
133                 } else {\r
134                         // Limit number of cores to 1 for ANY execution which does not set\r
135                         // Ncores explicitly using setNCore method or is run on local VM\r
136                         if (ncoreNumber == 0) {\r
137                                 setNCore(1);\r
138                         }\r
139                 }\r
140                 return super.getParameters(provider);\r
141         }\r
142 \r
143 }\r