1 package compbio.ws.client;
\r
3 import static compbio.ws.client.Constraints.pseparator;
\r
5 import java.io.BufferedReader;
\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
15 import compbio.data.sequence.Alignment;
\r
16 import compbio.data.sequence.ClustalAlignmentUtil;
\r
18 public class IOHelper {
\r
21 * Checks -i options and return the File if one was provided, null otherwise
\r
27 * @throws IOException
\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
43 if (!mustExist && !file.exists()) {
\r
44 file.createNewFile();
\r
46 if (!file.canRead()) {
\r
47 System.out.println("Cannot read " + key + " file "
\r
48 + file.getAbsolutePath());
\r
58 * Load parameters from file
\r
60 * @throws IOException
\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
66 ArrayList<String> params = new ArrayList<String>();
\r
67 while ((line = reader.readLine()) != null) {
\r
69 if (line.length() == 0)
\r
76 static OutputStream getOutStream(File file) {
\r
77 assert file != null && file.exists();
\r
79 return new FileOutputStream(file);
\r
80 } catch (FileNotFoundException e) {
\r
81 e.printStackTrace();
\r
87 * Outputs clustal formatted alignment into the file represented by the
\r
92 * the alignment to output
\r
94 static void writeOut(OutputStream outStream, Alignment align) {
\r
96 ClustalAlignmentUtil.writeClustalAlignment(outStream, align);
\r
97 } catch (IOException e) {
\r
99 .println("Problems writing output file! Stack trace is below: ");
\r
100 e.printStackTrace();
\r
102 if (outStream != null) {
\r
105 } catch (IOException ignored) {
\r
106 // e.printStackTrace();
\r