1 /* Copyright (c) 2011 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.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.FileInputStream;
\r
23 import java.io.FileReader;
\r
24 import java.io.BufferedReader;
\r
25 import java.io.FileNotFoundException;
\r
26 import java.io.FileOutputStream;
\r
27 import java.io.IOException;
\r
28 import java.util.List;
\r
29 import java.util.Map;
\r
31 import org.apache.log4j.Logger;
\r
33 import compbio.data.sequence.Alignment;
\r
34 import compbio.data.sequence.ClustalAlignmentUtil;
\r
35 import compbio.data.sequence.FastaSequence;
\r
36 import compbio.data.sequence.RNAstruct;
\r
37 import compbio.data.sequence.Score;
\r
38 import compbio.data.sequence.SequenceUtil;
\r
39 import compbio.data.sequence.UnknownFileFormatException;
\r
40 import compbio.engine.client.ConfExecutable;
\r
41 import compbio.engine.client.ConfiguredExecutable;
\r
42 import compbio.engine.client.Executable;
\r
43 import compbio.engine.conf.PropertyHelperManager;
\r
44 import compbio.metadata.JobSubmissionException;
\r
45 import compbio.metadata.PresetManager;
\r
46 import compbio.metadata.RunnerConfig;
\r
47 import compbio.util.PropertyHelper;
\r
49 public final class Util {
\r
51 public static Logger log = Logger.getLogger(Util.class);
\r
53 private static final PropertyHelper ph = PropertyHelperManager
\r
54 .getPropertyHelper();
\r
56 public static final String SPACE = " ";
\r
58 public static synchronized <T> RunnerConfig<T> getSupportedOptions(
\r
59 Class<? extends Executable<T>> clazz) {
\r
61 return ConfExecutable.getRunnerOptions(clazz);
\r
62 } catch (FileNotFoundException e) {
\r
64 "Could not load " + clazz + " Parameters !"
\r
65 + e.getMessage(), e.getCause());
\r
66 } catch (IOException e) {
\r
67 log.error("IO exception while reading " + clazz + " Parameters !"
\r
68 + e.getMessage(), e.getCause());
\r
73 public static <T> PresetManager<T> getPresets(
\r
74 Class<? extends Executable<T>> clazz) {
\r
76 return ConfExecutable.getRunnerPresets(clazz);
\r
77 } catch (FileNotFoundException e) {
\r
79 "No presets are found for " + clazz + " executable! "
\r
80 + e.getLocalizedMessage(), e.getCause());
\r
81 } catch (IOException e) {
\r
82 log.warn("IO exception while reading presents! for " + clazz
\r
83 + " executable! " + e.getLocalizedMessage(), e.getCause());
\r
88 public static final Alignment readClustalFile(String workDirectory,
\r
89 String clustFile) throws UnknownFileFormatException, IOException,
\r
90 FileNotFoundException, NullPointerException {
\r
91 assert !compbio.util.Util.isEmpty(workDirectory);
\r
92 assert !compbio.util.Util.isEmpty(clustFile);
\r
93 File cfile = new File(compbio.engine.client.Util.getFullPath(
\r
94 workDirectory, clustFile));
\r
95 log.trace("CLUSTAL OUTPUT FILE PATH: " + cfile.getAbsolutePath());
\r
96 if (!(cfile.exists() && cfile.length() > 0)) {
\r
97 throw new FileNotFoundException("Result for the jobId "
\r
98 + workDirectory + " with file name " + clustFile
\r
99 + " is not found!");
\r
101 return ClustalAlignmentUtil.readClustalFile(cfile);
\r
104 public static final Map<String, Score> readJronnFile(String workDirectory,
\r
105 String clustFile) throws UnknownFileFormatException, IOException,
\r
106 FileNotFoundException, NullPointerException {
\r
107 assert !compbio.util.Util.isEmpty(workDirectory);
\r
108 assert !compbio.util.Util.isEmpty(clustFile);
\r
109 File cfile = new File(compbio.engine.client.Util.getFullPath(
\r
110 workDirectory, clustFile));
\r
111 log.trace("Jronn OUTPUT FILE PATH: " + cfile.getAbsolutePath());
\r
112 if (!(cfile.exists() && cfile.length() > 0)) {
\r
113 throw new FileNotFoundException("Result for the jobId "
\r
114 + workDirectory + " with file name " + clustFile
\r
115 + " is not found!");
\r
117 return SequenceUtil.readJRonn(cfile);
\r
120 public static void writeInput(List<FastaSequence> sequences,
\r
121 ConfiguredExecutable<?> exec) throws JobSubmissionException {
\r
124 File filein = new File(exec.getInput());
\r
125 FileOutputStream fout = new FileOutputStream(filein);
\r
126 log.debug("File path: " + filein.getAbsolutePath());
\r
127 SequenceUtil.writeFasta(fout, sequences);
\r
129 } catch (IOException e) {
\r
130 log.error("IOException while writing input file into the disk: "
\r
131 + e.getLocalizedMessage(), e);
\r
132 throw new JobSubmissionException(
\r
133 "We are sorry by JABAWS server seems to have a problem! "
\r
134 + e.getLocalizedMessage(), e);
\r
138 public static void writeClustalInput(List<FastaSequence> sequences,
\r
139 ConfiguredExecutable<?> exec, char gapChar) throws JobSubmissionException {
\r
142 File filein = new File(exec.getInput());
\r
143 FileOutputStream fout = new FileOutputStream(filein);
\r
144 log.debug("File path: " + filein.getAbsolutePath());
\r
145 SequenceUtil.writeClustal(fout, sequences, gapChar);
\r
147 } catch (IOException e) {
\r
148 log.error("IOException while writing input file into the disk: "
\r
149 + e.getLocalizedMessage(), e);
\r
150 throw new JobSubmissionException(
\r
151 "We are sorry but JABAWS server seems to have a problem! "
\r
152 + e.getLocalizedMessage(), e);
\r
158 // public static final RNAstruct readRNAStructFile(String workDirectory,
\r
159 // String structFile) throws IOException, FileNotFoundException {
\r
160 // assert !compbio.util.Util.isEmpty(workDirectory);
\r
161 // assert !compbio.util.Util.isEmpty(structFile);
\r
162 // File sfile = new File(compbio.engine.client.Util.getFullPath(
\r
163 // workDirectory, structFile));
\r
164 // if(!(sfile.exists() && sfile.length() > 0)) {
\r
165 // throw new FileNotFoundException("Result for the jobId "
\r
166 // + workDirectory + "with file name " + structFile
\r
167 // + " is not found!");
\r
169 // return compbio.data.sequence.SequenceUtil.readRNAalifoldResults(
\r
170 // new FileInputStream(sfile));
\r
173 public static String readRNAStruct(String workDirectory,
\r
174 String structFile) throws IOException, FileNotFoundException {
\r
175 assert !compbio.util.Util.isEmpty(workDirectory);
\r
176 assert !compbio.util.Util.isEmpty(structFile);
\r
177 File sfile = new File(compbio.engine.client.Util.getFullPath(
\r
178 workDirectory, structFile));
\r
179 if(!(sfile.exists() && sfile.length() > 0)) {
\r
180 throw new FileNotFoundException("Result for the jobId "
\r
181 + workDirectory + "with file name " + structFile
\r
182 + " is not found!");
\r
184 return readFile(sfile);
\r
187 public static String readFile(File inputFile) throws
\r
188 FileNotFoundException, IOException {
\r
190 BufferedReader input = new BufferedReader(new FileReader(inputFile));
\r
192 String file = new String();
\r
193 String line = new String();
\r
196 line = input.readLine();
\r
198 if (line != null) {
\r
199 file = file + line + "\r\n";
\r