| 1 |
package barcode; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Collection; |
| 5 |
import java.util.Collections; |
| 6 |
import java.util.HashMap; |
| 7 |
import java.util.LinkedHashMap; |
| 8 |
import java.util.LinkedHashSet; |
| 9 |
import java.util.Map.Entry; |
| 10 |
import java.util.Set; |
| 11 |
|
| 12 |
import fileIO.ByteFile; |
| 13 |
import fileIO.ByteStreamWriter; |
| 14 |
import hiseq.IlluminaHeaderParser2; |
| 15 |
import shared.LineParser1; |
| 16 |
import shared.LineParserS1; |
| 17 |
import shared.Tools; |
| 18 |
import stream.Read; |
| 19 |
import structures.ByteBuilder; |
| 20 |
import structures.LongList; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class LegacyFileWriter { |
| 29 |
|
| 30 |
public LegacyFileWriter(){} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
long writeTopUnknownBarcodes(final Set<String> expectedSet, |
| 39 |
HashMap<String, String> assignmentMap, final byte delimiter, |
| 40 |
final String fname, final int lane, final int lines, |
| 41 |
boolean overwrite) { |
| 42 |
if(fname==null) {return 0;} |
| 43 |
ArrayList<Barcode> list=new ArrayList<Barcode>(counts); |
| 44 |
Collections.sort(list); |
| 45 |
|
| 46 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(fname, overwrite, false, true); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
bsw.println("Lane,index,index2,# Reads,% of Unknown Barcodes,% of All Reads"); |
| 51 |
|
| 52 |
|
| 53 |
long total=0; |
| 54 |
long expected=0; |
| 55 |
for(Barcode bc : list) { |
| 56 |
total+=bc.count(); |
| 57 |
if(assignmentMap.containsKey(bc.name)) {expected+=bc.count();} |
| 58 |
} |
| 59 |
final long unexpected=total-expected; |
| 60 |
final float invTotal=1f/Tools.max(1, total); |
| 61 |
final float invUnexpected=1f/Tools.max(1, unexpected); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
int printed=0; |
| 66 |
ByteBuilder bb=new ByteBuilder(); |
| 67 |
for(Barcode bc : list) { |
| 68 |
if(!assignmentMap.containsKey(bc.name)) { |
| 69 |
if(printed>=lines) {break;} |
| 70 |
printed++; |
| 71 |
bb.clear().append(lane).comma(); |
| 72 |
bc.appendIndex(bb, delimiter, 1).comma(); |
| 73 |
|
| 74 |
bc.appendIndex(bb, delimiter, 2).comma(); |
| 75 |
|
| 76 |
|
| 77 |
bb.append(bc.count()).comma(); |
| 78 |
bb.append(bc.count()*invUnexpected, 6).comma(); |
| 79 |
bb.append(bc.count()*invTotal, 6).nl(); |
| 80 |
bsw.print(bb); |
| 81 |
} |
| 82 |
} |
| 83 |
bsw.poison(); |
| 84 |
return unexpected; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
void writeIndexHoppingCounts(final Set<String> expectedSet, |
| 101 |
LinkedHashMap<String,String> sampleMap, final byte delimiter, final String fname, |
| 102 |
final int lane, boolean overwrite) { |
| 103 |
if(delimiter<=0) {return;} |
| 104 |
LinkedHashSet<String> leftSet=new LinkedHashSet<String>(); |
| 105 |
LinkedHashSet<String> rightSet=new LinkedHashSet<String>(); |
| 106 |
LineParserS1 lp=new LineParserS1(delimiter); |
| 107 |
for(String s : expectedSet) { |
| 108 |
lp.set(s); |
| 109 |
assert(lp.terms()==2) : "Wrong number of barcodes in '"+s+ |
| 110 |
"'; expected 2 for delimiter "+(int)delimiter; |
| 111 |
leftSet.add(lp.parseString(0)); |
| 112 |
rightSet.add(lp.parseString(1)); |
| 113 |
} |
| 114 |
|
| 115 |
final HashMap<String, Barcode> countMap=new HashMap<String, Barcode>(Tools.max(8, counts.size())); |
| 116 |
float allMult=1f/Tools.max(1, (totalR1)); |
| 117 |
for(Barcode bc : counts) {countMap.put(bc.name, bc);} |
| 118 |
|
| 119 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(fname, overwrite, false, true); |
| 120 |
bsw.println("Lane,SampleID,index,index2,# Reads,% of Hopped Reads,% of All Reads"); |
| 121 |
final ByteBuilder bb=new ByteBuilder(); |
| 122 |
for(String s : expectedSet) { |
| 123 |
String sample=(sampleMap==null ? null : sampleMap.get(s)); |
| 124 |
lp.set(s); |
| 125 |
bb.clear().append(lane).comma().append(sample==null ? "" : sample).comma(); |
| 126 |
bb.append(lp.parseByteArray(0)).comma(); |
| 127 |
bb.append(lp.parseByteArray(1)).comma(); |
| 128 |
Barcode bc=countMap.get(s); |
| 129 |
long count=(bc==null ? 0 : bc.count()); |
| 130 |
bb.append(count).comma().comma().append(count*allMult,6).nl(); |
| 131 |
bsw.print(bb); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
long hoppedCount=0; |
| 136 |
for(String left : leftSet) { |
| 137 |
bb.clear().append(left).append(delimiter); |
| 138 |
final int len=bb.length; |
| 139 |
for(String right : rightSet) { |
| 140 |
bb.setLength(len).append(right); |
| 141 |
String pair=bb.toString(); |
| 142 |
if(!expectedSet.contains(pair)) { |
| 143 |
Barcode bc=countMap.get(pair); |
| 144 |
if(bc!=null) { |
| 145 |
hoppedCount+=bc.count(); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
final float hoppedMult=1f/(Tools.max(1, hoppedCount)); |
| 151 |
|
| 152 |
ByteBuilder bb2=new ByteBuilder(); |
| 153 |
for(String left : leftSet) { |
| 154 |
bb.clear().append(left).append(delimiter); |
| 155 |
final int len=bb.length; |
| 156 |
for(String right : rightSet) { |
| 157 |
bb.setLength(len).append(right); |
| 158 |
String pair=bb.toString(); |
| 159 |
if(!expectedSet.contains(pair)) { |
| 160 |
Barcode bc=countMap.get(pair); |
| 161 |
long count=(bc==null ? 0 : bc.count()); |
| 162 |
|
| 163 |
|
| 164 |
lp.set(pair); |
| 165 |
bb2.clear().append(lane).comma().comma(); |
| 166 |
bb2.append(lp.parseByteArray(0)).comma(); |
| 167 |
bb2.append(lp.parseByteArray(1)).comma(); |
| 168 |
bb2.append(count).comma().append(count*hoppedMult, 6).comma(); |
| 169 |
bb2.append(count*allMult,6).nl(); |
| 170 |
bsw.print(bb2); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
bsw.poison(); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
int writeQualityMetrics(final Set<String> expectedSet, LinkedHashMap<String,String> sampleMap, |
| 224 |
final byte delimiter, final String fname, final int lane, boolean overwrite) { |
| 225 |
if(fname==null) {return 0;} |
| 226 |
|
| 227 |
LinkedHashSet<String> set2=new LinkedHashSet<String>(expectedSet); |
| 228 |
set2.remove(UNDETERMINED); |
| 229 |
set2.add(UNDETERMINED); |
| 230 |
|
| 231 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(fname, overwrite, false, true); |
| 232 |
bsw.println(QTag.header()); |
| 233 |
ByteBuilder bb=new ByteBuilder(); |
| 234 |
int lines=0; |
| 235 |
for(String name : set2) { |
| 236 |
String sample=(sampleMap==null ? null : sampleMap.get(name)); |
| 237 |
QTag[] tags=tagMap.get(name); |
| 238 |
if(tags==null) { |
| 239 |
tags=new QTag[] {new QTag(), new QTag()}; |
| 240 |
} |
| 241 |
for(int pairnum=0; pairnum<tags.length && tags[pairnum]!=null; pairnum++) { |
| 242 |
QTag tag=tags[pairnum]; |
| 243 |
bb.clear(); |
| 244 |
tag.appendTo(bb, lane, pairnum, sample==null ? UNDETERMINED : sample, |
| 245 |
delimiter, name==UNDETERMINED ? "" : name); |
| 246 |
bsw.print(bb.nl()); |
| 247 |
lines++; |
| 248 |
} |
| 249 |
} |
| 250 |
bsw.poison(); |
| 251 |
return lines; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
void writeDemultiplexStats(String fname, HashMap<String, String> assignmentMap, |
| 260 |
Set<String> expected, LinkedHashMap<String,String> sampleMap, |
| 261 |
int lane, boolean overwrite) { |
| 262 |
|
| 263 |
LinkedHashMap<String, long[]> hdistMap=new LinkedHashMap<String, long[]>(expected.size()); |
| 264 |
final int maxHDist=3; |
| 265 |
for(String name : expected) { |
| 266 |
hdistMap.put(name, new long[maxHDist+1]); |
| 267 |
} |
| 268 |
|
| 269 |
HashMap<String, Barcode> countMap=new HashMap<String, Barcode>(counts.size()); |
| 270 |
for(Barcode bc : counts) {countMap.put(bc.name, bc);} |
| 271 |
|
| 272 |
for(Entry<String, String> e : assignmentMap.entrySet()) { |
| 273 |
String key=e.getKey(); |
| 274 |
String value=e.getValue(); |
| 275 |
long[] distArray=hdistMap.get(value); |
| 276 |
assert(distArray!=null) : value; |
| 277 |
int dist=Tools.max(Barcode.hdistL(key, value), Barcode.hdistR(key, value)); |
| 278 |
Barcode bc=countMap.get(key); |
| 279 |
long count=(bc==null ? 0 : bc.count()); |
| 280 |
distArray[Tools.min(maxHDist, dist)]+=count; |
| 281 |
} |
| 282 |
|
| 283 |
String header="Lane,SampleID,Index,# Reads,# Perfect Index Reads," |
| 284 |
+ "# One Mismatch Index Reads,# Two Mismatch Index Reads," |
| 285 |
+ "% Reads,% Perfect Index Reads,% One Mismatch Index Reads,% Two Mismatch Index Reads"; |
| 286 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(fname, overwrite, false, true); |
| 287 |
bsw.println(header); |
| 288 |
ByteBuilder bb=new ByteBuilder(); |
| 289 |
|
| 290 |
final float totalMult=1f/totalR1; |
| 291 |
for(String name : expected) { |
| 292 |
String sample=(sampleMap==null ? null : sampleMap.get(name)); |
| 293 |
final long[] dist=hdistMap.get(name); |
| 294 |
final long sum=Tools.sum(dist); |
| 295 |
final float localMult=1f/sum; |
| 296 |
bb.clear().append(lane); |
| 297 |
bb.comma().append(sample==null ? "" : sample); |
| 298 |
bb.comma().append(name.replace('+', '-')); |
| 299 |
bb.comma().append(sum); |
| 300 |
for(int i=0; i<=2; i++) {bb.comma().append(dist[i]);} |
| 301 |
bb.comma().append(sum*totalMult, 4); |
| 302 |
for(int i=0; i<=2; i++) {bb.comma().append(dist[i]*localMult, 4);} |
| 303 |
bsw.print(bb.nl()); |
| 304 |
|
| 305 |
} |
| 306 |
|
| 307 |
{ |
| 308 |
long unknown=totalR1-totalR1Assigned; |
| 309 |
String sample="Undetermined"; |
| 310 |
|
| 311 |
final long sum=unknown; |
| 312 |
|
| 313 |
final long[] dist=new long[] {sum, 0, 0}; |
| 314 |
final float localMult=1f/sum; |
| 315 |
bb.clear().append(lane); |
| 316 |
bb.comma().append(sample==null ? "" : sample); |
| 317 |
bb.comma().append(""); |
| 318 |
bb.comma().append(sum); |
| 319 |
for(int i=0; i<=2; i++) {bb.comma().append(dist[i]);} |
| 320 |
bb.comma().append(sum*totalMult, 4); |
| 321 |
for(int i=0; i<=2; i++) {bb.comma().append(dist[i]*localMult, 4);} |
| 322 |
bsw.print(bb.nl()); |
| 323 |
} |
| 324 |
|
| 325 |
bsw.poison(); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
void writeDemultiplexTileStats(String fname, LinkedHashMap<String,String> sampleMap, |
| 337 |
Collection<String> expected, int lane, byte delimiter, boolean overwrite) { |
| 338 |
if(tileMap==null) {return;} |
| 339 |
if(expected==null) {expected=sampleMap.keySet();} |
| 340 |
String header="Lane,SampleID,Index,Tile,# Reads,% Reads"; |
| 341 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(fname, overwrite, false, true); |
| 342 |
bsw.println(header); |
| 343 |
ByteBuilder bb=new ByteBuilder(); |
| 344 |
ArrayList<Integer> tiles=new ArrayList<Integer>(tileMap.keySet()); |
| 345 |
Collections.sort(tiles); |
| 346 |
final float invTotal=1f/Tools.max(1, totalR1); |
| 347 |
|
| 348 |
for(Integer tile : tiles) { |
| 349 |
LinkedHashMap<String, Barcode> tmap=tileMap.get(tile); |
| 350 |
|
| 351 |
|
| 352 |
for(String name : expected) { |
| 353 |
Barcode bc=tmap.get(name); |
| 354 |
if(bc!=null) { |
| 355 |
String sampleID=(sampleMap==null ? null : sampleMap.get(bc.name)); |
| 356 |
bb.clear(); |
| 357 |
bb.append(lane); |
| 358 |
bb.comma().append(sampleID); |
| 359 |
Barcode.appendIndex(bb.comma(), delimiter, 1, bc.name); |
| 360 |
if(delimiter>=0 && (byte)bc.name.indexOf(delimiter)>0) { |
| 361 |
bb.dash(); |
| 362 |
Barcode.appendIndex(bb, delimiter, 2, bc.name); |
| 363 |
} |
| 364 |
bb.comma().append(tile); |
| 365 |
bb.comma().append(bc.count()); |
| 366 |
bb.comma().append(bc.count()*invTotal, 4).nl(); |
| 367 |
bsw.print(bb); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
bsw.poison(); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
void add(Read r, String barcode) { |
| 376 |
if(barcode==null) {barcode=UNDETERMINED;} |
| 377 |
QTag[] qtags=tagMap.get(barcode); |
| 378 |
if(qtags==null) { |
| 379 |
qtags=new QTag[] {new QTag(), new QTag()}; |
| 380 |
tagMap.put(barcode, qtags); |
| 381 |
} |
| 382 |
qtags[r.pairnum()].add(r); |
| 383 |
String dest=(String) r.obj; |
| 384 |
if(r.pairnum()==0) { |
| 385 |
totalR1++; |
| 386 |
totalR1Assigned+=(barcode==UNDETERMINED ? 0 : 1); |
| 387 |
if(r.mate!=null) { |
| 388 |
totalR2++; |
| 389 |
totalR2Assigned+=(barcode==UNDETERMINED ? 0 : 1); |
| 390 |
} |
| 391 |
|
| 392 |
if(tileMap!=null && dest!=null) { |
| 393 |
ihp.parse(r); |
| 394 |
Integer tile=ihp.tile(); |
| 395 |
LinkedHashMap<String, Barcode> tmap=tileMap.get(tile); |
| 396 |
if(tmap==null) { |
| 397 |
tmap=new LinkedHashMap<String, Barcode>(); |
| 398 |
tileMap.put(tile, tmap); |
| 399 |
} |
| 400 |
Barcode bc=tmap.get(dest); |
| 401 |
if(bc==null) { |
| 402 |
bc=new Barcode(dest); |
| 403 |
tmap.put(dest, bc); |
| 404 |
} |
| 405 |
assert(tileMap.size()>0 && tmap.size()>0) : |
| 406 |
tileMap.size()+", "+tmap.size()+", "+tile+", "+dest+"\n"+ihp; |
| 407 |
bc.increment(1); |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
LinkedHashMap<String, String> loadSampleMap(String fname, |
| 414 |
byte barcodeDelimiter, boolean rcIndex1, boolean rcIndex2){ |
| 415 |
if(fname==null) {return null;} |
| 416 |
LinkedHashMap<String, String> map=new LinkedHashMap<String, String>(); |
| 417 |
ByteFile bf=ByteFile.makeByteFile(fname, true); |
| 418 |
LineParser1 lp=new LineParser1(','); |
| 419 |
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()) { |
| 420 |
if(!Tools.startsWith(line, '#')) { |
| 421 |
lp.set(line); |
| 422 |
if(lp.terms()<2) { |
| 423 |
lp=new LineParser1('\t'); |
| 424 |
lp.set(line); |
| 425 |
assert(lp.terms()>=2) : "Can't find delimiter in line '"+new String(line)+"'"; |
| 426 |
} |
| 427 |
assert(lp.terms()>=2) : new String(line)+", "+lp; |
| 428 |
String key=lp.parseString(0), value=lp.parseString(1); |
| 429 |
key=BarcodeStats.fixBarcode(key, barcodeDelimiter, rcIndex1, rcIndex2); |
| 430 |
map.put(key, value); |
| 431 |
} |
| 432 |
} |
| 433 |
bf.close(); |
| 434 |
return map; |
| 435 |
} |
| 436 |
|
| 437 |
static class QTag { |
| 438 |
|
| 439 |
int add(Read r) { |
| 440 |
return add(r.quality); |
| 441 |
} |
| 442 |
|
| 443 |
int add(byte[] quals) { |
| 444 |
reads++; |
| 445 |
bases+=quals.length; |
| 446 |
int q30=quals.length; |
| 447 |
int q30Count=0; |
| 448 |
|
| 449 |
for(int i=0; i<quals.length; i++) { |
| 450 |
final int q=quals[i]; |
| 451 |
|
| 452 |
qsum.increment(i, q); |
| 453 |
qcount.increment(i, 1); |
| 454 |
if(q>=30) { |
| 455 |
q30Count++; |
| 456 |
}else if(i<q30){ |
| 457 |
q30=i; |
| 458 |
} |
| 459 |
} |
| 460 |
q30PositionSum+=q30; |
| 461 |
q30CountSum+=q30Count; |
| 462 |
return q30; |
| 463 |
} |
| 464 |
|
| 465 |
long yield() {return bases;} |
| 466 |
long yieldQ30() {return q30CountSum;} |
| 467 |
long qualityScoreSum() {return (long)qsum.sum();} |
| 468 |
double meanQualityScore() {return qsum.sum()/bases;} |
| 469 |
double fractionQ30() {return yieldQ30()/(double)yield();} |
| 470 |
|
| 471 |
|
| 472 |
ByteBuilder appendTo(ByteBuilder bb, int lane, int pairnum, |
| 473 |
String sampleID, byte delimiter, String barcode) { |
| 474 |
bb.append(lane); |
| 475 |
bb.comma().append(sampleID); |
| 476 |
Barcode.appendIndex(bb.comma(), delimiter, 1, barcode); |
| 477 |
Barcode.appendIndex(bb.comma(), delimiter, 2, barcode); |
| 478 |
bb.comma().append(pairnum+1); |
| 479 |
bb.comma().append(yield()); |
| 480 |
bb.comma().append(yieldQ30()); |
| 481 |
bb.comma().append(qualityScoreSum()); |
| 482 |
bb.comma().append(meanQualityScore(),2); |
| 483 |
bb.comma().append(fractionQ30(),2); |
| 484 |
return bb; |
| 485 |
} |
| 486 |
|
| 487 |
static String header() { |
| 488 |
return "Lane,SampleID,index,index2,ReadNumber," |
| 489 |
+ "Yield,YieldQ30,QualityScoreSum,Mean Quality Score (PF),% Q30"; |
| 490 |
} |
| 491 |
|
| 492 |
long reads=0; |
| 493 |
long bases=0; |
| 494 |
long q30PositionSum=0; |
| 495 |
long q30CountSum=0; |
| 496 |
LongList qsum=new LongList(); |
| 497 |
LongList qcount=new LongList(); |
| 498 |
} |
| 499 |
|
| 500 |
private long totalR1=0; |
| 501 |
private long totalR2=0; |
| 502 |
private long totalR1Assigned=0; |
| 503 |
private long totalR2Assigned=0; |
| 504 |
|
| 505 |
Collection<Barcode> counts=null; |
| 506 |
final HashMap<String, QTag[]> tagMap=new HashMap<String, QTag[]>(); |
| 507 |
private final static String UNDETERMINED="Undetermined";; |
| 508 |
LinkedHashMap<Integer, LinkedHashMap<String, Barcode>> tileMap=new LinkedHashMap<Integer, LinkedHashMap<String, Barcode>>(); |
| 509 |
final IlluminaHeaderParser2 ihp=new IlluminaHeaderParser2(); |
| 510 |
|
| 511 |
|
| 512 |
} |