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.HashSet;
\r
14 import java.util.List;
\r
15 import java.util.Map;
\r
17 import compbio.data.sequence.Alignment;
\r
18 import compbio.data.sequence.ClustalAlignmentUtil;
\r
19 import compbio.data.sequence.Score;
\r
21 public class IOHelper {
\r
24 * Checks -i options and return the File if one was provided, null otherwise
\r
30 * @throws IOException
\r
32 static File getFile(String[] cmd, String key, boolean mustExist)
\r
33 throws IOException {
\r
34 assert key != null && key.trim().length() != 0;
\r
35 for (int i = 0; i < cmd.length; i++) {
\r
36 String filename = cmd[i];
\r
37 filename = filename.trim();
\r
38 if (filename.toLowerCase().startsWith(key + pseparator)) {
\r
39 filename = filename.substring((key + pseparator).length());
\r
40 File file = new File(filename);
\r
41 if (mustExist && !file.exists()) {
\r
42 System.out.println(key + " file " + file.getAbsolutePath()
\r
43 + " does not exist");
\r
46 if (!mustExist && !file.exists()) {
\r
47 file.createNewFile();
\r
49 if (!file.canRead()) {
\r
50 System.out.println("Cannot read " + key + " file "
\r
51 + file.getAbsolutePath());
\r
61 * Load parameters from file
\r
63 * @throws IOException
\r
65 static List<String> loadParameters(File paramsfile) throws IOException {
\r
66 assert paramsfile != null && paramsfile.exists();
\r
67 BufferedReader reader = new BufferedReader(new FileReader(paramsfile));
\r
69 ArrayList<String> params = new ArrayList<String>();
\r
70 while ((line = reader.readLine()) != null) {
\r
72 if (line.length() == 0)
\r
79 static OutputStream getOutStream(File file) {
\r
80 assert file != null && file.exists();
\r
82 return new FileOutputStream(file);
\r
83 } catch (FileNotFoundException e) {
\r
84 e.printStackTrace();
\r
90 * Outputs clustal formatted alignment into the file represented by the
\r
95 * the alignment to output
\r
97 static void writeOut(OutputStream outStream, Alignment align) {
\r
99 ClustalAlignmentUtil.writeClustalAlignment(outStream, align);
\r
100 } catch (IOException e) {
\r
102 .println("Problems writing output file! Stack trace is below: ");
\r
103 e.printStackTrace();
\r
105 if (outStream != null) {
\r
108 } catch (IOException ignored) {
\r
109 // e.printStackTrace();
\r
116 * Outputs AAcon results into the file represented by the outStream
\r
120 * the AACon scores to output
\r
122 static void writeOut(OutputStream outStream,
\r
123 Map<String, HashSet<Score>> result) {
\r
125 for (Map.Entry<String, HashSet<Score>> entry : result.entrySet()) {
\r
126 System.out.println(">" + entry.getKey());
\r
127 Score.write(entry.getValue(), outStream);
\r
129 } catch (IOException e) {
\r
131 .println("Problems writing output file! Stack trace is below: ");
\r
132 e.printStackTrace();
\r
134 if (outStream != null) {
\r
137 } catch (IOException ignored) {
\r
138 // e.printStackTrace();
\r