Debian Codegraph @

current/fun/Foo.java

raw symbols SCIP
1 package fun;
2
3 import java.io.PrintStream;
4
5 import fileIO.ByteFile;
6 import fileIO.ByteFile1;
7 import fileIO.ByteFile2;
8 import fileIO.ByteStreamWriter;
9 import fileIO.FileFormat;
10 import fileIO.ReadWrite;
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 import structures.LongList;
18
19 /**
20 * Reads a text file.
21 * Prints it to another text file.
22 * Filters out invalid lines and prints them to an optional third file.
23 * @author Brian Bushnell
24 * @date May 9, 2016
25 *
26 */
27 public class Foo {
28
29 /*--------------------------------------------------------------*/
30 /*---------------- Initialization ----------------*/
31 /*--------------------------------------------------------------*/
32
33 /**
34 * Code entrance from the command line.
35 * @param args Command line arguments
36 */
37 public static void main(String[] args){
38 //Start a timer immediately upon code entrance.
39 Timer t=new Timer();
40
41 //Create an instance of this class
42 Foo x=new Foo(args);
43
44 //Run the object
45 x.process(t);
46
47 //Close the print stream if it was redirected
48 Shared.closeStream(x.outstream);
49 }
50
51 /**
52 * Constructor.
53 * @param args Command line arguments
54 */
55 public Foo(String[] args){
56
57 {//Preparse block for help, config files, and outstream
58 PreParser pp=new PreParser(args, /*getClass()*/null, false);
59 args=pp.args;
60 outstream=pp.outstream;
61 }
62
63 //Set shared static variables prior to parsing
64 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
65 ReadWrite.setZipThreads(Shared.threads());
66
67 {//Parse the arguments
68 final Parser parser=parse(args);
69 overwrite=parser.overwrite;
70 append=parser.append;
71
72 in1=parser.in1;
73
74 out1=parser.out1;
75 }
76
77 validateParams();
78 fixExtensions(); //Add or remove .gz or .bz2 as needed
79 checkFileExistence(); //Ensure files can be read and written
80 checkStatics(); //Adjust file-related static fields as needed for this program
81
82 ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, false);
83 ffoutInvalid=FileFormat.testOutput(outInvalid, FileFormat.TXT, null, true, overwrite, append, false);
84 ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true);
85 }
86
87 /*--------------------------------------------------------------*/
88 /*---------------- Initialization Helpers ----------------*/
89 /*--------------------------------------------------------------*/
90
91 /** Parse arguments from the command line */
92 private Parser parse(String[] args){
93
94 //Create a parser object
95 Parser parser=new Parser();
96
97 //Set any necessary Parser defaults here
98 //parser.foo=bar;
99 // parser.out1="stdout";
100
101 //Parse each argument
102 for(int i=0; i<args.length; i++){
103 String arg=args[i];
104
105 //Break arguments into their constituent parts, in the form of "a=b"
106 String[] split=arg.split("=");
107 String a=split[0].toLowerCase();
108 String b=split.length>1 ? split[1] : null;
109 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
110
111 if(a.equals("invalid")){
112 outInvalid=b;
113 }else if(a.equals("lines")){
114 maxLines=Long.parseLong(b);
115 if(maxLines<0){maxLines=Long.MAX_VALUE;}
116 }else if(a.equals("verbose")){
117 verbose=Parse.parseBoolean(b);
118 ByteFile1.verbose=verbose;
119 ByteFile2.verbose=verbose;
120 ReadWrite.verbose=verbose;
121 }else if(a.equals("parse_flag_goes_here")){
122 long fake_variable=Parse.parseKMG(b);
123 //Set a variable here
124 }else if(parser.parse(arg, a, b)){
125 //do nothing
126 }else{
127 outstream.println("Unknown parameter "+args[i]);
128 assert(false) : "Unknown parameter "+args[i];
129 throw new RuntimeException("Unknown parameter "+args[i]);
130 }
131 }
132
133 return parser;
134 }
135
136 /** Add or remove .gz or .bz2 as needed */
137 private void fixExtensions(){
138 in1=Tools.fixExtension(in1);
139 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
140 }
141
142 /** Ensure files can be read and written */
143 private void checkFileExistence(){
144 //Ensure output files can be written
145 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
146 outstream.println((out1==null)+", "+out1);
147 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n");
148 }
149
150 //Ensure input files can be read
151 if(!Tools.testInputFiles(false, true, in1)){
152 throw new RuntimeException("\nCan't read some input files.\n");
153 }
154
155 //Ensure that no file was specified multiple times
156 if(!Tools.testForDuplicateFiles(true, in1, out1)){
157 throw new RuntimeException("\nSome file names were specified multiple times.\n");
158 }
159 }
160
161 /** Adjust file-related static fields as needed for this program */
162 private static void checkStatics(){
163 //Adjust the number of threads for input file reading
164 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
165 ByteFile.FORCE_MODE_BF2=true;
166 }
167
168 // if(!ByteFile.FORCE_MODE_BF2){
169 // ByteFile.FORCE_MODE_BF2=false;
170 // ByteFile.FORCE_MODE_BF1=true;
171 // }
172 }
173
174 /** Ensure parameter ranges are within bounds and required parameters are set */
175 private boolean validateParams(){
176 // assert(minfoo>0 && minfoo<=maxfoo) : minfoo+", "+maxfoo;
177 // assert(false) : "TODO";
178 return true;
179 }
180
181 /*--------------------------------------------------------------*/
182 /*---------------- Outer Methods ----------------*/
183 /*--------------------------------------------------------------*/
184
185 /** Create streams and process all data */
186 void process(Timer t){
187
188 ByteFile bf=ByteFile.makeByteFile(ffin1);
189 ByteStreamWriter bsw=makeBSW(ffout1);
190 ByteStreamWriter bswInvalid=makeBSW(ffoutInvalid);
191
192 // assert(false) : "Header goes here.";
193 if(bsw!=null){
194 // assert(false) : "Header goes here.";
195 }
196
197 processInner(bf, bsw, bswInvalid);
198
199 errorState|=bf.close();
200 if(bsw!=null){errorState|=bsw.poisonAndWait();}
201 if(bswInvalid!=null){errorState|=bswInvalid.poisonAndWait();}
202
203 t.stop();
204
205 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8));
206
207 outstream.println();
208 outstream.println("Valid Lines: \t"+linesOut);
209 outstream.println("Invalid Lines: \t"+(linesProcessed-linesOut));
210
211 //Throw an exception of there was an error in a thread
212 if(errorState){
213 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
214 }
215 }
216
217 /*--------------------------------------------------------------*/
218 /*---------------- Inner Methods ----------------*/
219 /*--------------------------------------------------------------*/
220
221 /**Example line:<br>
222 *
223 120395044|1465795004|3191|11574|MEP_itags|1465795004|FAu|1|14297|14297
224 |-rw-r-----|1496172205|1496172205|1048576|1516819694
225 |%2Fdna%2F.snapshots%2F2023-05-17%2Fprojectdirs%2FMEP%2Fitags%2FiTagger3%2F
226 data%2FDIVERSITY%2F1%2F1822.qiime%2Fbdiv_even1000%2Funweighted_unifrac_emperor
227 _pcoa_plot%2Femperor_required_resources%2Fjs%2Fjquery.colorPicker.js
228 */
229 private void processInner(ByteFile bf, ByteStreamWriter bsw, ByteStreamWriter bswInvalid){
230 byte[] line=bf.nextLine();
231
232 byte delimiter=(byte)'|';
233 // SuperLongList sizes=new SuperLongList(10000000);
234 LongList sizes=new LongList(100000000);
235
236 // INODE - Specifies the file's inode number
237 // GENERATION Number - Specifies a number that is incremented whenever an INODE number is reused.
238 // SMNAPSHOT ID - Specifies the snapshot ID
239 // FILE_SIZE - Specifies the current size or length of the file, in bytes.
240
241
242 while(line!=null){
243 linesProcessed++;
244 bytesProcessed+=(line.length+1);
245 if(line.length>0){
246 int a=0, b=0;
247
248 while(b<line.length && line[b]!=delimiter){b++;}
249 assert(b>a) : "Missing term : '"+new String(line)+"'";
250 // long w=Parse.parseLong(line, a, b);
251 b++;
252 a=b;
253
254 while(b<line.length && line[b]!=delimiter){b++;}
255 assert(b>a) : "Missing term : '"+new String(line)+"'";
256 // long y=Parse.parseLong(line, a, b);
257 b++;
258 a=b;
259
260 while(b<line.length && line[b]!=delimiter){b++;}
261 assert(b>a) : "Missing term : '"+new String(line)+"'";
262 // long z=Parse.parseLong(line, a, b);
263 b++;
264 a=b;
265
266 while(b<line.length && line[b]!=delimiter){b++;}
267 assert(b>a) : "Missing term : '"+new String(line)+"'";
268 long size=Parse.parseLong(line, a, b);
269 b++;
270 a=b;
271
272 while(b<line.length && line[b]!=delimiter){b++;}
273 assert(b>a) : "Missing term : '"+new String(line)+"'";
274 // long z=Parse.parseLong(line, a, b);
275 b++;
276 a=b;
277
278 while(b<line.length && line[b]!=delimiter){b++;}
279 assert(b>a) : "Missing term : '"+new String(line)+"'";
280 // long z=Parse.parseLong(line, a, b);
281 b++;
282 a=b;
283
284 while(b<line.length && line[b]!=delimiter){b++;}
285 assert(b>a) : "Missing term : '"+new String(line)+"'";
286 if(line[a]=='F') {
287 linesOut++;
288 sizes.add(size);
289 }
290 b++;
291 a=b;
292 }
293 if(linesProcessed>=maxLines) {break;}
294 line=bf.nextLine();
295 }
296 sizes.sort();
297
298 long total=sizes.sumLong();
299 long mode=sizes.mode();
300 long mean=sizes.sumLong()/sizes.size;
301 long median=sizes.get((int)(sizes.size*0.5));
302 System.out.println("total lines:\t"+Tools.padKMB(linesProcessed, 0)+"\t("+linesProcessed+")");
303 System.out.println("total size: \t"+Tools.padKMB(total, 0)+"\t("+total+")"+"\t"+"("+((total/(1024L*1024L*1024L*1024L)))+" tebibytes)");
304 System.out.println("median size:\t"+median);
305 System.out.println("mean size: \t"+mean);
306 System.out.println("mode size: \t"+mode);
307 System.out.println("P80 size: \t"+sizes.get((int)(sizes.size*0.8)));
308 System.out.println("P90 size: \t"+sizes.get((int)(sizes.size*0.9)));
309 System.out.println("P95 size: \t"+sizes.get((int)(sizes.size*0.95)));
310 System.out.println("P99 size: \t"+sizes.get((int)(sizes.size*0.99)));
311 }
312
313 private static ByteStreamWriter makeBSW(FileFormat ff){
314 if(ff==null){return null;}
315 ByteStreamWriter bsw=new ByteStreamWriter(ff);
316 bsw.start();
317 return bsw;
318 }
319
320 /*--------------------------------------------------------------*/
321 /*---------------- Fields ----------------*/
322 /*--------------------------------------------------------------*/
323
324 /** Primary input file path */
325 private String in1=null;
326
327 /** Primary output file path */
328 private String out1=null;
329
330 /** Junk output file path */
331 private String outInvalid=null;
332
333 /*--------------------------------------------------------------*/
334
335 private long linesProcessed=0;
336 private long linesOut=0;
337 private long bytesProcessed=0;
338 private long bytesOut=0;
339
340 private long maxLines=Long.MAX_VALUE;
341
342 /*--------------------------------------------------------------*/
343 /*---------------- Final Fields ----------------*/
344 /*--------------------------------------------------------------*/
345
346 /** Input File */
347 private final FileFormat ffin1;
348 /** Output File */
349 private final FileFormat ffout1;
350 /** Optional Output File for Junk */
351 private final FileFormat ffoutInvalid;
352
353 /*--------------------------------------------------------------*/
354 /*---------------- Common Fields ----------------*/
355 /*--------------------------------------------------------------*/
356
357 /** Print status messages to this output stream */
358 private PrintStream outstream=System.err;
359 /** Print verbose messages */
360 public static boolean verbose=false;
361 /** True if an error was encountered */
362 public boolean errorState=false;
363 /** Overwrite existing output files */
364 private boolean overwrite=true;
365 /** Append to existing output files */
366 private boolean append=false;
367
368 }