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