| 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 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class PlotHist { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public static void main(String[] args){ |
| 41 |
|
| 42 |
Timer t=new Timer(); |
| 43 |
|
| 44 |
|
| 45 |
PlotHist x=new PlotHist(args); |
| 46 |
|
| 47 |
|
| 48 |
x.process(t); |
| 49 |
|
| 50 |
|
| 51 |
Shared.closeStream(x.outstream); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public PlotHist(String[] args){ |
| 59 |
|
| 60 |
{ |
| 61 |
PreParser pp=new PreParser(args, null, false); |
| 62 |
args=pp.args; |
| 63 |
outstream=pp.outstream; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 68 |
ReadWrite.setZipThreads(Shared.threads()); |
| 69 |
|
| 70 |
{ |
| 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(); |
| 80 |
checkFileExistence(); |
| 81 |
checkStatics(); |
| 82 |
|
| 83 |
ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private Parser parse(String[] args){ |
| 92 |
|
| 93 |
|
| 94 |
Parser parser=new Parser(); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
for(int i=0; i<args.length; i++){ |
| 102 |
String arg=args[i]; |
| 103 |
|
| 104 |
|
| 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 |
|
| 120 |
}else if(parser.parse(arg, a, b)){ |
| 121 |
|
| 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 |
|
| 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 |
|
| 141 |
private void checkFileExistence(){ |
| 142 |
|
| 143 |
|
| 144 |
if(!Tools.testInputFiles(false, true, in1)){ |
| 145 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
private static void checkStatics(){ |
| 151 |
|
| 152 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 153 |
ByteFile.FORCE_MODE_BF2=true; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
private boolean validateParams(){ |
| 164 |
assert(in1!=null); |
| 165 |
assert(bins>0); |
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 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 |
|
| 191 |
if(errorState){ |
| 192 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 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 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 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 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 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 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
private PrintStream outstream=System.err; |
| 310 |
|
| 311 |
public static boolean verbose=false; |
| 312 |
|
| 313 |
public boolean errorState=false; |
| 314 |
|
| 315 |
private boolean overwrite=true; |
| 316 |
|
| 317 |
private boolean append=false; |
| 318 |
|
| 319 |
} |