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