f033bf01d616434001a3225f990d5d25ac478627
[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.RNAstruct;\r
35 import compbio.data.sequence.Score;\r
36 import compbio.data.sequence.SequenceUtil;\r
37 import compbio.data.sequence.UnknownFileFormatException;\r
38 import compbio.engine.client.ConfExecutable;\r
39 import compbio.engine.client.ConfiguredExecutable;\r
40 import compbio.engine.client.Executable;\r
41 import compbio.engine.conf.PropertyHelperManager;\r
42 import compbio.metadata.JobSubmissionException;\r
43 import compbio.metadata.PresetManager;\r
44 import compbio.metadata.RunnerConfig;\r
45 import compbio.util.PropertyHelper;\r
46 \r
47 public final class Util {\r
48 \r
49         public static Logger log = Logger.getLogger(Util.class);\r
50 \r
51         private static final PropertyHelper ph = PropertyHelperManager\r
52                         .getPropertyHelper();\r
53 \r
54         public static final String SPACE = " ";\r
55 \r
56         public static synchronized <T> RunnerConfig<T> getSupportedOptions(\r
57                         Class<? extends Executable<T>> clazz) {\r
58                 try {\r
59                         return ConfExecutable.getRunnerOptions(clazz);\r
60                 } catch (FileNotFoundException e) {\r
61                         log.error(\r
62                                         "Could not load " + clazz + " Parameters !"\r
63                                                         + e.getMessage(), e.getCause());\r
64                 } catch (IOException e) {\r
65                         log.error("IO exception while reading " + clazz + " Parameters !"\r
66                                         + e.getMessage(), e.getCause());\r
67                 }\r
68                 return null;\r
69         }\r
70 \r
71         public static <T> PresetManager<T> getPresets(\r
72                         Class<? extends Executable<T>> clazz) {\r
73                 try {\r
74                         return ConfExecutable.getRunnerPresets(clazz);\r
75                 } catch (FileNotFoundException e) {\r
76                         log.warn(\r
77                                         "No presets are found for " + clazz + " executable! "\r
78                                                         + e.getLocalizedMessage(), e.getCause());\r
79                 } catch (IOException e) {\r
80                         log.warn("IO exception while reading presents! for " + clazz\r
81                                         + " executable! " + e.getLocalizedMessage(), e.getCause());\r
82                 }\r
83                 return null;\r
84         }\r
85 \r
86         public static final Alignment readClustalFile(String workDirectory,\r
87                         String clustFile) throws UnknownFileFormatException, IOException,\r
88                         FileNotFoundException, NullPointerException {\r
89                 assert !compbio.util.Util.isEmpty(workDirectory);\r
90                 assert !compbio.util.Util.isEmpty(clustFile);\r
91                 File cfile = new File(compbio.engine.client.Util.getFullPath(\r
92                                 workDirectory, clustFile));\r
93                 log.trace("CLUSTAL OUTPUT FILE PATH: " + cfile.getAbsolutePath());\r
94                 if (!(cfile.exists() && cfile.length() > 0)) {\r
95                         throw new FileNotFoundException("Result for the jobId "\r
96                                         + workDirectory + " with file name " + clustFile\r
97                                         + " is not found!");\r
98                 }\r
99                 return ClustalAlignmentUtil.readClustalFile(cfile);\r
100         }\r
101 \r
102         public static final Map<String, Score> readJronnFile(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("Jronn 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 SequenceUtil.readJRonn(cfile);\r
116         }\r
117 \r
118         public static void writeInput(List<FastaSequence> sequences,\r
119                         ConfiguredExecutable<?> exec) throws JobSubmissionException {\r
120 \r
121                 try {\r
122                         File filein = new File(exec.getInput());\r
123                         FileOutputStream fout = new FileOutputStream(filein);\r
124                         log.debug("File path: " + filein.getAbsolutePath());\r
125                         SequenceUtil.writeFasta(fout, sequences);\r
126                         fout.close();\r
127                 } catch (IOException e) {\r
128                         log.error("IOException while writing input file into the disk: "\r
129                                         + e.getLocalizedMessage(), e);\r
130                         throw new JobSubmissionException(\r
131                                         "We are sorry by JABAWS server seems to have a problem! "\r
132                                                         + e.getLocalizedMessage(), e);\r
133                 }\r
134         }\r
135         \r
136         public static void writeClustalInput(List<FastaSequence> sequences,\r
137                         ConfiguredExecutable<?> exec, char gapChar) throws JobSubmissionException {\r
138                 \r
139                 try {\r
140                         File filein = new File(exec.getInput());\r
141                         FileOutputStream fout = new FileOutputStream(filein);\r
142                         log.debug("File path: " + filein.getAbsolutePath());\r
143                         SequenceUtil.writeClustal(fout, sequences, gapChar);\r
144                         fout.close();\r
145                 } catch (IOException e) {\r
146                         log.error("IOException while writing input file into the disk: "\r
147                                         + e.getLocalizedMessage(), e);\r
148                         throw new JobSubmissionException(\r
149                                         "We are sorry but JABAWS server seems to have a problem! "\r
150                                                 + e.getLocalizedMessage(), e);\r
151                 }\r
152         }\r
153         \r
154         public static final RNAstruct readRNAStructFile(String workDirectory,\r
155                         String structFile) throws IOException, FileNotFoundException {\r
156                 assert !compbio.util.Util.isEmpty(workDirectory);\r
157                 assert !compbio.util.Util.isEmpty(structFile);\r
158                 File sfile = new File(compbio.engine.client.Util.getFullPath(\r
159                                 workDirectory, structFile));\r
160                 log.trace("RNAALIFOLD OUTPUT FILE PATH: " + sfile.getAbsolutePath());\r
161                 if(!(sfile.exists() && sfile.length() > 0)) {\r
162                         throw new FileNotFoundException("Result for the jobId "\r
163                                         + workDirectory + "with file name " + structFile\r
164                                         + " is not found!");\r
165                 }\r
166                 return compbio.data.sequence.SequenceUtil.readRNAalifoldResults(\r
167                                 new FileInputStream(sfile));\r
168         }\r
169         \r
170         \r
171 \r
172 }\r