--- /dev/null
+package compbio.ws.client;\r
+\r
+import static compbio.ws.client.Constraints.pseparator;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.FileReader;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import compbio.data.sequence.Alignment;\r
+import compbio.data.sequence.ClustalAlignmentUtil;\r
+\r
+public class IOHelper {\r
+\r
+ /**\r
+ * Checks -i options and return the File if one was provided, null otherwise\r
+ * \r
+ * @param cmd\r
+ * @param key\r
+ * @param mustExist\r
+ * @return\r
+ * @throws IOException\r
+ */\r
+ static File getFile(String[] cmd, String key, boolean mustExist)\r
+ throws IOException {\r
+ assert key != null && key.trim().length() != 0;\r
+ for (int i = 0; i < cmd.length; i++) {\r
+ String filename = cmd[i];\r
+ filename = filename.trim();\r
+ if (filename.toLowerCase().startsWith(key + pseparator)) {\r
+ filename = filename.substring((key + pseparator).length());\r
+ File file = new File(filename);\r
+ if (mustExist && !file.exists()) {\r
+ System.out.println(key + " file " + file.getAbsolutePath()\r
+ + " does not exist");\r
+ return null;\r
+ }\r
+ if (!mustExist && !file.exists()) {\r
+ file.createNewFile();\r
+ }\r
+ if (!file.canRead()) {\r
+ System.out.println("Cannot read " + key + " file "\r
+ + file.getAbsolutePath());\r
+ return null;\r
+ }\r
+ return file;\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Load parameters from file\r
+ * \r
+ * @throws IOException\r
+ */\r
+ static List<String> loadParameters(File paramsfile) throws IOException {\r
+ assert paramsfile != null && paramsfile.exists();\r
+ BufferedReader reader = new BufferedReader(new FileReader(paramsfile));\r
+ String line = null;\r
+ ArrayList<String> params = new ArrayList<String>();\r
+ while ((line = reader.readLine()) != null) {\r
+ line = line.trim();\r
+ if (line.length() == 0)\r
+ continue;\r
+ params.add(line);\r
+ }\r
+ return params;\r
+ }\r
+\r
+ static OutputStream getOutStream(File file) {\r
+ assert file != null && file.exists();\r
+ try {\r
+ return new FileOutputStream(file);\r
+ } catch (FileNotFoundException e) {\r
+ e.printStackTrace();\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Outputs clustal formatted alignment into the file represented by the\r
+ * outStream\r
+ * \r
+ * @param outStream\r
+ * @param align\r
+ * the alignment to output\r
+ */\r
+ static void writeOut(OutputStream outStream, Alignment align) {\r
+ try {\r
+ ClustalAlignmentUtil.writeClustalAlignment(outStream, align);\r
+ } catch (IOException e) {\r
+ System.err\r
+ .println("Problems writing output file! Stack trace is below: ");\r
+ e.printStackTrace();\r
+ } finally {\r
+ if (outStream != null) {\r
+ try {\r
+ outStream.close();\r
+ } catch (IOException ignored) {\r
+ // e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+}\r