Next version of JABA
[jabaws.git] / runner / compbio / runner / Util.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.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;\r
20 \r
21 import java.io.File;\r
22 import java.io.FileNotFoundException;\r
23 import java.io.FileOutputStream;\r
24 import java.io.IOException;\r
25 import java.util.List;\r
26 \r
27 import org.apache.log4j.Logger;\r
28 \r
29 import compbio.data.sequence.Alignment;\r
30 import compbio.data.sequence.ClustalAlignmentUtil;\r
31 import compbio.data.sequence.FastaSequence;\r
32 import compbio.data.sequence.SequenceUtil;\r
33 import compbio.data.sequence.UnknownFileFormatException;\r
34 import compbio.engine.client.ConfExecutable;\r
35 import compbio.engine.client.ConfiguredExecutable;\r
36 import compbio.engine.client.Executable;\r
37 import compbio.engine.conf.PropertyHelperManager;\r
38 import compbio.metadata.LimitsManager;\r
39 import compbio.metadata.PresetManager;\r
40 import compbio.metadata.RunnerConfig;\r
41 import compbio.util.PropertyHelper;\r
42 \r
43 public final class Util {\r
44 \r
45     private static Logger log = Logger.getLogger(Util.class);\r
46 \r
47     private static final PropertyHelper ph = PropertyHelperManager\r
48             .getPropertyHelper();\r
49 \r
50     public static final String SPACE = " ";\r
51 \r
52     /**\r
53      * For now just assume that all parameters which came in needs setting it\r
54      * will be a client responsibility to prepare RunnerConfig object then\r
55      * \r
56      * @param rconfig\r
57      * @return\r
58      * \r
59      *         public static List<String> toOptionString(RunnerConfig<?>\r
60      *         rconfig) { String option = ""; List<String> options = new\r
61      *         ArrayList<String>(); for (Parameter<?> par :\r
62      *         rconfig.getParameters()) { if (par.getPossibleValues().isEmpty())\r
63      *         { option = par.getOptionName(); } else { option =\r
64      *         par.getOptionName() + "=" + par.getPossibleValues().get(0); } //\r
65      *         separate options options.add(option); } return options; }\r
66      */\r
67 \r
68     public static <T> LimitsManager<T> getLimits(\r
69             Class<? extends Executable<T>> clazz) {\r
70         LimitsManager<T> limits = null;\r
71         try {\r
72             limits = ConfExecutable.getRunnerLimits(clazz);\r
73             if (limits == null) {\r
74                 return null;\r
75             }\r
76         } catch (FileNotFoundException e) {\r
77             log.warn("No limits are found for " + clazz + " executable! "\r
78                     + e.getLocalizedMessage(), e.getCause());\r
79             return null;\r
80         } catch (IOException e) {\r
81             log.warn("IO exception while attempting to read limits for "\r
82                     + clazz + " executable! " + e.getLocalizedMessage(), e\r
83                     .getCause());\r
84             return null;\r
85         }\r
86         return limits;\r
87     }\r
88 \r
89     public static synchronized <T> RunnerConfig<T> getSupportedOptions(\r
90             Class<? extends Executable<T>> clazz) {\r
91         try {\r
92             return ConfExecutable.getRunnerOptions(clazz);\r
93         } catch (FileNotFoundException e) {\r
94             log.error("Could not load " + clazz + " Parameters !"\r
95                     + e.getMessage(), e.getCause());\r
96         } catch (IOException e) {\r
97             log.error("IO exception while reading " + clazz + " Parameters !"\r
98                     + e.getMessage(), e.getCause());\r
99         }\r
100         return null;\r
101     }\r
102 \r
103     public static <T> PresetManager<T> getPresets(\r
104             Class<? extends Executable<T>> clazz) {\r
105         try {\r
106             return ConfExecutable.getRunnerPresets(clazz);\r
107         } catch (FileNotFoundException e) {\r
108             log.warn("No presets are found for " + clazz + " executable! "\r
109                     + e.getLocalizedMessage(), e.getCause());\r
110         } catch (IOException e) {\r
111             log.warn("IO exception while reading presents! for " + clazz\r
112                     + " executable! " + e.getLocalizedMessage(), e.getCause());\r
113         }\r
114         return null;\r
115     }\r
116 \r
117     public static final Alignment readClustalFile(String workDirectory,\r
118             String clustFile) throws UnknownFileFormatException, IOException,\r
119             FileNotFoundException, NullPointerException {\r
120         assert !compbio.util.Util.isEmpty(workDirectory);\r
121         assert !compbio.util.Util.isEmpty(clustFile);\r
122         File cfile = new File(compbio.engine.client.Util.getFullPath(\r
123                 workDirectory, clustFile));\r
124         log.trace("CLUSTAL OUTPUT FILE PATH: " + cfile.getAbsolutePath());\r
125         if (!(cfile.exists() && cfile.length() > 0)) {\r
126             throw new FileNotFoundException("Result for the jobId "\r
127                     + workDirectory + " with file name " + clustFile\r
128                     + " is not found!");\r
129         }\r
130         return ClustalAlignmentUtil.readClustalFile(cfile);\r
131     }\r
132 \r
133     public static void writeInput(List<FastaSequence> sequences,\r
134             ConfiguredExecutable<?> exec) {\r
135 \r
136         File filein = new File(exec.getInput());\r
137         try {\r
138             FileOutputStream fout = new FileOutputStream(filein);\r
139             log.debug("File path: " + filein.getAbsolutePath());\r
140             SequenceUtil.writeFasta(fout, sequences);\r
141             fout.close();\r
142         } catch (FileNotFoundException e) {\r
143             e.printStackTrace();\r
144         } catch (IOException e) {\r
145             e.printStackTrace();\r
146         }\r
147     }\r
148 \r
149 }\r