Debian Codegraph @

current/hiseq/PlotHist.java

raw symbols SCIP
1 package hiseq;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.Arrays;
6
7 import fileIO.ByteFile;
8 import fileIO.ByteFile1;
9 import fileIO.ByteFile2;
10 import fileIO.ByteStreamWriter;
11 import fileIO.FileFormat;
12 import fileIO.ReadWrite;
13 import shared.LineParser1;
14 import shared.Parse;
15 import shared.Parser;
16 import shared.PreParser;
17 import shared.Shared;
18 import shared.Timer;
19 import shared.Tools;
20 import structures.ByteBuilder;
21
22 /**
23 * Generates histograms from a square matrix.
24 * Intended for plotting a TileDump.
25 * If rewritten to use FloatList, it could ignore outlier percentiles...
26 * @author Brian Bushnell
27 * @date Nov 5, 2024
28 *
29 */
30 public class PlotHist {
31
32 /*--------------------------------------------------------------*/
33 /*---------------- Initialization ----------------*/
34 /*--------------------------------------------------------------*/
35
36 /**
37 * Code entrance from the command line.
38 * @param args Command line arguments
39 */
40 public static void main(String[] args){
41 //Start a timer immediately upon code entrance.
42 Timer t=new Timer();
43
44 //Create an instance of this class
45 PlotHist x=new PlotHist(args);
46
47 //Run the object
48 x.process(t);
49
50 //Close the print stream if it was redirected
51 Shared.closeStream(x.outstream);
52 }
53
54 /**
55 * Constructor.
56 * @param args Command line arguments
57 */
58 public PlotHist(String[] args){
59
60 {//Preparse block for help, config files, and outstream
61 PreParser pp=new PreParser(args, /*getClass()*/null, false);
62 args=pp.args;
63 outstream=pp.outstream;
64 }
65
66 //Set shared static variables prior to parsing
67 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
68 ReadWrite.setZipThreads(Shared.threads());
69
70 {//Parse the arguments
71 final Parser parser=parse(args);
72 overwrite=parser.overwrite;
73 append=parser.append;
74
75 in1=parser.in1;
76 }
77
78 validateParams();
79 fixExtensions(); //Add or remove .gz or .bz2 as needed
80 checkFileExistence(); //Ensure files can be read and written
81 checkStatics(); //Adjust file-related static fields as needed for this program
82
83 ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true);
84 }
85
86 /*--------------------------------------------------------------*/
87 /*---------------- Initialization Helpers ----------------*/
88 /*--------------------------------------------------------------*/
89
90 /** Parse arguments from the command line */
91 private Parser parse(String[] args){
92
93 //Create a parser object
94 Parser parser=new Parser();
95
96 //Set any necessary Parser defaults here
97 //parser.foo=bar;
98 // parser.out1="stdout";
99
100 //Parse each argument
101 for(int i=0; i<args.length; i++){
102 String arg=args[i];
103
104 //Break arguments into their constituent parts, in the form of "a=b"
105 String[] split=arg.split("=");
106 String a=split[0].toLowerCase();
107 String b=split.length>1 ? split[1] : null;
108 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
109
110 if(a.equals("bins")){
111 bins=Integer.parseInt(b);
112 }else if(a.equals("verbose")){
113 verbose=Parse.parseBoolean(b);
114 ByteFile1.verbose=verbose;
115 ByteFile2.verbose=verbose;
116 ReadWrite.verbose=verbose;
117 }else if(a.equals("parse_flag_goes_here")){
118 long fake_variable=Parse.parseKMG(b);
119 //Set a variable here
120 }else if(parser.parse(arg, a, b)){
121 //do nothing
122 }else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){
123 parser.in1=arg;
124 }else{
125 outstream.println("Unknown parameter "+args[i]);
126 assert(false) : "Unknown parameter "+args[i];
127 throw new RuntimeException("Unknown parameter "+args[i]);
128 }
129 }
130
131 return parser;
132 }
133
134 /** Add or remove .gz or .bz2 as needed */
135 private void fixExtensions(){
136 in1=Tools.fixExtension(in1);
137 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
138 }
139
140 /** Ensure files can be read and written */
141 private void checkFileExistence(){
142
143 //Ensure input files can be read
144 if(!Tools.testInputFiles(false, true, in1)){
145 throw new RuntimeException("\nCan't read some input files.\n");
146 }
147 }
148
149 /** Adjust file-related static fields as needed for this program */
150 private static void checkStatics(){
151 //Adjust the number of threads for input file reading
152 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
153 ByteFile.FORCE_MODE_BF2=true;
154 }
155
156 // if(!ByteFile.FORCE_MODE_BF2){
157 // ByteFile.FORCE_MODE_BF2=false;
158 // ByteFile.FORCE_MODE_BF1=true;
159 // }
160 }
161
162 /** Ensure parameter ranges are within bounds and required parameters are set */
163 private boolean validateParams(){
164 assert(in1!=null);
165 assert(bins>0);
166 return true;
167 }
168
169 /*--------------------------------------------------------------*/
170 /*---------------- Outer Methods ----------------*/
171 /*--------------------------------------------------------------*/
172
173 /** Create streams and process all data */
174 void process(Timer t){
175
176 ByteFile bf=ByteFile.makeByteFile(ffin1);
177
178 processInner(bf);
179 writeData();
180
181 errorState|=bf.close();
182
183 t.stop();
184
185 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8));
186
187 outstream.println();
188 outstream.println("Lines Out: \t"+linesOut);
189
190 //Throw an exception of there was an error in a thread
191 if(errorState){
192 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
193 }
194 }
195
196 /*--------------------------------------------------------------*/
197 /*---------------- Inner Methods ----------------*/
198 /*--------------------------------------------------------------*/
199
200 private void gatherData(ByteFile bf) {
201 byte[] line=bf.nextLine();
202
203 LineParser1 lp=new LineParser1('\t');
204 while(line!=null){
205 if(line.length>0){
206 final boolean valid=(line[0]!='#');
207 if(valid) {
208 lp.set(line);
209 int lineTerms=Tools.min(terms, lp.terms());
210 for(int term=0; term<lineTerms; term++) {
211 double d=lp.parseDouble(term);
212 maxArray[term]=Tools.max(maxArray[term], d);
213 }
214 }else {
215 header=new String(line).substring(1).split("\t");
216 terms=header.length;
217 maxArray=new double[terms];
218 }
219 }
220 line=bf.nextLine();
221 }
222 bf.reset();
223 countMatrix=new long[terms][bins+1];
224 }
225
226 private void processInner(ByteFile bf){
227 gatherData(bf);
228
229 byte[] line=bf.nextLine();
230
231 LineParser1 lp=new LineParser1('\t');
232 while(line!=null){
233 if(line.length>0){
234 linesProcessed++;
235 bytesProcessed+=(line.length+1);
236
237 final boolean valid=(line[0]!='#');
238
239 if(valid){
240 lp.set(line);
241 int lineTerms=Tools.min(terms, lp.terms());
242 for(int term=0; term<lineTerms; term++) {
243 double d=lp.parseDouble(term);
244 int bin=Tools.max(0, (int)((d/maxArray[term])*bins));
245 assert(bin<=bins && term<countMatrix.length) :
246 "\n"+new String(line)+"\nterm="+term+", bin="+bin+"/"+bins+", d="+d+
247 ", max="+maxArray[term]+"\n"+Arrays.toString(maxArray);
248 countMatrix[term][bin]++;
249 }
250 }
251 }
252 line=bf.nextLine();
253 }
254 }
255
256 private void writeData() {
257 ByteBuilder bb=new ByteBuilder();
258 for(int term=0; term<terms; term++) {
259 String name=header[term];
260 long[] counts=countMatrix[term];
261 ByteStreamWriter bsw=ByteStreamWriter.makeBSW(name+".tsv", overwrite, false, false);
262 bb.clear().append(name+"\tcount\n");
263 linesOut++;
264 double incr=maxArray[term]/bins;
265 double binStart=0;
266 for(int bin=0; bin<counts.length; bin++) {
267 bb.append(binStart, 6, true).tab().append(counts[bin]).nl();
268 linesOut++;
269 binStart+=incr;
270 }
271 bytesOut+=bb.length();
272 bsw.print(bb);
273 bsw.poison();
274 }
275 }
276
277 /*--------------------------------------------------------------*/
278 /*---------------- Fields ----------------*/
279 /*--------------------------------------------------------------*/
280
281 /** Primary input file path */
282 private String in1=null;
283
284 /*--------------------------------------------------------------*/
285
286 private long linesProcessed=0;
287 private long linesOut=0;
288 private long bytesProcessed=0;
289 private long bytesOut=0;
290
291 /*--------------------------------------------------------------*/
292 /*---------------- Final Fields ----------------*/
293 /*--------------------------------------------------------------*/
294
295 /** Input File */
296 private final FileFormat ffin1;
297
298 String[] header;
299 long[][] countMatrix;
300 double[] maxArray;
301 int bins=1000;
302 int terms=0;
303
304 /*--------------------------------------------------------------*/
305 /*---------------- Common Fields ----------------*/
306 /*--------------------------------------------------------------*/
307
308 /** Print status messages to this output stream */
309 private PrintStream outstream=System.err;
310 /** Print verbose messages */
311 public static boolean verbose=false;
312 /** True if an error was encountered */
313 public boolean errorState=false;
314 /** Overwrite existing output files */
315 private boolean overwrite=true;
316 /** Append to existing output files */
317 private boolean append=false;
318
319 }