0e08e0a9aafd0a48a0059f907b9a27e1b2bb1711
[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         }\r
69 \r
70         // HashMap<Method, float[]>\r
71         @SuppressWarnings("unchecked")\r
72         @Override\r
73         public JpredAlignment getResults(String workDirectory) throws ResultNotAvailableException {\r
74                 JpredAlignment annotations = null;\r
75                 try {\r
76                         InputStream inStream = new FileInputStream(new File(workDirectory, getOutput()));\r
77                         annotations = new JpredAlignment(SequenceUtil.readJpredFile(inStream), new AlignmentMetadata(Program.Jpred, '-'));\r
78                 } catch (FileNotFoundException e) {\r
79                         log.error(e.getMessage(), e.getCause());\r
80                         throw new ResultNotAvailableException(e);\r
81                 } catch (IOException e) {\r
82                         log.error(e.getMessage(), e.getCause());\r
83                         throw new ResultNotAvailableException(e);\r
84                 } catch (NullPointerException e) {\r
85                         log.error(e.getMessage(), e.getCause());\r
86                         throw new ResultNotAvailableException(e);\r
87                 }\r
88                 return annotations;\r
89         }\r
90 \r
91         @Override\r
92         public List<String> getCreatedFiles() {\r
93                 return Arrays.asList(getOutput(), getError());\r
94         }\r
95 \r
96         @Override\r
97         public Jpred setInput(String inFile) {\r
98                 super.setInput(inFile);\r
99                 cbuilder.setParam("-in " + inFile);\r
100                 return this;\r
101         }\r
102 \r
103         @Override\r
104         public Jpred setOutput(String outFile) {\r
105                 super.setOutput(outFile);\r
106                 cbuilder.setParam("-outfile " + outFile);\r
107                 return this;\r
108         }\r
109 \r
110         @SuppressWarnings("unchecked")\r
111         @Override\r
112         public Class<Jpred> getType() {\r
113                 return (Class<Jpred>) this.getClass();\r
114         }\r
115 \r
116         public static String getStatFile() {\r
117                 return STAT_FILE;\r
118         }\r
119 \r
120         public void setNCore(int ncoreNumber) {\r
121                 if (0 < ncoreNumber && ncoreNumber < 9) {\r
122                         this.ncoreNumber = ncoreNumber;\r
123                         cbuilder.setParam("-ncpu " + Integer.toString(getNCore()));\r
124                 } else {\r
125                         throw new IndexOutOfBoundsException("Number of cores must be between 1 and 8 ");\r
126                 }\r
127         }\r
128 \r
129         int getNCore() {\r
130                 return ncoreNumber;\r
131         }\r
132 \r
133         @Override\r
134         public CommandBuilder<Jpred> getParameters(ExecProvider provider) {\r
135                 // If number of cores is provided, set it for the cluster execution\r
136                 // only!\r
137                 if (provider == Executable.ExecProvider.Cluster) {\r
138                         int cpunum = SkeletalExecutable.getClusterCpuNum(getType());\r
139                         cpunum = (cpunum == 0) ? 1 : cpunum;\r
140                         setNCore(cpunum);\r
141                 } else {\r
142                         // Limit number of cores to 1 for ANY execution which does not set\r
143                         // Ncores explicitly using setNCore method or is run on local VM\r
144                         if (ncoreNumber == 0) {\r
145                                 setNCore(1);\r
146                         }\r
147                 }\r
148                 return super.getParameters(provider);\r
149         }\r
150 \r
151 }\r