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