Refactoring: rename duplicated Util classes
[jabaws.git] / runner / compbio / runner / RunnerUtil.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
4  * \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
7  * \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
11  * \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
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.runner;\r
20 \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
28 \r
29 import org.apache.log4j.Logger;\r
30 \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.engine.client.EngineUtil;\r
43 import compbio.metadata.JobSubmissionException;\r
44 import compbio.metadata.PresetManager;\r
45 import compbio.metadata.RunnerConfig;\r
46 \r
47 public final class RunnerUtil {\r
48 \r
49         public static Logger log = Logger.getLogger(RunnerUtil.class);\r
50 \r
51         public static synchronized <T> RunnerConfig<T> getSupportedOptions(\r
52                         Class<? extends Executable<T>> clazz) {\r
53                 try {\r
54                         return ConfExecutable.getRunnerOptions(clazz);\r
55                 } catch (FileNotFoundException e) {\r
56                         log.error("Could not load " + clazz + " parameters !" + e.getMessage(), e.getCause());\r
57                 } catch (IOException e) {\r
58                         log.error("IO exception while reading " + clazz + " parameters !" + e.getMessage(), e.getCause());\r
59                 }\r
60                 return null;\r
61         }\r
62 \r
63         public static <T> PresetManager<T> getPresets(\r
64                         Class<? extends Executable<T>> clazz) {\r
65                 try {\r
66                         return ConfExecutable.getRunnerPresets(clazz);\r
67                 } catch (FileNotFoundException e) {\r
68                         log.warn("No presets are found for " + clazz + "! " + e.getLocalizedMessage(), e.getCause());\r
69                 } catch (IOException e) {\r
70                         log.warn("IO exception while reading presets for " + clazz + "! " + e.getLocalizedMessage(), e.getCause());\r
71                 }\r
72                 return null;\r
73         }\r
74 \r
75         public static final Alignment readClustalFile(String workDirectory, String clustFile) \r
76                                 throws UnknownFileFormatException, IOException, FileNotFoundException, NullPointerException {\r
77                 assert !compbio.util.Util.isEmpty(workDirectory);\r
78                 assert !compbio.util.Util.isEmpty(clustFile);\r
79                 File cfile = new File(EngineUtil.getFullPath(workDirectory, clustFile));\r
80                 log.trace("CLUSTAL OUTPUT FILE PATH: " + cfile.getAbsolutePath());\r
81                 if (!(cfile.exists() && cfile.length() > 0)) {\r
82                         throw new FileNotFoundException("Result for the jobId " + workDirectory + " with file name " + clustFile + " is not found!");\r
83                 }\r
84                 return ClustalAlignmentUtil.readClustalFile(cfile);\r
85         }\r
86 \r
87         public static final Map<String, Score> readJronnFile(String workDirectory,\r
88                         String clustFile) throws UnknownFileFormatException, IOException,\r
89                         FileNotFoundException, NullPointerException {\r
90                 assert !compbio.util.Util.isEmpty(workDirectory);\r
91                 assert !compbio.util.Util.isEmpty(clustFile);\r
92                 File cfile = new File(EngineUtil.getFullPath(workDirectory, clustFile));\r
93                 log.trace("Jronn OUTPUT FILE PATH: " + cfile.getAbsolutePath());\r
94                 if (!(cfile.exists() && cfile.length() > 0)) {\r
95                         throw new FileNotFoundException("Result for the jobId " + workDirectory + " with file name " + clustFile + " is not found!");\r
96                 }\r
97                 return SequenceUtil.readJRonn(cfile);\r
98         }\r
99 \r
100         public static void writeInput(List<FastaSequence> sequences,\r
101                         ConfiguredExecutable<?> exec) throws JobSubmissionException {\r
102                 try {\r
103                         File filein = new File(exec.getInput());\r
104                         FileOutputStream fout = new FileOutputStream(filein);\r
105                         log.debug("File path: " + filein.getAbsolutePath());\r
106                         SequenceUtil.writeFasta(fout, sequences);\r
107                         fout.close();\r
108                 } catch (IOException e) {\r
109                         log.error("IOException while writing input file into the disk: " + e.getLocalizedMessage(), e);\r
110                         throw new JobSubmissionException("We are sorry by JABAWS server seems to have a problem! " + e.getLocalizedMessage(), e);\r
111                 }\r
112         }\r
113 \r
114         public static void writeClustalInput(List<FastaSequence> sequences,\r
115                         ConfiguredExecutable<?> exec, char gapChar) throws JobSubmissionException {\r
116                 try {\r
117                         File filein = new File(exec.getInput());\r
118                         FileOutputStream fout = new FileOutputStream(filein);\r
119                         log.debug("File path: " + filein.getAbsolutePath());\r
120                         SequenceUtil.writeClustal(fout, sequences, gapChar);\r
121                         fout.close();\r
122                 } catch (IOException e) {\r
123                         log.error("IOException while writing input file into the disk: " + e.getLocalizedMessage(), e);\r
124                         throw new JobSubmissionException("We are sorry but JABAWS server seems to have a problem! " + e.getLocalizedMessage(), e);\r
125                 }\r
126         }\r
127 \r
128         public static RNAStructScoreManager readRNAStruct(String workDirectory,\r
129                         String structFile) throws IOException, FileNotFoundException {\r
130                 \r
131                 assert !compbio.util.Util.isEmpty(workDirectory);\r
132                 assert !compbio.util.Util.isEmpty(structFile);\r
133                 // The stdout from RNAalifold\r
134                 File sFile = new File(EngineUtil.getFullPath( workDirectory, structFile));\r
135                 // Base pair probability matrix (-p option)\r
136                 File aliFile = new File(EngineUtil.getFullPath( workDirectory, "alifold.out"));\r
137                 // Check that stdout file exists\r
138                 if(!(sFile.exists() && sFile.length() > 0)) {\r
139                         throw new FileNotFoundException("Result for the jobId " + workDirectory + "with file name " + structFile + " is not found!");\r
140                 }\r
141                 // Check that base pair probability file exists\r
142                 if(!aliFile.exists()) {\r
143                         log.warn("The file alifold.out is not found for the jobId "\r
144                                         + workDirectory + "Is the -p or --MEA option not specified?");\r
145                         return RNAStructReader.readRNAStructStream(new FileInputStream(sFile));\r
146                         \r
147                 } else {\r
148                         return RNAStructReader.readRNAStructStream(new FileInputStream(sFile), \r
149                                         new FileInputStream(aliFile));\r
150                 }\r
151         }\r
152 \r
153 }\r
154 \r
155 \r