Bug Fixg: testing RNAalifold jobs have been considered as real ones
[jabaws.git] / webservices / compbio / stat / collector / InputFilter.java
1 package compbio.stat.collector;\r
2 \r
3 import java.io.BufferedReader;\r
4 import java.io.File;\r
5 import java.io.FileReader;\r
6 import java.io.IOException;\r
7 \r
8 import org.apache.log4j.Logger;\r
9 \r
10 import compbio.util.FileUtil;\r
11 import compbio.util.Util;\r
12 import compbio.ws.client.WSTester;\r
13 \r
14 public class InputFilter {\r
15 \r
16         private static final Logger log = Logger.getLogger(InputFilter.class);\r
17 \r
18         /**\r
19          * Accepts input as valid unless it is a test input\r
20          * \r
21          * @param input\r
22          * @return\r
23          */\r
24         static boolean accept(File input) {\r
25                 if (input == null)\r
26                         return true;\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
35                 // references\r
36                 boolean isReference = compareLines(input, fastainput);\r
37                 if (!isReference) {\r
38                         isReference = compareLines(input, aligninput);\r
39                 }\r
40                 if (!isReference) {\r
41                         isReference = compareClustalLines(input, rnaaligninput);\r
42                 }\r
43                 // only accept genuine input\r
44                 return !isReference;\r
45         }\r
46 \r
47         private static boolean compareLines(File input, String[] reference) {\r
48                 BufferedReader reader = null;\r
49                 boolean status = true;\r
50                 try {\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
57                                         status = false;\r
58                                         break;\r
59                                 }\r
60                                 line = line.trim();\r
61                                 if (!line.equals(reference[i].trim())) {\r
62                                         status = false;\r
63                                         break;\r
64                                 }\r
65                         }\r
66                         reader.close();\r
67                 } catch (IOException ioe) {\r
68                         log.warn(ioe, ioe.getCause());\r
69                 } finally {\r
70                         FileUtil.closeSilently(reader);\r
71                 }\r
72                 return status;\r
73         }\r
74 \r
75         private static boolean compareClustalLines(File input, String[] reference) {\r
76                 BufferedReader reader = null;\r
77                 boolean status = true;\r
78                 try {\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
82                         int i = 0;\r
83                         String line;\r
84                         while (null != (line = reader.readLine())) {\r
85                                 if (!Util.isEmpty(line)) {\r
86                                         line = line.trim();\r
87                                         if (!line.equals(reference[i].trim())) {\r
88                                                 status = false;\r
89                                                 break;\r
90                                         }\r
91                                         ++i;\r
92                                 }\r
93                         }\r
94                         reader.close();\r
95                 } catch (IOException ioe) {\r
96                         log.warn(ioe, ioe.getCause());\r
97                 } finally {\r
98                         FileUtil.closeSilently(reader);\r
99                 }\r
100                 return status;\r
101         }\r
102 }\r