137c570a843aef899a63efb4b03886e66a8e5bbf
[jabaws.git] / webservices / compbio / ws / client / IOHelper.java
1 package compbio.ws.client;\r
2 \r
3 import static compbio.ws.client.Constraints.pseparator;\r
4 \r
5 import java.io.BufferedReader;\r
6 import java.io.File;\r
7 import java.io.FileReader;\r
8 import java.io.FileWriter;\r
9 import java.io.IOException;\r
10 import java.io.Writer;\r
11 import java.util.ArrayList;\r
12 import java.util.List;\r
13 \r
14 import compbio.data.sequence.Alignment;\r
15 import compbio.data.sequence.ClustalAlignmentUtil;\r
16 import compbio.data.sequence.ScoreManager;\r
17 \r
18 public class IOHelper {\r
19 \r
20         /**\r
21          * Checks -i options and return the File if one was provided, null otherwise\r
22          * \r
23          * @param cmd\r
24          * @param key\r
25          * @param mustExist\r
26          * @return\r
27          * @throws IOException\r
28          */\r
29         static File getFile(String[] cmd, String key, boolean mustExist)\r
30                         throws IOException {\r
31                 assert key != null && key.trim().length() != 0;\r
32                 for (int i = 0; i < cmd.length; i++) {\r
33                         String filename = cmd[i];\r
34                         filename = filename.trim();\r
35                         if (filename.toLowerCase().startsWith(key + pseparator)) {\r
36                                 filename = filename.substring((key + pseparator).length());\r
37                                 File file = new File(filename);\r
38                                 if (mustExist && !file.exists()) {\r
39                                         System.out.println(key + " file " + file.getAbsolutePath()\r
40                                                         + " does not exist");\r
41                                         return null;\r
42                                 }\r
43                                 if (!mustExist && !file.exists()) {\r
44                                         file.createNewFile();\r
45                                 }\r
46                                 if (!file.canRead()) {\r
47                                         System.out.println("Cannot read " + key + " file "\r
48                                                         + file.getAbsolutePath());\r
49                                         return null;\r
50                                 }\r
51                                 return file;\r
52                         }\r
53                 }\r
54                 return null;\r
55         }\r
56 \r
57         /**\r
58          * Load parameters from file\r
59          * \r
60          * @throws IOException\r
61          */\r
62         static List<String> loadParameters(File paramsfile) throws IOException {\r
63                 assert paramsfile != null && paramsfile.exists();\r
64                 BufferedReader reader = new BufferedReader(new FileReader(paramsfile));\r
65                 String line = null;\r
66                 ArrayList<String> params = new ArrayList<String>();\r
67                 while ((line = reader.readLine()) != null) {\r
68                         line = line.trim();\r
69                         if (line.length() == 0)\r
70                                 continue;\r
71                         params.add(line);\r
72                 }\r
73                 return params;\r
74         }\r
75 \r
76         static Writer getWriter(File file) throws IOException {\r
77                 assert file != null && file.exists();\r
78                 return new FileWriter(file);\r
79         }\r
80 \r
81         /**\r
82          * Outputs clustal formatted alignment into the file represented by the\r
83          * outStream\r
84          * \r
85          * @param outStream\r
86          * @param align\r
87          *            the alignment to output\r
88          */\r
89         static void writeOut(Writer writer, Alignment align) {\r
90                 try {\r
91                         ClustalAlignmentUtil.writeClustalAlignment(writer, align);\r
92                 } catch (IOException e) {\r
93                         System.err\r
94                                         .println("Problems writing output file! Stack trace is below: ");\r
95                         e.printStackTrace();\r
96                 } finally {\r
97                         if (writer != null) {\r
98                                 try {\r
99                                         writer.close();\r
100                                 } catch (IOException ignored) {\r
101                                         // e.printStackTrace();\r
102                                 }\r
103                         }\r
104                 }\r
105         }\r
106 \r
107         /**\r
108          * Outputs AAcon results into the file represented by the outStream\r
109          * \r
110          * @param outStream\r
111          * @param result\r
112          *            the AACon scores to output\r
113          */\r
114         static void writeOut(Writer writer, ScoreManager results) {\r
115                 if (results == null) {\r
116                         return;\r
117                 }\r
118                 try {\r
119                         results.writeOut(writer);\r
120                 } catch (IOException e) {\r
121                         System.err\r
122                                         .println("Problems writing output file! Stack trace is below: ");\r
123                         e.printStackTrace();\r
124                 } finally {\r
125                         if (writer != null) {\r
126                                 try {\r
127                                         writer.close();\r
128                                 } catch (IOException ignored) {\r
129                                         // e.printStackTrace();\r
130                                 }\r
131                         }\r
132                 }\r
133         }\r
134 \r
135 }\r