Reverted some changes I made for testing back to their original states.
[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.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
38 \r
39 \r
40 \r
41 \r
42 import org.apache.log4j.Logger;\r
43 \r
44 import compbio.data.sequence.Alignment;\r
45 import compbio.data.sequence.ClustalAlignmentUtil;\r
46 import compbio.data.sequence.FastaSequence;\r
47 import compbio.data.sequence.RNAStructScoreManager;\r
48 import compbio.data.sequence.Score;\r
49 import compbio.data.sequence.ScoreManager;\r
50 import compbio.data.sequence.Range;\r
51 import compbio.data.sequence.RNAStructReader;\r
52 import compbio.data.sequence.SequenceUtil;\r
53 import compbio.data.sequence.UnknownFileFormatException;\r
54 import compbio.engine.client.ConfExecutable;\r
55 import compbio.engine.client.ConfiguredExecutable;\r
56 import compbio.engine.client.Executable;\r
57 import compbio.engine.conf.PropertyHelperManager;\r
58 import compbio.metadata.JobSubmissionException;\r
59 import compbio.metadata.PresetManager;\r
60 import compbio.metadata.RunnerConfig;\r
61 import compbio.util.PropertyHelper;\r
62 \r
63 public final class Util {\r
64 \r
65         public static Logger log = Logger.getLogger(Util.class);\r
66 \r
67         private static final PropertyHelper ph = PropertyHelperManager\r
68                         .getPropertyHelper();\r
69 \r
70         public static final String SPACE = " ";\r
71 \r
72         public static synchronized <T> RunnerConfig<T> getSupportedOptions(\r
73                         Class<? extends Executable<T>> clazz) {\r
74                 try {\r
75                         return ConfExecutable.getRunnerOptions(clazz);\r
76                 } catch (FileNotFoundException e) {\r
77                         log.error(\r
78                                         "Could not load " + clazz + " Parameters !"\r
79                                                         + e.getMessage(), e.getCause());\r
80                 } catch (IOException e) {\r
81                         log.error("IO exception while reading " + clazz + " Parameters !"\r
82                                         + e.getMessage(), e.getCause());\r
83                 }\r
84                 return null;\r
85         }\r
86 \r
87         public static <T> PresetManager<T> getPresets(\r
88                         Class<? extends Executable<T>> clazz) {\r
89                 try {\r
90                         return ConfExecutable.getRunnerPresets(clazz);\r
91                 } catch (FileNotFoundException e) {\r
92                         log.warn(\r
93                                         "No presets are found for " + clazz + " executable! "\r
94                                                         + e.getLocalizedMessage(), e.getCause());\r
95                 } catch (IOException e) {\r
96                         log.warn("IO exception while reading presents! for " + clazz\r
97                                         + " executable! " + e.getLocalizedMessage(), e.getCause());\r
98                 }\r
99                 return null;\r
100         }\r
101 \r
102         public static final Alignment readClustalFile(String workDirectory,\r
103                         String clustFile) throws UnknownFileFormatException, IOException,\r
104                         FileNotFoundException, NullPointerException {\r
105                 assert !compbio.util.Util.isEmpty(workDirectory);\r
106                 assert !compbio.util.Util.isEmpty(clustFile);\r
107                 File cfile = new File(compbio.engine.client.Util.getFullPath(\r
108                                 workDirectory, clustFile));\r
109                 log.trace("CLUSTAL OUTPUT FILE PATH: " + cfile.getAbsolutePath());\r
110                 if (!(cfile.exists() && cfile.length() > 0)) {\r
111                         throw new FileNotFoundException("Result for the jobId "\r
112                                         + workDirectory + " with file name " + clustFile\r
113                                         + " is not found!");\r
114                 }\r
115                 return ClustalAlignmentUtil.readClustalFile(cfile);\r
116         }\r
117 \r
118         public static final Map<String, Score> readJronnFile(String workDirectory,\r
119                         String clustFile) throws UnknownFileFormatException, IOException,\r
120                         FileNotFoundException, NullPointerException {\r
121                 assert !compbio.util.Util.isEmpty(workDirectory);\r
122                 assert !compbio.util.Util.isEmpty(clustFile);\r
123                 File cfile = new File(compbio.engine.client.Util.getFullPath(\r
124                                 workDirectory, clustFile));\r
125                 log.trace("Jronn OUTPUT FILE PATH: " + cfile.getAbsolutePath());\r
126                 if (!(cfile.exists() && cfile.length() > 0)) {\r
127                         throw new FileNotFoundException("Result for the jobId "\r
128                                         + workDirectory + " with file name " + clustFile\r
129                                         + " is not found!");\r
130                 }\r
131                 return SequenceUtil.readJRonn(cfile);\r
132         }\r
133 \r
134         public static void writeInput(List<FastaSequence> sequences,\r
135                         ConfiguredExecutable<?> exec) throws JobSubmissionException {\r
136 \r
137                 try {\r
138                         File filein = new File(exec.getInput());\r
139                         FileOutputStream fout = new FileOutputStream(filein);\r
140                         log.debug("File path: " + filein.getAbsolutePath());\r
141                         SequenceUtil.writeFasta(fout, sequences);\r
142                         fout.close();\r
143                 } catch (IOException e) {\r
144                         log.error("IOException while writing input file into the disk: "\r
145                                         + e.getLocalizedMessage(), e);\r
146                         throw new JobSubmissionException(\r
147                                         "We are sorry by JABAWS server seems to have a problem! "\r
148                                                         + e.getLocalizedMessage(), e);\r
149                 }\r
150         }\r
151         \r
152         public static void writeClustalInput(List<FastaSequence> sequences,\r
153                         ConfiguredExecutable<?> exec, char gapChar) throws JobSubmissionException {\r
154                 \r
155                 try {\r
156                         File filein = new File(exec.getInput());\r
157                         FileOutputStream fout = new FileOutputStream(filein);\r
158                         log.debug("File path: " + filein.getAbsolutePath());\r
159                         SequenceUtil.writeClustal(fout, sequences, gapChar);\r
160                         fout.close();\r
161                 } catch (IOException e) {\r
162                         log.error("IOException while writing input file into the disk: "\r
163                                         + e.getLocalizedMessage(), e);\r
164                         throw new JobSubmissionException(\r
165                                         "We are sorry but JABAWS server seems to have a problem! "\r
166                                                 + e.getLocalizedMessage(), e);\r
167                 }\r
168         }\r
169 \r
170         \r
171         public static RNAStructScoreManager readRNAStruct(String workDirectory,\r
172                         String structFile) throws IOException, FileNotFoundException {\r
173                 \r
174                 assert !compbio.util.Util.isEmpty(workDirectory);\r
175                 assert !compbio.util.Util.isEmpty(structFile);\r
176                 // The stdout from RNAalifold\r
177                 File sFile = new File(compbio.engine.client.Util.getFullPath(\r
178                                 workDirectory, structFile));\r
179                 // Base pair probability matrix (-p option)\r
180                 File aliFile = new File(compbio.engine.client.Util.getFullPath(\r
181                                 workDirectory, "alifold.out"));\r
182                 // Check that stdout file exists\r
183                 if(!(sFile.exists() && sFile.length() > 0)) {\r
184                         throw new FileNotFoundException("Result for the jobId "\r
185                                         + workDirectory + "with file name " + structFile\r
186                                         + " is not found!");\r
187                 }\r
188                 // Check that base pair probability file exists\r
189                 if(!aliFile.exists()) {\r
190                         log.warn("The file alifold.out is not found for the jobId "\r
191                                         + workDirectory + "Is the -p or --MEA option not specified?");\r
192                         return RNAStructReader.readRNAStructStream(new FileInputStream(sFile));\r
193                         \r
194                 } else {\r
195                         return RNAStructReader.readRNAStructStream(new FileInputStream(sFile), \r
196                                         new FileInputStream(aliFile));\r
197                 }\r
198         }\r
199         \r
200 \r
201         \r
202         public static String readFile(File inputFile) throws \r
203                         FileNotFoundException, IOException {\r
204 \r
205                 BufferedReader input    = new BufferedReader(new FileReader(inputFile));\r
206 \r
207                 String file = new String();\r
208                 String line = new String();\r
209 \r
210                 while (true) {\r
211                         line = input.readLine();\r
212 \r
213                         if (line != null) {\r
214                                 file = file + line + "\r\n";\r
215                         } else break;\r
216                 }\r
217                 // Close file\r
218                 input.close();\r
219                 return file;\r
220         }\r
221 }\r
222 \r
223 \r