Debian Codegraph @

current/driver/FilterLines.java

raw symbols SCIP
1 package driver;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.LinkedHashSet;
6
7 import fileIO.FileFormat;
8 import fileIO.ReadWrite;
9 import fileIO.TextFile;
10 import fileIO.TextStreamWriter;
11 import shared.Parse;
12 import shared.Parser;
13 import shared.PreParser;
14 import shared.Shared;
15 import shared.Timer;
16 import shared.Tools;
17
18 /**
19 * Filters text lines by exact match or substring.
20 * @author Brian Bushnell
21 * @date Jul 6, 2015
22 *
23 */
24 public class FilterLines {
25
26 public static void main(String[] args){
27 Timer t=new Timer();
28 FilterLines x=new FilterLines(args);
29 x.process(t);
30
31 //Close the print stream if it was redirected
32 Shared.closeStream(x.outstream);
33 }
34
35 public FilterLines(String[] args){
36
37 {//Preparse block for help, config files, and outstream
38 PreParser pp=new PreParser(args, getClass(), false);
39 args=pp.args;
40 outstream=pp.outstream;
41 }
42
43 Shared.capBuffers(4);
44 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
45 ReadWrite.setZipThreads(Shared.threads());
46
47 Parser parser=new Parser();
48 for(int i=0; i<args.length; i++){
49 String arg=args[i];
50 String[] split=arg.split("=");
51 String a=split[0].toLowerCase();
52 String b=split.length>1 ? split[1] : null;
53
54 if(parser.parse(arg, a, b)){
55 //do nothing
56 }else if(a.equals("verbose")){
57 verbose=Parse.parseBoolean(b);
58 ReadWrite.verbose=verbose;
59 }else if(a.equals("names")){
60 if(b!=null){
61 String[] x=b.split(",");
62 for(String s : x){
63 names.add(s);
64 }
65 }
66 }else if(a.equals("lines") || a.equals("maxlines")){
67 maxLines=Parse.parseKMG(b);
68 }else if(a.equals("substrings") || a.equals("substring")){
69 if(b==null){b="t";}
70 if(b.equals("header")){
71 lineSubstringOfName=true;
72 }else if(b.equals("name")){
73 nameSubstringOfLine=true;
74 }else{
75 nameSubstringOfLine=lineSubstringOfName=Parse.parseBoolean(b);
76 }
77 }else if(a.equals("prefix") || a.equals("prefixmode")){
78 prefixMode=Parse.parseBoolean(b);
79 }else if(a.equals("replace")){
80 assert(b!=null) : "Bad parameter: "+arg;
81 String[] split2=b.split(",");
82 assert(split2.length==2);
83 replace1=split2[0];
84 replace2=split2[1];
85 }else if(a.equals("casesensitive") || a.equals("case")){
86 ignoreCase=!Parse.parseBoolean(b);
87 }else if(a.equals("include") || a.equals("retain")){
88 exclude=!Parse.parseBoolean(b);
89 }else if(a.equals("exclude") || a.equals("remove")){
90 exclude=Parse.parseBoolean(b);
91 }else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){
92 parser.in1=arg;
93 }else if(parser.out1==null && i==1 && !arg.contains("=")){
94 parser.out1=arg;
95 }else{
96 outstream.println("Unknown parameter "+args[i]);
97 assert(false) : "Unknown parameter "+args[i];
98 // throw new RuntimeException("Unknown parameter "+args[i]);
99 }
100 }
101
102 {
103 String[] x=names.toArray(new String[names.size()]);
104 names.clear();
105 for(String s : x){
106 Tools.addNames(s, names, true);
107 }
108 }
109 if(ignoreCase){
110 String[] x=names.toArray(new String[names.size()]);
111 names.clear();
112 for(String s : x){
113 names.add(s.toLowerCase());
114 }
115 }
116
117 {//Process parser fields
118 overwrite=parser.overwrite;
119 append=parser.append;
120
121 in1=parser.in1;
122
123 out1=parser.out1;
124 }
125
126 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
127
128 if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;}
129
130 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
131 outstream.println(out1);
132 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n");
133 }
134
135 ffout1=FileFormat.testOutput(out1, FileFormat.TEXT, null, true, overwrite, append, false);
136
137 ffin1=FileFormat.testInput(in1, FileFormat.TEXT, null, true, true);
138 }
139
140 void process(Timer t){
141
142
143 final TextFile tf=new TextFile(ffin1);
144
145 final TextStreamWriter tsw;
146 if(out1!=null){
147 tsw=new TextStreamWriter(ffout1);
148 tsw.start();
149 }else{tsw=null;}
150
151 long linesProcessed=0;
152
153 long linesOut=0;
154 long bytesOut=0;
155
156 {
157 for(String line0=tf.readLine(true); line0!=null; line0=tf.readLine(true)){
158 if(maxLines>0 && linesProcessed>=maxLines){break;}
159 linesProcessed++;
160
161 String line=(ignoreCase ? line0.toLowerCase() : line0);
162 if(replace1!=null){line=line.replace(replace1, replace2);}
163 String prefix=null;
164 if(prefixMode){
165 for(int x=1; x<line.length(); x++){
166 char c=line.charAt(x-1);
167 char next=line.charAt(x);
168 if(Character.isWhitespace(c)){
169 prefix=line.substring(0, x).trim();
170 break;
171 }
172 }
173 }
174
175 boolean keepThisLine;
176 boolean match;
177 {
178 match=(names.contains(line) || (prefix!=null && names.contains(prefix)));
179 if(!match && (nameSubstringOfLine || lineSubstringOfName)){
180 for(String name : names){
181 if((lineSubstringOfName && name.contains(line)) || (nameSubstringOfLine && line.contains(name))){match=true;}
182 else if(prefix!=null && ((lineSubstringOfName && name.contains(prefix)) || (nameSubstringOfLine && prefix.contains(name)))){match=true;}
183 }
184 }
185 keepThisLine=(match!=exclude);
186 }
187
188 // assert(false) : names.contains(name)+", "+name+", "+prefix+", "+exclude;
189
190 if(keepThisLine){
191 if(tsw!=null){tsw.println(line0);}
192 linesOut++;
193 bytesOut+=line0.length();
194 }
195 }
196 }
197
198 errorState|=tf.close();
199 if(tsw!=null){errorState|=tsw.poisonAndWait();}
200
201 t.stop();
202
203 double rpnano=linesProcessed/(double)(t.elapsed);
204
205 outstream.println("\nTime: "+t);
206 outstream.println("Lines Processed: "+linesProcessed+" \t"+Tools.format("%.2fk reads/sec", rpnano*1000000));
207 outstream.println("Lines Out: "+linesOut);
208
209 if(errorState){
210 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
211 }
212 }
213
214 /*--------------------------------------------------------------*/
215
216
217 /*--------------------------------------------------------------*/
218
219 private String in1=null;
220
221 private String out1=null;
222
223 /*--------------------------------------------------------------*/
224
225 private boolean exclude=true;
226 private boolean nameSubstringOfLine=false;
227 private boolean lineSubstringOfName=false;
228 private boolean ignoreCase=true;
229 private boolean prefixMode=false;
230 private long maxLines=-1;
231
232 private String replace1=null;
233 private String replace2=null;
234
235 private LinkedHashSet<String> names=new LinkedHashSet<String>();
236
237 /*--------------------------------------------------------------*/
238
239 private final FileFormat ffin1;
240
241 private final FileFormat ffout1;
242
243
244 /*--------------------------------------------------------------*/
245
246 private PrintStream outstream=System.err;
247 public static boolean verbose=false;
248 public boolean errorState=false;
249 private boolean overwrite=true;
250 private boolean append=false;
251 private boolean useSharedHeader=false;
252
253 }