41bddd4099209b837b3556a6d4d18ffe37092ca5
[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.JobSubmissionException;\r
41 import compbio.metadata.PresetManager;\r
42 import compbio.metadata.RunnerConfig;\r
43 import compbio.util.PropertyHelper;\r
44 \r
45 public final class Util {\r
46 \r
47         public static Logger log = Logger.getLogger(Util.class);\r
48 \r
49         private static final PropertyHelper ph = PropertyHelperManager\r
50                         .getPropertyHelper();\r
51 \r
52         public static final String SPACE = " ";\r
53 \r
54         public static synchronized <T> RunnerConfig<T> getSupportedOptions(\r
55                         Class<? extends Executable<T>> clazz) {\r
56                 try {\r
57                         return ConfExecutable.getRunnerOptions(clazz);\r
58                 } catch (FileNotFoundException e) {\r
59                         log.error(\r
60                                         "Could not load " + clazz + " Parameters !"\r
61                                                         + e.getMessage(), e.getCause());\r
62                 } catch (IOException e) {\r
63                         log.error("IO exception while reading " + clazz + " Parameters !"\r
64                                         + e.getMessage(), e.getCause());\r
65                 }\r
66                 return null;\r
67         }\r
68 \r
69         public static <T> PresetManager<T> getPresets(\r
70                         Class<? extends Executable<T>> clazz) {\r
71                 try {\r
72                         return ConfExecutable.getRunnerPresets(clazz);\r
73                 } catch (FileNotFoundException e) {\r
74                         log.warn(\r
75                                         "No presets are found for " + clazz + " executable! "\r
76                                                         + e.getLocalizedMessage(), e.getCause());\r
77                 } catch (IOException e) {\r
78                         log.warn("IO exception while reading presents! for " + clazz\r
79                                         + " executable! " + e.getLocalizedMessage(), e.getCause());\r
80                 }\r
81                 return null;\r
82         }\r
83 \r
84         public static final Alignment readClustalFile(String workDirectory,\r
85                         String clustFile) throws UnknownFileFormatException, IOException,\r
86                         FileNotFoundException, NullPointerException {\r
87                 assert !compbio.util.Util.isEmpty(workDirectory);\r
88                 assert !compbio.util.Util.isEmpty(clustFile);\r
89                 File cfile = new File(compbio.engine.client.Util.getFullPath(\r
90                                 workDirectory, clustFile));\r
91                 log.trace("CLUSTAL OUTPUT FILE PATH: " + cfile.getAbsolutePath());\r
92                 if (!(cfile.exists() && cfile.length() > 0)) {\r
93                         throw new FileNotFoundException("Result for the jobId "\r
94                                         + workDirectory + " with file name " + clustFile\r
95                                         + " is not found!");\r
96                 }\r
97                 return ClustalAlignmentUtil.readClustalFile(cfile);\r
98         }\r
99 \r
100         public static final Map<String, Score> readJronnFile(String workDirectory,\r
101                         String clustFile) throws UnknownFileFormatException, IOException,\r
102                         FileNotFoundException, NullPointerException {\r
103                 assert !compbio.util.Util.isEmpty(workDirectory);\r
104                 assert !compbio.util.Util.isEmpty(clustFile);\r
105                 File cfile = new File(compbio.engine.client.Util.getFullPath(\r
106                                 workDirectory, clustFile));\r
107                 log.trace("Jronn OUTPUT FILE PATH: " + cfile.getAbsolutePath());\r
108                 if (!(cfile.exists() && cfile.length() > 0)) {\r
109                         throw new FileNotFoundException("Result for the jobId "\r
110                                         + workDirectory + " with file name " + clustFile\r
111                                         + " is not found!");\r
112                 }\r
113                 return SequenceUtil.readJRonn(cfile);\r
114         }\r
115 \r
116         public static void writeInput(List<FastaSequence> sequences,\r
117                         ConfiguredExecutable<?> exec) throws JobSubmissionException {\r
118 \r
119                 try {\r
120                         File filein = new File(exec.getInput());\r
121                         FileOutputStream fout = new FileOutputStream(filein);\r
122                         log.debug("File path: " + filein.getAbsolutePath());\r
123                         SequenceUtil.writeFasta(fout, sequences);\r
124                         fout.close();\r
125                 } catch (IOException e) {\r
126                         log.error("IOException while writing input file into the disk: "\r
127                                         + e.getLocalizedMessage(), e);\r
128                         throw new JobSubmissionException(\r
129                                         "We are sorry by JABAWS server seems to have a problem! "\r
130                                                         + e.getLocalizedMessage(), e);\r
131                 }\r
132         }\r
133 \r
134 }\r