| 1 |
package barcode; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Collections; |
| 5 |
import java.util.HashSet; |
| 6 |
import java.util.LinkedHashMap; |
| 7 |
import java.util.Map.Entry; |
| 8 |
|
| 9 |
import fileIO.ByteStreamWriter; |
| 10 |
import fileIO.FileFormat; |
| 11 |
import fileIO.ReadWrite; |
| 12 |
import fileIO.TextFile; |
| 13 |
import shared.LineParserS1; |
| 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 stream.ConcurrentReadInputStream; |
| 21 |
import stream.Read; |
| 22 |
import structures.ByteBuilder; |
| 23 |
import structures.ListNum; |
| 24 |
import structures.LongList; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class CompareLabels { |
| 32 |
|
| 33 |
public static void main(String[] args){ |
| 34 |
|
| 35 |
Timer t=new Timer(); |
| 36 |
|
| 37 |
|
| 38 |
CompareLabels x=new CompareLabels(args); |
| 39 |
|
| 40 |
|
| 41 |
x.process(t); |
| 42 |
|
| 43 |
|
| 44 |
Shared.closeStream(x.outstream); |
| 45 |
} |
| 46 |
|
| 47 |
public CompareLabels(String[] args){ |
| 48 |
|
| 49 |
{ |
| 50 |
PreParser pp=new PreParser(args, getClass(), false); |
| 51 |
args=pp.args; |
| 52 |
outstream=pp.outstream; |
| 53 |
} |
| 54 |
|
| 55 |
Parser parser=new Parser(); |
| 56 |
parser.out1=out1; |
| 57 |
for(int i=0; i<args.length; i++){ |
| 58 |
String arg=args[i]; |
| 59 |
String[] split=arg.split("="); |
| 60 |
String a=split[0].toLowerCase(); |
| 61 |
String b=split.length>1 ? split[1] : null; |
| 62 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 63 |
|
| 64 |
if(a.equals("parse_flag_goes_here")){ |
| 65 |
|
| 66 |
}else if(a.equals("delimiter")){ |
| 67 |
delimiter=Parse.parseSymbolToCharacter(b); |
| 68 |
}else if(a.equals("swap")){ |
| 69 |
swap=Parse.parseBoolean(b); |
| 70 |
}else if(a.equals("labelstats")){ |
| 71 |
labelStats=b; |
| 72 |
}else if(a.equals("quantset")){ |
| 73 |
quantSetFile=b; |
| 74 |
}else if(parser.parse(arg, a, b)){ |
| 75 |
|
| 76 |
}else{ |
| 77 |
|
| 78 |
assert(false) : "Unknown parameter "+args[i]; |
| 79 |
outstream.println("Unknown parameter "+args[i]); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
{ |
| 84 |
Parser.processQuality(); |
| 85 |
|
| 86 |
maxReads=parser.maxReads; |
| 87 |
in1=parser.in1; |
| 88 |
out1=parser.out1; |
| 89 |
} |
| 90 |
|
| 91 |
lp=new LineParserS1(delimiter); |
| 92 |
ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, true, false, false); |
| 93 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true); |
| 94 |
|
| 95 |
map=(labelStats==null ? null : new LinkedHashMap<String, Label>()); |
| 96 |
if(quantSetFile!=null) { |
| 97 |
quantSet=new HashSet<String>(); |
| 98 |
quantSet.add("UNKNOWN"); |
| 99 |
for(String s : TextFile.toStringLines(quantSetFile)) {quantSet.add(s);} |
| 100 |
}else { |
| 101 |
quantSet=null; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void process(Timer t){ |
| 106 |
|
| 107 |
final ConcurrentReadInputStream cris; |
| 108 |
{ |
| 109 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null); |
| 110 |
cris.start(); |
| 111 |
} |
| 112 |
boolean paired=cris.paired(); |
| 113 |
|
| 114 |
processInner(cris); |
| 115 |
|
| 116 |
ReadWrite.closeStream(cris); |
| 117 |
if(verbose){outstream.println("Finished.");} |
| 118 |
|
| 119 |
if(ffout1!=null) { |
| 120 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(ffout1); |
| 121 |
summarize(bsw); |
| 122 |
bsw.poisonAndWait(); |
| 123 |
} |
| 124 |
if(labelStats!=null) { |
| 125 |
FileFormat ff=FileFormat.testOutput(labelStats, FileFormat.TXT, null, true, true, false, false); |
| 126 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(ff); |
| 127 |
printMap(bsw); |
| 128 |
bsw.poisonAndWait(); |
| 129 |
} |
| 130 |
|
| 131 |
t.stop(); |
| 132 |
outstream.println("Time: \t"+t); |
| 133 |
outstream.println("Reads Processed: "+readsProcessed+" \t"+Tools.format("%.2fk reads/sec", (readsProcessed/(double)(t.elapsed))*1000000)); |
| 134 |
} |
| 135 |
|
| 136 |
private void processInner(ConcurrentReadInputStream cris) { |
| 137 |
ListNum<Read> ln=cris.nextList(); |
| 138 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 139 |
|
| 140 |
if(reads!=null && !reads.isEmpty()){ |
| 141 |
Read r=reads.get(0); |
| 142 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 143 |
} |
| 144 |
|
| 145 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 146 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 147 |
|
| 148 |
for(int idx=0; idx<reads.size(); idx++){ |
| 149 |
final Read r1=reads.get(idx); |
| 150 |
readsProcessed+=r1.pairCount(); |
| 151 |
basesProcessed+=r1.pairLength(); |
| 152 |
labelsProcessed++; |
| 153 |
|
| 154 |
processRead(r1); |
| 155 |
} |
| 156 |
|
| 157 |
cris.returnList(ln); |
| 158 |
if(verbose){outstream.println("Returned a list.");} |
| 159 |
ln=cris.nextList(); |
| 160 |
reads=(ln!=null ? ln.list : null); |
| 161 |
} |
| 162 |
if(ln!=null){ |
| 163 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
private void summarize(ByteStreamWriter bsw) { |
| 168 |
if(bsw==null) {return;} |
| 169 |
ByteBuilder bb=new ByteBuilder(); |
| 170 |
long valid=labelsProcessed-invalidCount; |
| 171 |
float fracMult=1f/(Tools.max(1, labelsProcessed)); |
| 172 |
float ppmMult=1000000f/labelsProcessed; |
| 173 |
|
| 174 |
|
| 175 |
long count1=aaCount+auCount+abCount; |
| 176 |
long count2=aaCount+uaCount+abCount; |
| 177 |
float mult1=1f/Tools.max(count1, 1); |
| 178 |
float mult2=1f/Tools.max(count2, 1); |
| 179 |
float ryield1=aaCount*mult2; |
| 180 |
float ryield2=aaCount*mult1; |
| 181 |
float ayield1=count1*fracMult; |
| 182 |
float ayield2=count2*fracMult; |
| 183 |
float contam1=abCount*mult1; |
| 184 |
float contam2=abCount*mult2; |
| 185 |
|
| 186 |
bb.append("#Labels").tab().append(labelsProcessed).nl(); |
| 187 |
bb.append("#Valid").tab().append(valid).tab().append(fracMult*valid, 6).nl(); |
| 188 |
bb.append("#TermsPerRead").tab().append(termCounts.meanHist(), 6).nl(); |
| 189 |
bb.append("#RelYield1").tab().append(ryield1, 5).nl(); |
| 190 |
bb.append("#RelYield2").tab().append(ryield2, 5).nl(); |
| 191 |
bb.append("#AbsYield1").tab().append(ayield1, 5).nl(); |
| 192 |
bb.append("#AbsYield2").tab().append(ayield2, 5).nl(); |
| 193 |
bb.append("#Contam1_PPM").tab().append(contam1, 2).nl(); |
| 194 |
bb.append("#Contam2_PPM").tab().append(contam2, 2).nl(); |
| 195 |
bb.append("#Metric").tab().append("Count").tab().append("Rate").tab().append("PPM").nl(); |
| 196 |
bb.append("AACount").tab().append(aaCount).tab().append(fracMult*aaCount, 5).tab().append(ppmMult*aaCount, 2).nl(); |
| 197 |
bb.append("UUCount").tab().append(uuCount).tab().append(fracMult*uuCount, 5).tab().append(ppmMult*uuCount, 2).nl(); |
| 198 |
bb.append("AUCount").tab().append(auCount).tab().append(fracMult*auCount, 5).tab().append(ppmMult*auCount, 2).nl(); |
| 199 |
bb.append("UACount").tab().append(uaCount).tab().append(fracMult*uaCount, 5).tab().append(ppmMult*uaCount, 2).nl(); |
| 200 |
bb.append("ABCount").tab().append(abCount).tab().append(fracMult*abCount, 5).tab().append(ppmMult*abCount, 2).nl(); |
| 201 |
bsw.println(bb); |
| 202 |
} |
| 203 |
|
| 204 |
private void printMap(ByteStreamWriter bsw) { |
| 205 |
if(bsw==null) {return;} |
| 206 |
ByteBuilder bb=new ByteBuilder(); |
| 207 |
long valid=labelsProcessed-invalidCount; |
| 208 |
float fracMult=1f/labelsProcessed; |
| 209 |
float ppmMult=1000000f/labelsProcessed; |
| 210 |
bb.append("#Labels").tab().append(labelsProcessed).nl(); |
| 211 |
bb.append("#Valid").tab().append(valid).tab().append(fracMult*valid, 5).nl(); |
| 212 |
bb.append("#TermsPerRead").tab().append(termCounts.meanHist(), 5).nl(); |
| 213 |
bb.append(Label.header()); |
| 214 |
bsw.println(bb); |
| 215 |
|
| 216 |
ArrayList<Label> list=new ArrayList<Label>(); |
| 217 |
for(Entry<String, Label> e : map.entrySet()) { |
| 218 |
Label lab=e.getValue(); |
| 219 |
list.add(lab); |
| 220 |
} |
| 221 |
Collections.sort(list); |
| 222 |
for(Label lab : list) { |
| 223 |
bb.clear(); |
| 224 |
lab.appendTo(bb); |
| 225 |
bsw.println(bb); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
private void processRead(Read r) { |
| 232 |
lp.set(r.id); |
| 233 |
final int terms=lp.terms(); |
| 234 |
termCounts.increment(terms); |
| 235 |
if(terms<2) { |
| 236 |
invalidCount++; |
| 237 |
return; |
| 238 |
} |
| 239 |
final String s1=lp.parseString(terms-(swap ? 1 : 2)); |
| 240 |
final String s2=lp.parseString(terms-(swap ? 2 : 1)); |
| 241 |
final boolean unknown1=s1.equalsIgnoreCase("UNKNOWN"); |
| 242 |
final boolean unknown2=s2.equalsIgnoreCase("UNKNOWN"); |
| 243 |
final boolean equal=s1.equals(s2); |
| 244 |
if(quantSet!=null && !(quantSet.contains(s1) && quantSet.contains(s2))) { |
| 245 |
invalidCount++; |
| 246 |
return; |
| 247 |
} |
| 248 |
if(unknown1) { |
| 249 |
if(unknown2) {uuCount++;} |
| 250 |
else {uaCount++;} |
| 251 |
}else if(unknown2) { |
| 252 |
auCount++; |
| 253 |
}else if(s1.equals(s2)) { |
| 254 |
aaCount++; |
| 255 |
}else { |
| 256 |
abCount++; |
| 257 |
} |
| 258 |
|
| 259 |
if(map!=null) { |
| 260 |
final Label lab1=getLabel(s1); |
| 261 |
final Label lab2=(equal ? lab1 : getLabel(s2)); |
| 262 |
if(equal) {lab1.aa++;} |
| 263 |
else if(unknown1) { |
| 264 |
lab1.ua++; |
| 265 |
lab2.ua++; |
| 266 |
}else if(unknown2) { |
| 267 |
lab1.au++; |
| 268 |
lab2.au++; |
| 269 |
}else { |
| 270 |
lab1.ab++; |
| 271 |
lab2.ba++; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
private Label getLabel(String s) { |
| 277 |
if(s==null) {return null;} |
| 278 |
Label l=map.get(s); |
| 279 |
if(l==null) { |
| 280 |
l=new Label(s); |
| 281 |
map.put(s, l); |
| 282 |
} |
| 283 |
return l; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
private static class Label implements Comparable<Label> { |
| 289 |
|
| 290 |
Label(String s) { |
| 291 |
name=s; |
| 292 |
unknown="UNKNOWN".equalsIgnoreCase(name); |
| 293 |
} |
| 294 |
|
| 295 |
final long count() {return aa+au+ua+ab+ba;} |
| 296 |
|
| 297 |
|
| 298 |
final long count1() {return aa+ab+(unknown ? ua : au);} |
| 299 |
|
| 300 |
|
| 301 |
final long count2() {return aa+ba+(unknown ? au : ua);} |
| 302 |
|
| 303 |
final long tp() {return aa;} |
| 304 |
final long fp() {return ba;} |
| 305 |
final long fn() {return au;} |
| 306 |
|
| 307 |
@Override |
| 308 |
public int compareTo(Label o) { |
| 309 |
|
| 310 |
if(name.equalsIgnoreCase("UNKNOWN") && !o.name.equalsIgnoreCase("UNKNOWN")) {return -1;} |
| 311 |
long dif=count()-o.count(); |
| 312 |
if(dif>0) {return -1;} |
| 313 |
else if(dif<0) {return 1;} |
| 314 |
return name.compareTo(o.name); |
| 315 |
} |
| 316 |
|
| 317 |
public static String header() { |
| 318 |
ByteBuilder bb=new ByteBuilder(); |
| 319 |
bb.append("#Name").tab().append("Count").tab().append("Count1").tab().append("Count2"); |
| 320 |
bb.tab().append("AA").tab().append("AU").tab().append("UA"); |
| 321 |
bb.tab().append("AB").tab().append("BA"); |
| 322 |
bb.tab().append("Yield1").tab().append("Yield2"); |
| 323 |
bb.tab().append("Contam1").tab().append("Contam2"); |
| 324 |
return bb.toString(); |
| 325 |
} |
| 326 |
|
| 327 |
public ByteBuilder appendTo(ByteBuilder bb) { |
| 328 |
long count=count(); |
| 329 |
long count1=count1(); |
| 330 |
long count2=count2(); |
| 331 |
float mult1=1f/Tools.max(count1, 1); |
| 332 |
float mult2=1f/Tools.max(count2, 1); |
| 333 |
float yield1=aa*mult2; |
| 334 |
float yield2=aa*mult1; |
| 335 |
float contam1=ab*mult1; |
| 336 |
float contam2=ba*mult2; |
| 337 |
bb.append(name); |
| 338 |
bb.tab().append(count).tab().append(count1).tab().append(count2); |
| 339 |
bb.tab().append(aa).tab().append(au).tab().append(ua); |
| 340 |
bb.tab().append(ab).tab().append(ba); |
| 341 |
bb.tab().append(yield1, 5).tab().append(yield2, 5); |
| 342 |
bb.tab().append(contam1*1000000, 2).tab().append(contam2*1000000, 2); |
| 343 |
return bb; |
| 344 |
} |
| 345 |
|
| 346 |
final String name; |
| 347 |
final boolean unknown; |
| 348 |
long aa=0; |
| 349 |
long au=0; |
| 350 |
long ua=0; |
| 351 |
long ab=0; |
| 352 |
long ba=0; |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
private String in1=null; |
| 359 |
private String out1="stdout.txt"; |
| 360 |
|
| 361 |
private final FileFormat ffin1; |
| 362 |
private final FileFormat ffout1; |
| 363 |
|
| 364 |
private char delimiter='\t'; |
| 365 |
private final LineParserS1 lp; |
| 366 |
|
| 367 |
private boolean swap=false; |
| 368 |
private String labelStats=null; |
| 369 |
private String quantSetFile=null; |
| 370 |
|
| 371 |
private final LinkedHashMap<String, Label> map; |
| 372 |
private final HashSet<String> quantSet; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
private long maxReads=-1; |
| 377 |
|
| 378 |
private long readsProcessed=0; |
| 379 |
private long basesProcessed=0; |
| 380 |
private long labelsProcessed=0; |
| 381 |
|
| 382 |
private LongList termCounts=new LongList(); |
| 383 |
private long uuCount=0; |
| 384 |
private long uaCount=0; |
| 385 |
private long auCount=0; |
| 386 |
private long aaCount=0; |
| 387 |
private long abCount=0; |
| 388 |
private long invalidCount=0; |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
private java.io.PrintStream outstream=System.err; |
| 393 |
public static boolean verbose=false; |
| 394 |
|
| 395 |
} |