Mercurial > repos > rliterman > csp2
comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/fun/Calc.java @ 68:5028fdace37b
planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author | jpayne |
---|---|
date | Tue, 18 Mar 2025 16:23:26 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:0e9998148a16 | 68:5028fdace37b |
---|---|
1 package fun; | |
2 | |
3 import java.io.PrintStream; | |
4 import java.util.Locale; | |
5 | |
6 import shared.Parse; | |
7 import shared.Parser; | |
8 import shared.PreParser; | |
9 import shared.ReadStats; | |
10 import shared.Shared; | |
11 import shared.Timer; | |
12 | |
13 public class Calc { | |
14 | |
15 /*--------------------------------------------------------------*/ | |
16 /*---------------- Initialization ----------------*/ | |
17 /*--------------------------------------------------------------*/ | |
18 | |
19 /** | |
20 * Code entrance from the command line. | |
21 * @param args Command line arguments | |
22 */ | |
23 public static void main(String[] args){ | |
24 Timer t=new Timer(); | |
25 Calc x=new Calc(args); | |
26 x.process(t); | |
27 | |
28 //Close the print stream if it was redirected | |
29 Shared.closeStream(x.outstream); | |
30 } | |
31 | |
32 /** | |
33 * Constructor. | |
34 * @param args Command line arguments | |
35 */ | |
36 public Calc(String[] args){ | |
37 | |
38 {//Preparse block for help, config files, and outstream | |
39 PreParser pp=new PreParser(args, getClass(), false); | |
40 args=pp.args; | |
41 outstream=pp.outstream; | |
42 } | |
43 | |
44 //Create a parser object | |
45 Parser parser=new Parser(); | |
46 | |
47 //Parse each argument | |
48 for(int i=0; i<args.length; i++){ | |
49 String arg=args[i]; | |
50 | |
51 //Break arguments into their constituent parts, in the form of "a=b" | |
52 String[] split=arg.split("="); | |
53 String a=split[0].toLowerCase(); | |
54 String b=split.length>1 ? split[1] : null; | |
55 | |
56 if(a.equals("verbose")){ | |
57 verbose=Parse.parseBoolean(b); | |
58 }else if(a.equals("parse_flag_goes_here")){ | |
59 long fake_variable=Parse.parseKMG(b); | |
60 //Set a variable here | |
61 }else if(a.equals("num") || a.equals("numstats")){ | |
62 numStats=Integer.parseInt(b); | |
63 }else if(parser.parse(arg, a, b)){//Parse standard flags in the parser | |
64 //do nothing | |
65 }else{ | |
66 outstream.println("Unknown parameter "+args[i]); | |
67 assert(false) : "Unknown parameter "+args[i]; | |
68 } | |
69 } | |
70 | |
71 {//Process parser fields | |
72 | |
73 overwrite=ReadStats.overwrite=parser.overwrite; | |
74 append=ReadStats.append=parser.append; | |
75 | |
76 out1=parser.out1; | |
77 } | |
78 } | |
79 | |
80 | |
81 /*--------------------------------------------------------------*/ | |
82 /*---------------- Outer Methods ----------------*/ | |
83 /*--------------------------------------------------------------*/ | |
84 | |
85 /** Create read streams and process all data */ | |
86 void process(Timer t){ | |
87 | |
88 //Process the read stream | |
89 processInner(numStats); | |
90 | |
91 if(verbose){outstream.println("Finished; closing streams.");} | |
92 | |
93 //Report timing and results | |
94 { | |
95 t.stop(); | |
96 outstream.println("Time: \t"+t); | |
97 } | |
98 | |
99 //Throw an exception of there was an error in a thread | |
100 if(errorState){ | |
101 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); | |
102 } | |
103 } | |
104 | |
105 /** Iterate through the reads */ | |
106 void processInner(int numStats){ | |
107 int bits=numStats*5; | |
108 final int iters=1<<bits; | |
109 final int buckets=1+31*numStats; | |
110 int[] counts=new int[buckets]; | |
111 for(int i=0; i<iters; i++){ | |
112 counts[sum(i)]++; | |
113 } | |
114 int[] cumulative=new int[buckets]; | |
115 cumulative[0]=counts[0]; | |
116 for(int i=1; i<buckets; i++){ | |
117 cumulative[i]=cumulative[i-1]+counts[i]; | |
118 } | |
119 //StringBuilder sb=new StringBuilder(); | |
120 final double mult=100.0/iters; | |
121 for(int i=0; i<buckets; i++){ | |
122 String s=(String.format(Locale.ROOT, "%d\t%.4f%%\n", i, cumulative[i]*mult)); | |
123 System.out.print(s); | |
124 } | |
125 } | |
126 | |
127 int sum(int stats){ | |
128 int sum=0; | |
129 while(stats>0){ | |
130 sum+=(stats&0x1F); | |
131 stats>>>=5; | |
132 } | |
133 return sum; | |
134 } | |
135 | |
136 /*--------------------------------------------------------------*/ | |
137 /*---------------- Inner Methods ----------------*/ | |
138 /*--------------------------------------------------------------*/ | |
139 | |
140 /*--------------------------------------------------------------*/ | |
141 /*---------------- Fields ----------------*/ | |
142 /*--------------------------------------------------------------*/ | |
143 | |
144 /** Primary output file path */ | |
145 private String out1="stdout.txt"; | |
146 | |
147 private int numStats=6; | |
148 | |
149 /*--------------------------------------------------------------*/ | |
150 /*---------------- Common Fields ----------------*/ | |
151 /*--------------------------------------------------------------*/ | |
152 | |
153 /** Print status messages to this output stream */ | |
154 private PrintStream outstream=System.err; | |
155 /** Print verbose messages */ | |
156 public static boolean verbose=false; | |
157 /** True if an error was encountered */ | |
158 public boolean errorState=false; | |
159 /** Overwrite existing output files */ | |
160 private boolean overwrite=false; | |
161 /** Append to existing output files */ | |
162 private boolean append=false; | |
163 | |
164 } |