| 1 |
package var2; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.concurrent.ArrayBlockingQueue; |
| 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.Parse; |
| 14 |
import shared.Parser; |
| 15 |
import shared.PreParser; |
| 16 |
import shared.Shared; |
| 17 |
import shared.Timer; |
| 18 |
import shared.Tools; |
| 19 |
import stream.ConcurrentGenericReadInputStream; |
| 20 |
import stream.FastaReadInputStream; |
| 21 |
import structures.ByteBuilder; |
| 22 |
import structures.ListNum; |
| 23 |
import structures.StringPair; |
| 24 |
import var2.CallVariants2.Sample; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class MergeSamples { |
| 32 |
|
| 33 |
public static void main(String[] args){ |
| 34 |
Timer t=new Timer(); |
| 35 |
MergeSamples x=new MergeSamples(args); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
Shared.closeStream(x.outstream); |
| 40 |
} |
| 41 |
|
| 42 |
public MergeSamples(){ |
| 43 |
threads=Shared.threads(); |
| 44 |
inq=new ArrayBlockingQueue<ListNum<VCFLine[]>>(threads+1); |
| 45 |
} |
| 46 |
|
| 47 |
public MergeSamples(String[] args){ |
| 48 |
|
| 49 |
{ |
| 50 |
PreParser pp=new PreParser(args, getClass(), false); |
| 51 |
args=pp.args; |
| 52 |
outstream=pp.outstream; |
| 53 |
} |
| 54 |
|
| 55 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 56 |
ReadWrite.setZipThreads(Shared.threads()); |
| 57 |
|
| 58 |
Parser parser=new Parser(); |
| 59 |
for(int i=0; i<args.length; i++){ |
| 60 |
String arg=args[i]; |
| 61 |
String[] split=arg.split("="); |
| 62 |
String a=split[0].toLowerCase(); |
| 63 |
String b=split.length>1 ? split[1] : null; |
| 64 |
|
| 65 |
if(parser.parse(arg, a, b)){ |
| 66 |
|
| 67 |
}else if(a.equals("invalid")){ |
| 68 |
outInvalid=b; |
| 69 |
}else if(a.equals("lines")){ |
| 70 |
maxLines=Long.parseLong(b); |
| 71 |
if(maxLines<0){maxLines=Long.MAX_VALUE;} |
| 72 |
}else if(a.equals("verbose")){ |
| 73 |
verbose=Parse.parseBoolean(b); |
| 74 |
ByteFile1.verbose=verbose; |
| 75 |
ByteFile2.verbose=verbose; |
| 76 |
stream.FastaReadInputStream.verbose=verbose; |
| 77 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 78 |
stream.FastqReadInputStream.verbose=verbose; |
| 79 |
ReadWrite.verbose=verbose; |
| 80 |
}else{ |
| 81 |
outstream.println("Unknown parameter "+args[i]); |
| 82 |
assert(false) : "Unknown parameter "+args[i]; |
| 83 |
|
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
{ |
| 88 |
overwrite=parser.overwrite; |
| 89 |
append=parser.append; |
| 90 |
|
| 91 |
in1=parser.in1; |
| 92 |
|
| 93 |
out1=parser.out1; |
| 94 |
} |
| 95 |
|
| 96 |
assert(FastaReadInputStream.settingsOK()); |
| 97 |
|
| 98 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 99 |
|
| 100 |
if(!ByteFile.FORCE_MODE_BF2){ |
| 101 |
ByteFile.FORCE_MODE_BF2=false; |
| 102 |
ByteFile.FORCE_MODE_BF1=true; |
| 103 |
} |
| 104 |
|
| 105 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 106 |
|
| 107 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 108 |
outstream.println((out1==null)+", "+out1); |
| 109 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n"); |
| 110 |
} |
| 111 |
threads=Shared.threads(); |
| 112 |
inq=new ArrayBlockingQueue<ListNum<VCFLine[]>>(threads+1); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public void mergeSamples(ArrayList<Sample> list, ScafMap scafMap, String outVcf, String scoreHistFile){ |
| 118 |
map=scafMap; |
| 119 |
ArrayList<StringPair> vcfList=new ArrayList<StringPair>(list.size()); |
| 120 |
for(Sample s : list){vcfList.add(new StringPair(s.name, s.vcfName));} |
| 121 |
mergeFiles(vcfList, outVcf, scoreHistFile); |
| 122 |
} |
| 123 |
|
| 124 |
public void mergeFiles(ArrayList<StringPair> list, String outVcf, String scoreHistFile){ |
| 125 |
System.err.println("Merging "+list); |
| 126 |
final int ways=list.size(); |
| 127 |
ByteFile[] bfa=new ByteFile[ways]; |
| 128 |
final boolean allowSubprocess=(ways<=4); |
| 129 |
for(int i=0; i<ways; i++){ |
| 130 |
StringPair pair=list.get(i); |
| 131 |
FileFormat ff=FileFormat.testInput(pair.b, FileFormat.VCF, null, allowSubprocess, false); |
| 132 |
bfa[i]=ByteFile.makeByteFile(ff); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
mergeMT(outVcf, bfa); |
| 139 |
|
| 140 |
if(scoreHistFile!=null){ |
| 141 |
CallVariants.writeScoreHist(scoreHistFile, scoreArray); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
private void mergeST(String outVcf, ByteFile[] bfa){ |
| 148 |
ByteStreamWriter bswVcf=null; |
| 149 |
if(outVcf!=null){ |
| 150 |
bswVcf=new ByteStreamWriter(outVcf, true, false, true, FileFormat.VCF); |
| 151 |
bswVcf.start(); |
| 152 |
} |
| 153 |
|
| 154 |
ByteBuilder bb=new ByteBuilder(34000); |
| 155 |
VCFLine[] row=processRow(bfa, bb); |
| 156 |
while(row!=null){ |
| 157 |
|
| 158 |
if(row[0]!=null){ |
| 159 |
VCFLine merged=merge(row); |
| 160 |
merged.toText(bb); |
| 161 |
bb.nl(); |
| 162 |
if(bb.length>32000){ |
| 163 |
if(bswVcf!=null){bswVcf.print(bb);} |
| 164 |
bb=new ByteBuilder(34000); |
| 165 |
} |
| 166 |
} |
| 167 |
row=processRow(bfa, bb); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
if(bswVcf!=null){ |
| 172 |
if(bb.length>0){bswVcf.print(bb);} |
| 173 |
bswVcf.poisonAndWait(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
private void mergeMT(String outVcf, ByteFile[] bfa){ |
| 178 |
ByteStreamWriter bswVcf=null; |
| 179 |
if(outVcf!=null){ |
| 180 |
FileFormat ff=FileFormat.testOutput(outVcf, FileFormat.VCF, null, true, true, append, true); |
| 181 |
bswVcf=new ByteStreamWriter(ff); |
| 182 |
bswVcf.start(); |
| 183 |
} |
| 184 |
|
| 185 |
ArrayList<MergeThread> alpt=spawnThreads(bswVcf); |
| 186 |
|
| 187 |
long nextID=0; |
| 188 |
ByteBuilder header=new ByteBuilder(34000); |
| 189 |
|
| 190 |
VCFLine[] row=processRow(bfa, header); |
| 191 |
while(row!=null && row[0]==null){ |
| 192 |
row=processRow(bfa, header); |
| 193 |
} |
| 194 |
if(bswVcf!=null){ |
| 195 |
bswVcf.add(header, nextID); |
| 196 |
nextID++; |
| 197 |
} |
| 198 |
|
| 199 |
ListNum<VCFLine[]> list=new ListNum<VCFLine[]>(new ArrayList<VCFLine[]>(200), nextID); |
| 200 |
while(row!=null){ |
| 201 |
if(row[0]!=null){ |
| 202 |
list.add(row); |
| 203 |
if(list.size()>=200){ |
| 204 |
putList(list); |
| 205 |
nextID++; |
| 206 |
list=new ListNum<VCFLine[]>(new ArrayList<VCFLine[]>(200), nextID); |
| 207 |
} |
| 208 |
} |
| 209 |
row=processRow(bfa, header); |
| 210 |
} |
| 211 |
if(list.size()>0){ |
| 212 |
putList(list); |
| 213 |
nextID++; |
| 214 |
} |
| 215 |
|
| 216 |
putList(POISON_LIST); |
| 217 |
|
| 218 |
waitForFinish(alpt); |
| 219 |
|
| 220 |
if(bswVcf!=null){bswVcf.poisonAndWait();} |
| 221 |
} |
| 222 |
|
| 223 |
VCFLine[] processRow(ByteFile[] bfa, ByteBuilder bb){ |
| 224 |
byte[][] lines=new byte[bfa.length][]; |
| 225 |
for(int i=0; i<bfa.length; i++){ |
| 226 |
byte[] line=bfa[i].nextLine(); |
| 227 |
if(line==null){return null;} |
| 228 |
lines[i]=line; |
| 229 |
} |
| 230 |
|
| 231 |
VCFLine[] row=new VCFLine[bfa.length]; |
| 232 |
if(lines[0][0]=='#'){ |
| 233 |
processHeader(lines, bb); |
| 234 |
return row; |
| 235 |
} |
| 236 |
for(int i=0; i<lines.length; i++){ |
| 237 |
byte[] line=lines[i]; |
| 238 |
row[i]=new VCFLine(line); |
| 239 |
if(i>0){assert(row[i].pos==row[0].pos) : "\n"+row[0]+"\n"+row[i];} |
| 240 |
} |
| 241 |
return row; |
| 242 |
} |
| 243 |
|
| 244 |
void processHeader(byte[][] lines, ByteBuilder bb){ |
| 245 |
String[][] matrix=new String[lines.length][]; |
| 246 |
for(int i=0; i<lines.length; i++){ |
| 247 |
matrix[i]=new String(lines[i]).split("="); |
| 248 |
} |
| 249 |
|
| 250 |
if(matrix[0][0].equals("##ploidy")){ |
| 251 |
ploidy=Integer.parseInt(matrix[0][1]); |
| 252 |
bb.append("##ploidy="+ploidy+"\n"); |
| 253 |
}else if(matrix[0][0].equals("##reads")){ |
| 254 |
for(String[] split : matrix){ |
| 255 |
reads+=Long.parseLong(split[1]); |
| 256 |
} |
| 257 |
bb.append("##reads="+reads+"\n"); |
| 258 |
}else if(matrix[0][0].equals("##pairedReads")){ |
| 259 |
for(String[] split : matrix){ |
| 260 |
pairedReads+=Long.parseLong(split[1]); |
| 261 |
} |
| 262 |
bb.append("##pairedReads="+pairedReads+"\n"); |
| 263 |
}else if(matrix[0][0].equals("##properlyPairedReads")){ |
| 264 |
for(String[] split : matrix){ |
| 265 |
properlyPairedReads+=Long.parseLong(split[1]); |
| 266 |
} |
| 267 |
properPairRate=properlyPairedReads*1.0/(Tools.max(1, reads)); |
| 268 |
bb.append("##properlyPairedReads="+properlyPairedReads+"\n"); |
| 269 |
bb.append("##properPairRate="+Tools.format("%.4f\n", properPairRate)); |
| 270 |
}else if(matrix[0][0].equals("##properPairRate")){ |
| 271 |
|
| 272 |
}else if(matrix[0][0].equals("##totalQualityAvg")){ |
| 273 |
totalQualityAvg=0; |
| 274 |
for(String[] split : matrix){ |
| 275 |
totalQualityAvg+=Float.parseFloat(split[1]); |
| 276 |
} |
| 277 |
totalQualityAvg/=lines.length; |
| 278 |
bb.append("##totalQualityAvg="+Tools.format("%.4f\n", totalQualityAvg)); |
| 279 |
}else if(matrix[0][0].equals("##mapqAvg")){ |
| 280 |
mapqAvg=0; |
| 281 |
for(String[] split : matrix){ |
| 282 |
mapqAvg+=Float.parseFloat(split[1]); |
| 283 |
} |
| 284 |
mapqAvg/=lines.length; |
| 285 |
bb.append("##mapqAvg="+Tools.format("%.2f\n", mapqAvg)); |
| 286 |
}else if(matrix[0][0].equals("##readLengthAvg")){ |
| 287 |
readLengthAvg=0; |
| 288 |
for(String[] split : matrix){ |
| 289 |
readLengthAvg+=Float.parseFloat(split[1]); |
| 290 |
} |
| 291 |
readLengthAvg/=lines.length; |
| 292 |
bb.append("##readLengthAvg="+Tools.format("%.2f\n", readLengthAvg)); |
| 293 |
}else if(matrix[0][0].startsWith("#CHROM\tPOS\t")){ |
| 294 |
bb.append(lines[0]); |
| 295 |
for(int i=1; i<lines.length; i++){ |
| 296 |
String[] split=new String(lines[i]).split("\t"); |
| 297 |
bb.tab().append(split[split.length-1]); |
| 298 |
} |
| 299 |
bb.nl(); |
| 300 |
}else{ |
| 301 |
bb.append(lines[0]); |
| 302 |
bb.nl(); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
VCFLine merge(VCFLine[] row){ |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
Var sum=null; |
| 312 |
VCFLine best=null; |
| 313 |
for(VCFLine line : row){ |
| 314 |
if(best==null || line.qual>best.qual){best=line;} |
| 315 |
Var v=line.toVar(); |
| 316 |
assert(v!=null); |
| 317 |
if(sum==null){sum=v;} |
| 318 |
else{ |
| 319 |
sum.add(v); |
| 320 |
sum.addCoverage(v); |
| 321 |
} |
| 322 |
} |
| 323 |
assert(best!=null); |
| 324 |
assert(sum!=null) : row.length+", "+row[0]; |
| 325 |
|
| 326 |
|
| 327 |
ByteBuilder bb=sum.toVCF(new ByteBuilder(), properPairRate, totalQualityAvg, mapqAvg, readLengthAvg, ploidy, map, filter, trimWhitespace); |
| 328 |
VCFLine merged=new VCFLine(bb.toBytes()); |
| 329 |
merged.samples.clear(); |
| 330 |
for(VCFLine line : row){ |
| 331 |
merged.samples.addAll(line.samples); |
| 332 |
} |
| 333 |
if(merged.qual<best.qual){ |
| 334 |
merged.qual=best.qual; |
| 335 |
merged.filter=best.filter; |
| 336 |
} |
| 337 |
scoreArray[(int)merged.qual]++; |
| 338 |
return merged; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
final ListNum<VCFLine[]> takeList(){ |
| 346 |
ListNum<VCFLine[]> list=null; |
| 347 |
while(list==null){ |
| 348 |
try { |
| 349 |
list=inq.take(); |
| 350 |
} catch (InterruptedException e) { |
| 351 |
|
| 352 |
e.printStackTrace(); |
| 353 |
} |
| 354 |
} |
| 355 |
return list; |
| 356 |
} |
| 357 |
|
| 358 |
final void putList(ListNum<VCFLine[]> list){ |
| 359 |
while(list!=null){ |
| 360 |
try { |
| 361 |
inq.put(list); |
| 362 |
list=null; |
| 363 |
} catch (InterruptedException e) { |
| 364 |
|
| 365 |
e.printStackTrace(); |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
private ArrayList<MergeThread> spawnThreads(ByteStreamWriter bsw){ |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
ArrayList<MergeThread> alpt=new ArrayList<MergeThread>(threads); |
| 377 |
for(int i=0; i<threads; i++){ |
| 378 |
alpt.add(new MergeThread(bsw)); |
| 379 |
} |
| 380 |
if(verbose){outstream.println("Spawned threads.");} |
| 381 |
|
| 382 |
|
| 383 |
for(MergeThread pt : alpt){ |
| 384 |
pt.start(); |
| 385 |
} |
| 386 |
if(verbose){outstream.println("Started threads.");} |
| 387 |
|
| 388 |
|
| 389 |
return alpt; |
| 390 |
} |
| 391 |
|
| 392 |
private void waitForFinish(ArrayList<MergeThread> alpt){ |
| 393 |
|
| 394 |
boolean allSuccess=true; |
| 395 |
for(MergeThread pt : alpt){ |
| 396 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 397 |
try { |
| 398 |
|
| 399 |
pt.join(); |
| 400 |
} catch (InterruptedException e) { |
| 401 |
|
| 402 |
e.printStackTrace(); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
private class MergeThread extends Thread { |
| 409 |
|
| 410 |
MergeThread(ByteStreamWriter bsw_){ |
| 411 |
bsw=bsw_; |
| 412 |
} |
| 413 |
|
| 414 |
@Override |
| 415 |
public void run(){ |
| 416 |
ListNum<VCFLine[]> list=takeList(); |
| 417 |
while(list!=null && list!=POISON_LIST){ |
| 418 |
processList(list); |
| 419 |
list=takeList(); |
| 420 |
} |
| 421 |
putList(POISON_LIST); |
| 422 |
} |
| 423 |
|
| 424 |
private void processList(ListNum<VCFLine[]> list){ |
| 425 |
ByteBuilder bb=new ByteBuilder(4096); |
| 426 |
for(VCFLine[] row : list){ |
| 427 |
mergeRow(row, bb); |
| 428 |
} |
| 429 |
if(bsw!=null){bsw.add(bb, list.id);} |
| 430 |
} |
| 431 |
|
| 432 |
private void mergeRow(VCFLine[] row, ByteBuilder bb){ |
| 433 |
if(row[0]!=null){ |
| 434 |
VCFLine merged=merge(row); |
| 435 |
merged.toText(bb); |
| 436 |
bb.nl(); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
private final ByteStreamWriter bsw; |
| 441 |
|
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
final ListNum<VCFLine[]> POISON_LIST=new ListNum<VCFLine[]>(null, -1); |
| 447 |
private final ArrayBlockingQueue<ListNum<VCFLine[]>> inq; |
| 448 |
private final int threads; |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
long readsSum; |
| 453 |
long pairsSum; |
| 454 |
int ploidy=1; |
| 455 |
|
| 456 |
double properPairRate; |
| 457 |
double totalQualityAvg; |
| 458 |
double mapqAvg; |
| 459 |
double readLengthAvg; |
| 460 |
|
| 461 |
long reads; |
| 462 |
long pairedReads; |
| 463 |
long properlyPairedReads; |
| 464 |
|
| 465 |
VarFilter filter; |
| 466 |
ScafMap map; |
| 467 |
boolean trimWhitespace=true; |
| 468 |
|
| 469 |
private String in1=null; |
| 470 |
private String out1=null; |
| 471 |
private String outInvalid=null; |
| 472 |
|
| 473 |
long[] scoreArray=new long[200]; |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
private long linesProcessed=0; |
| 478 |
private long linesValid=0; |
| 479 |
private long bytesProcessed=0; |
| 480 |
|
| 481 |
private long maxLines=Long.MAX_VALUE; |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
private PrintStream outstream=System.err; |
| 487 |
public static boolean verbose=false; |
| 488 |
public boolean errorState=false; |
| 489 |
private boolean overwrite=true; |
| 490 |
private boolean append=false; |
| 491 |
|
| 492 |
} |