Debian Codegraph @

current/template/A_SampleBasic.java

raw symbols SCIP
1 package template;
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.ByteBuilder;
18
19 /**
20 * Reads a text file.
21 * Prints it to another text file.
22 * @author Brian Bushnell
23 * @date April 9, 2020
24 *
25 */
26 public class A_SampleBasic {
27
28 /*--------------------------------------------------------------*/
29 /*---------------- Initialization ----------------*/
30 /*--------------------------------------------------------------*/
31
32 /**
33 * Code entrance from the command line.
34 * @param args Command line arguments
35 */
36 public static void main(String[] args){
37 //Start a timer immediately upon code entrance.
38 Timer t=new Timer();
39
40 //Create an instance of this class
41 A_SampleBasic x=new A_SampleBasic(args);
42
43 //Run the object
44 x.process(t);
45
46 //Close the print stream if it was redirected
47 Shared.closeStream(x.outstream);
48 }
49
50 //hmmsearch --domtblout out.txt --cut_ga mito_pfams.hmm mito4.faa
51
52 /**
53 * Constructor.
54 * @param args Command line arguments
55 */
56 public A_SampleBasic(String[] args){
57
58 {//Preparse block for help, config files, and outstream
59 PreParser pp=new PreParser(args, /*getClass()*/null, false);
60 args=pp.args;
61 outstream=pp.outstream;
62 }
63
64 //Set shared static variables prior to parsing
65
66 {//Parse the arguments
67 final Parser parser=parse(args);
68 parser.out1="stdout.txt";
69 overwrite=parser.overwrite;
70 append=parser.append;
71
72 in=parser.in1;
73
74 out=parser.out1;
75 }
76
77 fixExtensions(); //Add or remove .gz or .bz2 as needed
78 checkFileExistence(); //Ensure files can be read and written
79 checkStatics(); //Adjust file-related static fields as needed for this program
80
81 ffout=FileFormat.testOutput(out, FileFormat.TXT, null, true, overwrite, append, false);
82 ffin=FileFormat.testInput(in, FileFormat.TXT, null, true, true);
83 }
84
85 /*--------------------------------------------------------------*/
86 /*---------------- Initialization Helpers ----------------*/
87 /*--------------------------------------------------------------*/
88
89 /** Parse arguments from the command line */
90 private Parser parse(String[] args){
91
92 Parser parser=new Parser();
93 for(int i=0; i<args.length; i++){
94 String arg=args[i];
95 String[] split=arg.split("=");
96 String a=split[0].toLowerCase();
97 String b=split.length>1 ? split[1] : null;
98 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
99
100 if(a.equals("verbose")){
101 verbose=Parse.parseBoolean(b);
102 ByteFile1.verbose=verbose;
103 ByteFile2.verbose=verbose;
104 ReadWrite.verbose=verbose;
105 }else if(parser.parse(arg, a, b)){
106 //do nothing
107 }else{
108 outstream.println("Unknown parameter "+args[i]);
109 assert(false) : "Unknown parameter "+args[i];
110 // throw new RuntimeException("Unknown parameter "+args[i]);
111 }
112 }
113
114 return parser;
115 }
116
117 /** Add or remove .gz or .bz2 as needed */
118 private void fixExtensions(){
119 in=Tools.fixExtension(in);
120 if(in==null){throw new RuntimeException("Error - at least one input file is required.");}
121 }
122
123 /** Ensure files can be read and written */
124 private void checkFileExistence(){
125 //Ensure output files can be written
126 if(!Tools.testOutputFiles(overwrite, append, false, out)){
127 outstream.println((out==null)+", "+out);
128 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n");
129 }
130
131 //Ensure input files can be read
132 if(!Tools.testInputFiles(false, true, in)){
133 throw new RuntimeException("\nCan't read some input files.\n");
134 }
135
136 //Ensure that no file was specified multiple times
137 if(!Tools.testForDuplicateFiles(true, in, out)){
138 throw new RuntimeException("\nSome file names were specified multiple times.\n");
139 }
140 }
141
142 /** Adjust file-related static fields as needed for this program */
143 private static void checkStatics(){
144 //Adjust the number of threads for input file reading
145 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
146 ByteFile.FORCE_MODE_BF2=true;
147 }
148 }
149
150 /*--------------------------------------------------------------*/
151 /*---------------- Outer Methods ----------------*/
152 /*--------------------------------------------------------------*/
153
154 void process(Timer t){
155
156 ByteFile bf=ByteFile.makeByteFile(ffin);
157 ByteStreamWriter bsw=makeBSW(ffout);
158
159 // assert(false) : "Header goes here.";
160 if(bsw!=null){
161 // assert(false) : "Header goes here.";
162 }
163
164 processInner(bf, bsw);
165
166 errorState|=bf.close();
167 if(bsw!=null){errorState|=bsw.poisonAndWait();}
168
169 t.stop();
170
171 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8));
172
173 outstream.println();
174 outstream.println("Valid Lines: \t"+linesOut);
175 outstream.println("Invalid Lines: \t"+(linesProcessed-linesOut));
176
177 if(errorState){
178 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
179 }
180 }
181
182 /*--------------------------------------------------------------*/
183 /*---------------- Inner Methods ----------------*/
184 /*--------------------------------------------------------------*/
185
186 private void processInner(ByteFile bf, ByteStreamWriter bsw){
187 byte[] line=bf.nextLine();
188 ByteBuilder bb=new ByteBuilder();
189
190 while(line!=null){
191 if(line.length>0){
192 linesProcessed++;
193 bytesProcessed+=(line.length+1);
194
195 if(true){
196 for(int i=0; i<line.length && line[i]!='\t'; i++){
197 bb.append(line[i]);
198 }
199 bb.nl();
200 linesOut++;
201 bytesOut+=bb.length();
202 bsw.print(bb.toBytes());
203 bb.clear();
204 }
205 }
206 line=bf.nextLine();
207 }
208 }
209
210 private static ByteStreamWriter makeBSW(FileFormat ff){
211 if(ff==null){return null;}
212 ByteStreamWriter bsw=new ByteStreamWriter(ff);
213 bsw.start();
214 return bsw;
215 }
216
217 /*--------------------------------------------------------------*/
218 /*---------------- Fields ----------------*/
219 /*--------------------------------------------------------------*/
220
221 private String in=null;
222 private String out=null;
223
224 /*--------------------------------------------------------------*/
225
226 private long linesProcessed=0;
227 private long linesOut=0;
228 private long bytesProcessed=0;
229 private long bytesOut=0;
230
231 /*--------------------------------------------------------------*/
232 /*---------------- Final Fields ----------------*/
233 /*--------------------------------------------------------------*/
234
235 private final FileFormat ffin;
236 private final FileFormat ffout;
237
238 /*--------------------------------------------------------------*/
239 /*---------------- Common Fields ----------------*/
240 /*--------------------------------------------------------------*/
241
242 private PrintStream outstream=System.err;
243 public static boolean verbose=false;
244 public boolean errorState=false;
245 private boolean overwrite=true;
246 private boolean append=false;
247
248 }