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
27 import org.apache.log4j.Logger;
\r
29 import compbio.data.sequence.Alignment;
\r
30 import compbio.data.sequence.AnnotatedSequence;
\r
31 import compbio.data.sequence.ClustalAlignmentUtil;
\r
32 import compbio.data.sequence.FastaSequence;
\r
33 import compbio.data.sequence.SequenceUtil;
\r
34 import compbio.data.sequence.UnknownFileFormatException;
\r
35 import compbio.engine.client.ConfExecutable;
\r
36 import compbio.engine.client.ConfiguredExecutable;
\r
37 import compbio.engine.client.Executable;
\r
38 import compbio.engine.conf.PropertyHelperManager;
\r
39 import compbio.metadata.LimitsManager;
\r
40 import compbio.metadata.PresetManager;
\r
41 import compbio.metadata.RunnerConfig;
\r
42 import compbio.util.PropertyHelper;
\r
44 public final class Util {
\r
46 private static Logger log = Logger.getLogger(Util.class);
\r
48 private static final PropertyHelper ph = PropertyHelperManager
\r
49 .getPropertyHelper();
\r
51 public static final String SPACE = " ";
\r
54 * For now just assume that all parameters which came in needs setting it
\r
55 * will be a client responsibility to prepare RunnerConfig object then
\r
60 * public static List<String> toOptionString(RunnerConfig<?>
\r
61 * rconfig) { String option = ""; List<String> options = new
\r
62 * ArrayList<String>(); for (Parameter<?> par :
\r
63 * rconfig.getParameters()) { if (par.getPossibleValues().isEmpty())
\r
64 * { option = par.getOptionName(); } else { option =
\r
65 * par.getOptionName() + "=" + par.getPossibleValues().get(0); } //
\r
66 * separate options options.add(option); } return options; }
\r
69 public static <T> LimitsManager<T> getLimits(
\r
70 Class<? extends Executable<T>> clazz) {
\r
71 LimitsManager<T> limits = null;
\r
73 limits = ConfExecutable.getRunnerLimits(clazz);
\r
74 if (limits == null) {
\r
77 } catch (FileNotFoundException e) {
\r
78 log.warn("No limits are found for " + clazz + " executable! "
\r
79 + e.getLocalizedMessage(), e.getCause());
\r
81 } catch (IOException e) {
\r
82 log.warn("IO exception while attempting to read limits for "
\r
83 + clazz + " executable! " + e.getLocalizedMessage(), e
\r
90 public static synchronized <T> RunnerConfig<T> getSupportedOptions(
\r
91 Class<? extends Executable<T>> clazz) {
\r
93 return ConfExecutable.getRunnerOptions(clazz);
\r
94 } catch (FileNotFoundException e) {
\r
95 log.error("Could not load " + clazz + " Parameters !"
\r
96 + e.getMessage(), e.getCause());
\r
97 } catch (IOException e) {
\r
98 log.error("IO exception while reading " + clazz + " Parameters !"
\r
99 + e.getMessage(), e.getCause());
\r
104 public static <T> PresetManager<T> getPresets(
\r
105 Class<? extends Executable<T>> clazz) {
\r
107 return ConfExecutable.getRunnerPresets(clazz);
\r
108 } catch (FileNotFoundException e) {
\r
109 log.warn("No presets are found for " + clazz + " executable! "
\r
110 + e.getLocalizedMessage(), e.getCause());
\r
111 } catch (IOException e) {
\r
112 log.warn("IO exception while reading presents! for " + clazz
\r
113 + " executable! " + e.getLocalizedMessage(), e.getCause());
\r
118 public static final Alignment readClustalFile(String workDirectory,
\r
119 String clustFile) throws UnknownFileFormatException, IOException,
\r
120 FileNotFoundException, NullPointerException {
\r
121 assert !compbio.util.Util.isEmpty(workDirectory);
\r
122 assert !compbio.util.Util.isEmpty(clustFile);
\r
123 File cfile = new File(compbio.engine.client.Util.getFullPath(
\r
124 workDirectory, clustFile));
\r
125 log.trace("CLUSTAL OUTPUT FILE PATH: " + cfile.getAbsolutePath());
\r
126 if (!(cfile.exists() && cfile.length() > 0)) {
\r
127 throw new FileNotFoundException("Result for the jobId "
\r
128 + workDirectory + " with file name " + clustFile
\r
129 + " is not found!");
\r
131 return ClustalAlignmentUtil.readClustalFile(cfile);
\r
134 public static final List<AnnotatedSequence> readJronnFile(
\r
135 String workDirectory, String clustFile)
\r
136 throws UnknownFileFormatException, IOException,
\r
137 FileNotFoundException, NullPointerException {
\r
138 assert !compbio.util.Util.isEmpty(workDirectory);
\r
139 assert !compbio.util.Util.isEmpty(clustFile);
\r
140 File cfile = new File(compbio.engine.client.Util.getFullPath(
\r
141 workDirectory, clustFile));
\r
142 log.trace("Jronn OUTPUT FILE PATH: " + cfile.getAbsolutePath());
\r
143 if (!(cfile.exists() && cfile.length() > 0)) {
\r
144 throw new FileNotFoundException("Result for the jobId "
\r
145 + workDirectory + " with file name " + clustFile
\r
146 + " is not found!");
\r
148 return SequenceUtil.readJRonn(cfile);
\r
151 public static void writeInput(List<FastaSequence> sequences,
\r
152 ConfiguredExecutable<?> exec) {
\r
154 File filein = new File(exec.getInput());
\r
156 FileOutputStream fout = new FileOutputStream(filein);
\r
157 log.debug("File path: " + filein.getAbsolutePath());
\r
158 SequenceUtil.writeFasta(fout, sequences);
\r
160 } catch (FileNotFoundException e) {
\r
161 e.printStackTrace();
\r
162 } catch (IOException e) {
\r
163 e.printStackTrace();
\r