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.FileReader;
\r
8 import java.io.FileWriter;
\r
9 import java.io.IOException;
\r
10 import java.io.Writer;
\r
11 import java.util.ArrayList;
\r
12 import java.util.List;
\r
14 import compbio.data.sequence.Alignment;
\r
15 import compbio.data.sequence.ClustalAlignmentUtil;
\r
16 import compbio.data.sequence.ScoreManager;
\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 Writer getWriter(File file) throws IOException {
\r
77 assert file != null && file.exists();
\r
78 return new FileWriter(file);
\r
82 * Outputs clustal formatted alignment into the file represented by the
\r
87 * the alignment to output
\r
89 static void writeOut(Writer writer, Alignment align) {
\r
91 ClustalAlignmentUtil.writeClustalAlignment(writer, align);
\r
92 } catch (IOException e) {
\r
94 .println("Problems writing output file! Stack trace is below: ");
\r
95 e.printStackTrace();
\r
97 if (writer != null) {
\r
100 } catch (IOException ignored) {
\r
101 // e.printStackTrace();
\r
108 * Outputs AAcon results into the file represented by the outStream
\r
112 * the AACon scores to output
\r
114 static void writeOut(Writer writer, ScoreManager results) {
\r
116 results.writeOut(writer);
\r
117 } catch (IOException e) {
\r
119 .println("Problems writing output file! Stack trace is below: ");
\r
120 e.printStackTrace();
\r
122 if (writer != null) {
\r
125 } catch (IOException ignored) {
\r
126 // e.printStackTrace();
\r