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