Clean up logging system
[jabaws.git] / runner / compbio / runner / Util.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.metadata.JobSubmissionException;\r
43 import compbio.metadata.PresetManager;\r
44 import compbio.metadata.RunnerConfig;\r
45 \r
46 public final class Util {\r
47 \r
48         public static Logger log = Logger.getLogger(Util.class);\r
49 \r
50         public static synchronized <T> RunnerConfig<T> getSupportedOptions(\r
51                         Class<? extends Executable<T>> clazz) {\r
52                 try {\r
53                         return ConfExecutable.getRunnerOptions(clazz);\r
54                 } catch (FileNotFoundException e) {\r
55                         log.error(\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
61                 }\r
62                 return null;\r
63         }\r
64 \r
65         public static <T> PresetManager<T> getPresets(\r
66                         Class<? extends Executable<T>> clazz) {\r
67                 try {\r
68                         return ConfExecutable.getRunnerPresets(clazz);\r
69                 } catch (FileNotFoundException e) {\r
70                         log.warn(\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
76                 }\r
77                 return null;\r
78         }\r
79 \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
92                 }\r
93                 return ClustalAlignmentUtil.readClustalFile(cfile);\r
94         }\r
95 \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
108                 }\r
109                 return SequenceUtil.readJRonn(cfile);\r
110         }\r
111 \r
112         public static void writeInput(List<FastaSequence> sequences,\r
113                         ConfiguredExecutable<?> exec) throws JobSubmissionException {\r
114                 try {\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
119                         fout.close();\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
126                 }\r
127         }\r
128 \r
129         public static void writeClustalInput(List<FastaSequence> sequences,\r
130                         ConfiguredExecutable<?> exec, char gapChar) throws JobSubmissionException {\r
131                 \r
132                 try {\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
137                         fout.close();\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
144                 }\r
145         }\r
146 \r
147         public static RNAStructScoreManager readRNAStruct(String workDirectory,\r
148                         String structFile) throws IOException, FileNotFoundException {\r
149                 \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
163                 }\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
169                         \r
170                 } else {\r
171                         return RNAStructReader.readRNAStructStream(new FileInputStream(sFile), \r
172                                         new FileInputStream(aliFile));\r
173                 }\r
174         }\r
175 \r
176 }\r
177 \r
178 \r