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