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
14 import java.util.Set;
\r
16 import compbio.data.sequence.Alignment;
\r
17 import compbio.data.sequence.ClustalAlignmentUtil;
\r
18 import compbio.data.sequence.Score;
\r
20 public class IOHelper {
\r
23 * Checks -i options and return the File if one was provided, null otherwise
\r
29 * @throws IOException
\r
31 static File getFile(String[] cmd, String key, boolean mustExist)
\r
32 throws IOException {
\r
33 assert key != null && key.trim().length() != 0;
\r
34 for (int i = 0; i < cmd.length; i++) {
\r
35 String filename = cmd[i];
\r
36 filename = filename.trim();
\r
37 if (filename.toLowerCase().startsWith(key + pseparator)) {
\r
38 filename = filename.substring((key + pseparator).length());
\r
39 File file = new File(filename);
\r
40 if (mustExist && !file.exists()) {
\r
41 System.out.println(key + " file " + file.getAbsolutePath()
\r
42 + " does not exist");
\r
45 if (!mustExist && !file.exists()) {
\r
46 file.createNewFile();
\r
48 if (!file.canRead()) {
\r
49 System.out.println("Cannot read " + key + " file "
\r
50 + file.getAbsolutePath());
\r
60 * Load parameters from file
\r
62 * @throws IOException
\r
64 static List<String> loadParameters(File paramsfile) throws IOException {
\r
65 assert paramsfile != null && paramsfile.exists();
\r
66 BufferedReader reader = new BufferedReader(new FileReader(paramsfile));
\r
68 ArrayList<String> params = new ArrayList<String>();
\r
69 while ((line = reader.readLine()) != null) {
\r
71 if (line.length() == 0)
\r
78 static OutputStream getOutStream(File file) {
\r
79 assert file != null && file.exists();
\r
81 return new FileOutputStream(file);
\r
82 } catch (FileNotFoundException e) {
\r
83 e.printStackTrace();
\r
89 * Outputs clustal formatted alignment into the file represented by the
\r
94 * the alignment to output
\r
96 static void writeOut(OutputStream outStream, Alignment align) {
\r
98 ClustalAlignmentUtil.writeClustalAlignment(outStream, align);
\r
99 } catch (IOException e) {
\r
101 .println("Problems writing output file! Stack trace is below: ");
\r
102 e.printStackTrace();
\r
104 if (outStream != null) {
\r
107 } catch (IOException ignored) {
\r
108 // e.printStackTrace();
\r
115 * Outputs AAcon results into the file represented by the outStream
\r
119 * the AACon scores to output
\r
121 static void writeOut(OutputStream outStream, Set<Score> result) {
\r
123 Score.write(result, outStream);
\r
124 } catch (IOException e) {
\r
126 .println("Problems writing output file! Stack trace is below: ");
\r
127 e.printStackTrace();
\r
129 if (outStream != null) {
\r
132 } catch (IOException ignored) {
\r
133 // e.printStackTrace();
\r