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