1 package compbio.stat.collector;
\r
3 import java.io.BufferedReader;
\r
5 import java.io.FileReader;
\r
6 import java.io.IOException;
\r
8 import org.apache.log4j.Logger;
\r
10 import compbio.util.FileUtil;
\r
11 import compbio.util.Util;
\r
12 import compbio.ws.client.WSTester;
\r
14 public class InputFilter {
\r
16 private static final Logger log = Logger.getLogger(InputFilter.class);
\r
19 * Accepts input as valid unless it is a test input
\r
24 static boolean accept(File input) {
\r
27 assert input.isFile() : "Input file is not a file! " + input;
\r
28 String[] fastainput = WSTester.fastaInput2records.split("\n");
\r
29 assert fastainput.length == 4;
\r
30 String[] aligninput = WSTester.fastaAlignment.split("\n");
\r
31 assert aligninput.length == 4;
\r
32 String[] rnaaligninput = WSTester.clustalRNAAlignment.split("\n");
\r
33 assert aligninput.length == 5;
\r
34 // We do now know the type of the input here so must compare with both
\r
36 boolean isReference = compareLines(input, fastainput);
\r
38 isReference = compareLines(input, aligninput);
\r
41 isReference = compareClustalLines(input, rnaaligninput);
\r
43 // only accept genuine input
\r
44 return !isReference;
\r
47 private static boolean compareLines(File input, String[] reference) {
\r
48 BufferedReader reader = null;
\r
49 boolean status = true;
\r
51 reader = new BufferedReader(new FileReader(input));
\r
52 // only compare first four lines of the file with reference
\r
53 // because the reference length is only 4 lines
\r
54 for (int i = 0; i < reference.length; i++) {
\r
55 String line = reader.readLine();
\r
56 if (Util.isEmpty(line)) {
\r
61 if (!line.equals(reference[i].trim())) {
\r
67 } catch (IOException ioe) {
\r
68 log.warn(ioe, ioe.getCause());
\r
70 FileUtil.closeSilently(reader);
\r
75 private static boolean compareClustalLines(File input, String[] reference) {
\r
76 BufferedReader reader = null;
\r
77 boolean status = true;
\r
79 reader = new BufferedReader(new FileReader(input));
\r
80 // only compare first four lines of the file with reference
\r
81 // because the reference length is only 4 lines
\r
84 while (null != (line = reader.readLine())) {
\r
85 if (!Util.isEmpty(line)) {
\r
87 if (!line.equals(reference[i].trim())) {
\r
95 } catch (IOException ioe) {
\r
96 log.warn(ioe, ioe.getCause());
\r
98 FileUtil.closeSilently(reader);
\r