| 1 |
package barcode; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Collections; |
| 6 |
import java.util.HashMap; |
| 7 |
import java.util.LinkedHashSet; |
| 8 |
import java.util.Map.Entry; |
| 9 |
|
| 10 |
import align2.BandedAlignerConcrete; |
| 11 |
import dna.AminoAcid; |
| 12 |
import fileIO.ByteFile; |
| 13 |
import fileIO.ByteStreamWriter; |
| 14 |
import fileIO.FileFormat; |
| 15 |
import fileIO.TextFile; |
| 16 |
import shared.LineParser2; |
| 17 |
import shared.Tools; |
| 18 |
import structures.ByteBuilder; |
| 19 |
import structures.LongList; |
| 20 |
|
| 21 |
public class BarcodeStats { |
| 22 |
|
| 23 |
public static void main(String[] args) { |
| 24 |
|
| 25 |
String fname=args[0]; |
| 26 |
int codesPerRead=(args.length>1 ? Integer.parseInt(args[1]) : 1); |
| 27 |
char delimiter=(args.length>2 ? args[2].charAt(0) : 0); |
| 28 |
String expected=(args.length>3 ? args[3] : null); |
| 29 |
|
| 30 |
BarcodeStats bs=new BarcodeStats(delimiter, codesPerRead, null); |
| 31 |
if(expected!=null) {bs.loadBarcodeList(expected, delimiter, false, false);} |
| 32 |
bs.loadBarcodeCounts(fname); |
| 33 |
if(codesPerRead>1) { |
| 34 |
bs.leftStats=bs.makeLeft(); |
| 35 |
bs.rightStats=bs.makeRight(); |
| 36 |
bs.leftStats.calcStats(); |
| 37 |
bs.rightStats.calcStats(); |
| 38 |
} |
| 39 |
bs.calcStats(); |
| 40 |
String s=bs.toStats("Barcodes:"); |
| 41 |
System.err.println(s); |
| 42 |
if(bs.leftStats!=null){ |
| 43 |
System.err.println(bs.leftStats.toStats("Left: ")); |
| 44 |
} |
| 45 |
if(bs.rightStats!=null){ |
| 46 |
System.err.println(bs.rightStats.toStats("Right: ")); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public BarcodeStats(int delimiter_, int barcodesPerRead_, String label_) { |
| 55 |
delimiter=(char)delimiter_; |
| 56 |
assert(delimiter==delimiter_) : "Invalid delimiter; character value "+delimiter_; |
| 57 |
barcodesPerRead=barcodesPerRead_; |
| 58 |
label=label_; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static LinkedHashSet<String> loadBarcodeSet(LinkedHashSet<String> barcodeSet, |
| 73 |
byte barcodeDelimiter, boolean rcIndex1, boolean rcIndex2){ |
| 74 |
if(barcodeSet==null || barcodeSet.isEmpty()) {return null;} |
| 75 |
int barcodeLength=0; |
| 76 |
|
| 77 |
|
| 78 |
@SuppressWarnings("unchecked") |
| 79 |
LinkedHashSet<String> tempSet=new LinkedHashSet<String>(barcodeSet); |
| 80 |
barcodeSet.clear(); |
| 81 |
for(String s : tempSet) { |
| 82 |
File f=new File(s); |
| 83 |
if(f.exists() && f.isFile()){ |
| 84 |
TextFile tf=new TextFile(s); |
| 85 |
String[] lines=tf.toStringLines(); |
| 86 |
for(String line : lines){ |
| 87 |
|
| 88 |
if(barcodeDelimiter>0 || rcIndex1 || rcIndex2) { |
| 89 |
line=BarcodeStats.fixBarcode(line, barcodeDelimiter, rcIndex1, rcIndex2); |
| 90 |
assert(barcodeDelimiter<1 || line.indexOf(barcodeDelimiter)>=0) : |
| 91 |
line+", "+barcodeDelimiter; |
| 92 |
} |
| 93 |
assert(barcodeLength==0 || barcodeLength==line.length()) : barcodeLength+", "+line.length()+ |
| 94 |
", "+"'"+Character.toString(barcodeDelimiter<0 ? '0' : barcodeDelimiter)+"'="+((int)(barcodeDelimiter))+"\n"+line; |
| 95 |
barcodeLength=line.length(); |
| 96 |
barcodeSet.add(line); |
| 97 |
} |
| 98 |
}else{ |
| 99 |
if(barcodeDelimiter>0 || rcIndex1 || rcIndex2) { |
| 100 |
s=BarcodeStats.fixBarcode(s, barcodeDelimiter, rcIndex1, rcIndex2); |
| 101 |
assert(barcodeDelimiter<1 || s.indexOf(barcodeDelimiter)>=0) : |
| 102 |
s+", "+barcodeDelimiter; |
| 103 |
} |
| 104 |
assert(barcodeLength==0 || barcodeLength==s.length()); |
| 105 |
barcodeLength=s.length(); |
| 106 |
barcodeSet.add(s); |
| 107 |
} |
| 108 |
} |
| 109 |
return barcodeSet; |
| 110 |
} |
| 111 |
|
| 112 |
public int loadBarcodeList(String fname, int forceDelimiter, boolean rcIndex1, boolean rcIndex2){ |
| 113 |
if(fname==null){return 0;} |
| 114 |
String[] codes; |
| 115 |
if(new File(fname).exists()) { |
| 116 |
codes=TextFile.toStringLines(fname); |
| 117 |
}else { |
| 118 |
codes=fname.split(","); |
| 119 |
} |
| 120 |
for(int i=0; i<codes.length; i++){ |
| 121 |
String s=codes[i]; |
| 122 |
linesProcessed++; |
| 123 |
bytesProcessed+=s.length(); |
| 124 |
if(!Tools.startsWith(s, '#')){ |
| 125 |
|
| 126 |
|
| 127 |
s=fixBarcode(s, forceDelimiter, rcIndex1, rcIndex2); |
| 128 |
Barcode b=new Barcode(s); |
| 129 |
expectedCodeList.add(b); |
| 130 |
expectedCodeMap.put(b.name, b); |
| 131 |
} |
| 132 |
} |
| 133 |
return expectedCodeList.size(); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public static String fixBarcode(String s, int delimiter, boolean rcIndex1, boolean rcIndex2) { |
| 149 |
if(s==null) {return null;} |
| 150 |
final int tab=s.indexOf('\t'); |
| 151 |
if(delimiter!='\t' && tab>0) {s=s.substring(0, tab);} |
| 152 |
if(delimiter>0) {s=fixDelimiter(s, delimiter);} |
| 153 |
s=rcompBarcode(s, delimiter, rcIndex1, rcIndex2); |
| 154 |
return s; |
| 155 |
} |
| 156 |
|
| 157 |
public static String rcompBarcode(String s, int delimiter, boolean rcIndex1, boolean rcIndex2) { |
| 158 |
if(s==null || (!rcIndex1 && !rcIndex2)) {return s;} |
| 159 |
int idx=s.indexOf((char)delimiter); |
| 160 |
if(idx<0) { |
| 161 |
return rcIndex1 ? AminoAcid.reverseComplementBases(s) : s; |
| 162 |
}else { |
| 163 |
String a=s.substring(0, idx); |
| 164 |
String b=s.substring(idx+1); |
| 165 |
a=(rcIndex1 ? AminoAcid.reverseComplementBases(a) : a); |
| 166 |
b=(rcIndex2 ? AminoAcid.reverseComplementBases(b) : b); |
| 167 |
return a+((char)delimiter)+b; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
public static String fixDelimiter(String s, int forceDelimiter) { |
| 172 |
if(s==null || forceDelimiter<1 || s.indexOf(forceDelimiter)>=0) {return s;} |
| 173 |
for(int i=0; i<s.length(); i++) { |
| 174 |
char c=s.charAt(i); |
| 175 |
if(!Character.isLetter(c)) { |
| 176 |
return s.replace(c, (char)forceDelimiter); |
| 177 |
} |
| 178 |
} |
| 179 |
assert(false) : "Can't find delimiter in '"+s+"'"; |
| 180 |
return s; |
| 181 |
} |
| 182 |
|
| 183 |
public long loadBarcodeCounts(String fname){ |
| 184 |
LineParser2 lp=new LineParser2('\t'); |
| 185 |
if(fname==null){ |
| 186 |
assert(false) : "Null filename when loading barcode counts."; |
| 187 |
return 0; |
| 188 |
} |
| 189 |
long added=0; |
| 190 |
long sum=0; |
| 191 |
ByteFile bf=ByteFile.makeByteFile(fname, true); |
| 192 |
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()){ |
| 193 |
linesProcessed++; |
| 194 |
bytesProcessed+=line.length; |
| 195 |
if(!Tools.startsWith(line, '#')){ |
| 196 |
lp.set(line); |
| 197 |
String name=lp.parseString(); |
| 198 |
long count=lp.parseLong(); |
| 199 |
increment(name, count); |
| 200 |
added++; |
| 201 |
sum+=count; |
| 202 |
} |
| 203 |
} |
| 204 |
bf.close(); |
| 205 |
return added; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public BarcodeStats split(int pairnum) { |
| 211 |
assert(barcodesPerRead==2) : barcodesPerRead; |
| 212 |
BarcodeStats split=new BarcodeStats((char)0, 1, pairnum==0 ? "Left" : "Right"); |
| 213 |
split.length1=(pairnum==0 ? length1 : length2); |
| 214 |
for(Barcode b : expectedCodeList) { |
| 215 |
split.addExpectedCode(b.getStringForPairnum(pairnum, delimiter)); |
| 216 |
} |
| 217 |
for(Entry<String, Barcode> e : codeMap.entrySet()){ |
| 218 |
Barcode b=e.getValue(); |
| 219 |
String code=b.getStringForPairnum(pairnum, delimiter); |
| 220 |
split.increment(code, b.count()); |
| 221 |
|
| 222 |
} |
| 223 |
return split; |
| 224 |
} |
| 225 |
|
| 226 |
public static int findDelimiter(String fname) { |
| 227 |
TextFile tf=new TextFile(fname); |
| 228 |
int delimiter=-1; |
| 229 |
for(String line=tf.nextLine(); line!=null && delimiter==-1; line=tf.nextLine()){ |
| 230 |
if(!Tools.startsWith(line, '#')){ |
| 231 |
String b=Tools.tabPattern.split(line)[0]; |
| 232 |
delimiter=FileFormat.barcodeDelimiter(b); |
| 233 |
break; |
| 234 |
} |
| 235 |
} |
| 236 |
tf.close(); |
| 237 |
return delimiter; |
| 238 |
} |
| 239 |
|
| 240 |
public static BarcodeStats loadStatic(String fname, String expectedBarcodeFile, int expectedBarcodeCount) { |
| 241 |
BarcodeStats bs=loadStatic(fname); |
| 242 |
if(expectedBarcodeFile!=null){ |
| 243 |
bs.loadBarcodeList(expectedBarcodeFile, bs.delimiter, false, false); |
| 244 |
}else if(expectedBarcodeCount>0) { |
| 245 |
ArrayList<Barcode> list=bs.toList(); |
| 246 |
for(int i=0, max=Tools.min(expectedBarcodeCount, list.size()); i<max; i++) { |
| 247 |
Barcode b=list.get(i); |
| 248 |
bs.addExpectedCode(b.name); |
| 249 |
} |
| 250 |
} |
| 251 |
return bs; |
| 252 |
} |
| 253 |
|
| 254 |
public static BarcodeStats loadStatic(String fname) { |
| 255 |
int delimiter=findDelimiter(fname); |
| 256 |
BarcodeStats bs=new BarcodeStats(delimiter>0 ? delimiter : 0, delimiter>0 ? 2 : 1, null); |
| 257 |
bs.loadBarcodeCounts(fname); |
| 258 |
return bs; |
| 259 |
} |
| 260 |
|
| 261 |
public BarcodeStats makeLeft() {return split(0);} |
| 262 |
public BarcodeStats makeRight() {return split(1);} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public void addExpectedCodes(BarcodeStats bs) { |
| 269 |
assert(expectedCodeList.isEmpty()); |
| 270 |
expectedCodeList.addAll(bs.expectedCodeList); |
| 271 |
expectedCodeMap.putAll(bs.expectedCodeMap); |
| 272 |
} |
| 273 |
|
| 274 |
public int addExpectedCode(String s) { |
| 275 |
if(expectedCodeMap.containsKey(s)){return 0;} |
| 276 |
return addExpectedCode(new Barcode(s)); |
| 277 |
} |
| 278 |
|
| 279 |
public int addExpectedCode(Barcode b) { |
| 280 |
if(expectedCodeMap.containsKey(b.name)){return 0;} |
| 281 |
expectedCodeMap.put(b.name, b); |
| 282 |
expectedCodeList.add(b); |
| 283 |
return 1; |
| 284 |
} |
| 285 |
|
| 286 |
public void increment(String key, long amt) { |
| 287 |
Barcode b=codeMap.get(key); |
| 288 |
if(b==null){ |
| 289 |
b=new Barcode(key); |
| 290 |
codeMap.put(key, b); |
| 291 |
} |
| 292 |
b.increment(amt); |
| 293 |
} |
| 294 |
|
| 295 |
public void increment(String key, long amt, int tile) { |
| 296 |
final String key2=(tile==0 ? key : key+tile); |
| 297 |
Barcode b=codeMap.get(key2); |
| 298 |
if(b==null){ |
| 299 |
b=new Barcode(key); |
| 300 |
b.tile=tile; |
| 301 |
codeMap.put(key2, b); |
| 302 |
}else { |
| 303 |
assert(b.tile==tile); |
| 304 |
} |
| 305 |
b.increment(amt); |
| 306 |
} |
| 307 |
|
| 308 |
public void incrementBad(String key, long amt) { |
| 309 |
Barcode b=badPairMap.get(key); |
| 310 |
if(b==null){ |
| 311 |
b=new Barcode(key); |
| 312 |
badPairMap.put(key, b); |
| 313 |
} |
| 314 |
b.increment(amt); |
| 315 |
} |
| 316 |
|
| 317 |
public void incrementGood(String key, long amt) { |
| 318 |
Barcode b=goodPairMap.get(key); |
| 319 |
if(b==null){ |
| 320 |
b=new Barcode(key); |
| 321 |
goodPairMap.put(key, b); |
| 322 |
} |
| 323 |
b.increment(amt); |
| 324 |
} |
| 325 |
|
| 326 |
public void incrementMismatches(final String s, final long count, LongList transitions) { |
| 327 |
final Barcode b=findClosest(s, 6, 1); |
| 328 |
if(b==null) {return;} |
| 329 |
final String s2=b.name; |
| 330 |
for(int i=0; i<s.length(); i++) { |
| 331 |
final char q=s.charAt(i), r=s2.charAt(i); |
| 332 |
if(Tools.isLetter(q) && r!='N') { |
| 333 |
if(q!=r) { |
| 334 |
incrementMismatch(i, r, q, count, transitions); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
public void incrementMismatch(int pos, int ref, int query, long incr, LongList transitions) { |
| 342 |
int idx=Transition.encode(pos, ref, query); |
| 343 |
transitions.increment(idx, incr); |
| 344 |
} |
| 345 |
|
| 346 |
public ByteBuilder printTransitions() {return printTransitions(transitions, null);} |
| 347 |
|
| 348 |
public static ByteBuilder printTransitions(LongList transitions, ByteBuilder bb) { |
| 349 |
if(bb==null) {bb=new ByteBuilder();} |
| 350 |
ArrayList<Transition> list=new ArrayList<Transition>(); |
| 351 |
for(int i=0; i<transitions.size(); i++) { |
| 352 |
long count=transitions.get(i); |
| 353 |
if(count>0) { |
| 354 |
Transition t=Transition.decode(i); |
| 355 |
t.count=count; |
| 356 |
list.add(t); |
| 357 |
} |
| 358 |
} |
| 359 |
Collections.sort(list); |
| 360 |
bb.append("#pos\tref\tquery\tcount\n"); |
| 361 |
for(Transition t : list) { |
| 362 |
t.appendTo(bb).nl(); |
| 363 |
} |
| 364 |
|
| 365 |
return bb; |
| 366 |
} |
| 367 |
|
| 368 |
public void calcStats() { |
| 369 |
|
| 370 |
|
| 371 |
for(Entry<String, Barcode> e : codeMap.entrySet()) { |
| 372 |
Barcode b=e.getValue(); |
| 373 |
totalCodes+=b.count(); |
| 374 |
totalCodesU++; |
| 375 |
|
| 376 |
Barcode eb=expectedCodeMap.get(b.name); |
| 377 |
byte mer=-1; |
| 378 |
if(eb!=null){ |
| 379 |
eb.increment(b.count()); |
| 380 |
expectedCodes+=b.count(); |
| 381 |
expectedCodesU++; |
| 382 |
|
| 383 |
}else{ |
| 384 |
if(!expectedCodeList.isEmpty()) { |
| 385 |
incrementMismatches(b.name, b.count(), transitions); |
| 386 |
} |
| 387 |
if(b.countUndefined()>0){ |
| 388 |
nCodes+=b.count(); |
| 389 |
nCodesU++; |
| 390 |
}else if((mer=b.checkHomopolymer())>=0){ |
| 391 |
polymerArray[mer]+=b.count(); |
| 392 |
polymerArrayU[mer]++; |
| 393 |
}else if(!expectedCodeList.isEmpty() && barcodesPerRead<2){ |
| 394 |
int hdist=calcHdist(b); |
| 395 |
int edist=(hdist>1 && calcEdist ? calcEdist(b) : hdist); |
| 396 |
hdistArray[hdist]+=b.count(); |
| 397 |
edistArray[edist]+=b.count(); |
| 398 |
hdistArrayU[hdist]++; |
| 399 |
edistArrayU[edist]++; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
if(leftStats!=null && !leftStats.expectedCodeList.isEmpty()) { |
| 404 |
assert(rightStats!=null); |
| 405 |
String left=b.leftString(delimiter); |
| 406 |
String right=b.rightString(delimiter); |
| 407 |
|
| 408 |
|
| 409 |
if(eb==null) { |
| 410 |
final boolean leftMatched=leftStats.expectedCodeMap.containsKey(left), rightMatched=rightStats.expectedCodeMap.containsKey(right); |
| 411 |
final int matched=(leftMatched ? 1 : 0)+(rightMatched ? 1 : 0); |
| 412 |
validArray[matched]+=b.count(); |
| 413 |
validArrayU[matched]++; |
| 414 |
if(matched==2){ |
| 415 |
leftStats.incrementBad(left, b.count()); |
| 416 |
rightStats.incrementBad(right, b.count()); |
| 417 |
} |
| 418 |
}else{ |
| 419 |
leftStats.incrementGood(left, b.count()); |
| 420 |
rightStats.incrementGood(right, b.count()); |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
if(leftStats!=null && !leftStats.expectedCodeList.isEmpty()) { |
| 426 |
badPairFraction=(float)(validArray[2]/(1.0*totalCodes)); |
| 427 |
goodPairFraction=(float)(expectedCodes/(1.0*totalCodes)); |
| 428 |
}else{ |
| 429 |
long bad=0, good=0; |
| 430 |
for(Barcode e : expectedCodeList) { |
| 431 |
{ |
| 432 |
Barcode bc=badPairMap.get(e.name); |
| 433 |
bad+=(bc==null ? 0 : bc.count()); |
| 434 |
} |
| 435 |
{ |
| 436 |
Barcode bc=goodPairMap.get(e.name); |
| 437 |
good+=(bc==null ? 0 : bc.count()); |
| 438 |
} |
| 439 |
} |
| 440 |
badPairFraction=(float)(bad/(1.0*totalCodes)); |
| 441 |
goodPairFraction=(float)(good/(1.0*totalCodes)); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
public Barcode findClosest(String s, int maxHDist) { |
| 446 |
assert(!expectedCodeList.isEmpty()); |
| 447 |
Barcode best=expectedCodeMap.get(s); |
| 448 |
if(best!=null || maxHDist<1) {return best;} |
| 449 |
int min=s.length(); |
| 450 |
for(int i=0; i<expectedCodeList.size(); i++) { |
| 451 |
Barcode b=expectedCodeList.get(i); |
| 452 |
int dist=b.hdist(s); |
| 453 |
if(dist<min) { |
| 454 |
best=b; |
| 455 |
min=dist; |
| 456 |
} |
| 457 |
} |
| 458 |
assert(min>0); |
| 459 |
return min<=maxHDist ? best : null; |
| 460 |
} |
| 461 |
|
| 462 |
public Barcode findClosest(String s, int maxHDist, int clearzone) { |
| 463 |
assert(!expectedCodeList.isEmpty()); |
| 464 |
Barcode best=expectedCodeMap.get(s); |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
if(best!=null || maxHDist<1) {return best;} |
| 470 |
int hdist=s.length(); |
| 471 |
int hdist2=hdist; |
| 472 |
assert(best==null); |
| 473 |
|
| 474 |
for(int i=0; i<expectedCodeList.size(); i++) { |
| 475 |
Barcode b=expectedCodeList.get(i); |
| 476 |
int d=b.hdist(s); |
| 477 |
if(d<hdist2) { |
| 478 |
|
| 479 |
hdist2=d; |
| 480 |
if(d<hdist) { |
| 481 |
|
| 482 |
hdist2=hdist; |
| 483 |
best=b; |
| 484 |
hdist=d; |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
if(hdist<=maxHDist && hdist+clearzone<=hdist2) {return best;} |
| 489 |
return null; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
public int calcHdist(String s) { |
| 494 |
assert(!expectedCodeList.isEmpty()); |
| 495 |
if(expectedCodeMap.containsKey(s)) {return 0;} |
| 496 |
int min=s.length(); |
| 497 |
for(int i=0; i<expectedCodeList.size(); i++) { |
| 498 |
Barcode b=expectedCodeList.get(i); |
| 499 |
min=Tools.min(min, b.hdist(s)); |
| 500 |
} |
| 501 |
return min; |
| 502 |
} |
| 503 |
|
| 504 |
public int calcHdist(Barcode b) { |
| 505 |
assert(!expectedCodeList.isEmpty()); |
| 506 |
assert(!expectedCodeMap.containsKey(b.name)) : "Check this first."; |
| 507 |
int min=b.length(); |
| 508 |
for(Barcode expected : expectedCodeList) { |
| 509 |
min=Tools.min(min, b.hdist(expected)); |
| 510 |
if(min<=1) { |
| 511 |
assert(min==1); |
| 512 |
return min; |
| 513 |
} |
| 514 |
} |
| 515 |
return min; |
| 516 |
} |
| 517 |
|
| 518 |
public int calcEdist(Barcode b) { |
| 519 |
assert(!expectedCodeList.isEmpty()); |
| 520 |
assert(!expectedCodeMap.containsKey(b.name)) : "Check this first."; |
| 521 |
int min=b.length(); |
| 522 |
for(Barcode expected : expectedCodeList) { |
| 523 |
min=Tools.min(min, b.edist(expected, bandy)); |
| 524 |
if(min<=1) { |
| 525 |
assert(min==1); |
| 526 |
return min; |
| 527 |
} |
| 528 |
} |
| 529 |
return min; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
public BarcodeStats copySpecial() { |
| 537 |
BarcodeStats bs=new BarcodeStats(delimiter, barcodesPerRead, label); |
| 538 |
bs.addExpectedCodes(this); |
| 539 |
return bs; |
| 540 |
} |
| 541 |
|
| 542 |
public void merge(BarcodeStats bs) { |
| 543 |
for(Entry<String, Barcode> e : bs.codeMap.entrySet()) { |
| 544 |
Barcode b=e.getValue(); |
| 545 |
increment(b.name, b.count()); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
public boolean isBadPair(String s){ |
| 560 |
if(expectedCodeMap.containsKey(s)) {return false;} |
| 561 |
String[] split=split(s, delimiter); |
| 562 |
return leftStats.expectedCodeMap.containsKey(split[0]) && rightStats.expectedCodeMap.containsKey(split[1]); |
| 563 |
} |
| 564 |
|
| 565 |
public boolean hasErrors(String s){ |
| 566 |
if(expectedCodeMap.containsKey(s)) {return false;} |
| 567 |
String[] split=split(s, delimiter); |
| 568 |
return !leftStats.expectedCodeMap.containsKey(split[0]) || !rightStats.expectedCodeMap.containsKey(split[1]); |
| 569 |
} |
| 570 |
|
| 571 |
private static String[] split(String s, int delimiter){ |
| 572 |
String a=null, b=null; |
| 573 |
if(delimiter<=0) { |
| 574 |
a=s.substring(0, s.length()/2); |
| 575 |
b=s.substring(s.length()/2, s.length()); |
| 576 |
}else{ |
| 577 |
for(int i=0; a==null && i<s.length(); i++) { |
| 578 |
if(s.charAt(i)==delimiter){ |
| 579 |
a=s.substring(0, i); |
| 580 |
b=s.substring(i+1, s.length()); |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
assert(a!=null && a.length()>0); |
| 585 |
assert(b!=null && b.length()>0); |
| 586 |
return new String[] {a, b}; |
| 587 |
} |
| 588 |
|
| 589 |
public int size() {return codeMap.size();} |
| 590 |
|
| 591 |
public ArrayList<Barcode> toList(){ |
| 592 |
ArrayList<Barcode> list=new ArrayList<Barcode>(codeMap.size()); |
| 593 |
list.addAll(codeMap.values()); |
| 594 |
Collections.sort(list); |
| 595 |
return list; |
| 596 |
} |
| 597 |
|
| 598 |
public long hdistSum(int min, int max){ |
| 599 |
long sum=0; |
| 600 |
for(int i=min; i<=max && i<hdistArray.length; i++) { |
| 601 |
sum+=hdistArray[i]; |
| 602 |
} |
| 603 |
return sum; |
| 604 |
} |
| 605 |
|
| 606 |
public long edistSum(int min, int max){ |
| 607 |
long sum=0; |
| 608 |
for(int i=min; i<=max && i<edistArray.length; i++) { |
| 609 |
sum+=edistArray[i]; |
| 610 |
} |
| 611 |
return sum; |
| 612 |
} |
| 613 |
|
| 614 |
String pad(long x) { |
| 615 |
String s=""+x; |
| 616 |
while(s.length()<10) {s=s+" ";} |
| 617 |
return s; |
| 618 |
} |
| 619 |
|
| 620 |
public String toStats(String hdr) { |
| 621 |
StringBuilder sb=new StringBuilder(); |
| 622 |
sb.append(hdr+"\t"+pad(totalCodes)+"\t("+totalCodesU+" unique)"); |
| 623 |
if(barcodesPerRead>1) { |
| 624 |
sb.append("\n-codesPerRead\t"+pad(barcodesPerRead)); |
| 625 |
sb.append("\n-delimiter\t"+(delimiter>0 ? Character.toString(delimiter) : "")); |
| 626 |
if(length1>0) {sb.append("\n-bcLength1\t"+pad(length1));} |
| 627 |
if(length2>0) {sb.append("\n-bcLength2\t"+pad(length2));} |
| 628 |
}else { |
| 629 |
if(length1>0) {sb.append("\n-bcLength\t"+pad(length1));} |
| 630 |
} |
| 631 |
if(!expectedCodeList.isEmpty()) { |
| 632 |
sb.append("\n-expected\t"+pad(expectedCodes)+"\t("+expectedCodesU+" unique)"); |
| 633 |
sb.append("\n-unexpected\t"+pad(totalCodes-expectedCodes)+"\t("+(totalCodesU-expectedCodesU)+" unique)"); |
| 634 |
} |
| 635 |
sb.append("\n-codesWithNs\t"+pad(nCodes)+"\t("+nCodesU+" unique)"); |
| 636 |
sb.append("\n-homopolymers\t"+pad(shared.Vector.sum(polymerArray))+"\t("+shared.Vector.sum(polymerArrayU)+" unique)"); |
| 637 |
if(polymerArray[0]>0) {sb.append("\n--polyA\t"+polymerArray[0]);} |
| 638 |
if(polymerArray[1]>0) {sb.append("\n--polyC\t"+polymerArray[1]);} |
| 639 |
if(polymerArray[2]>0) {sb.append("\n--polyG\t"+polymerArray[2]);} |
| 640 |
if(polymerArray[3]>0) {sb.append("\n--polyT\t"+polymerArray[3]);} |
| 641 |
if(barcodesPerRead>1 && !expectedCodeList.isEmpty()){ |
| 642 |
sb.append("\n-badPair\t"+pad(validArray[2])+"\t("+validArrayU[2]+" unique)"+"\t"+String.format("(%.4f)",badPairFraction)); |
| 643 |
sb.append("\n-singleMatch\t"+pad(validArray[1])+"\t("+validArrayU[1]+" unique)"+"\t"+String.format("(%.4f)",(float)(validArray[1]/(1.0*totalCodes)))); |
| 644 |
sb.append("\n-neitherMatch\t"+pad(validArray[0])+"\t("+validArrayU[0]+" unique)"+"\t"+String.format("(%.4f)",(float)(validArray[0]/(1.0*totalCodes)))); |
| 645 |
} |
| 646 |
if(!expectedCodeList.isEmpty() && barcodesPerRead<2) { |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
sb.append("\n-hDist 1,2,3+\t"+hdistSum(1,1)+"\t"+hdistSum(2,2)+"\t"+hdistSum(3,999)); |
| 652 |
if(calcEdist) { |
| 653 |
sb.append("\n-eDist 1,2,3+\t"+edistSum(1,1)+"\t"+edistSum(2,2)+"\t"+edistSum(3,999)); |
| 654 |
} |
| 655 |
} |
| 656 |
return sb.toString(); |
| 657 |
} |
| 658 |
|
| 659 |
public void printToFile(String fname, boolean overwrite){ |
| 660 |
FileFormat ff=FileFormat.testOutput(fname, FileFormat.TXT, null, true, overwrite, false, false); |
| 661 |
printToFile(ff); |
| 662 |
} |
| 663 |
|
| 664 |
public void printToFile(FileFormat ff){printToFile(ff, 0);} |
| 665 |
|
| 666 |
public void printToFile(FileFormat ff, long minCount){ |
| 667 |
if(ff==null) {return;} |
| 668 |
ArrayList<Barcode> list=toList(); |
| 669 |
long sum=0; |
| 670 |
for(Barcode bc : list) {sum+=bc.count();} |
| 671 |
|
| 672 |
try { |
| 673 |
ByteStreamWriter bsw=new ByteStreamWriter(ff); |
| 674 |
bsw.start(); |
| 675 |
bsw.println("#Barcodes\t"+sum); |
| 676 |
bsw.println("#Unique\t"+list.size()); |
| 677 |
ByteBuilder bb=new ByteBuilder(128); |
| 678 |
for(Barcode bc : list){ |
| 679 |
if(bc.count()>=minCount) { |
| 680 |
bc.appendTo(bb).nl(); |
| 681 |
bsw.print(bb); |
| 682 |
bb.clear(); |
| 683 |
} |
| 684 |
} |
| 685 |
errorState|=bsw.poisonAndWait(); |
| 686 |
} catch (Throwable e) { |
| 687 |
System.err.println("ERROR - Could not write barcode file "+ff.name()+": "+e.toString()); |
| 688 |
errorState=true; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
public final HashMap<String, Barcode> codeMap=new HashMap<String, Barcode>(); |
| 697 |
public HashMap<String, Barcode> badPairMap=new HashMap<String, Barcode>(); |
| 698 |
public HashMap<String, Barcode> goodPairMap=new HashMap<String, Barcode>(); |
| 699 |
public final ArrayList<Barcode> expectedCodeList=new ArrayList<Barcode>(); |
| 700 |
public final HashMap<String,Barcode> expectedCodeMap=new HashMap<String,Barcode>(); |
| 701 |
|
| 702 |
public final char delimiter; |
| 703 |
public final int barcodesPerRead; |
| 704 |
public int length1; |
| 705 |
public int length2; |
| 706 |
|
| 707 |
public long linesProcessed=0, bytesProcessed=0; |
| 708 |
|
| 709 |
public long nCodes=0; |
| 710 |
public long expectedCodes=0; |
| 711 |
public long totalCodes=0; |
| 712 |
public float badPairFraction=-1; |
| 713 |
public float goodPairFraction=-1; |
| 714 |
|
| 715 |
public long[] validArray=new long[3]; |
| 716 |
public long[] polymerArray=new long[4]; |
| 717 |
public long[] hdistArray=new long[30]; |
| 718 |
public long[] edistArray=new long[30]; |
| 719 |
|
| 720 |
public long nCodesU=0; |
| 721 |
public long expectedCodesU=0; |
| 722 |
public long totalCodesU=0; |
| 723 |
public long[] validArrayU=new long[3]; |
| 724 |
public long[] polymerArrayU=new long[4]; |
| 725 |
public long[] hdistArrayU=new long[30]; |
| 726 |
public long[] edistArrayU=new long[30]; |
| 727 |
|
| 728 |
public LongList transitions=new LongList(); |
| 729 |
|
| 730 |
public BarcodeStats leftStats=null; |
| 731 |
public BarcodeStats rightStats=null; |
| 732 |
private final BandedAlignerConcrete bandy=new BandedAlignerConcrete(31); |
| 733 |
private String label; |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
public static boolean calcEdist=false; |
| 740 |
public static boolean errorState=false; |
| 741 |
|
| 742 |
} |