47f169baaa22981ebac848bd92b5bbe9df9cd419
[jabaws.git] / runner / compbio / runner / Util.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.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.FileNotFoundException;\r
23 import java.io.FileOutputStream;\r
24 import java.io.IOException;\r
25 import java.util.List;\r
26 import java.util.Map;\r
27 \r
28 import org.apache.log4j.Logger;\r
29 \r
30 import compbio.data.sequence.Alignment;\r
31 import compbio.data.sequence.ClustalAlignmentUtil;\r
32 import compbio.data.sequence.FastaSequence;\r
33 import compbio.data.sequence.Score;\r
34 import compbio.data.sequence.SequenceUtil;\r
35 import compbio.data.sequence.UnknownFileFormatException;\r
36 import compbio.engine.client.ConfExecutable;\r
37 import compbio.engine.client.ConfiguredExecutable;\r
38 import compbio.engine.client.Executable;\r
39 import compbio.engine.conf.PropertyHelperManager;\r
40 import compbio.metadata.PresetManager;\r
41 import compbio.metadata.RunnerConfig;\r
42 import compbio.util.PropertyHelper;\r
43 \r
44 public final class Util {\r
45 \r
46         public static Logger log = Logger.getLogger(Util.class);\r
47 \r
48         private static final PropertyHelper ph = PropertyHelperManager\r
49                         .getPropertyHelper();\r
50 \r
51         public static final String SPACE = " ";\r
52 \r
53         public static synchronized <T> RunnerConfig<T> getSupportedOptions(\r
54                         Class<? extends Executable<T>> clazz) {\r
55                 try {\r
56                         return ConfExecutable.getRunnerOptions(clazz);\r
57                 } catch (FileNotFoundException e) {\r
58                         log.error(\r
59                                         "Could not load " + clazz + " Parameters !"\r
60                                                         + e.getMessage(), e.getCause());\r
61                 } catch (IOException e) {\r
62                         log.error("IO exception while reading " + clazz + " Parameters !"\r
63                                         + e.getMessage(), e.getCause());\r
64                 }\r
65                 return null;\r
66         }\r
67 \r
68         public static <T> PresetManager<T> getPresets(\r
69                         Class<? extends Executable<T>> clazz) {\r
70                 try {\r
71                         return ConfExecutable.getRunnerPresets(clazz);\r
72                 } catch (FileNotFoundException e) {\r
73                         log.warn(\r
74                                         "No presets are found for " + clazz + " executable! "\r
75                                                         + e.getLocalizedMessage(), e.getCause());\r
76                 } catch (IOException e) {\r
77                         log.warn("IO exception while reading presents! for " + clazz\r
78                                         + " executable! " + e.getLocalizedMessage(), e.getCause());\r
79                 }\r
80                 return null;\r
81         }\r
82 \r
83         public static final Alignment readClustalFile(String workDirectory,\r
84                         String clustFile) throws UnknownFileFormatException, IOException,\r
85                         FileNotFoundException, NullPointerException {\r
86                 assert !compbio.util.Util.isEmpty(workDirectory);\r
87                 assert !compbio.util.Util.isEmpty(clustFile);\r
88                 File cfile = new File(compbio.engine.client.Util.getFullPath(\r
89                                 workDirectory, clustFile));\r
90                 log.trace("CLUSTAL OUTPUT FILE PATH: " + cfile.getAbsolutePath());\r
91                 if (!(cfile.exists() && cfile.length() > 0)) {\r
92                         throw new FileNotFoundException("Result for the jobId "\r
93                                         + workDirectory + " with file name " + clustFile\r
94                                         + " is not found!");\r
95                 }\r
96                 return ClustalAlignmentUtil.readClustalFile(cfile);\r
97         }\r
98 \r
99         public static final Map<String, Score> readJronnFile(String workDirectory,\r
100                         String clustFile) throws UnknownFileFormatException, IOException,\r
101                         FileNotFoundException, NullPointerException {\r
102                 assert !compbio.util.Util.isEmpty(workDirectory);\r
103                 assert !compbio.util.Util.isEmpty(clustFile);\r
104                 File cfile = new File(compbio.engine.client.Util.getFullPath(\r
105                                 workDirectory, clustFile));\r
106                 log.trace("Jronn OUTPUT FILE PATH: " + cfile.getAbsolutePath());\r
107                 if (!(cfile.exists() && cfile.length() > 0)) {\r
108                         throw new FileNotFoundException("Result for the jobId "\r
109                                         + workDirectory + " with file name " + clustFile\r
110                                         + " is not found!");\r
111                 }\r
112                 return SequenceUtil.readJRonn(cfile);\r
113         }\r
114 \r
115         public static void writeInput(List<FastaSequence> sequences,\r
116                         ConfiguredExecutable<?> exec) {\r
117 \r
118                 File filein = new File(exec.getInput());\r
119                 try {\r
120                         FileOutputStream fout = new FileOutputStream(filein);\r
121                         log.debug("File path: " + filein.getAbsolutePath());\r
122                         SequenceUtil.writeFasta(fout, sequences);\r
123                         fout.close();\r
124                 } catch (FileNotFoundException e) {\r
125                         e.printStackTrace();\r
126                 } catch (IOException e) {\r
127                         e.printStackTrace();\r
128                 }\r
129         }\r
130 \r
131 }\r