1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.runner;
\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 import java.util.Map;
\r
28 import org.apache.log4j.Logger;
\r
30 import compbio.data.sequence.Alignment;
\r
31 import compbio.data.sequence.ClustalAlignmentUtil;
\r
32 import compbio.data.sequence.FastaSequence;
\r
33 import compbio.data.sequence.Score;
\r
34 import compbio.data.sequence.SequenceUtil;
\r
35 import compbio.data.sequence.UnknownFileFormatException;
\r
36 import compbio.engine.client.ConfExecutable;
\r
37 import compbio.engine.client.ConfiguredExecutable;
\r
38 import compbio.engine.client.Executable;
\r
39 import compbio.engine.conf.PropertyHelperManager;
\r
40 import compbio.metadata.LimitsManager;
\r
41 import compbio.metadata.PresetManager;
\r
42 import compbio.metadata.RunnerConfig;
\r
43 import compbio.util.PropertyHelper;
\r
45 public final class Util {
\r
47 private static Logger log = Logger.getLogger(Util.class);
\r
49 private static final PropertyHelper ph = PropertyHelperManager
\r
50 .getPropertyHelper();
\r
52 public static final String SPACE = " ";
\r
55 * For now just assume that all parameters which came in needs setting it
\r
56 * will be a client responsibility to prepare RunnerConfig object then
\r
61 * public static List<String> toOptionString(RunnerConfig<?>
\r
62 * rconfig) { String option = ""; List<String> options = new
\r
63 * ArrayList<String>(); for (Parameter<?> par :
\r
64 * rconfig.getParameters()) { if (par.getPossibleValues().isEmpty())
\r
65 * { option = par.getOptionName(); } else { option =
\r
66 * par.getOptionName() + "=" + par.getPossibleValues().get(0); } //
\r
67 * separate options options.add(option); } return options; }
\r
70 public static <T> LimitsManager<T> getLimits(
\r
71 Class<? extends Executable<T>> clazz) {
\r
72 LimitsManager<T> limits = null;
\r
74 limits = ConfExecutable.getRunnerLimits(clazz);
\r
75 if (limits == null) {
\r
78 } catch (FileNotFoundException e) {
\r
80 "No limits are found for " + clazz + " executable! "
\r
81 + e.getLocalizedMessage(), e.getCause());
\r
83 } catch (IOException e) {
\r
84 log.warn("IO exception while attempting to read limits for "
\r
85 + clazz + " executable! " + e.getLocalizedMessage(),
\r
92 public static synchronized <T> RunnerConfig<T> getSupportedOptions(
\r
93 Class<? extends Executable<T>> clazz) {
\r
95 return ConfExecutable.getRunnerOptions(clazz);
\r
96 } catch (FileNotFoundException e) {
\r
98 "Could not load " + clazz + " Parameters !"
\r
99 + e.getMessage(), e.getCause());
\r
100 } catch (IOException e) {
\r
101 log.error("IO exception while reading " + clazz + " Parameters !"
\r
102 + e.getMessage(), e.getCause());
\r
107 public static <T> PresetManager<T> getPresets(
\r
108 Class<? extends Executable<T>> clazz) {
\r
110 return ConfExecutable.getRunnerPresets(clazz);
\r
111 } catch (FileNotFoundException e) {
\r
113 "No presets are found for " + clazz + " executable! "
\r
114 + e.getLocalizedMessage(), e.getCause());
\r
115 } catch (IOException e) {
\r
116 log.warn("IO exception while reading presents! for " + clazz
\r
117 + " executable! " + e.getLocalizedMessage(), e.getCause());
\r
122 public static final Alignment readClustalFile(String workDirectory,
\r
123 String clustFile) throws UnknownFileFormatException, IOException,
\r
124 FileNotFoundException, NullPointerException {
\r
125 assert !compbio.util.Util.isEmpty(workDirectory);
\r
126 assert !compbio.util.Util.isEmpty(clustFile);
\r
127 File cfile = new File(compbio.engine.client.Util.getFullPath(
\r
128 workDirectory, clustFile));
\r
129 log.trace("CLUSTAL OUTPUT FILE PATH: " + cfile.getAbsolutePath());
\r
130 if (!(cfile.exists() && cfile.length() > 0)) {
\r
131 throw new FileNotFoundException("Result for the jobId "
\r
132 + workDirectory + " with file name " + clustFile
\r
133 + " is not found!");
\r
135 return ClustalAlignmentUtil.readClustalFile(cfile);
\r
138 public static final Map<String, Score> readJronnFile(String workDirectory,
\r
139 String clustFile) throws UnknownFileFormatException, IOException,
\r
140 FileNotFoundException, NullPointerException {
\r
141 assert !compbio.util.Util.isEmpty(workDirectory);
\r
142 assert !compbio.util.Util.isEmpty(clustFile);
\r
143 File cfile = new File(compbio.engine.client.Util.getFullPath(
\r
144 workDirectory, clustFile));
\r
145 log.trace("Jronn OUTPUT FILE PATH: " + cfile.getAbsolutePath());
\r
146 if (!(cfile.exists() && cfile.length() > 0)) {
\r
147 throw new FileNotFoundException("Result for the jobId "
\r
148 + workDirectory + " with file name " + clustFile
\r
149 + " is not found!");
\r
151 return SequenceUtil.readJRonn(cfile);
\r
154 public static void writeInput(List<FastaSequence> sequences,
\r
155 ConfiguredExecutable<?> exec) {
\r
157 File filein = new File(exec.getInput());
\r
159 FileOutputStream fout = new FileOutputStream(filein);
\r
160 log.debug("File path: " + filein.getAbsolutePath());
\r
161 SequenceUtil.writeFasta(fout, sequences);
\r
163 } catch (FileNotFoundException e) {
\r
164 e.printStackTrace();
\r
165 } catch (IOException e) {
\r
166 e.printStackTrace();
\r