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.FileNotFoundException;
\r
24 import java.io.FileOutputStream;
\r
25 import java.io.IOException;
\r
26 import java.util.List;
\r
27 import java.util.Map;
\r
29 import org.apache.log4j.Logger;
\r
31 import compbio.data.sequence.Alignment;
\r
32 import compbio.data.sequence.ClustalAlignmentUtil;
\r
33 import compbio.data.sequence.FastaSequence;
\r
34 import compbio.data.sequence.RNAStructScoreManager;
\r
35 import compbio.data.sequence.Score;
\r
36 import compbio.data.sequence.RNAStructReader;
\r
37 import compbio.data.sequence.SequenceUtil;
\r
38 import compbio.data.sequence.UnknownFileFormatException;
\r
39 import compbio.engine.client.ConfExecutable;
\r
40 import compbio.engine.client.ConfiguredExecutable;
\r
41 import compbio.engine.client.Executable;
\r
42 import compbio.metadata.JobSubmissionException;
\r
43 import compbio.metadata.PresetManager;
\r
44 import compbio.metadata.RunnerConfig;
\r
46 public final class Util {
\r
48 public static Logger log = Logger.getLogger(Util.class);
\r
50 public static synchronized <T> RunnerConfig<T> getSupportedOptions(
\r
51 Class<? extends Executable<T>> clazz) {
\r
53 return ConfExecutable.getRunnerOptions(clazz);
\r
54 } catch (FileNotFoundException e) {
\r
56 "Could not load " + clazz + " Parameters !"
\r
57 + e.getMessage(), e.getCause());
\r
58 } catch (IOException e) {
\r
59 log.error("IO exception while reading " + clazz + " Parameters !"
\r
60 + e.getMessage(), e.getCause());
\r
65 public static <T> PresetManager<T> getPresets(
\r
66 Class<? extends Executable<T>> clazz) {
\r
68 return ConfExecutable.getRunnerPresets(clazz);
\r
69 } catch (FileNotFoundException e) {
\r
71 "No presets are found for " + clazz + " executable! "
\r
72 + e.getLocalizedMessage(), e.getCause());
\r
73 } catch (IOException e) {
\r
74 log.warn("IO exception while reading presents! for " + clazz
\r
75 + " executable! " + e.getLocalizedMessage(), e.getCause());
\r
80 public static final Alignment readClustalFile(String workDirectory,
\r
81 String clustFile) throws UnknownFileFormatException, IOException,
\r
82 FileNotFoundException, NullPointerException {
\r
83 assert !compbio.util.Util.isEmpty(workDirectory);
\r
84 assert !compbio.util.Util.isEmpty(clustFile);
\r
85 File cfile = new File(compbio.engine.client.Util.getFullPath(
\r
86 workDirectory, clustFile));
\r
87 log.trace("CLUSTAL OUTPUT FILE PATH: " + cfile.getAbsolutePath());
\r
88 if (!(cfile.exists() && cfile.length() > 0)) {
\r
89 throw new FileNotFoundException("Result for the jobId "
\r
90 + workDirectory + " with file name " + clustFile
\r
91 + " is not found!");
\r
93 return ClustalAlignmentUtil.readClustalFile(cfile);
\r
96 public static final Map<String, Score> readJronnFile(String workDirectory,
\r
97 String clustFile) throws UnknownFileFormatException, IOException,
\r
98 FileNotFoundException, NullPointerException {
\r
99 assert !compbio.util.Util.isEmpty(workDirectory);
\r
100 assert !compbio.util.Util.isEmpty(clustFile);
\r
101 File cfile = new File(compbio.engine.client.Util.getFullPath(
\r
102 workDirectory, clustFile));
\r
103 log.trace("Jronn OUTPUT FILE PATH: " + cfile.getAbsolutePath());
\r
104 if (!(cfile.exists() && cfile.length() > 0)) {
\r
105 throw new FileNotFoundException("Result for the jobId "
\r
106 + workDirectory + " with file name " + clustFile
\r
107 + " is not found!");
\r
109 return SequenceUtil.readJRonn(cfile);
\r
112 public static void writeInput(List<FastaSequence> sequences,
\r
113 ConfiguredExecutable<?> exec) throws JobSubmissionException {
\r
115 File filein = new File(exec.getInput());
\r
116 FileOutputStream fout = new FileOutputStream(filein);
\r
117 log.debug("File path: " + filein.getAbsolutePath());
\r
118 SequenceUtil.writeFasta(fout, sequences);
\r
120 } catch (IOException e) {
\r
121 log.error("IOException while writing input file into the disk: "
\r
122 + e.getLocalizedMessage(), e);
\r
123 throw new JobSubmissionException(
\r
124 "We are sorry by JABAWS server seems to have a problem! "
\r
125 + e.getLocalizedMessage(), e);
\r
129 public static void writeClustalInput(List<FastaSequence> sequences,
\r
130 ConfiguredExecutable<?> exec, char gapChar) throws JobSubmissionException {
\r
133 File filein = new File(exec.getInput());
\r
134 FileOutputStream fout = new FileOutputStream(filein);
\r
135 log.debug("File path: " + filein.getAbsolutePath());
\r
136 SequenceUtil.writeClustal(fout, sequences, gapChar);
\r
138 } catch (IOException e) {
\r
139 log.error("IOException while writing input file into the disk: "
\r
140 + e.getLocalizedMessage(), e);
\r
141 throw new JobSubmissionException(
\r
142 "We are sorry but JABAWS server seems to have a problem! "
\r
143 + e.getLocalizedMessage(), e);
\r
147 public static RNAStructScoreManager readRNAStruct(String workDirectory,
\r
148 String structFile) throws IOException, FileNotFoundException {
\r
150 assert !compbio.util.Util.isEmpty(workDirectory);
\r
151 assert !compbio.util.Util.isEmpty(structFile);
\r
152 // The stdout from RNAalifold
\r
153 File sFile = new File(compbio.engine.client.Util.getFullPath(
\r
154 workDirectory, structFile));
\r
155 // Base pair probability matrix (-p option)
\r
156 File aliFile = new File(compbio.engine.client.Util.getFullPath(
\r
157 workDirectory, "alifold.out"));
\r
158 // Check that stdout file exists
\r
159 if(!(sFile.exists() && sFile.length() > 0)) {
\r
160 throw new FileNotFoundException("Result for the jobId "
\r
161 + workDirectory + "with file name " + structFile
\r
162 + " is not found!");
\r
164 // Check that base pair probability file exists
\r
165 if(!aliFile.exists()) {
\r
166 log.warn("The file alifold.out is not found for the jobId "
\r
167 + workDirectory + "Is the -p or --MEA option not specified?");
\r
168 return RNAStructReader.readRNAStructStream(new FileInputStream(sFile));
\r
171 return RNAStructReader.readRNAStructStream(new FileInputStream(sFile),
\r
172 new FileInputStream(aliFile));
\r