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.Arrays;
\r
29 import java.util.List;
\r
30 import java.util.ArrayList;
\r
31 import java.util.Map;
\r
32 import java.util.Set;
\r
33 import java.util.TreeSet;
\r
34 import java.util.TreeMap;
\r
35 import java.util.regex.Pattern;
\r
36 import java.util.regex.Matcher;
\r
37 import java.util.Scanner;
\r
41 import org.apache.log4j.Logger;
\r
43 import compbio.data.sequence.Alignment;
\r
44 import compbio.data.sequence.ClustalAlignmentUtil;
\r
45 import compbio.data.sequence.FastaSequence;
\r
46 import compbio.data.sequence.RNAStruct;
\r
47 import compbio.data.sequence.Score;
\r
48 import compbio.data.sequence.ScoreManager;
\r
49 import compbio.data.sequence.Range;
\r
50 import compbio.data.sequence.RNAStructReader;
\r
51 import compbio.data.sequence.SequenceUtil;
\r
52 import compbio.data.sequence.UnknownFileFormatException;
\r
53 import compbio.engine.client.ConfExecutable;
\r
54 import compbio.engine.client.ConfiguredExecutable;
\r
55 import compbio.engine.client.Executable;
\r
56 import compbio.engine.conf.PropertyHelperManager;
\r
57 import compbio.metadata.JobSubmissionException;
\r
58 import compbio.metadata.PresetManager;
\r
59 import compbio.metadata.RunnerConfig;
\r
60 import compbio.util.PropertyHelper;
\r
62 public final class Util {
\r
64 public static Logger log = Logger.getLogger(Util.class);
\r
66 private static final PropertyHelper ph = PropertyHelperManager
\r
67 .getPropertyHelper();
\r
69 public static final String SPACE = " ";
\r
71 public static synchronized <T> RunnerConfig<T> getSupportedOptions(
\r
72 Class<? extends Executable<T>> clazz) {
\r
74 return ConfExecutable.getRunnerOptions(clazz);
\r
75 } catch (FileNotFoundException e) {
\r
77 "Could not load " + clazz + " Parameters !"
\r
78 + e.getMessage(), e.getCause());
\r
79 } catch (IOException e) {
\r
80 log.error("IO exception while reading " + clazz + " Parameters !"
\r
81 + e.getMessage(), e.getCause());
\r
86 public static <T> PresetManager<T> getPresets(
\r
87 Class<? extends Executable<T>> clazz) {
\r
89 return ConfExecutable.getRunnerPresets(clazz);
\r
90 } catch (FileNotFoundException e) {
\r
92 "No presets are found for " + clazz + " executable! "
\r
93 + e.getLocalizedMessage(), e.getCause());
\r
94 } catch (IOException e) {
\r
95 log.warn("IO exception while reading presents! for " + clazz
\r
96 + " executable! " + e.getLocalizedMessage(), e.getCause());
\r
101 public static final Alignment readClustalFile(String workDirectory,
\r
102 String clustFile) throws UnknownFileFormatException, IOException,
\r
103 FileNotFoundException, NullPointerException {
\r
104 assert !compbio.util.Util.isEmpty(workDirectory);
\r
105 assert !compbio.util.Util.isEmpty(clustFile);
\r
106 File cfile = new File(compbio.engine.client.Util.getFullPath(
\r
107 workDirectory, clustFile));
\r
108 log.trace("CLUSTAL OUTPUT FILE PATH: " + cfile.getAbsolutePath());
\r
109 if (!(cfile.exists() && cfile.length() > 0)) {
\r
110 throw new FileNotFoundException("Result for the jobId "
\r
111 + workDirectory + " with file name " + clustFile
\r
112 + " is not found!");
\r
114 return ClustalAlignmentUtil.readClustalFile(cfile);
\r
117 public static final Map<String, Score> readJronnFile(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("Jronn 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
130 return SequenceUtil.readJRonn(cfile);
\r
133 public static void writeInput(List<FastaSequence> sequences,
\r
134 ConfiguredExecutable<?> exec) throws JobSubmissionException {
\r
137 File filein = new File(exec.getInput());
\r
138 FileOutputStream fout = new FileOutputStream(filein);
\r
139 log.debug("File path: " + filein.getAbsolutePath());
\r
140 SequenceUtil.writeFasta(fout, sequences);
\r
142 } catch (IOException e) {
\r
143 log.error("IOException while writing input file into the disk: "
\r
144 + e.getLocalizedMessage(), e);
\r
145 throw new JobSubmissionException(
\r
146 "We are sorry by JABAWS server seems to have a problem! "
\r
147 + e.getLocalizedMessage(), e);
\r
151 public static void writeClustalInput(List<FastaSequence> sequences,
\r
152 ConfiguredExecutable<?> exec, char gapChar) throws JobSubmissionException {
\r
155 File filein = new File(exec.getInput());
\r
156 FileOutputStream fout = new FileOutputStream(filein);
\r
157 log.debug("File path: " + filein.getAbsolutePath());
\r
158 SequenceUtil.writeClustal(fout, sequences, gapChar);
\r
160 } catch (IOException e) {
\r
161 log.error("IOException while writing input file into the disk: "
\r
162 + e.getLocalizedMessage(), e);
\r
163 throw new JobSubmissionException(
\r
164 "We are sorry but JABAWS server seems to have a problem! "
\r
165 + e.getLocalizedMessage(), e);
\r
170 public static ScoreManager readRNAStruct(String workDirectory,
\r
171 String structFile) throws IOException, FileNotFoundException {
\r
173 assert !compbio.util.Util.isEmpty(workDirectory);
\r
174 assert !compbio.util.Util.isEmpty(structFile);
\r
175 // The stdout from RNAalifold
\r
176 File sFile = new File(compbio.engine.client.Util.getFullPath(
\r
177 workDirectory, structFile));
\r
178 // Base pair probability matrix (-p option)
\r
179 File aliFile = new File(compbio.engine.client.Util.getFullPath(
\r
180 workDirectory, "alifold.out"));
\r
181 // Check that stdout file exists
\r
182 if(!(sFile.exists() && sFile.length() > 0)) {
\r
183 throw new FileNotFoundException("Result for the jobId "
\r
184 + workDirectory + "with file name " + structFile
\r
185 + " is not found!");
\r
187 // Check that base pair probability file exists
\r
188 if(!aliFile.exists()) {
\r
189 log.warn("The file alifold.out is not found for the jobId "
\r
190 + workDirectory + "Is the -p or --MEA option not specified?");
\r
191 return RNAStructReader.readRNAStructStream(new FileInputStream(sFile));
\r
194 return RNAStructReader.readRNAStructStream(new FileInputStream(sFile),
\r
195 new FileInputStream(aliFile));
\r
201 public static String readFile(File inputFile) throws
\r
202 FileNotFoundException, IOException {
\r
204 BufferedReader input = new BufferedReader(new FileReader(inputFile));
\r
206 String file = new String();
\r
207 String line = new String();
\r
210 line = input.readLine();
\r
212 if (line != null) {
\r
213 file = file + line + "\r\n";
\r