| 1 |
package barcode; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.Collections; |
| 5 |
|
| 6 |
import fileIO.ByteFile; |
| 7 |
import fileIO.ByteFile1; |
| 8 |
import fileIO.ByteFile2; |
| 9 |
import fileIO.ByteStreamWriter; |
| 10 |
import fileIO.FileFormat; |
| 11 |
import fileIO.ReadWrite; |
| 12 |
import shared.Parse; |
| 13 |
import shared.Parser; |
| 14 |
import shared.PreParser; |
| 15 |
import shared.Shared; |
| 16 |
import shared.Timer; |
| 17 |
import shared.Tools; |
| 18 |
import structures.ByteBuilder; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class AnalyzeBarcodes { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public static void main(String[] args){ |
| 40 |
|
| 41 |
Timer t=new Timer(); |
| 42 |
|
| 43 |
|
| 44 |
AnalyzeBarcodes x=new AnalyzeBarcodes(args); |
| 45 |
|
| 46 |
|
| 47 |
x.process(t); |
| 48 |
|
| 49 |
|
| 50 |
Shared.closeStream(x.outstream); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public AnalyzeBarcodes(String[] args){ |
| 58 |
|
| 59 |
{ |
| 60 |
PreParser pp=new PreParser(args, null, false); |
| 61 |
args=pp.args; |
| 62 |
outstream=pp.outstream; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 67 |
ReadWrite.setZipThreads(Shared.threads()); |
| 68 |
|
| 69 |
{ |
| 70 |
final Parser parser=parse(args); |
| 71 |
overwrite=parser.overwrite; |
| 72 |
append=parser.append; |
| 73 |
|
| 74 |
in1=parser.in1; |
| 75 |
} |
| 76 |
|
| 77 |
validateParams(); |
| 78 |
fixExtensions(); |
| 79 |
checkFileExistence(); |
| 80 |
checkStatics(); |
| 81 |
|
| 82 |
ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private Parser parse(String[] args){ |
| 91 |
|
| 92 |
|
| 93 |
Parser parser=new Parser(); |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
for(int i=0; i<args.length; i++){ |
| 101 |
String arg=args[i]; |
| 102 |
|
| 103 |
|
| 104 |
String[] split=arg.split("="); |
| 105 |
String a=split[0].toLowerCase(); |
| 106 |
String b=split.length>1 ? split[1] : null; |
| 107 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 108 |
|
| 109 |
if(a.equals("lines")){ |
| 110 |
maxLines=Long.parseLong(b); |
| 111 |
if(maxLines<0){maxLines=Long.MAX_VALUE;} |
| 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("expected") || a.equals("expectedbarcodes")){ |
| 118 |
if(Tools.isNumeric(b)) { |
| 119 |
expectedBarcodeCount=Integer.parseInt(b); |
| 120 |
}else { |
| 121 |
expectedBarcodeFile=b; |
| 122 |
} |
| 123 |
}else if(a.equals("outleft") || a.equals("left") || a.equals("leftstats")){ |
| 124 |
outLeft=b; |
| 125 |
}else if(a.equals("outright") || a.equals("right") || a.equals("rightstats")){ |
| 126 |
outRight=b; |
| 127 |
}else if(a.equals("outpair") || a.equals("pair") || a.equals("pairstats")){ |
| 128 |
outPair=b; |
| 129 |
}else if(a.equals("parse_flag_goes_here")){ |
| 130 |
long fake_variable=Parse.parseKMG(b); |
| 131 |
|
| 132 |
}else if(parser.parse(arg, a, b)){ |
| 133 |
|
| 134 |
}else{ |
| 135 |
outstream.println("Unknown parameter "+args[i]); |
| 136 |
assert(false) : "Unknown parameter "+args[i]; |
| 137 |
throw new RuntimeException("Unknown parameter "+args[i]); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return parser; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
private void fixExtensions(){ |
| 146 |
in1=Tools.fixExtension(in1); |
| 147 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
private void checkFileExistence(){ |
| 152 |
|
| 153 |
if(!Tools.testOutputFiles(overwrite, append, false, outLeft, outRight, outPair)){ |
| 154 |
|
| 155 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to some output files.\n"); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
if(!Tools.testInputFiles(false, true, in1, expectedBarcodeFile)){ |
| 160 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
if(!Tools.testForDuplicateFiles(true, in1, expectedBarcodeFile, outLeft, outRight, outPair)){ |
| 165 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
private static void checkStatics(){ |
| 171 |
|
| 172 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 173 |
ByteFile.FORCE_MODE_BF2=true; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
private boolean validateParams(){ |
| 184 |
|
| 185 |
assert(in1!=null && expectedBarcodeFile!=null) : "Barcode stats and expected barcodes must be specified."; |
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
void process(Timer t){ |
| 195 |
|
| 196 |
bs=BarcodeStats.loadStatic(in1, expectedBarcodeFile, expectedBarcodeCount); |
| 197 |
linesProcessed+=bs.linesProcessed; |
| 198 |
bytesProcessed+=bs.bytesProcessed; |
| 199 |
bs.leftStats=bs.makeLeft(); |
| 200 |
bs.rightStats=bs.makeRight(); |
| 201 |
bs.calcStats(); |
| 202 |
bs.leftStats.calcStats(); |
| 203 |
bs.rightStats.calcStats(); |
| 204 |
|
| 205 |
|
| 206 |
float badPairFraction=bs.badPairFraction; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
if(outLeft!=null){ |
| 211 |
ByteStreamWriter bsw=makeBSW(outLeft); |
| 212 |
printMismatchVsTotal(bs.leftStats, bsw); |
| 213 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 214 |
} |
| 215 |
|
| 216 |
if(outRight!=null){ |
| 217 |
ByteStreamWriter bsw=makeBSW(outRight); |
| 218 |
printMismatchVsTotal(bs.rightStats, bsw); |
| 219 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 220 |
} |
| 221 |
|
| 222 |
if(outPair!=null){ |
| 223 |
ByteStreamWriter bsw=makeBSW(outPair); |
| 224 |
printActualVsExpected(bs, bsw); |
| 225 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 226 |
} |
| 227 |
|
| 228 |
t.stop(); |
| 229 |
|
| 230 |
outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8)); |
| 231 |
|
| 232 |
outstream.println(); |
| 233 |
outstream.println("Lines Out: \t"+linesOut); |
| 234 |
outstream.println("Bytes Out: \t"+bytesOut); |
| 235 |
|
| 236 |
|
| 237 |
if(errorState){ |
| 238 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
void printMismatchVsTotal(BarcodeStats bs, ByteStreamWriter bsw) { |
| 247 |
ByteBuilder bb=new ByteBuilder(); |
| 248 |
bb.append("#Name\tTotal\tBadPair\n"); |
| 249 |
linesOut++; |
| 250 |
|
| 251 |
for(Barcode e : bs.expectedCodeList) { |
| 252 |
Barcode bTotal=bs.codeMap.get(e.name); |
| 253 |
if(bTotal!=null) {e.setCount(bTotal.count());} |
| 254 |
} |
| 255 |
Collections.sort(bs.expectedCodeList); |
| 256 |
for(Barcode e : bs.expectedCodeList) { |
| 257 |
Barcode bTotal=bs.codeMap.get(e.name); |
| 258 |
Barcode bBad=bs.badPairMap.get(e.name); |
| 259 |
bb.append(e.name).tab().append((bTotal==null ? 0 : bTotal.count())).tab().append(bBad==null ? 0 : bBad.count()).nl(); |
| 260 |
bsw.print(bb); |
| 261 |
bytesOut+=bb.length(); |
| 262 |
bb.clear(); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
void printActualVsExpected(BarcodeStats bs, ByteStreamWriter bsw) { |
| 267 |
ByteBuilder bb=new ByteBuilder(); |
| 268 |
bb.append("#Name\tExpectedCount\tActualCount\n"); |
| 269 |
linesOut++; |
| 270 |
String dString=(bs.delimiter<1 ? "" : Character.toString((char)bs.delimiter)); |
| 271 |
final double badRate=bs.badPairFraction; |
| 272 |
final double validLeft=bs.leftStats.expectedCodes; |
| 273 |
final double validRight=bs.rightStats.expectedCodes; |
| 274 |
|
| 275 |
|
| 276 |
final double badMult=bs.validArray[0]/(validLeft*validRight); |
| 277 |
|
| 278 |
for(Barcode ebLeft : bs.leftStats.expectedCodeList) { |
| 279 |
final double leftCount; |
| 280 |
{ |
| 281 |
Barcode bLeft=bs.leftStats.codeMap.get(ebLeft.name); |
| 282 |
leftCount=(bLeft==null ? 0 : bLeft.count()); |
| 283 |
} |
| 284 |
for(Barcode ebRight : bs.rightStats.expectedCodeList) { |
| 285 |
final double rightCount; |
| 286 |
{ |
| 287 |
Barcode bRight=bs.rightStats.codeMap.get(ebRight.name); |
| 288 |
rightCount=(bRight==null ? 0 : bRight.count()); |
| 289 |
} |
| 290 |
String pair=ebLeft.name+dString+ebRight.name; |
| 291 |
if(!bs.expectedCodeMap.containsKey(pair)) { |
| 292 |
Barcode bPair=bs.codeMap.get(pair); |
| 293 |
final double pairCount=(bPair==null ? 0 : bPair.count()); |
| 294 |
final double expectedCount=leftCount*rightCount*badMult; |
| 295 |
|
| 296 |
bb.append(pair).tab().append(expectedCount,2).tab().append(pairCount,0).nl(); |
| 297 |
bsw.print(bb); |
| 298 |
bytesOut+=bb.length(); |
| 299 |
linesOut++; |
| 300 |
bb.clear(); |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
private ByteStreamWriter makeBSW(String fname){ |
| 307 |
FileFormat ff=FileFormat.testOutput(fname, FileFormat.TXT, null, true, overwrite, append, false); |
| 308 |
return makeBSW(ff); |
| 309 |
} |
| 310 |
|
| 311 |
private static ByteStreamWriter makeBSW(FileFormat ff){ |
| 312 |
if(ff==null){return null;} |
| 313 |
ByteStreamWriter bsw=new ByteStreamWriter(ff); |
| 314 |
bsw.start(); |
| 315 |
return bsw; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
private String in1=null; |
| 324 |
|
| 325 |
private String expectedBarcodeFile=null; |
| 326 |
private int expectedBarcodeCount=-1; |
| 327 |
|
| 328 |
|
| 329 |
private String outLeft="leftStats.txt"; |
| 330 |
private String outRight="rightStats.txt"; |
| 331 |
private String outPair="pairStats.txt"; |
| 332 |
|
| 333 |
private BarcodeStats bs; |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
private long linesProcessed=0; |
| 338 |
private long bytesProcessed=0; |
| 339 |
private long linesOut=0; |
| 340 |
private long bytesOut=0; |
| 341 |
|
| 342 |
private long maxLines=Long.MAX_VALUE; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
private final FileFormat ffin1; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
private PrintStream outstream=System.err; |
| 357 |
|
| 358 |
public static boolean verbose=false; |
| 359 |
|
| 360 |
public boolean errorState=false; |
| 361 |
|
| 362 |
private boolean overwrite=true; |
| 363 |
|
| 364 |
private boolean append=false; |
| 365 |
|
| 366 |
} |