1 /* Copyright (c) 2011 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.ws.client;
\r
20 import static compbio.ws.client.Constraints.pseparator;
\r
22 import java.io.BufferedReader;
\r
23 import java.io.File;
\r
24 import java.io.FileReader;
\r
25 import java.io.FileWriter;
\r
26 import java.io.IOException;
\r
27 import java.io.Writer;
\r
28 import java.util.ArrayList;
\r
29 import java.util.List;
\r
31 import compbio.data.sequence.Alignment;
\r
32 import compbio.data.sequence.ClustalAlignmentUtil;
\r
33 import compbio.data.sequence.ScoreManager;
\r
35 public class IOHelper {
\r
38 * Checks -i options and return the File if one was provided, null otherwise
\r
44 * @throws IOException
\r
46 static File getFile(String[] cmd, String key, boolean mustExist)
\r
47 throws IOException {
\r
48 assert key != null && key.trim().length() != 0;
\r
49 for (String c : cmd) {
\r
50 String filename = c;
\r
51 filename = filename.trim();
\r
52 if (filename.toLowerCase().startsWith(key + pseparator)) {
\r
53 filename = filename.substring((key + pseparator).length());
\r
54 File file = new File(filename);
\r
55 if (mustExist && !file.exists()) {
\r
56 System.out.println(key + " file " + file.getAbsolutePath()
\r
57 + " does not exist");
\r
60 if (!mustExist && !file.exists()) {
\r
61 file.createNewFile();
\r
63 if (!file.canRead()) {
\r
64 System.out.println("Cannot read " + key + " file "
\r
65 + file.getAbsolutePath());
\r
75 * Load parameters from file
\r
77 * @throws IOException
\r
79 static List<String> loadParameters(File paramsfile) throws IOException {
\r
80 assert paramsfile != null && paramsfile.exists();
\r
81 BufferedReader reader = new BufferedReader(new FileReader(paramsfile));
\r
83 ArrayList<String> params = new ArrayList<String>();
\r
84 while ((line = reader.readLine()) != null) {
\r
86 if (line.length() == 0)
\r
93 static Writer getWriter(File file) throws IOException {
\r
94 assert file != null && file.exists();
\r
95 return new FileWriter(file);
\r
99 * Outputs clustal formatted alignment into the file represented by the
\r
104 * the alignment to output
\r
106 static void writeOut(Writer writer, Alignment align) {
\r
108 ClustalAlignmentUtil.writeClustalAlignment(writer, align);
\r
109 } catch (IOException e) {
\r
111 .println("Problems writing output file! Stack trace is below: ");
\r
112 e.printStackTrace();
\r
114 if (writer != null) {
\r
117 } catch (IOException ignored) {
\r
118 // e.printStackTrace();
\r
125 * Outputs AAcon results into the file represented by the outStream
\r
129 * the AACon scores to output
\r
131 static void writeOut(Writer writer, ScoreManager results) {
\r
132 if (results == null) {
\r
136 results.writeOut(writer);
\r
137 } catch (IOException e) {
\r
139 .println("Problems writing output file! Stack trace is below: ");
\r
140 e.printStackTrace();
\r
142 if (writer != null) {
\r
145 } catch (IOException ignored) {
\r
146 // e.printStackTrace();
\r