| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Arrays; |
| 7 |
import java.util.BitSet; |
| 8 |
import java.util.HashMap; |
| 9 |
|
| 10 |
import align2.QualityTools; |
| 11 |
import dna.ChromosomeArray; |
| 12 |
import dna.Data; |
| 13 |
import dna.Scaffold; |
| 14 |
import fileIO.ByteFile; |
| 15 |
import fileIO.ByteStreamWriter; |
| 16 |
import fileIO.FileFormat; |
| 17 |
import fileIO.ReadWrite; |
| 18 |
import fileIO.TextFile; |
| 19 |
import fileIO.TextStreamWriter; |
| 20 |
import shared.KillSwitch; |
| 21 |
import shared.LineParser1; |
| 22 |
import shared.Parse; |
| 23 |
import shared.Parser; |
| 24 |
import shared.PreParser; |
| 25 |
import shared.Shared; |
| 26 |
import shared.Timer; |
| 27 |
import shared.Tools; |
| 28 |
import shared.TrimRead; |
| 29 |
import stream.Read; |
| 30 |
import stream.SamLine; |
| 31 |
import stream.SamLineStreamer; |
| 32 |
import stream.ScaffoldCoordinates; |
| 33 |
import stream.SiteScore; |
| 34 |
import structures.ByteBuilder; |
| 35 |
import structures.CoverageArray; |
| 36 |
import structures.CoverageArray2; |
| 37 |
import structures.CoverageArray3; |
| 38 |
import structures.ListNum; |
| 39 |
import structures.LongList; |
| 40 |
import tracker.ReadStats; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class CoveragePileup { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static void main(String[] args){ |
| 54 |
Timer t=new Timer(); |
| 55 |
|
| 56 |
CoveragePileup x=new CoveragePileup(args); |
| 57 |
|
| 58 |
x.process(); |
| 59 |
|
| 60 |
t.stop(); |
| 61 |
if(!KEY_VALUE){ |
| 62 |
x.outstream.println(); |
| 63 |
x.outstream.println("Time: \t"+t); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
Shared.closeStream(x.outstream); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public CoveragePileup(String[] args){ |
| 75 |
|
| 76 |
{ |
| 77 |
PreParser pp=new PreParser(args, printCommand ? getClass() : null, false); |
| 78 |
args=pp.args; |
| 79 |
outstream=pp.outstream; |
| 80 |
} |
| 81 |
|
| 82 |
int vectorMode=-1; |
| 83 |
boolean outset=false; |
| 84 |
ReadWrite.USE_UNPIGZ=true; |
| 85 |
|
| 86 |
|
| 87 |
for(int i=0; i<args.length; i++){ |
| 88 |
final String arg=args[i]; |
| 89 |
final String[] split=arg.split("="); |
| 90 |
String a=split[0].toLowerCase(); |
| 91 |
String b=split.length>1 ? split[1] : null; |
| 92 |
|
| 93 |
if(a.equals("ref") || a.equals("reference") || a.equals("fasta")){ |
| 94 |
reference=b; |
| 95 |
}else if(a.equals("addfromref")){ |
| 96 |
ADD_FROM_REF=Parse.parseBoolean(b); |
| 97 |
}else if(a.equals("addfromreads")){ |
| 98 |
ADD_FROM_READS=Parse.parseBoolean(b); |
| 99 |
}else if(a.equals("in") || a.equals("in1")){ |
| 100 |
in1=b; |
| 101 |
}else if(a.equals("in2")){ |
| 102 |
in2=b; |
| 103 |
}else if(a.equals("covin") || a.equals("incov")){ |
| 104 |
covIn.clear(); |
| 105 |
if(b!=null) { |
| 106 |
for(String cf : Tools.commaPattern.split(b)) {covIn.add(cf);} |
| 107 |
} |
| 108 |
}else if(a.equals("out") || a.equals("coveragestats") || a.equals("covstats") || a.equals("stats")){ |
| 109 |
covstats=b; |
| 110 |
outset=true; |
| 111 |
}else if(a.equals("minscaf") || a.equals("covminscaf")){ |
| 112 |
minscaf=Integer.parseInt(b); |
| 113 |
}else if(a.equals("mindepth") || a.equals("mincov")){ |
| 114 |
minDepthToBeCovered=Integer.parseInt(b); |
| 115 |
}else if(a.equals("border")){ |
| 116 |
border=Integer.parseInt(b); |
| 117 |
}else if(a.equals("qtrim")){ |
| 118 |
if(b==null || b.length()==0){qtrimRight=qtrimLeft=true;} |
| 119 |
else if(b.equalsIgnoreCase("left") || b.equalsIgnoreCase("l")){qtrimLeft=true;qtrimRight=false;} |
| 120 |
else if(b.equalsIgnoreCase("right") || b.equalsIgnoreCase("r")){qtrimLeft=false;qtrimRight=true;} |
| 121 |
else if(b.equalsIgnoreCase("both") || b.equalsIgnoreCase("rl") || b.equalsIgnoreCase("lr")){qtrimLeft=qtrimRight=true;} |
| 122 |
else if(Tools.isDigit(b.charAt(0))){ |
| 123 |
trimq=Float.parseFloat(b); |
| 124 |
qtrimRight=trimq>0; |
| 125 |
}else{qtrimRight=qtrimLeft=Parse.parseBoolean(b);} |
| 126 |
}else if(a.equals("trimq") || a.equals("trimquality")){ |
| 127 |
trimq=Float.parseFloat(b); |
| 128 |
}else if(a.equals("ss") || a.equals("samstreamer")){ |
| 129 |
if(b!=null && Tools.isDigit(b.charAt(0))){ |
| 130 |
useStreamer=true; |
| 131 |
streamerThreads=Tools.max(1, Integer.parseInt(b)); |
| 132 |
}else{ |
| 133 |
useStreamer=Parse.parseBoolean(b); |
| 134 |
} |
| 135 |
}else if(a.equals("minq") || a.equals("minmapq")){ |
| 136 |
minMapq=Integer.parseInt(b); |
| 137 |
}else if(a.equals("outsam")){ |
| 138 |
outsam=b; |
| 139 |
}else if(a.equals("rpkm") || a.equals("fpkm") || a.equals("outrpkm")){ |
| 140 |
outrpkm=b; |
| 141 |
}else if(a.equals("outorf")){ |
| 142 |
outorf=b; |
| 143 |
}else if(a.equals("orffasta") || a.equals("fastaorf")){ |
| 144 |
orffasta=b; |
| 145 |
}else if(a.equals("basecov") || a.equals("outcov")){ |
| 146 |
basecov=b; |
| 147 |
}else if(a.equals("ranges") || a.equals("rangecov")){ |
| 148 |
rangecov=b; |
| 149 |
|
| 150 |
|
| 151 |
}else if(a.equals("bincov") || a.equals("outbinned")){ |
| 152 |
bincov=b; |
| 153 |
}else if(a.equals("normcov") || a.equals("outnormalized")){ |
| 154 |
normcov=b; |
| 155 |
}else if(a.equals("normcovo") || a.equals("outnormalizedoverall")){ |
| 156 |
normcovOverall=b; |
| 157 |
}else if(a.equals("delta")){ |
| 158 |
DELTA_ONLY=Parse.parseBoolean(b); |
| 159 |
}else if(a.equals("physical") || a.equals("physicalcoverage") || a.equals("physcov")){ |
| 160 |
PHYSICAL_COVERAGE=Parse.parseBoolean(b); |
| 161 |
}else if(a.equals("tlen")){ |
| 162 |
USE_TLEN=Parse.parseBoolean(b); |
| 163 |
}else if(a.equals("hist") || a.equals("histogram") || a.equals("covhist")){ |
| 164 |
histogram=b; |
| 165 |
}else if(a.equals("reads")){ |
| 166 |
maxReads=Parse.parseKMG(b); |
| 167 |
}else if(a.equals("scafs") || a.equals("scaffolds")){ |
| 168 |
initialScaffolds=Tools.max(128, (int)(Tools.min(Long.parseLong(b),2000000000))); |
| 169 |
}else if(a.equals("binsize")){ |
| 170 |
binsize=Integer.parseInt(b); |
| 171 |
}else if(a.equals("32bit")){ |
| 172 |
bits32=Parse.parseBoolean(b); |
| 173 |
}else if(a.equals("bitset") || a.equals("usebitset") || a.equals("bitsets") || a.equals("usebitsets")){ |
| 174 |
|
| 175 |
vectorMode=Parse.parseBoolean(b) ? BITSET_MODE : NOTHING_MODE; |
| 176 |
}else if(a.equals("array") || a.equals("arrays") || a.equals("usearrays")){ |
| 177 |
vectorMode=Parse.parseBoolean(b) ? ARRAY_MODE : NOTHING_MODE; |
| 178 |
}else if(a.equals("median") || a.equals("calcmedian")){ |
| 179 |
if(Parse.parseBoolean(b)){ |
| 180 |
vectorMode=ARRAY_MODE; |
| 181 |
} |
| 182 |
}else if(a.startsWith("nonzero") || a.equals("nzo")){ |
| 183 |
NONZERO_ONLY=Parse.parseBoolean(b); |
| 184 |
if(verbose){outstream.println("Set NONZERO_ONLY to "+NONZERO_ONLY);} |
| 185 |
}else if(a.equals("append") || a.equals("app")){ |
| 186 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 187 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 188 |
overwrite=Parse.parseBoolean(b); |
| 189 |
if(verbose){outstream.println("Set overwrite to "+overwrite);} |
| 190 |
}else if(a.equalsIgnoreCase("twocolumn")){ |
| 191 |
TWOCOLUMN=Parse.parseBoolean(b); |
| 192 |
if(verbose){outstream.println("Set TWOCOLUMN to "+TWOCOLUMN);} |
| 193 |
}else if(a.equalsIgnoreCase("keyvalue") || a.equalsIgnoreCase("machineout")){ |
| 194 |
KEY_VALUE=Parse.parseBoolean(b); |
| 195 |
}else if(a.equalsIgnoreCase("countgc")){ |
| 196 |
COUNT_GC=Parse.parseBoolean(b); |
| 197 |
if(verbose){outstream.println("Set COUNT_GC to "+COUNT_GC);} |
| 198 |
}else if(a.equals("secondary") || a.equals("usesecondary")){ |
| 199 |
USE_SECONDARY=Parse.parseBoolean(b); |
| 200 |
if(verbose){outstream.println("Set USE_SECONDARY_ALIGNMENTS to "+USE_SECONDARY);} |
| 201 |
}else if(a.equals("softclip") || a.equals("includesoftclip")){ |
| 202 |
INCLUDE_SOFT_CLIP=Parse.parseBoolean(b); |
| 203 |
if(verbose){outstream.println("Set INCLUDE_SOFT_CLIP to "+INCLUDE_SOFT_CLIP);} |
| 204 |
}else if(a.equals("keepshortbins") || a.equals("ksb")){ |
| 205 |
KEEP_SHORT_BINS=Parse.parseBoolean(b); |
| 206 |
if(verbose){outstream.println("Set KEEP_SHORT_BINS to "+KEEP_SHORT_BINS);} |
| 207 |
}else if(a.equals("strandedcoverage") || a.equals("strandedcov") || a.equals("covstranded") || a.equals("stranded")){ |
| 208 |
STRANDED=Parse.parseBoolean(b); |
| 209 |
}else if(a.equals("startcov") || a.equals("covstart") || a.equals("startonly")){ |
| 210 |
START_ONLY=Parse.parseBoolean(b); |
| 211 |
}else if(a.equals("stopcov") || a.equals("covstop") || a.equals("stoponly")){ |
| 212 |
STOP_ONLY=Parse.parseBoolean(b); |
| 213 |
}else if(a.equals("concise")){ |
| 214 |
CONCISE=Parse.parseBoolean(b); |
| 215 |
}else if(a.equals("verbose")){ |
| 216 |
verbose=Parse.parseBoolean(b); |
| 217 |
}else if(a.equals("normc") || a.equals("normalizecoverage")){ |
| 218 |
NORMALIZE_COVERAGE=Parse.parseBoolean(b); |
| 219 |
}else if(a.equals("header") || a.equals("hdr")){ |
| 220 |
printHeader=Parse.parseBoolean(b); |
| 221 |
}else if(a.equals("headerpound") || a.equals("#")){ |
| 222 |
headerPound=Parse.parseBoolean(b); |
| 223 |
}else if(a.equals("stdev")){ |
| 224 |
calcCovStdev=Parse.parseBoolean(b); |
| 225 |
}else if(a.equals("delcov") || a.equals("dels") || a.equals("includedels") || a.equals("includedeletions") || a.equals("delcoverage")){ |
| 226 |
INCLUDE_DELETIONS=Parse.parseBoolean(b); |
| 227 |
}else if(a.equals("dupecoverage") || a.equals("dupecov") || a.equals("dupes") || a.equals("duplicates") || a.equals("includeduplicates")){ |
| 228 |
INCLUDE_DUPLICATES=Parse.parseBoolean(b); |
| 229 |
}else if(a.equals("ignoredupes") || a.equals("ignoreduplicates")){ |
| 230 |
INCLUDE_DUPLICATES=!Parse.parseBoolean(b); |
| 231 |
}else if(a.equals("normb") || a.equals("normalizebins")){ |
| 232 |
try { |
| 233 |
NORMALIZE_LENGTH_BINS=Integer.parseInt(b); |
| 234 |
} catch (NumberFormatException e) { |
| 235 |
boolean x=Parse.parseBoolean(b); |
| 236 |
NORMALIZE_LENGTH_BINS=x ? 100 : -1; |
| 237 |
} |
| 238 |
}else if(a.equals("covwindow")){ |
| 239 |
if(b==null || b.length()<1 || Character.isLetter(b.charAt(0))){ |
| 240 |
USE_WINDOW=Parse.parseBoolean(b); |
| 241 |
}else{ |
| 242 |
LOW_COV_WINDOW=Integer.parseInt(b); |
| 243 |
USE_WINDOW=(LOW_COV_WINDOW>0); |
| 244 |
} |
| 245 |
}else if(a.equals("covwindowavg")){ |
| 246 |
LOW_COV_DEPTH=Double.parseDouble(b); |
| 247 |
}else if(a.equals("k")){ |
| 248 |
k=Integer.parseInt(b); |
| 249 |
}else if(Parser.parseCommonStatic(arg, a, b)){ |
| 250 |
|
| 251 |
}else if(Parser.parseZip(arg, a, b)){ |
| 252 |
|
| 253 |
}else if(in1==null && arg.indexOf('=')<0 && new File(arg).exists()){ |
| 254 |
in1=arg; |
| 255 |
}else{ |
| 256 |
throw new RuntimeException("Unknown parameter: "+args[i]); |
| 257 |
} |
| 258 |
|
| 259 |
} |
| 260 |
trimE=(float)QualityTools.phredToProbError(trimq); |
| 261 |
|
| 262 |
|
| 263 |
ReadWrite.SAMTOOLS_IGNORE_FLAG|=ReadWrite.SAM_UNMAPPED; |
| 264 |
if(!USE_SECONDARY){ReadWrite.SAMTOOLS_IGNORE_FLAG|=ReadWrite.SAM_SECONDARY;} |
| 265 |
if(!INCLUDE_DUPLICATES){ReadWrite.SAMTOOLS_IGNORE_FLAG|=ReadWrite.SAM_DUPLICATE;} |
| 266 |
|
| 267 |
if(outsam==null){ |
| 268 |
SamLine.PARSE_0=false; |
| 269 |
SamLine.PARSE_6=false; |
| 270 |
SamLine.PARSE_7=false; |
| 271 |
SamLine.PARSE_8=false; |
| 272 |
if(k<1 && trimq<1){SamLine.PARSE_10=false;} |
| 273 |
SamLine.PARSE_OPTIONAL=false; |
| 274 |
} |
| 275 |
|
| 276 |
if(vectorMode>-1){ |
| 277 |
USE_BITSETS=(vectorMode==BITSET_MODE); |
| 278 |
USE_COVERAGE_ARRAYS=(vectorMode==ARRAY_MODE); |
| 279 |
}else{ |
| 280 |
if(histogram==null && basecov==null && bincov==null && normcov==null && rangecov==null && |
| 281 |
normcovOverall==null && outorf==null && !calcCovStdev){ |
| 282 |
USE_COVERAGE_ARRAYS=false; |
| 283 |
if(TWOCOLUMN){ |
| 284 |
USE_BITSETS=false; |
| 285 |
}else{ |
| 286 |
USE_BITSETS=true; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if(verbose){ |
| 292 |
outstream.println("Set USE_COVERAGE_ARRAYS to "+USE_COVERAGE_ARRAYS); |
| 293 |
outstream.println("Set USE_BITSETS to "+USE_BITSETS); |
| 294 |
} |
| 295 |
|
| 296 |
if(maxReads<0){maxReads=Long.MAX_VALUE;} |
| 297 |
{ |
| 298 |
final String a=(args.length>0 ? args[0] : null); |
| 299 |
|
| 300 |
if(in1==null && a!=null && a.indexOf('=')<0 && (a.startsWith("stdin") || new File(a).exists())){in1=a;} |
| 301 |
|
| 302 |
if(in1==null){in1="stdin";} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
} |
| 309 |
assert(in1!=null); |
| 310 |
|
| 311 |
|
| 312 |
if(STRANDED){ |
| 313 |
assert(basecov==null || basecov.indexOf('#')>=0) : "Output filenames must contain '#' symbol for strand-specific output."; |
| 314 |
assert(bincov==null || bincov.indexOf('#')>=0) : "Output filenames must contain '#' symbol for strand-specific output."; |
| 315 |
assert(normcov==null || normcov.indexOf('#')>=0) : "Output filenames must contain '#' symbol for strand-specific output."; |
| 316 |
assert(normcovOverall==null || normcovOverall.indexOf('#')>=0) : "Output filenames must contain '#' symbol for strand-specific output."; |
| 317 |
assert(histogram==null || histogram.indexOf('#')>=0) : "Output filenames must contain '#' symbol for strand-specific output."; |
| 318 |
assert(covstats==null || covstats.indexOf('#')>=0) : "Output filenames must contain '#' symbol for strand-specific output."; |
| 319 |
} |
| 320 |
|
| 321 |
if(!Tools.testInputFiles(false, true, in1, in2, reference)){ |
| 322 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 323 |
} |
| 324 |
|
| 325 |
if(!Tools.testOutputFiles(overwrite, append, false, basecov, rangecov, bincov, normcov, normcovOverall, histogram, covstats, outrpkm)){ |
| 326 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+ |
| 327 |
basecov+", "+rangecov+", "+bincov+", "+normcov+", "+normcovOverall+", "+histogram+", "+covstats+", "+outrpkm+"\n"); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public void clear(){ |
| 339 |
list=null; |
| 340 |
table=null; |
| 341 |
pairTable=null; |
| 342 |
|
| 343 |
program=null; |
| 344 |
version=null; |
| 345 |
|
| 346 |
in1=null; |
| 347 |
covstats=null; |
| 348 |
outsam=null; |
| 349 |
outorf=null; |
| 350 |
outrpkm=null; |
| 351 |
reference=null; |
| 352 |
histogram=null; |
| 353 |
basecov=null; |
| 354 |
bincov=null; |
| 355 |
normcov=null; |
| 356 |
normcovOverall=null; |
| 357 |
orffasta=null; |
| 358 |
|
| 359 |
error=false; |
| 360 |
|
| 361 |
refBases=0; |
| 362 |
mappedBases=0; |
| 363 |
mappedNonClippedBases=0; |
| 364 |
mappedBasesWithDels=0; |
| 365 |
mappedReads=0; |
| 366 |
properPairs=0; |
| 367 |
readsProcessed=0; |
| 368 |
basesProcessed=0; |
| 369 |
kmersProcessed=0; |
| 370 |
mappedKmers=0; |
| 371 |
totalCoveredBases1=0; |
| 372 |
totalCoveredBases2=0; |
| 373 |
scaffoldsWithCoverage1=0; |
| 374 |
scaffoldsWithCoverage2=0; |
| 375 |
totalScaffolds=0; |
| 376 |
} |
| 377 |
|
| 378 |
public void createDataStructures(){ |
| 379 |
refBases=0; |
| 380 |
mappedBases=0; |
| 381 |
mappedNonClippedBases=0; |
| 382 |
mappedBasesWithDels=0; |
| 383 |
mappedReads=0; |
| 384 |
properPairs=0; |
| 385 |
readsProcessed=0; |
| 386 |
basesProcessed=0; |
| 387 |
kmersProcessed=0; |
| 388 |
mappedKmers=0; |
| 389 |
totalCoveredBases1=0; |
| 390 |
totalCoveredBases2=0; |
| 391 |
scaffoldsWithCoverage1=0; |
| 392 |
scaffoldsWithCoverage2=0; |
| 393 |
totalScaffolds=0; |
| 394 |
error=false; |
| 395 |
list=new ArrayList<Scaffold>(initialScaffolds); |
| 396 |
table=new HashMap<String, Scaffold>(initialScaffolds); |
| 397 |
|
| 398 |
if(PHYSICAL_COVERAGE){ |
| 399 |
pairTable=new HashMap<String, SamLine>(); |
| 400 |
if(COUNT_GC){ |
| 401 |
COUNT_GC=false; |
| 402 |
outstream.println("COUNT_GC disabled for physical coverage mode."); |
| 403 |
} |
| 404 |
if(USE_SECONDARY){ |
| 405 |
USE_SECONDARY=false; |
| 406 |
outstream.println("USE_SECONDARY disabled for physical coverage mode."); |
| 407 |
} |
| 408 |
|
| 409 |
SamLine.PARSE_0=true; |
| 410 |
SamLine.PARSE_6=true; |
| 411 |
SamLine.PARSE_7=true; |
| 412 |
SamLine.PARSE_8=true; |
| 413 |
SamLine.PARSE_10=false; |
| 414 |
SamLine.PARSE_OPTIONAL=false; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
public void process(){ |
| 425 |
createDataStructures(); |
| 426 |
|
| 427 |
|
| 428 |
if(in1!=null && FileFormat.isBamFile(in1)){ |
| 429 |
if(Data.SAMTOOLS() && useStreamer){ReadWrite.USE_SAMBAMBA=false;} |
| 430 |
} |
| 431 |
ByteFile tf=ByteFile.makeByteFile(in1, false); |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
final ByteStreamWriter tsw=(outsam==null ? null : new ByteStreamWriter(outsam, overwrite, false, true)); |
| 436 |
if(tsw!=null){tsw.start();} |
| 437 |
ReadWrite.USE_SAMBAMBA=true; |
| 438 |
|
| 439 |
|
| 440 |
processHeader(tf, tsw); |
| 441 |
|
| 442 |
|
| 443 |
processReference(); |
| 444 |
|
| 445 |
if(maxReads<0){maxReads=Long.MAX_VALUE;} |
| 446 |
|
| 447 |
if(covIn!=null && !covIn.isEmpty()) { |
| 448 |
for(String s : covIn) { |
| 449 |
loadCoverageFile(s); |
| 450 |
} |
| 451 |
}else if(useStreamer && !FileFormat.isStdio(in1)){ |
| 452 |
tf.close(); |
| 453 |
|
| 454 |
processViaStreamer(tsw); |
| 455 |
|
| 456 |
}else{ |
| 457 |
|
| 458 |
processViaByteFile(tf, tsw); |
| 459 |
|
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
printOutput(); |
| 464 |
|
| 465 |
if(orffasta!=null){ |
| 466 |
processOrfsFasta(orffasta, outorf, table); |
| 467 |
} |
| 468 |
|
| 469 |
if(tsw!=null){tsw.waitForFinish();} |
| 470 |
} |
| 471 |
|
| 472 |
private void processViaStreamer(ByteStreamWriter tsw){ |
| 473 |
SamLineStreamer ss=new SamLineStreamer(in1, streamerThreads, false, maxReads); |
| 474 |
ss.start(); |
| 475 |
ByteBuilder bb=new ByteBuilder(33000); |
| 476 |
for(ListNum<SamLine> ln=ss.nextLines(); ln!=null && ln.size()>0; ln=ss.nextLines()){ |
| 477 |
ArrayList<SamLine> list=(ln==null ? null : ln.list); |
| 478 |
for(SamLine sl : list){ |
| 479 |
if(tsw!=null){ |
| 480 |
sl.toBytes(bb); |
| 481 |
bb.nl(); |
| 482 |
if(bb.length>=16384){ |
| 483 |
tsw.print(bb); |
| 484 |
bb.clear(); |
| 485 |
} |
| 486 |
} |
| 487 |
processSamLine(sl); |
| 488 |
} |
| 489 |
} |
| 490 |
if(tsw!=null){ |
| 491 |
if(bb.length>0){tsw.print(bb);} |
| 492 |
tsw.poison(); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
private void processViaByteFile(ByteFile tf, ByteStreamWriter tsw){ |
| 497 |
for(byte[] line=tf.nextLine(); line!=null && readsProcessed<maxReads; line=tf.nextLine()){ |
| 498 |
if(tsw!=null){tsw.println(line);} |
| 499 |
processSamLine(line); |
| 500 |
} |
| 501 |
|
| 502 |
tf.close(); |
| 503 |
if(tsw!=null){tsw.poison();} |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
private void loadCoverageFile(String fname) { |
| 509 |
ByteFile bf=ByteFile.makeByteFile(fname, true); |
| 510 |
|
| 511 |
byte[] line=bf.nextLine(); |
| 512 |
for(; line!=null && Tools.startsWith(line, '#'); line=bf.nextLine()) {} |
| 513 |
if(line==null) {return;} |
| 514 |
lp.set(line); |
| 515 |
String name=lp.parseString(0); |
| 516 |
Scaffold scaf=table.get(name); |
| 517 |
|
| 518 |
for(; line!=null; line=bf.nextLine()) { |
| 519 |
assert(!Tools.startsWith(line, '#')); |
| 520 |
lp.set(line); |
| 521 |
int len=lp.length(0); |
| 522 |
if(len>0 && !lp.termEquals(name, 0)) { |
| 523 |
name=lp.parseString(0); |
| 524 |
scaf=table.get(name); |
| 525 |
} |
| 526 |
int pos=lp.parseInt(1); |
| 527 |
int cov=lp.parseInt(2); |
| 528 |
if(cov>0) { |
| 529 |
if(USE_COVERAGE_ARRAYS){ |
| 530 |
if(scaf.obj0==null){ |
| 531 |
scaf.obj0=(bits32 ? new CoverageArray3(table.size(), scaf.length) : new CoverageArray2(table.size(), scaf.length)); |
| 532 |
} |
| 533 |
CoverageArray ca=(CoverageArray)scaf.obj0; |
| 534 |
ca.increment(pos, cov); |
| 535 |
}else if(USE_BITSETS){ |
| 536 |
if(scaf.obj0==null){scaf.obj0=new BitSet(scaf.length+1);} |
| 537 |
BitSet bs=(BitSet)scaf.obj0; |
| 538 |
bs.set(pos); |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
public void processHeader(ByteFile tf, ByteStreamWriter tsw){ |
| 553 |
byte[] line=null; |
| 554 |
for(line=tf.nextLine(); line!=null && (line.length==0 || line[0]=='@'); line=tf.nextLine()){ |
| 555 |
|
| 556 |
if(tsw!=null){tsw.println(line);} |
| 557 |
|
| 558 |
if(line.length>2){ |
| 559 |
final byte a=line[1], b=line[2]; |
| 560 |
|
| 561 |
if(a=='S' && b=='Q'){ |
| 562 |
lp.set(line); |
| 563 |
Scaffold scaf=new Scaffold(lp); |
| 564 |
if(COUNT_GC){scaf.basecount=KillSwitch.allocLong1D(8);} |
| 565 |
assert(!table.containsKey(scaf.name)) : "\nDuplicate scaffold name!\n"+scaf+"\n\n"+table.get(scaf.name); |
| 566 |
table.put(scaf.name, scaf); |
| 567 |
list.add(scaf); |
| 568 |
refBases+=scaf.length; |
| 569 |
|
| 570 |
|
| 571 |
}else if(a=='P' && b=='G'){ |
| 572 |
lp.set(line); |
| 573 |
for(int i=1, terms=lp.terms(); i<terms; i++) { |
| 574 |
if(lp.termStartsWith("PN:", i)){ |
| 575 |
if(program==null){ |
| 576 |
lp.incrementA(3); |
| 577 |
program=lp.parseStringFromCurrentField(); |
| 578 |
} |
| 579 |
}else if(lp.termStartsWith("VN:", i)){ |
| 580 |
if(version==null){ |
| 581 |
lp.incrementA(3); |
| 582 |
version=lp.parseStringFromCurrentField(); |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
}else if(a=='R' && b=='G'){ |
| 587 |
|
| 588 |
}else if(a=='H' && b=='D'){ |
| 589 |
|
| 590 |
}else if(a=='C' && b=='O'){ |
| 591 |
|
| 592 |
}else{ |
| 593 |
|
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
if(line!=null){tf.pushBack(line);} |
| 598 |
|
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
public void loadScaffoldsFromIndex(int minChrom, int maxChrom){ |
| 603 |
|
| 604 |
final int[][] lengths=Data.scaffoldLengths; |
| 605 |
final int[][] locs=Data.scaffoldLocs; |
| 606 |
final byte[][][] names=Data.scaffoldNames; |
| 607 |
final int[] counts=new int[8]; |
| 608 |
for(int chrom=minChrom; chrom<=maxChrom; chrom++){ |
| 609 |
final ChromosomeArray ca=Data.getChromosome(chrom); |
| 610 |
|
| 611 |
if(lengths[chrom]!=null){ |
| 612 |
final int[] clengths=lengths[chrom]; |
| 613 |
final int[] clocs=locs[chrom]; |
| 614 |
final byte[][] cnames=names[chrom]; |
| 615 |
for(int idx=0; idx<clengths.length; idx++){ |
| 616 |
final int length=clengths[idx]; |
| 617 |
final int loc=clocs[idx]; |
| 618 |
final String name=new String(cnames[idx]); |
| 619 |
final Scaffold scaf=new Scaffold(name, length); |
| 620 |
if(ca!=null){ |
| 621 |
scaf.gc=ca.calcGC(loc, length, counts); |
| 622 |
} |
| 623 |
if(COUNT_GC){scaf.basecount=KillSwitch.allocLong1D(8);} |
| 624 |
assert(!table.containsKey(scaf.name)) : "\nDuplicate scaffold name!\n"+scaf+"\n\n"+table.get(scaf.name); |
| 625 |
table.put(scaf.name, scaf); |
| 626 |
list.add(scaf); |
| 627 |
refBases+=scaf.length; |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
public void processReference(){ |
| 635 |
if(reference==null){return;} |
| 636 |
|
| 637 |
ByteFile bf=ByteFile.makeByteFile(reference, false); |
| 638 |
Scaffold scaf=null; |
| 639 |
int len=0; |
| 640 |
final long[] acgtn=KillSwitch.allocLong1D(8); |
| 641 |
boolean addLen=false; |
| 642 |
for(byte[] s=bf.nextLine(); s!=null; s=bf.nextLine()){ |
| 643 |
if(s.length>0 && s[0]=='>'){ |
| 644 |
if(scaf!=null){ |
| 645 |
if(scaf.length>0 && scaf.length!=len){ |
| 646 |
outstream.println("ERROR: Scaffold "+scaf.name+" has contradictory lengths of "+scaf.length+" and "+len+"\n" |
| 647 |
+ "This probably indicates a corrupt or incorrect reference."); |
| 648 |
errorState=true; |
| 649 |
if(Shared.EA()){KillSwitch.kill();} |
| 650 |
} |
| 651 |
|
| 652 |
scaf.length=len; |
| 653 |
if(addLen){ |
| 654 |
refBases+=scaf.length; |
| 655 |
addLen=false; |
| 656 |
} |
| 657 |
scaf.gc=(float)((acgtn[1]+acgtn[2])*1d/Data.max(1, acgtn[0]+acgtn[1]+acgtn[2]+acgtn[3])); |
| 658 |
scaf=null; |
| 659 |
len=0; |
| 660 |
Arrays.fill(acgtn, 0); |
| 661 |
} |
| 662 |
|
| 663 |
String name=new String(s, 1, s.length-1); |
| 664 |
scaf=table.get(name); |
| 665 |
if(ADD_FROM_REF && scaf==null){ |
| 666 |
scaf=new Scaffold(name, 0); |
| 667 |
if(!warned){ |
| 668 |
outstream.println("Warning - SAM header did not include "+name+"\nAbsent scaffolds will be added; further warnings will be suppressed."); |
| 669 |
warned=true; |
| 670 |
} |
| 671 |
if(COUNT_GC){scaf.basecount=KillSwitch.allocLong1D(8);} |
| 672 |
table.put(name, scaf); |
| 673 |
list.add(scaf); |
| 674 |
addLen=true; |
| 675 |
} |
| 676 |
}else{ |
| 677 |
len+=s.length; |
| 678 |
for(int i=0; i<s.length; i++){ |
| 679 |
acgtn[charToNum[s[i]]]++; |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
if(scaf!=null){ |
| 684 |
if(scaf.length>0 && scaf.length!=len){ |
| 685 |
outstream.println("ERROR: Scaffold "+scaf.name+" has contradictory lengths of "+scaf.length+" and "+len+"\n" |
| 686 |
+ "This probably indicates a corrupt or incorrect reference."); |
| 687 |
errorState=true; |
| 688 |
if(Shared.EA()){KillSwitch.kill();} |
| 689 |
} |
| 690 |
|
| 691 |
scaf.length=len; |
| 692 |
if(addLen){ |
| 693 |
refBases+=scaf.length; |
| 694 |
addLen=false; |
| 695 |
} |
| 696 |
scaf.gc=(float)((acgtn[1]+acgtn[2])*1d/Data.max(1, acgtn[0]+acgtn[1]+acgtn[2]+acgtn[3])); |
| 697 |
scaf=null; |
| 698 |
len=0; |
| 699 |
Arrays.fill(acgtn, 0); |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
public void processOrfsFasta(String fname_in, String fname_out, HashMap<String, Scaffold> map){ |
| 705 |
TextFile tf=new TextFile(fname_in, false); |
| 706 |
assert(!fname_in.equalsIgnoreCase(fname_out)); |
| 707 |
TextStreamWriter tsw=new TextStreamWriter(fname_out, overwrite, false, true); |
| 708 |
tsw.start(); |
| 709 |
|
| 710 |
if(printHeader){ |
| 711 |
String pound=(headerPound ? "#" : ""); |
| 712 |
tsw.print(pound+"mappedBases="+mappedBases+"\n"); |
| 713 |
tsw.print(pound+"mappedNonClippedBases="+mappedNonClippedBases+"\n"); |
| 714 |
tsw.print(pound+"mappedBasesWithDels="+mappedBasesWithDels+"\n"); |
| 715 |
tsw.print(pound+"mappedReads="+mappedReads+"\n"); |
| 716 |
tsw.print(pound+"name\tlength\tdepthSum\tavgDepth\tavgDepth/mappedBases\tminDepth\tmaxDepth\tmedianDepth\tstdDevDepth\tfractionCovered\n"); |
| 717 |
} |
| 718 |
|
| 719 |
String line; |
| 720 |
final StringBuilder sb=new StringBuilder(500); |
| 721 |
|
| 722 |
|
| 723 |
while((line=tf.nextLine())!=null){ |
| 724 |
if(line.length()>1 && line.charAt(0)=='>'){ |
| 725 |
|
| 726 |
String[] split=line.split(" # "); |
| 727 |
|
| 728 |
String orfname=split[0].substring(1).trim(); |
| 729 |
String scafname=orfname; |
| 730 |
if(scafname.contains("_")){ |
| 731 |
int last=scafname.lastIndexOf('_'); |
| 732 |
boolean numeric=false; |
| 733 |
for(int i=last+1; i<scafname.length(); i++){ |
| 734 |
if(Tools.isDigit(scafname.charAt(i))){numeric=true;} |
| 735 |
else{numeric=false; break;} |
| 736 |
} |
| 737 |
if(numeric){scafname=scafname.substring(0, last);} |
| 738 |
} |
| 739 |
|
| 740 |
int start=Integer.parseInt(split[1].trim()); |
| 741 |
int stop=Integer.parseInt(split[2].trim()); |
| 742 |
int strand=Integer.parseInt(split[3].trim()); |
| 743 |
if(strand==1){strand=Shared.PLUS;}else{strand=Shared.MINUS;} |
| 744 |
Orf orf=new Orf(orfname, start, stop, (byte)strand); |
| 745 |
|
| 746 |
Scaffold scaf=map.get(scafname); |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
if(scaf==null){ |
| 753 |
outstream.println("Can't find scaffold for ("+orf+")\nfrom line\n"+line+"\nscafname='"+scafname+"'\norfname='"+orfname+"'"); |
| 754 |
if(ABORT_ON_ERROR){ |
| 755 |
tsw.poison(); |
| 756 |
throw new RuntimeException("Aborting."); |
| 757 |
} |
| 758 |
}else{ |
| 759 |
if(orf.start<0 && orf.stop>=scaf.length){ |
| 760 |
outstream.println("orf goes out of scaffold bounds.\n"+orf+"\n"+scaf); |
| 761 |
if(ABORT_ON_ERROR){ |
| 762 |
tsw.poison(); |
| 763 |
throw new RuntimeException("Aborting."); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 768 |
orf.readCoverageArray(ca); |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
sb.append(orf.name).append('\t'); |
| 776 |
sb.append(orf.length()).append('\t'); |
| 777 |
sb.append(orf.baseDepth).append('\t'); |
| 778 |
sb.append(Tools.format("%.4f", orf.avgCoverage())).append('\t'); |
| 779 |
sb.append(orf.avgCoverage()/mappedNonClippedBases); |
| 780 |
|
| 781 |
sb.append('\t'); |
| 782 |
sb.append(orf.minDepth).append('\t'); |
| 783 |
sb.append(orf.maxDepth).append('\t'); |
| 784 |
sb.append(orf.medianDepth).append('\t'); |
| 785 |
sb.append(Tools.format("%.4f",orf.stdevDepth)).append('\t'); |
| 786 |
sb.append(Tools.format("%.4f",orf.fractionCovered())); |
| 787 |
|
| 788 |
sb.append('\n'); |
| 789 |
tsw.print(sb.toString()); |
| 790 |
sb.setLength(0); |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
tsw.poisonAndWait(); |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
public boolean addCoverage(final String scafName, final byte[] seq, byte[] match, final int start0, final int stop0, final int readlen, |
| 803 |
final int nonClippedBases, final int strand, int incrementFrags, boolean properPair, SamLine sl){ |
| 804 |
Scaffold scaf=table.get(scafName); |
| 805 |
if(scaf==null){ |
| 806 |
if(ADD_FROM_READS){ |
| 807 |
if(!warned){ |
| 808 |
outstream.println("Warning: A read was mapped to unknown reference sequence "+scafName+ |
| 809 |
"\nFurther warnings will be suppressed. These scaffolds will be included, but \n" |
| 810 |
+ "some coverage statistics will be inaccurate as scaffold lengths are not known."); |
| 811 |
warned=true; |
| 812 |
} |
| 813 |
scaf=new Scaffold(scafName, 0); |
| 814 |
if(COUNT_GC){scaf.basecount=KillSwitch.allocLong1D(8);} |
| 815 |
table.put(scafName, scaf); |
| 816 |
list.add(scaf); |
| 817 |
return addCoverage(scaf, seq, match, start0, stop0, readlen, nonClippedBases, strand, incrementFrags, properPair, sl); |
| 818 |
}else if(EA){ |
| 819 |
KillSwitch.kill("ERROR: A read was mapped to unknown reference sequence "+scafName); |
| 820 |
}else if(!warned){ |
| 821 |
outstream.println("Warning: Can't find "+scafName+"\nThis scaffold will not be included. Further warnings will be suppressed."); |
| 822 |
warned=true; |
| 823 |
error=true; |
| 824 |
} |
| 825 |
return false; |
| 826 |
} |
| 827 |
return addCoverage(scaf, seq, match, start0, stop0, readlen, nonClippedBases, strand, incrementFrags, properPair, sl); |
| 828 |
} |
| 829 |
|
| 830 |
public boolean addCoverage(final Scaffold scaf, final byte[] seq, byte match[], final int start0, final int stop0, final int readlen, final int nonClippedBases, |
| 831 |
final int strand, int incrementFrags, boolean properPair, SamLine sl){ |
| 832 |
if(scaf==null){ |
| 833 |
assert(false) : "Adding coverage to a null Scaffold."; |
| 834 |
return false; |
| 835 |
} |
| 836 |
|
| 837 |
if(ADD_FROM_READS){ |
| 838 |
int x=scaf.length; |
| 839 |
scaf.length=Tools.max(scaf.length, stop0+1); |
| 840 |
if(scaf.length!=x){ |
| 841 |
refBases=refBases+scaf.length-x; |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
final int start=Tools.max(start0, 0); |
| 846 |
final int stop=Tools.min(stop0, scaf.length-1); |
| 847 |
|
| 848 |
assert(start>=0 && stop>=0) : "\nAn error was encountered when processing a read. Output will not be valid.\n"+ |
| 849 |
"\nscafName="+scaf.name+"\nseq="+new String(seq)+"\nstart="+start+ |
| 850 |
"\nstop="+stop+"\nreadlen="+readlen+"\nstrand="+strand+"\nscaf.length="+scaf.length+"\nscaf="+scaf; |
| 851 |
|
| 852 |
mappedBases+=readlen; |
| 853 |
mappedNonClippedBases+=nonClippedBases; |
| 854 |
mappedBasesWithDels+=(stop-start+1); |
| 855 |
mappedReads++; |
| 856 |
if(properPair){properPairs++;} |
| 857 |
|
| 858 |
scaf.readhits++; |
| 859 |
scaf.fraghits+=incrementFrags; |
| 860 |
if(strand==1){scaf.readhitsMinus++;} |
| 861 |
|
| 862 |
if(seq!=null && scaf.basecount!=null){ |
| 863 |
final long[] counts=scaf.basecount; |
| 864 |
for(int i=0; i<seq.length; i++){ |
| 865 |
counts[charToNum[seq[i]]]++; |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
if(!INCLUDE_DELETIONS && !START_ONLY && !STOP_ONLY){ |
| 870 |
assert(match!=null) : "Coverage excluding deletions cannot be calculated without a match string."; |
| 871 |
return addCoverageIgnoringDeletions(scaf, seq, match, start, stop, readlen, strand, incrementFrags); |
| 872 |
} |
| 873 |
|
| 874 |
final int basehits=stop-start+1; |
| 875 |
scaf.basehits+=basehits; |
| 876 |
|
| 877 |
if(USE_COVERAGE_ARRAYS){ |
| 878 |
if(scaf.obj0==null){ |
| 879 |
scaf.obj0=(bits32 ? new CoverageArray3(table.size(), scaf.length) : new CoverageArray2(table.size(), scaf.length)); |
| 880 |
if(STRANDED){ |
| 881 |
scaf.obj1=(bits32 ? new CoverageArray3(table.size(), scaf.length) : new CoverageArray2(table.size(), scaf.length)); |
| 882 |
} |
| 883 |
} |
| 884 |
CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 885 |
if(START_ONLY){ |
| 886 |
ca.increment(start); |
| 887 |
}else if(STOP_ONLY){ |
| 888 |
ca.increment(stop); |
| 889 |
}else{ |
| 890 |
ca.incrementRange(start, stop); |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
} |
| 897 |
}else if(USE_BITSETS){ |
| 898 |
if(scaf.obj0==null){ |
| 899 |
scaf.obj0=new BitSet(scaf.length+1); |
| 900 |
if(STRANDED){ |
| 901 |
scaf.obj1=new BitSet(scaf.length+1); |
| 902 |
} |
| 903 |
} |
| 904 |
BitSet bs=(BitSet)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 905 |
if(START_ONLY){ |
| 906 |
bs.set(start); |
| 907 |
}else if(STOP_ONLY){ |
| 908 |
bs.set(stop); |
| 909 |
}else{ |
| 910 |
bs.set(start, stop+1); |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
return true; |
| 915 |
} |
| 916 |
|
| 917 |
private boolean addCoverageIgnoringDeletions(final Scaffold scaf, final byte[] seq, byte match[], final int start, final int stop, final int readlen, final int strand, int incrementFrags){ |
| 918 |
assert(!INCLUDE_DELETIONS && !START_ONLY && !STOP_ONLY); |
| 919 |
assert(match!=null) : "Coverage excluding deletions cannot be calculated without a match string."; |
| 920 |
|
| 921 |
if(Read.isShortMatchString(match)){ |
| 922 |
match=Read.toLongMatchString(match); |
| 923 |
} |
| 924 |
|
| 925 |
int basehits=0; |
| 926 |
if(USE_COVERAGE_ARRAYS){ |
| 927 |
if(scaf.obj0==null){ |
| 928 |
scaf.obj0=(bits32 ? new CoverageArray3(table.size(), scaf.length) : new CoverageArray2(table.size(), scaf.length)); |
| 929 |
if(STRANDED){ |
| 930 |
scaf.obj1=(bits32 ? new CoverageArray3(table.size(), scaf.length) : new CoverageArray2(table.size(), scaf.length)); |
| 931 |
} |
| 932 |
} |
| 933 |
CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 934 |
for(int rpos=start, mpos=0; mpos<match.length && rpos<=stop; mpos++){ |
| 935 |
byte m=match[mpos]; |
| 936 |
if(m=='m' || m=='S' || m=='N'){ |
| 937 |
ca.increment(rpos, 1); |
| 938 |
basehits++; |
| 939 |
rpos++; |
| 940 |
}else if(m=='X' || m=='Y' || m=='C' || m=='I'){ |
| 941 |
|
| 942 |
}else if(m=='D'){ |
| 943 |
rpos++; |
| 944 |
}else{ |
| 945 |
assert(false) : "Unhandled symbol "+m; |
| 946 |
} |
| 947 |
} |
| 948 |
}else if(USE_BITSETS){ |
| 949 |
if(scaf.obj0==null){ |
| 950 |
scaf.obj0=new BitSet(scaf.length+1); |
| 951 |
if(STRANDED){ |
| 952 |
scaf.obj1=new BitSet(scaf.length+1); |
| 953 |
} |
| 954 |
} |
| 955 |
BitSet bs=(BitSet)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 956 |
for(int rpos=start, mpos=0; mpos<match.length && rpos<=stop; mpos++){ |
| 957 |
byte m=match[mpos]; |
| 958 |
if(m=='m' || m=='S' || m=='N'){ |
| 959 |
bs.set(rpos); |
| 960 |
basehits++; |
| 961 |
rpos++; |
| 962 |
}else if(m=='X' || m=='Y' || m=='C' || m=='I'){ |
| 963 |
|
| 964 |
}else if(m=='D'){ |
| 965 |
rpos++; |
| 966 |
}else{ |
| 967 |
assert(false) : "Unhandled symbol "+m; |
| 968 |
} |
| 969 |
} |
| 970 |
} |
| 971 |
scaf.basehits+=basehits; |
| 972 |
|
| 973 |
return true; |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
public boolean processSamLine(byte[] line){ |
| 978 |
if(line==null || line.length<3){ |
| 979 |
return false; |
| 980 |
}else if(line[0]=='@'){ |
| 981 |
if(!error){ |
| 982 |
outstream.println("Unexpected header line: "+line); |
| 983 |
outstream.println("This should not cause problems, and is probably due to concatenated sam files.\n" + |
| 984 |
"Supressing future unexpected header warnings."); |
| 985 |
error=true; |
| 986 |
} |
| 987 |
|
| 988 |
if(line[1]=='S' && line[2]=='Q'){ |
| 989 |
lp.set(line); |
| 990 |
String name=Scaffold.name(lp); |
| 991 |
if(!table.containsKey(name)){ |
| 992 |
Scaffold scaf=new Scaffold(lp); |
| 993 |
if(COUNT_GC){scaf.basecount=KillSwitch.allocLong1D(8);} |
| 994 |
table.put(scaf.name, scaf); |
| 995 |
list.add(scaf); |
| 996 |
refBases+=scaf.length; |
| 997 |
} |
| 998 |
} |
| 999 |
}else{ |
| 1000 |
SamLine sl=new SamLine(line); |
| 1001 |
return processSamLine(sl); |
| 1002 |
} |
| 1003 |
return false; |
| 1004 |
} |
| 1005 |
|
| 1006 |
private int trim(SamLine sl){ |
| 1007 |
assert(border>0 || (trimq>=0 && (qtrimLeft || qtrimRight))); |
| 1008 |
Read r=null; |
| 1009 |
Scaffold sc=null; |
| 1010 |
int leftTrimAmount=border, rightTrimAmount=border; |
| 1011 |
if(border>0){ |
| 1012 |
r=sl.toRead(false); |
| 1013 |
sc=table.get(sl.rnameS()); |
| 1014 |
assert(sc!=null) : sl+"\n\n"+sl.rnameS()+"\n\n"+table; |
| 1015 |
int skipTrimRange=Tools.max(10, border+5); |
| 1016 |
if(r.start<skipTrimRange){ |
| 1017 |
if(r.strand()==Shared.PLUS){leftTrimAmount=0;} |
| 1018 |
else{rightTrimAmount=0;} |
| 1019 |
} |
| 1020 |
if(r.stop>sc.length-skipTrimRange){ |
| 1021 |
if(r.strand()==Shared.PLUS){rightTrimAmount=0;} |
| 1022 |
else{leftTrimAmount=0;} |
| 1023 |
} |
| 1024 |
} |
| 1025 |
final int len0=sl.length(); |
| 1026 |
if(qtrimLeft || qtrimRight){ |
| 1027 |
long packed=TrimRead.testOptimal(sl.seq, sl.qual, trimE); |
| 1028 |
if(qtrimLeft){leftTrimAmount=Tools.max(leftTrimAmount, (int)((packed>>32)&0xFFFFFFFFL));} |
| 1029 |
if(qtrimRight){rightTrimAmount=Tools.max(rightTrimAmount, (int)((packed)&0xFFFFFFFFL));} |
| 1030 |
|
| 1031 |
} |
| 1032 |
final int trimmed; |
| 1033 |
if(leftTrimAmount<1 && rightTrimAmount<1){trimmed=0;} |
| 1034 |
else{ |
| 1035 |
if(r==null){r=sl.toRead(false);} |
| 1036 |
if(sc==null){sc=table.get(sl.rnameS());} |
| 1037 |
int scaflen=(sc==null ? 1999999999 : sc.length); |
| 1038 |
trimmed=TrimRead.trimReadWithMatch(r, sl, leftTrimAmount, rightTrimAmount, 0, scaflen, false); |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
return trimmed; |
| 1043 |
} |
| 1044 |
|
| 1045 |
public boolean processSamLine(SamLine sl){ |
| 1046 |
readsProcessed++; |
| 1047 |
basesProcessed+=sl.length(); |
| 1048 |
if(sl.duplicate() && !INCLUDE_DUPLICATES){return false;} |
| 1049 |
|
| 1050 |
if(border>0 || (trimq>=0 && (qtrimLeft || qtrimRight))){ |
| 1051 |
if(sl.mapped() && sl.seq!=null){ |
| 1052 |
int trimmed=trim(sl); |
| 1053 |
if(sl.length()<1 || trimmed<0){return false;} |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
final int kmers=Tools.countKmers(sl.seq, k); |
| 1058 |
final double cKmers=sl.qual==null ? kmers : Tools.countCorrectKmers(sl.qual, k); |
| 1059 |
kmersProcessed+=kmers; |
| 1060 |
correctKmers+=cKmers; |
| 1061 |
final boolean properPair=(sl.hasMate() && sl.mapped() && sl.primary() && sl.properPair()); |
| 1062 |
if(PHYSICAL_COVERAGE && properPair){ |
| 1063 |
SamLine mate=pairTable.remove(sl.qname); |
| 1064 |
if(mate==null){pairTable.put(sl.qname, sl);} |
| 1065 |
else{ |
| 1066 |
final int start1=sl.start(INCLUDE_SOFT_CLIP, false); |
| 1067 |
final int stop1=sl.stop(start1, INCLUDE_SOFT_CLIP, false); |
| 1068 |
final int start2=mate.start(INCLUDE_SOFT_CLIP, false); |
| 1069 |
final int stop2=mate.stop(start2, INCLUDE_SOFT_CLIP, false); |
| 1070 |
final int strand=(sl.pairnum()==0 ? sl.strand() : mate.strand()); |
| 1071 |
final int length=USE_TLEN ? sl.tlen : Tools.max(stop1, stop2)-Tools.min(start1, start2)+1; |
| 1072 |
mappedKmers+=kmers; |
| 1073 |
addCoverage(sl.rnameS(), null, null, Tools.min(start1, start2), Tools.max(stop1, stop2), length, sl.mappedNonClippedBases(), strand, 2, sl.properPair(), sl); |
| 1074 |
} |
| 1075 |
}else if(sl.mapped() && (USE_SECONDARY || sl.primary()) && sl.mapq>=minMapq){ |
| 1076 |
assert(sl.seq!=null || sl.cigar!=null) : "This program requires bases or a cigar string for every sam line. Problem line:\n"+sl+"\n"; |
| 1077 |
|
| 1078 |
final int length=sl.length(); |
| 1079 |
final int start=sl.start(INCLUDE_SOFT_CLIP, false); |
| 1080 |
final int stop=sl.stop(start, INCLUDE_SOFT_CLIP, false); |
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
final byte[] match=(INCLUDE_DELETIONS ? null : sl.toShortMatch(true)); |
| 1085 |
mappedKmers+=kmers; |
| 1086 |
return addCoverage(sl.rnameS(), sl.seq, match, start, stop, length, sl.mappedNonClippedBases(), sl.strand(), sl.hasMate() ? 1 : 2, sl.properPair(), sl); |
| 1087 |
} |
| 1088 |
return false; |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
public boolean processRead(Read r){ |
| 1093 |
readsProcessed++; |
| 1094 |
final int kmers=Tools.countKmers(r.bases, k); |
| 1095 |
final double cKmers=r.quality==null ? kmers : Tools.countCorrectKmers(r.quality, k); |
| 1096 |
kmersProcessed+=kmers; |
| 1097 |
correctKmers+=cKmers; |
| 1098 |
if(r.mapped() && r.bases!=null){ |
| 1099 |
if(USE_SECONDARY && r.sites!=null && r.sites.size()>0){ |
| 1100 |
boolean b=false; |
| 1101 |
for(SiteScore ss : r.sites){ |
| 1102 |
b=processRead(r, ss) || b; |
| 1103 |
} |
| 1104 |
return b; |
| 1105 |
}else{ |
| 1106 |
final Read mate=r.mate; |
| 1107 |
final boolean set1=coords.set(r); |
| 1108 |
final boolean set2=(PHYSICAL_COVERAGE && r.paired() && r.pairnum()==0 && coords2.set(mate)); |
| 1109 |
if(set1 && set2 && Tools.equals(coords.name, coords2.name)){ |
| 1110 |
final int start1=coords.start; |
| 1111 |
final int stop1=coords.stop; |
| 1112 |
final int start2=coords2.start; |
| 1113 |
final int stop2=coords2.stop; |
| 1114 |
final int strand=r.strand(); |
| 1115 |
final int length=Tools.max(stop1, stop2)-Tools.min(start1, start2)+1; |
| 1116 |
mappedKmers+=kmers; |
| 1117 |
addCoverage(new String(coords.name), null, null, Tools.min(start1, start2), Tools.max(stop1, stop2), length, |
| 1118 |
r.mappedNonClippedBases(), strand, 2-r.mateCount(), r.paired(), null); |
| 1119 |
}else{ |
| 1120 |
if(set1){ |
| 1121 |
mappedKmers+=kmers; |
| 1122 |
return addCoverage(new String(coords.name), r.bases, r.match, coords.start, coords.stop, r.length(), |
| 1123 |
r.mappedNonClippedBases(), coords.strand, 2-r.mateCount(), r.paired(), null); |
| 1124 |
} |
| 1125 |
} |
| 1126 |
} |
| 1127 |
} |
| 1128 |
return false; |
| 1129 |
} |
| 1130 |
|
| 1131 |
|
| 1132 |
public boolean processRead(Read r, SiteScore ss){ |
| 1133 |
if(ss!=null && r.bases!=null){ |
| 1134 |
if(coords.set(ss)){ |
| 1135 |
return addCoverage(new String(coords.name), r.bases, ss.match, coords.start, coords.stop, r.length(), |
| 1136 |
r.mappedNonClippedBases(), coords.strand, 1, r.paired(), null); |
| 1137 |
} |
| 1138 |
} |
| 1139 |
return false; |
| 1140 |
} |
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
public void printOutput(){ |
| 1149 |
|
| 1150 |
totalScaffolds=list.size(); |
| 1151 |
|
| 1152 |
long refKmers=0; |
| 1153 |
long refBases2=0; |
| 1154 |
for(Scaffold sc : list){ |
| 1155 |
refBases2+=sc.length; |
| 1156 |
if(sc.length>=k){refKmers+=sc.length-k+1;} |
| 1157 |
} |
| 1158 |
|
| 1159 |
String basecov1=(basecov==null ? null : (STRANDED ? basecov.replaceFirst("#", "1") : basecov)); |
| 1160 |
String rangecov1=(rangecov==null ? null : (STRANDED ? rangecov.replaceFirst("#", "1") : rangecov)); |
| 1161 |
String bincov1=(bincov==null ? null : (STRANDED ? bincov.replaceFirst("#", "1") : bincov)); |
| 1162 |
String normcov1=(normcov==null ? null : (STRANDED ? normcov.replaceFirst("#", "1") : normcov)); |
| 1163 |
String normcovOverall1=(normcovOverall==null ? null : (STRANDED ? normcovOverall.replaceFirst("#", "1") : normcovOverall)); |
| 1164 |
String histogram1=(histogram==null ? null : (STRANDED ? histogram.replaceFirst("#", "1") : histogram)); |
| 1165 |
String stats1=(covstats==null ? null : (STRANDED ? covstats.replaceFirst("#", "1") : covstats)); |
| 1166 |
|
| 1167 |
String basecov2=(basecov==null || !STRANDED ? null : basecov.replaceFirst("#", "2")); |
| 1168 |
String rangecov2=(rangecov==null || !STRANDED ? null : rangecov.replaceFirst("#", "2")); |
| 1169 |
String bincov2=(bincov==null || !STRANDED ? null : bincov.replaceFirst("#", "2")); |
| 1170 |
String normcov2=(normcov==null || !STRANDED ? null : normcov.replaceFirst("#", "2")); |
| 1171 |
String normcovOverall2=(normcovOverall==null ? null : (STRANDED ? normcovOverall.replaceFirst("#", "2") : normcovOverall)); |
| 1172 |
String histogram2=(histogram==null || !STRANDED ? null : histogram.replaceFirst("#", "2")); |
| 1173 |
String stats2=(covstats==null || !STRANDED ? null : covstats.replaceFirst("#", "2")); |
| 1174 |
|
| 1175 |
if(CONCISE){ |
| 1176 |
writeCoveragePerBaseConcise(basecov1, list, 0, minscaf); |
| 1177 |
writeCoveragePerBaseConcise(basecov2, list, 1, minscaf); |
| 1178 |
}else{ |
| 1179 |
writeCoveragePerBase(basecov1, list, DELTA_ONLY, 0, minscaf); |
| 1180 |
writeCoveragePerBase(basecov2, list, DELTA_ONLY, 1, minscaf); |
| 1181 |
} |
| 1182 |
if(KEEP_SHORT_BINS){ |
| 1183 |
writeCoveragePerBaseBinned2(bincov1, list, binsize, 0, minscaf); |
| 1184 |
writeCoveragePerBaseBinned2(bincov2, list, binsize, 1, minscaf); |
| 1185 |
}else{ |
| 1186 |
writeCoveragePerBaseBinned(bincov1, list, binsize, 0, minscaf); |
| 1187 |
writeCoveragePerBaseBinned(bincov2, list, binsize, 1, minscaf); |
| 1188 |
} |
| 1189 |
if(rangecov!=null) { |
| 1190 |
writeCoverageRanges(rangecov1, list, 0, minscaf); |
| 1191 |
writeCoverageRanges(rangecov2, list, 1, minscaf); |
| 1192 |
} |
| 1193 |
if(normcov!=null){ |
| 1194 |
writeCoveragePerBaseNormalized(normcov1, list, binsize, 0, minscaf); |
| 1195 |
writeCoveragePerBaseNormalized(normcov2, list, binsize, 1, minscaf); |
| 1196 |
} |
| 1197 |
if(normcovOverall!=null){ |
| 1198 |
writeCoveragePerBaseNormalizedOverall(normcovOverall1, list, binsize, 0, minscaf); |
| 1199 |
writeCoveragePerBaseNormalizedOverall(normcovOverall2, list, binsize, 1, minscaf); |
| 1200 |
} |
| 1201 |
if(outrpkm!=null){ |
| 1202 |
writeRPKM(outrpkm, in1, null, readsProcessed, NONZERO_ONLY,list); |
| 1203 |
} |
| 1204 |
|
| 1205 |
{ |
| 1206 |
long[] hist=writeStats(stats1, 0); |
| 1207 |
if(hist!=null){writeHist(histogram1, hist);} |
| 1208 |
|
| 1209 |
if(STRANDED){ |
| 1210 |
hist=writeStats(stats2, 1); |
| 1211 |
if(hist!=null){writeHist(histogram2, hist);} |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
final double mult=1.0/refBases; |
| 1216 |
double depthCovered=mappedBases*mult; |
| 1217 |
double depthCovered2=mappedNonClippedBases*mult; |
| 1218 |
double depthCovered3=mappedBasesWithDels*mult; |
| 1219 |
double pctScaffoldsWithCoverage=scaffoldsWithCoverage1*100.0/totalScaffolds; |
| 1220 |
double pctCovered=totalCoveredBases1*100*mult; |
| 1221 |
|
| 1222 |
if(!KEY_VALUE){ |
| 1223 |
outstream.println("Reads: \t"+readsProcessed); |
| 1224 |
outstream.println("Mapped reads: \t"+mappedReads); |
| 1225 |
|
| 1226 |
|
| 1227 |
outstream.println("Mapped bases: \t"+mappedNonClippedBases); |
| 1228 |
outstream.println("Ref scaffolds: \t"+totalScaffolds); |
| 1229 |
outstream.println("Ref bases: \t"+refBases); |
| 1230 |
|
| 1231 |
if(k>0){ |
| 1232 |
String kcovS=k+"-mer coverage:"; |
| 1233 |
String kcorrectS="Percent correct "+k+"-mers:"; |
| 1234 |
while(kcovS.length()<26){kcovS=kcovS+" ";} |
| 1235 |
while(kcovS.length()<26){kcorrectS=kcorrectS+" ";} |
| 1236 |
outstream.println(Tools.format("\n"+kcovS+" \t%.3f", mappedKmers*1.0/refKmers)); |
| 1237 |
outstream.println(Tools.format(kcorrectS+" \t%.3f", 100*correctKmers/kmersProcessed)); |
| 1238 |
|
| 1239 |
} |
| 1240 |
|
| 1241 |
outstream.println(Tools.format("\nPercent mapped: \t%.3f", mappedReads*100f/readsProcessed)); |
| 1242 |
outstream.println(Tools.format("Percent proper pairs: \t%.3f", properPairs*100f/readsProcessed)); |
| 1243 |
|
| 1244 |
outstream.println(Tools.format("Average coverage: \t%.3f", depthCovered2)); |
| 1245 |
outstream.println(Tools.format("Average coverage with deletions: \t%.3f", depthCovered3)); |
| 1246 |
if(USE_COVERAGE_ARRAYS && calcCovStdev){ |
| 1247 |
double[] stdev=standardDeviation(list, 0, minscaf); |
| 1248 |
outstream.println(Tools.format("Standard deviation: \t%.3f", stdev[1])); |
| 1249 |
} |
| 1250 |
outstream.println(Tools.format("Percent scaffolds with any coverage: \t%.2f", pctScaffoldsWithCoverage)); |
| 1251 |
if(USE_COVERAGE_ARRAYS || USE_BITSETS){ |
| 1252 |
outstream.println(Tools.format("Percent of reference bases covered: \t%.2f", pctCovered)); |
| 1253 |
} |
| 1254 |
}else{ |
| 1255 |
outstream.println("reads="+readsProcessed); |
| 1256 |
outstream.println("mappedReads="+mappedReads); |
| 1257 |
outstream.println("mappedBases="+mappedNonClippedBases); |
| 1258 |
outstream.println("mappedBasesWithDels="+mappedBasesWithDels); |
| 1259 |
outstream.println("refScaffolds="+totalScaffolds); |
| 1260 |
outstream.println("refBases="+refBases); |
| 1261 |
outstream.println(Tools.format("percentMapped=%.3f", mappedReads*100f/readsProcessed)); |
| 1262 |
outstream.println(Tools.format("percentPaired=%.3f", properPairs*100f/readsProcessed)); |
| 1263 |
outstream.println(Tools.format("averageCoverage=%.3f", depthCovered2)); |
| 1264 |
if(USE_COVERAGE_ARRAYS && calcCovStdev){ |
| 1265 |
double[] stdev=standardDeviation(list, 0, minscaf); |
| 1266 |
outstream.println(Tools.format("standardDeviation=%.3f", stdev[1])); |
| 1267 |
} |
| 1268 |
outstream.println(Tools.format("percentCoveredScaffolds=%.2f", pctScaffoldsWithCoverage)); |
| 1269 |
if(USE_COVERAGE_ARRAYS || USE_BITSETS){ |
| 1270 |
outstream.println(Tools.format("percentCoveredBases=%.2f", pctCovered)); |
| 1271 |
} |
| 1272 |
} |
| 1273 |
} |
| 1274 |
|
| 1275 |
public int basesUnderAverageCoverage(final int[] array, final double avg, final int window){ |
| 1276 |
if(array.length<window){return 0;} |
| 1277 |
final long limit=(long)Math.ceil(window*avg); |
| 1278 |
long covSum=0; |
| 1279 |
int baseCount=0; |
| 1280 |
for(int i=0; i<window; i++){ |
| 1281 |
covSum+=array[i]; |
| 1282 |
} |
| 1283 |
|
| 1284 |
boolean below=false; |
| 1285 |
int lastStop=-1, lastStart=0; |
| 1286 |
for(int a=0, b=window; b<array.length; a++, b++){ |
| 1287 |
if(covSum>=limit){ |
| 1288 |
if(below){ |
| 1289 |
baseCount=b-Tools.max(lastStop+1, lastStart); |
| 1290 |
lastStop=b-1; |
| 1291 |
below=false; |
| 1292 |
} |
| 1293 |
}else{ |
| 1294 |
if(!below){ |
| 1295 |
lastStart=a; |
| 1296 |
below=true; |
| 1297 |
} |
| 1298 |
} |
| 1299 |
covSum-=array[a]; |
| 1300 |
assert(covSum>=0); |
| 1301 |
covSum+=array[b]; |
| 1302 |
} |
| 1303 |
|
| 1304 |
if(below){ |
| 1305 |
baseCount=array.length-Tools.max(lastStop, lastStart); |
| 1306 |
} |
| 1307 |
|
| 1308 |
assert(baseCount>=0); |
| 1309 |
return baseCount; |
| 1310 |
} |
| 1311 |
|
| 1312 |
public int basesUnderAverageCoverage(final char[] array, final double avg, final int window){ |
| 1313 |
if(array.length<window){return 0;} |
| 1314 |
final long limit=(long)Math.ceil(window*avg); |
| 1315 |
long covSum=0; |
| 1316 |
int baseCount=0; |
| 1317 |
for(int i=0; i<window; i++){ |
| 1318 |
covSum+=array[i]; |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
boolean below=false; |
| 1324 |
int lastStop=-1, lastStart=0; |
| 1325 |
for(int a=0, b=window; b<array.length; a++, b++){ |
| 1326 |
if(covSum>=limit){ |
| 1327 |
if(below){ |
| 1328 |
baseCount+=b-Tools.max(lastStop+1, lastStart); |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
lastStop=b-1; |
| 1335 |
below=false; |
| 1336 |
} |
| 1337 |
}else{ |
| 1338 |
if(!below){ |
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
lastStart=a; |
| 1345 |
below=true; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
covSum-=array[a]; |
| 1349 |
assert(covSum>=0); |
| 1350 |
covSum+=array[b]; |
| 1351 |
} |
| 1352 |
|
| 1353 |
if(below){ |
| 1354 |
baseCount+=array.length-Tools.max(lastStop+1, lastStart); |
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
} |
| 1360 |
|
| 1361 |
assert(baseCount>=0); |
| 1362 |
return baseCount; |
| 1363 |
} |
| 1364 |
|
| 1365 |
public long[] writeStats(String fname, int strand){ |
| 1366 |
|
| 1367 |
final TextStreamWriter tsw=(fname==null ? null : new TextStreamWriter(fname, overwrite, false, true)); |
| 1368 |
|
| 1369 |
if(tsw!=null){ |
| 1370 |
tsw.start(); |
| 1371 |
if(printHeader){ |
| 1372 |
String pound=(headerPound ? "#" : ""); |
| 1373 |
if(TWOCOLUMN){ |
| 1374 |
tsw.println(pound+"ID\tAvg_fold"); |
| 1375 |
}else{ |
| 1376 |
tsw.println(pound+"ID\tAvg_fold\tLength\tRef_GC\tCovered_percent\tCovered_bases\tPlus_reads\tMinus_reads"+ |
| 1377 |
(COUNT_GC ? "\tRead_GC" : "")+ |
| 1378 |
(USE_COVERAGE_ARRAYS ? ("\tMedian_fold\tStd_Dev") : "")+ |
| 1379 |
(USE_WINDOW ? "\tUnder_"+Tools.format("%.0f",LOW_COV_DEPTH)+"/"+LOW_COV_WINDOW : "")); |
| 1380 |
} |
| 1381 |
} |
| 1382 |
|
| 1383 |
|
| 1384 |
} |
| 1385 |
|
| 1386 |
final int histmax=(bits32 ? 1000000 : Character.MAX_VALUE); |
| 1387 |
final LongList hist=new LongList(Character.MAX_VALUE); |
| 1388 |
|
| 1389 |
long coveredScafTemp=0; |
| 1390 |
long coveredBaseTemp=0; |
| 1391 |
for(Scaffold scaf : list){ |
| 1392 |
final long sum=scaf.basehits; |
| 1393 |
int covered=0; |
| 1394 |
int median=0; |
| 1395 |
int underWindowAverage=0; |
| 1396 |
final double stdev; |
| 1397 |
if(USE_COVERAGE_ARRAYS){ |
| 1398 |
CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1399 |
if(ca!=null){ |
| 1400 |
for(int i=0; i<scaf.length; i++){ |
| 1401 |
int x=ca.get(i); |
| 1402 |
hist.increment(Tools.min(x, histmax)); |
| 1403 |
|
| 1404 |
if(x>=minDepthToBeCovered){covered++;} |
| 1405 |
} |
| 1406 |
if(bits32){ |
| 1407 |
int[] array=((CoverageArray3)ca).array; |
| 1408 |
stdev=Tools.standardDeviation(array); |
| 1409 |
underWindowAverage=basesUnderAverageCoverage(array, LOW_COV_DEPTH, LOW_COV_WINDOW); |
| 1410 |
Shared.sort(array); |
| 1411 |
Tools.reverseInPlace(array); |
| 1412 |
median=ca.get(scaf.length/2); |
| 1413 |
}else{ |
| 1414 |
char[] array=((CoverageArray2)ca).array; |
| 1415 |
stdev=Tools.standardDeviation(array); |
| 1416 |
underWindowAverage=basesUnderAverageCoverage(array, LOW_COV_DEPTH, LOW_COV_WINDOW); |
| 1417 |
Arrays.sort(array); |
| 1418 |
Tools.reverseInPlace(array); |
| 1419 |
median=ca.get(scaf.length/2); |
| 1420 |
} |
| 1421 |
}else{ |
| 1422 |
hist.increment(0, scaf.length); |
| 1423 |
stdev=0; |
| 1424 |
} |
| 1425 |
}else if(USE_BITSETS){ |
| 1426 |
|
| 1427 |
BitSet bs=(BitSet)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1428 |
covered=(bs==null ? 0 : bs.cardinality()); |
| 1429 |
stdev=-1; |
| 1430 |
}else{ |
| 1431 |
stdev=-1; |
| 1432 |
} |
| 1433 |
|
| 1434 |
if(sum>0){ |
| 1435 |
coveredScafTemp++; |
| 1436 |
} |
| 1437 |
|
| 1438 |
if(tsw!=null && (sum>0 || !NONZERO_ONLY) && scaf.length>=minscaf){ |
| 1439 |
if(TWOCOLUMN){ |
| 1440 |
tsw.print(Tools.format("%s\t%.4f\n", scaf.name, sum/(double)scaf.length)); |
| 1441 |
}else if(COUNT_GC){ |
| 1442 |
long[] bc=scaf.basecount; |
| 1443 |
double gc=(bc[1]+bc[2])*1d/Data.max(1, bc[0]+bc[1]+bc[2]+bc[3]); |
| 1444 |
if(USE_COVERAGE_ARRAYS){ |
| 1445 |
if(USE_WINDOW){ |
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
tsw.print(Tools.format("%s\t%.4f\t%d\t%.4f\t%.4f\t%d\t%d\t%d\t%.4f\t%d\t%.2f\t%d\n", scaf.name, sum/(double)scaf.length, scaf.length, |
| 1452 |
scaf.gc, covered*100d/scaf.length, covered, (scaf.readhits-scaf.readhitsMinus), scaf.readhitsMinus, |
| 1453 |
gc, median, stdev, underWindowAverage)); |
| 1454 |
}else{ |
| 1455 |
tsw.print(Tools.format("%s\t%.4f\t%d\t%.4f\t%.4f\t%d\t%d\t%d\t%.4f\t%d\t%.2f\n", scaf.name, sum/(double)scaf.length, scaf.length, |
| 1456 |
scaf.gc, covered*100d/scaf.length, covered, (scaf.readhits-scaf.readhitsMinus), scaf.readhitsMinus, |
| 1457 |
gc, median, stdev)); |
| 1458 |
} |
| 1459 |
}else{ |
| 1460 |
tsw.print(Tools.format("%s\t%.4f\t%d\t%.4f\t%.4f\t%d\t%d\t%d\t%.4f\n", scaf.name, sum/(double)scaf.length, scaf.length, |
| 1461 |
scaf.gc, covered*100d/scaf.length, covered, (scaf.readhits-scaf.readhitsMinus), scaf.readhitsMinus, |
| 1462 |
gc)); |
| 1463 |
} |
| 1464 |
}else{ |
| 1465 |
if(USE_COVERAGE_ARRAYS){ |
| 1466 |
if(USE_WINDOW){ |
| 1467 |
tsw.print(Tools.format("%s\t%.4f\t%d\t%.4f\t%.4f\t%d\t%d\t%d\t%d\t%.2f\t%d\n", scaf.name, sum/(double)scaf.length, scaf.length, |
| 1468 |
scaf.gc, covered*100d/scaf.length, covered, (scaf.readhits-scaf.readhitsMinus), scaf.readhitsMinus, |
| 1469 |
median, stdev, underWindowAverage)); |
| 1470 |
}else{ |
| 1471 |
tsw.print(Tools.format("%s\t%.4f\t%d\t%.4f\t%.4f\t%d\t%d\t%d\t%d\t%.2f\n", scaf.name, sum/(double)scaf.length, scaf.length, |
| 1472 |
scaf.gc, covered*100d/scaf.length, covered, (scaf.readhits-scaf.readhitsMinus), scaf.readhitsMinus, |
| 1473 |
median, stdev)); |
| 1474 |
} |
| 1475 |
}else{ |
| 1476 |
tsw.print(Tools.format("%s\t%.4f\t%d\t%.4f\t%.4f\t%d\t%d\t%d\n", scaf.name, sum/(double)scaf.length, scaf.length, |
| 1477 |
scaf.gc, covered*100d/scaf.length, covered, (scaf.readhits-scaf.readhitsMinus), scaf.readhitsMinus)); |
| 1478 |
} |
| 1479 |
} |
| 1480 |
} |
| 1481 |
coveredBaseTemp+=covered; |
| 1482 |
} |
| 1483 |
|
| 1484 |
if(strand==0){ |
| 1485 |
scaffoldsWithCoverage1+=coveredScafTemp; |
| 1486 |
totalCoveredBases1+=coveredBaseTemp; |
| 1487 |
}else{ |
| 1488 |
scaffoldsWithCoverage2+=coveredScafTemp; |
| 1489 |
totalCoveredBases2+=coveredBaseTemp; |
| 1490 |
} |
| 1491 |
|
| 1492 |
if(tsw!=null){tsw.poisonAndWait();} |
| 1493 |
return hist==null ? null : hist.array; |
| 1494 |
} |
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
public static void writeHist(String fname, long[] counts){ |
| 1502 |
if(fname==null){return;} |
| 1503 |
assert(counts!=null) : "Can't write a histogram with null counts."; |
| 1504 |
ByteStreamWriter tsw=new ByteStreamWriter(fname, overwrite, false, false); |
| 1505 |
tsw.start(); |
| 1506 |
if(printHeader){ |
| 1507 |
if(headerPound){tsw.print('#');} |
| 1508 |
tsw.println("Coverage\tnumBases"); |
| 1509 |
} |
| 1510 |
int max=0; |
| 1511 |
for(max=counts.length-1; max>0 && counts[max]==0; max--){} |
| 1512 |
for(int i=0; i<=max; i++){ |
| 1513 |
long x=counts[i]; |
| 1514 |
tsw.print(i); |
| 1515 |
tsw.print('\t'); |
| 1516 |
tsw.println(x); |
| 1517 |
} |
| 1518 |
|
| 1519 |
tsw.poisonAndWait(); |
| 1520 |
} |
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
public void writeCoveragePerBase(String fname, ArrayList<Scaffold> list, boolean deltaOnly, int strand, int minscaf){ |
| 1532 |
if(fname==null || (!STRANDED && strand>0)){return;} |
| 1533 |
|
| 1534 |
if(verbose){outstream.println("Starting tsw "+fname);} |
| 1535 |
ByteStreamWriter tsw=new ByteStreamWriter(fname, overwrite, false, true); |
| 1536 |
if(verbose){outstream.println("Created tsw "+fname);} |
| 1537 |
tsw.start(); |
| 1538 |
|
| 1539 |
if(printHeader){ |
| 1540 |
if(headerPound){tsw.print('#');} |
| 1541 |
tsw.println("RefName\tPos\tCoverage"); |
| 1542 |
} |
| 1543 |
|
| 1544 |
for(Scaffold scaf : list){ |
| 1545 |
int last=-1; |
| 1546 |
CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1547 |
if(scaf.length>=minscaf){ |
| 1548 |
for(int i=0, len=scaf.length; i<len; i++){ |
| 1549 |
int x=(ca==null ? 0 : ca.get(i)); |
| 1550 |
if(!deltaOnly || x!=last){ |
| 1551 |
if(x>0 || !NONZERO_ONLY){ |
| 1552 |
tsw.print(scaf.name); |
| 1553 |
tsw.print('\t'); |
| 1554 |
tsw.print(i); |
| 1555 |
tsw.print('\t'); |
| 1556 |
tsw.println(x); |
| 1557 |
} |
| 1558 |
last=x; |
| 1559 |
} |
| 1560 |
} |
| 1561 |
} |
| 1562 |
} |
| 1563 |
|
| 1564 |
if(verbose){outstream.println("Closing tsw "+fname);} |
| 1565 |
tsw.poisonAndWait(); |
| 1566 |
if(verbose){outstream.println("Closed tsw "+fname);} |
| 1567 |
} |
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
public void writeCoveragePerBaseConcise(String fname, ArrayList<Scaffold> list, int strand, int minscaf){ |
| 1579 |
if(fname==null || (!STRANDED && strand>0)){return;} |
| 1580 |
|
| 1581 |
if(verbose){outstream.println("Starting tsw "+fname);} |
| 1582 |
ByteStreamWriter tsw=new ByteStreamWriter(fname, overwrite, false, true); |
| 1583 |
tsw.start(); |
| 1584 |
if(verbose){outstream.println("Started tsw "+fname);} |
| 1585 |
|
| 1586 |
|
| 1587 |
for(Scaffold scaf : list){ |
| 1588 |
tsw.print('#'); |
| 1589 |
tsw.println(scaf.name); |
| 1590 |
CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1591 |
if(scaf.length>=minscaf){ |
| 1592 |
for(int i=0; i<scaf.length; i++){ |
| 1593 |
int x=(ca==null ? 0 : ca.get(i)); |
| 1594 |
if(x>0){ |
| 1595 |
tsw.print(i); |
| 1596 |
tsw.print('\t'); |
| 1597 |
tsw.println(x); |
| 1598 |
} |
| 1599 |
} |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
if(verbose){outstream.println("Closing tsw "+fname);} |
| 1604 |
tsw.poisonAndWait(); |
| 1605 |
if(verbose){outstream.println("Closed tsw "+fname);} |
| 1606 |
|
| 1607 |
} |
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
public void writeCoverageRanges(String fname, ArrayList<Scaffold> list, int strand, int minscaf){ |
| 1619 |
if(fname==null || (!STRANDED && strand>0)){return;} |
| 1620 |
|
| 1621 |
if(verbose){outstream.println("Starting tsw "+fname);} |
| 1622 |
ByteStreamWriter tsw=new ByteStreamWriter(fname, overwrite, false, true); |
| 1623 |
tsw.start(); |
| 1624 |
if(verbose){outstream.println("Started tsw "+fname);} |
| 1625 |
|
| 1626 |
|
| 1627 |
for(Scaffold scaf : list){ |
| 1628 |
tsw.print('#'); |
| 1629 |
tsw.println(scaf.name); |
| 1630 |
CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1631 |
if(scaf.length>=minscaf){ |
| 1632 |
int start=-1; |
| 1633 |
int prev=-1; |
| 1634 |
long sum=0; |
| 1635 |
|
| 1636 |
for(int i=0; i<scaf.length; i++){ |
| 1637 |
int x=(ca==null ? 0 : ca.get(i)); |
| 1638 |
if(x>=minDepthToBeCovered){ |
| 1639 |
if(prev<minDepthToBeCovered) { |
| 1640 |
start=i; |
| 1641 |
} |
| 1642 |
sum+=x; |
| 1643 |
}else if(prev>=minDepthToBeCovered){ |
| 1644 |
float len=i-start+1; |
| 1645 |
tsw.print(start).print('-').print(i-1).tab().println(sum/len, 2); |
| 1646 |
sum=0; |
| 1647 |
} |
| 1648 |
prev=x; |
| 1649 |
} |
| 1650 |
if(prev>=minDepthToBeCovered){ |
| 1651 |
float len=scaf.length-start; |
| 1652 |
tsw.print(start).print('-').print(scaf.length-1).tab().println(sum/len, 2); |
| 1653 |
sum=0; |
| 1654 |
} |
| 1655 |
} |
| 1656 |
} |
| 1657 |
|
| 1658 |
if(verbose){outstream.println("Closing tsw "+fname);} |
| 1659 |
tsw.poisonAndWait(); |
| 1660 |
if(verbose){outstream.println("Closed tsw "+fname);} |
| 1661 |
|
| 1662 |
} |
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
public static void writeCoveragePerBaseBinned(String fname, ArrayList<Scaffold> list, int binsize, int strand, int minscaf){ |
| 1673 |
if(fname==null || (!STRANDED && strand>0)){return;} |
| 1674 |
TextStreamWriter tsw=new TextStreamWriter(fname, overwrite, false, false); |
| 1675 |
tsw.start(); |
| 1676 |
if(printHeader){ |
| 1677 |
String pound=(headerPound ? "#" : ""); |
| 1678 |
if(calcCovStdev){ |
| 1679 |
double[] stdev=standardDeviation(list, strand, minscaf); |
| 1680 |
if(stdev!=null){ |
| 1681 |
tsw.print(pound+"Mean\t"+Tools.format("%.3f", stdev[0])+"\n"); |
| 1682 |
tsw.print(pound+"STDev\t"+Tools.format("%.3f", stdev[1])+"\n"); |
| 1683 |
} |
| 1684 |
} |
| 1685 |
tsw.print(pound+"RefName\tCov\tPos\tRunningPos\n"); |
| 1686 |
} |
| 1687 |
|
| 1688 |
long running=0; |
| 1689 |
final float invbin=1f/binsize; |
| 1690 |
for(Scaffold scaf : list){ |
| 1691 |
if(scaf.length>=binsize && scaf.length>=minscaf){ |
| 1692 |
CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1693 |
int lastPos=-1, nextPos=binsize-1; |
| 1694 |
long sum=0; |
| 1695 |
for(int i=0; i<scaf.length; i++){ |
| 1696 |
int x=(ca==null ? 0 : ca.get(i)); |
| 1697 |
sum+=x; |
| 1698 |
if(i>=nextPos){ |
| 1699 |
if(sum>0 || !NONZERO_ONLY){ |
| 1700 |
tsw.print(Tools.format("%s\t%.2f\t%d\t%d\n", scaf.name, sum*invbin, (i+1), running)); |
| 1701 |
} |
| 1702 |
lastPos=i; |
| 1703 |
running+=binsize; |
| 1704 |
nextPos+=binsize; |
| 1705 |
sum=0; |
| 1706 |
} |
| 1707 |
} |
| 1708 |
} |
| 1709 |
} |
| 1710 |
|
| 1711 |
tsw.poisonAndWait(); |
| 1712 |
} |
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
public static void writeCoveragePerBaseBinned2(String fname, ArrayList<Scaffold> list, int binsize, int strand, int minscaf){ |
| 1722 |
if(fname==null || (!STRANDED && strand>0)){return;} |
| 1723 |
TextStreamWriter tsw=new TextStreamWriter(fname, overwrite, false, false); |
| 1724 |
tsw.start(); |
| 1725 |
if(printHeader){ |
| 1726 |
String pound=(headerPound ? "#" : ""); |
| 1727 |
if(calcCovStdev){ |
| 1728 |
double[] stdev=standardDeviationBinned(list, binsize, strand, minscaf); |
| 1729 |
if(stdev!=null){ |
| 1730 |
tsw.print(pound+"Mean\t"+Tools.format("%.3f", stdev[0])+"\n"); |
| 1731 |
tsw.print(pound+"STDev\t"+Tools.format("%.3f", stdev[1])+"\n"); |
| 1732 |
} |
| 1733 |
} |
| 1734 |
tsw.print(pound+"RefName\tCov\tPos\tRunningPos\n"); |
| 1735 |
} |
| 1736 |
|
| 1737 |
long running=0; |
| 1738 |
for(Scaffold scaf : list){ |
| 1739 |
CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1740 |
int lastPos=-1, nextPos=binsize-1; |
| 1741 |
long sum=0; |
| 1742 |
final int lim=scaf.length-1; |
| 1743 |
for(int i=0; i<scaf.length; i++){ |
| 1744 |
int x=(ca==null ? 0 : ca.get(i)); |
| 1745 |
sum+=x; |
| 1746 |
if(i>=nextPos || i==lim){ |
| 1747 |
int bin=(i-lastPos); |
| 1748 |
if(scaf.length>=minscaf){ |
| 1749 |
if(sum>0 || !NONZERO_ONLY){ |
| 1750 |
tsw.print(Tools.format("%s\t%.2f\t%d\t%d\n", scaf.name, sum/(float)bin, (i+1), running)); |
| 1751 |
} |
| 1752 |
} |
| 1753 |
running+=bin; |
| 1754 |
nextPos+=binsize; |
| 1755 |
lastPos=i; |
| 1756 |
sum=0; |
| 1757 |
} |
| 1758 |
} |
| 1759 |
} |
| 1760 |
|
| 1761 |
tsw.poisonAndWait(); |
| 1762 |
} |
| 1763 |
|
| 1764 |
|
| 1765 |
public static double[] standardDeviationBinnedUnsafe(String fname, ArrayList<Scaffold> scaffolds, int binsize, int strand, int minscaf){ |
| 1766 |
|
| 1767 |
LongList list=new LongList(); |
| 1768 |
for(Scaffold scaf : scaffolds){ |
| 1769 |
CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1770 |
int lastPos=-1, nextPos=binsize-1; |
| 1771 |
long sum=0; |
| 1772 |
final int lim=scaf.length-1; |
| 1773 |
for(int i=0; i<scaf.length; i++){ |
| 1774 |
int x=(ca==null ? 0 : ca.get(i)); |
| 1775 |
sum+=x; |
| 1776 |
if(i>=nextPos || i==lim){ |
| 1777 |
int bin=(i-lastPos); |
| 1778 |
if(scaf.length>=minscaf){ |
| 1779 |
list.add((int)(10*(sum/(double)bin))); |
| 1780 |
} |
| 1781 |
nextPos+=binsize; |
| 1782 |
lastPos=i; |
| 1783 |
sum=0; |
| 1784 |
} |
| 1785 |
} |
| 1786 |
} |
| 1787 |
list.sort(); |
| 1788 |
|
| 1789 |
double mean=0.1*list.mean(); |
| 1790 |
double median=0.1*list.median(); |
| 1791 |
double mode=0.1*list.mode(); |
| 1792 |
double stdev=0.1*list.stdev(); |
| 1793 |
return new double[] {mean, median, mode, stdev}; |
| 1794 |
} |
| 1795 |
|
| 1796 |
public static double[] standardDeviationBinned(ArrayList<Scaffold> scaffolds, int binsize, int strand, int minscaf){ |
| 1797 |
double totalSum=0; |
| 1798 |
long bins=0; |
| 1799 |
|
| 1800 |
for(Scaffold scaf : scaffolds){ |
| 1801 |
if(scaf.length>=minscaf){ |
| 1802 |
CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1803 |
int lastPos=-1, nextPos=binsize-1; |
| 1804 |
long tempSum=0; |
| 1805 |
final int lim=scaf.length-1; |
| 1806 |
for(int i=0; i<scaf.length; i++){ |
| 1807 |
int x=(ca==null ? 0 : ca.get(i)); |
| 1808 |
tempSum+=x; |
| 1809 |
if(i>=nextPos || i==lim){ |
| 1810 |
int bin=(i-lastPos); |
| 1811 |
double depth=(tempSum/(double)bin); |
| 1812 |
totalSum+=depth; |
| 1813 |
bins++; |
| 1814 |
nextPos+=binsize; |
| 1815 |
lastPos=i; |
| 1816 |
tempSum=0; |
| 1817 |
} |
| 1818 |
} |
| 1819 |
} |
| 1820 |
} |
| 1821 |
|
| 1822 |
if(bins<1){return new double[] {0, 0};} |
| 1823 |
final double mean=totalSum/(double)bins; |
| 1824 |
double sumdev2=0; |
| 1825 |
|
| 1826 |
for(Scaffold scaf : scaffolds){ |
| 1827 |
if(scaf.length>=minscaf){ |
| 1828 |
CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1829 |
int lastPos=-1, nextPos=binsize-1; |
| 1830 |
long tempSum=0; |
| 1831 |
final int lim=scaf.length-1; |
| 1832 |
for(int i=0; i<scaf.length; i++){ |
| 1833 |
int x=(ca==null ? 0 : ca.get(i)); |
| 1834 |
tempSum+=x; |
| 1835 |
if(i>=nextPos || i==lim){ |
| 1836 |
int bin=(i-lastPos); |
| 1837 |
double depth=(tempSum/(double)bin); |
| 1838 |
double dev=mean-depth; |
| 1839 |
sumdev2+=(dev*dev); |
| 1840 |
nextPos+=binsize; |
| 1841 |
lastPos=i; |
| 1842 |
tempSum=0; |
| 1843 |
} |
| 1844 |
} |
| 1845 |
} |
| 1846 |
} |
| 1847 |
|
| 1848 |
final double stdev=Math.sqrt(sumdev2/bins); |
| 1849 |
return new double[] {mean, stdev}; |
| 1850 |
} |
| 1851 |
|
| 1852 |
public static double[] standardDeviation(ArrayList<Scaffold> scaffolds, int strand, int minscaf){ |
| 1853 |
long totalSum=0, bins=0, scafsCounted=0; |
| 1854 |
for(Scaffold scaf : scaffolds){ |
| 1855 |
|
| 1856 |
if(scaf.length>=minscaf){ |
| 1857 |
final CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1858 |
bins+=scaf.length; |
| 1859 |
scafsCounted++; |
| 1860 |
if(ca!=null) { |
| 1861 |
for(int i=0; i<scaf.length; i++){ |
| 1862 |
int depth=(ca==null ? 0 : ca.get(i)); |
| 1863 |
totalSum+=depth; |
| 1864 |
} |
| 1865 |
} |
| 1866 |
} |
| 1867 |
} |
| 1868 |
|
| 1869 |
if(bins<1){return new double[] {0, 0};} |
| 1870 |
final double mean=totalSum/(double)bins; |
| 1871 |
double sumdev2=0; |
| 1872 |
|
| 1873 |
for(Scaffold scaf : scaffolds){ |
| 1874 |
if(scaf.length>=minscaf){ |
| 1875 |
final CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1876 |
double sumTemp=0; |
| 1877 |
if(ca!=null) { |
| 1878 |
for(int i=0; i<scaf.length; i++){ |
| 1879 |
int depth=(ca==null ? 0 : ca.get(i)); |
| 1880 |
double dev=mean-depth; |
| 1881 |
sumTemp+=(dev*dev); |
| 1882 |
} |
| 1883 |
}else { |
| 1884 |
sumTemp+=(mean*mean*scaf.length); |
| 1885 |
} |
| 1886 |
sumdev2+=sumTemp; |
| 1887 |
} |
| 1888 |
} |
| 1889 |
|
| 1890 |
final double stdev=Math.sqrt(sumdev2/bins); |
| 1891 |
|
| 1892 |
return new double[] {mean, stdev}; |
| 1893 |
} |
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
public static void writeCoveragePerBaseNormalized(String fname, ArrayList<Scaffold> list, double binsize, int strand, int minscaf){ |
| 1903 |
if(fname==null || (!STRANDED && strand>0)){return;} |
| 1904 |
TextStreamWriter tsw=new TextStreamWriter(fname, overwrite, false, false); |
| 1905 |
tsw.start(); |
| 1906 |
if(printHeader){ |
| 1907 |
String pound=(headerPound ? "#" : ""); |
| 1908 |
tsw.print(pound+"RefName\tBin\tCov\tPos\tRunningPos\n"); |
| 1909 |
} |
| 1910 |
|
| 1911 |
double running=0; |
| 1912 |
double invbin=1.0/binsize; |
| 1913 |
final double invbincount=1.0/NORMALIZE_LENGTH_BINS; |
| 1914 |
for(Scaffold scaf : list){ |
| 1915 |
if(NORMALIZE_LENGTH_BINS>0){ |
| 1916 |
binsize=scaf.length*invbincount; |
| 1917 |
invbin=1.0/binsize; |
| 1918 |
} |
| 1919 |
|
| 1920 |
if(scaf.length>=binsize && scaf.length>=minscaf){ |
| 1921 |
long max=-1; |
| 1922 |
|
| 1923 |
final CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 1924 |
double lastPos=-1, nextPos=binsize-1; |
| 1925 |
long sum=0; |
| 1926 |
|
| 1927 |
if(NORMALIZE_COVERAGE){ |
| 1928 |
for(int i=0; i<scaf.length; i++){ |
| 1929 |
int x=(ca==null ? 0 : ca.get(i)); |
| 1930 |
sum+=x; |
| 1931 |
if(i>=nextPos){ |
| 1932 |
max=Tools.max(sum, max); |
| 1933 |
running+=binsize; |
| 1934 |
nextPos+=binsize; |
| 1935 |
sum=0; |
| 1936 |
} |
| 1937 |
} |
| 1938 |
lastPos=-1; |
| 1939 |
nextPos=binsize-1; |
| 1940 |
sum=0; |
| 1941 |
assert(max>-1) : max; |
| 1942 |
} |
| 1943 |
max=Tools.max(max, 1); |
| 1944 |
final double binmult=(NORMALIZE_COVERAGE ? 1d/max : invbin); |
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
final String formatString=NORMALIZE_COVERAGE ? "%s\t%d\t%.5f\t%d\t%d\n" : "%s\t%d\t%.2f\t%d\t%d\n"; |
| 1949 |
int bin=1; |
| 1950 |
for(int i=0; i<scaf.length; i++){ |
| 1951 |
int x=(ca==null ? 0 : ca.get(i)); |
| 1952 |
sum+=x; |
| 1953 |
if(i>=nextPos){ |
| 1954 |
|
| 1955 |
tsw.print(String.format(formatString, scaf.name, bin, sum*binmult, (i+1), (long)running)); |
| 1956 |
bin++; |
| 1957 |
lastPos=i; |
| 1958 |
running+=binsize; |
| 1959 |
nextPos+=binsize; |
| 1960 |
sum=0; |
| 1961 |
} |
| 1962 |
} |
| 1963 |
} |
| 1964 |
} |
| 1965 |
|
| 1966 |
tsw.poisonAndWait(); |
| 1967 |
} |
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
public static void writeCoveragePerBaseNormalizedOverall(String fname, ArrayList<Scaffold> list, double binsize, int strand, int minscaf){ |
| 1978 |
if(fname==null || (!STRANDED && strand>0)){return;} |
| 1979 |
|
| 1980 |
assert(NORMALIZE_LENGTH_BINS>0) : "Must set 'normalizebins' flag to a positive integer."; |
| 1981 |
double running=0; |
| 1982 |
double invbin=1.0/binsize; |
| 1983 |
long usedScafs=0; |
| 1984 |
final double invbincount=1.0/NORMALIZE_LENGTH_BINS; |
| 1985 |
|
| 1986 |
double[] normalized=new double[NORMALIZE_LENGTH_BINS+1]; |
| 1987 |
double[] absolute=new double[NORMALIZE_LENGTH_BINS+1]; |
| 1988 |
|
| 1989 |
for(Scaffold scaf : list){ |
| 1990 |
if(NORMALIZE_LENGTH_BINS>0){ |
| 1991 |
binsize=scaf.length*invbincount; |
| 1992 |
invbin=1.0/binsize; |
| 1993 |
} |
| 1994 |
|
| 1995 |
if(scaf.length>=binsize && scaf.length>=minscaf){ |
| 1996 |
usedScafs++; |
| 1997 |
|
| 1998 |
if(scaf.readhits>0){ |
| 1999 |
long max=-1; |
| 2000 |
final CoverageArray ca=(CoverageArray)(STRANDED && strand==1 ? scaf.obj1 : scaf.obj0); |
| 2001 |
double lastPos=-1, nextPos=binsize-1; |
| 2002 |
long sum=0; |
| 2003 |
|
| 2004 |
{ |
| 2005 |
for(int i=0; i<scaf.length; i++){ |
| 2006 |
int x=(ca==null ? 0 : ca.get(i)); |
| 2007 |
sum+=x; |
| 2008 |
if(i>=nextPos){ |
| 2009 |
max=Tools.max(sum, max); |
| 2010 |
running+=binsize; |
| 2011 |
nextPos+=binsize; |
| 2012 |
sum=0; |
| 2013 |
} |
| 2014 |
} |
| 2015 |
lastPos=-1; |
| 2016 |
nextPos=binsize-1; |
| 2017 |
sum=0; |
| 2018 |
assert(max>-1) : max; |
| 2019 |
} |
| 2020 |
max=Tools.max(max, 1); |
| 2021 |
final double binmult=1d/max; |
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
int bin=1; |
| 2026 |
for(int i=0; i<scaf.length; i++){ |
| 2027 |
int x=(ca==null ? 0 : ca.get(i)); |
| 2028 |
sum+=x; |
| 2029 |
if(i>=nextPos){ |
| 2030 |
normalized[bin]+=(sum*binmult); |
| 2031 |
absolute[bin]+=(sum*invbin); |
| 2032 |
bin++; |
| 2033 |
lastPos=i; |
| 2034 |
running+=binsize; |
| 2035 |
nextPos+=binsize; |
| 2036 |
sum=0; |
| 2037 |
} |
| 2038 |
} |
| 2039 |
} |
| 2040 |
} |
| 2041 |
} |
| 2042 |
|
| 2043 |
TextStreamWriter tsw=new TextStreamWriter(fname, overwrite, false, false); |
| 2044 |
tsw.start(); |
| 2045 |
if(printHeader){ |
| 2046 |
String pound=(headerPound ? "#" : ""); |
| 2047 |
tsw.print(pound+"RefName\tBin\tAbsCov\tNormCov\n"); |
| 2048 |
} |
| 2049 |
double invScafs=1d/Tools.max(1, usedScafs); |
| 2050 |
|
| 2051 |
final double maxNorm=Tools.max(normalized); |
| 2052 |
final double normMult=1/maxNorm; |
| 2053 |
|
| 2054 |
for(int bin=1; bin<normalized.length; bin++){ |
| 2055 |
|
| 2056 |
|
| 2057 |
tsw.print(Tools.format("%s\t%d\t%.5f\t%.5f\n", "all", bin, absolute[bin]*invScafs, normalized[bin]*normMult)); |
| 2058 |
} |
| 2059 |
|
| 2060 |
tsw.poisonAndWait(); |
| 2061 |
} |
| 2062 |
|
| 2063 |
|
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
|
| 2068 |
public static void writeRPKM(String out, String in1, String in2, long readsIn, boolean printNonZeroOnly, ArrayList<Scaffold> list){ |
| 2069 |
if(out==null){return;} |
| 2070 |
final TextStreamWriter tsw=new TextStreamWriter(out, overwrite, false, false); |
| 2071 |
tsw.start(); |
| 2072 |
|
| 2073 |
|
| 2074 |
long mappedReads=0; |
| 2075 |
long mappedFrags=0; |
| 2076 |
for(Scaffold scaf : list){ |
| 2077 |
mappedReads+=scaf.readhits; |
| 2078 |
mappedFrags+=scaf.fraghits; |
| 2079 |
} |
| 2080 |
mappedFrags/=2; |
| 2081 |
|
| 2082 |
|
| 2083 |
tsw.print("#File\t"+(in1==null ? "" : in1)+(in2==null ? "" : "\t"+in2)+"\n"); |
| 2084 |
tsw.print(Tools.format("#Reads\t%d\n",readsIn)); |
| 2085 |
tsw.print(Tools.format("#Mapped\t%d\n",mappedReads)); |
| 2086 |
tsw.print(Tools.format("#RefSequences\t%d\n",list.size())); |
| 2087 |
tsw.print("#Name\tLength\tBases\tCoverage\tReads\tRPKM\tFrags\tFPKM\n"); |
| 2088 |
|
| 2089 |
final float readMult=1000000000f/Tools.max(1, mappedReads); |
| 2090 |
final float fragMult=1000000000f/Tools.max(1, mappedFrags); |
| 2091 |
|
| 2092 |
|
| 2093 |
for(final Scaffold scaf : list){ |
| 2094 |
final long reads=scaf.readhits; |
| 2095 |
final long frags=scaf.fraghits/2; |
| 2096 |
final long bases=scaf.basehits; |
| 2097 |
final String s=scaf.name; |
| 2098 |
final int len=scaf.length; |
| 2099 |
final double invlen=1.0/Tools.max(1, len); |
| 2100 |
final double readMult2=readMult*invlen; |
| 2101 |
final double fragMult2=fragMult*invlen; |
| 2102 |
if(reads>0 || !printNonZeroOnly){ |
| 2103 |
tsw.print(Tools.format("%s\t%d\t%d\t%.4f\t%d\t%.4f\t%d\t%.4f\n",s,len,bases,bases*invlen,reads,reads*readMult2,frags,frags*fragMult2)); |
| 2104 |
} |
| 2105 |
} |
| 2106 |
tsw.poisonAndWait(); |
| 2107 |
} |
| 2108 |
|
| 2109 |
|
| 2110 |
|
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
private ArrayList<Scaffold> list; |
| 2116 |
|
| 2117 |
private HashMap<String, Scaffold> table; |
| 2118 |
|
| 2119 |
private final ScaffoldCoordinates coords=new ScaffoldCoordinates(), coords2=new ScaffoldCoordinates(); |
| 2120 |
|
| 2121 |
|
| 2122 |
private String program=null; |
| 2123 |
|
| 2124 |
private String version=null; |
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
public String in1=null; |
| 2129 |
|
| 2130 |
public String in2=null; |
| 2131 |
|
| 2132 |
public ArrayList<String> covIn=new ArrayList<String>(); |
| 2133 |
|
| 2134 |
public String reference=null; |
| 2135 |
public String orffasta=null; |
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
public String outsam=null; |
| 2140 |
|
| 2141 |
public String covstats=null; |
| 2142 |
public String outorf=null; |
| 2143 |
|
| 2144 |
public String histogram=null; |
| 2145 |
|
| 2146 |
public String basecov=null; |
| 2147 |
|
| 2148 |
public String basecov_ps=null; |
| 2149 |
|
| 2150 |
public String bincov=null; |
| 2151 |
|
| 2152 |
public String normcov=null; |
| 2153 |
|
| 2154 |
public String normcovOverall=null; |
| 2155 |
|
| 2156 |
public String outrpkm=null; |
| 2157 |
|
| 2158 |
public String rangecov=null; |
| 2159 |
|
| 2160 |
|
| 2161 |
private boolean error=false; |
| 2162 |
|
| 2163 |
private boolean warned=false; |
| 2164 |
private final boolean EA=Shared.EA(); |
| 2165 |
|
| 2166 |
|
| 2167 |
public long refBases=0; |
| 2168 |
public long mappedBases=0; |
| 2169 |
public long mappedNonClippedBases=0; |
| 2170 |
public long mappedBasesWithDels=0; |
| 2171 |
public long mappedReads=0; |
| 2172 |
public long properPairs=0; |
| 2173 |
public long readsProcessed=0; |
| 2174 |
public long basesProcessed=0; |
| 2175 |
public long kmersProcessed=0; |
| 2176 |
public long mappedKmers=0; |
| 2177 |
public double correctKmers=0; |
| 2178 |
public long totalCoveredBases1=0; |
| 2179 |
public long totalCoveredBases2=0; |
| 2180 |
public long scaffoldsWithCoverage1=0; |
| 2181 |
public long scaffoldsWithCoverage2=0; |
| 2182 |
public long totalScaffolds=0; |
| 2183 |
|
| 2184 |
public int k=0; |
| 2185 |
|
| 2186 |
|
| 2187 |
public long maxReads=-1; |
| 2188 |
public int initialScaffolds=4096; |
| 2189 |
public int binsize=1000; |
| 2190 |
public boolean bits32=false; |
| 2191 |
public int minMapq=0; |
| 2192 |
public boolean useStreamer=true; |
| 2193 |
public int streamerThreads=2; |
| 2194 |
public int minDepthToBeCovered=1; |
| 2195 |
|
| 2196 |
private boolean qtrimLeft=false; |
| 2197 |
private boolean qtrimRight=false; |
| 2198 |
private float trimq=-1; |
| 2199 |
private final float trimE; |
| 2200 |
private int border=0; |
| 2201 |
|
| 2202 |
|
| 2203 |
public int minscaf=0; |
| 2204 |
|
| 2205 |
public HashMap<String, SamLine> pairTable=new HashMap<String, SamLine>(); |
| 2206 |
|
| 2207 |
public PrintStream outstream=System.err; |
| 2208 |
|
| 2209 |
private boolean errorState=false; |
| 2210 |
|
| 2211 |
private final LineParser1 lp=new LineParser1('\t'); |
| 2212 |
|
| 2213 |
|
| 2214 |
|
| 2215 |
|
| 2216 |
|
| 2217 |
|
| 2218 |
|
| 2219 |
public static boolean overwrite=true; |
| 2220 |
|
| 2221 |
public static boolean append=false; |
| 2222 |
|
| 2223 |
public static boolean verbose=false; |
| 2224 |
|
| 2225 |
public static boolean printCommand=true; |
| 2226 |
|
| 2227 |
|
| 2228 |
public static boolean printHeader=true; |
| 2229 |
|
| 2230 |
public static boolean headerPound=true; |
| 2231 |
|
| 2232 |
public static boolean calcCovStdev=true; |
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
public static int LOW_COV_WINDOW=500; |
| 2237 |
|
| 2238 |
public static double LOW_COV_DEPTH=5; |
| 2239 |
|
| 2240 |
public static boolean USE_WINDOW=false; |
| 2241 |
|
| 2242 |
|
| 2243 |
public static boolean COUNT_GC=true; |
| 2244 |
|
| 2245 |
public static boolean TWOCOLUMN=false; |
| 2246 |
|
| 2247 |
public static boolean STRANDED=false; |
| 2248 |
|
| 2249 |
public static boolean ADD_FROM_REF=true; |
| 2250 |
|
| 2251 |
public static boolean ADD_FROM_READS=false; |
| 2252 |
|
| 2253 |
public static boolean NONZERO_ONLY=false; |
| 2254 |
|
| 2255 |
public static boolean USE_COVERAGE_ARRAYS=true; |
| 2256 |
|
| 2257 |
public static boolean USE_BITSETS=false; |
| 2258 |
|
| 2259 |
public static boolean DELTA_ONLY=false; |
| 2260 |
|
| 2261 |
public static boolean USE_SECONDARY=true; |
| 2262 |
|
| 2263 |
public static boolean PHYSICAL_COVERAGE=false; |
| 2264 |
|
| 2265 |
public static boolean USE_TLEN=true; |
| 2266 |
|
| 2267 |
public static boolean ABORT_ON_ERROR=true; |
| 2268 |
|
| 2269 |
public static boolean KEEP_SHORT_BINS=true; |
| 2270 |
|
| 2271 |
public static boolean START_ONLY=false; |
| 2272 |
|
| 2273 |
public static boolean STOP_ONLY=false; |
| 2274 |
|
| 2275 |
public static boolean CONCISE=false; |
| 2276 |
|
| 2277 |
public static boolean NORMALIZE_COVERAGE=false; |
| 2278 |
|
| 2279 |
public static int NORMALIZE_LENGTH_BINS=100; |
| 2280 |
|
| 2281 |
public static boolean INCLUDE_SOFT_CLIP=false; |
| 2282 |
|
| 2283 |
public static boolean INCLUDE_DELETIONS=true; |
| 2284 |
|
| 2285 |
public static boolean INCLUDE_DUPLICATES=true; |
| 2286 |
|
| 2287 |
public static boolean KEY_VALUE; |
| 2288 |
|
| 2289 |
|
| 2290 |
private static final byte[] charToNum=AssemblyStats2.makeCharToNum(); |
| 2291 |
|
| 2292 |
private static final int NOTHING_MODE=0, BITSET_MODE=1, ARRAY_MODE=2; |
| 2293 |
|
| 2294 |
} |