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
17 import compbio.data.sequence.ScoreManager;
\r
19 public class IOHelper {
\r
22 * Checks -i options and return the File if one was provided, null otherwise
\r
28 * @throws IOException
\r
30 static File getFile(String[] cmd, String key, boolean mustExist)
\r
31 throws IOException {
\r
32 assert key != null && key.trim().length() != 0;
\r
33 for (int i = 0; i < cmd.length; i++) {
\r
34 String filename = cmd[i];
\r
35 filename = filename.trim();
\r
36 if (filename.toLowerCase().startsWith(key + pseparator)) {
\r
37 filename = filename.substring((key + pseparator).length());
\r
38 File file = new File(filename);
\r
39 if (mustExist && !file.exists()) {
\r
40 System.out.println(key + " file " + file.getAbsolutePath()
\r
41 + " does not exist");
\r
44 if (!mustExist && !file.exists()) {
\r
45 file.createNewFile();
\r
47 if (!file.canRead()) {
\r
48 System.out.println("Cannot read " + key + " file "
\r
49 + file.getAbsolutePath());
\r
59 * Load parameters from file
\r
61 * @throws IOException
\r
63 static List<String> loadParameters(File paramsfile) throws IOException {
\r
64 assert paramsfile != null && paramsfile.exists();
\r
65 BufferedReader reader = new BufferedReader(new FileReader(paramsfile));
\r
67 ArrayList<String> params = new ArrayList<String>();
\r
68 while ((line = reader.readLine()) != null) {
\r
70 if (line.length() == 0)
\r
77 static OutputStream getOutStream(File file) {
\r
78 assert file != null && file.exists();
\r
80 return new FileOutputStream(file);
\r
81 } catch (FileNotFoundException e) {
\r
82 e.printStackTrace();
\r
88 * Outputs clustal formatted alignment into the file represented by the
\r
93 * the alignment to output
\r
95 static void writeOut(OutputStream outStream, Alignment align) {
\r
97 ClustalAlignmentUtil.writeClustalAlignment(outStream, align);
\r
98 } catch (IOException e) {
\r
100 .println("Problems writing output file! Stack trace is below: ");
\r
101 e.printStackTrace();
\r
103 if (outStream != null) {
\r
106 } catch (IOException ignored) {
\r
107 // e.printStackTrace();
\r
114 * Outputs AAcon results into the file represented by the outStream
\r
118 * the AACon scores to output
\r
120 static void writeOut(OutputStream outStream, ScoreManager results) {
\r
122 results.writeOut(outStream);
\r
123 } catch (IOException e) {
\r
125 .println("Problems writing output file! Stack trace is below: ");
\r
126 e.printStackTrace();
\r
128 if (outStream != null) {
\r
131 } catch (IOException ignored) {
\r
132 // e.printStackTrace();
\r