| 1 | #!/bin/bash |
| 2 | |
| 3 | usage(){ |
| 4 | echo " |
| 5 | Written by Brian Bushnell |
| 6 | Last modified November 18, 2024 |
| 7 | |
| 8 | Description: Compares reads to the kmers in a reference dataset, optionally |
| 9 | allowing an edit distance. Splits the reads into two outputs - those that |
| 10 | match the reference, and those that don't. Can also trim (remove) the matching |
| 11 | parts of the reads rather than binning the reads. |
| 12 | Please read bbmap/docs/guides/BBDukGuide.txt for more information. |
| 13 | |
| 14 | Usage: bbduk.sh in=<input file> out=<output file> ref=<contaminant files> |
| 15 | |
| 16 | Input may be stdin or a fasta or fastq file, compressed or uncompressed. |
| 17 | If you pipe via stdin/stdout, please include the file type; e.g. for gzipped |
| 18 | fasta input, set in=stdin.fa.gz |
| 19 | |
| 20 | Input parameters: |
| 21 | in=<file> Main input. in=stdin.fq will pipe from stdin. |
| 22 | in2=<file> Input for 2nd read of pairs in a different file. |
| 23 | ref=<file,file> Comma-delimited list of reference files. |
| 24 | In addition to filenames, you may also use the keywords: |
| 25 | adapters, artifacts, phix, lambda, pjet, mtst, kapa |
| 26 | literal=<seq,seq> Comma-delimited list of literal reference sequences. |
| 27 | Polymers are also allowed with the 'poly' prefix; |
| 28 | for example, 'literal=ATGGT,polyGC' will add both ATGGT |
| 29 | and GCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGC - 32+ of them, |
| 30 | enough replicates to ensure that all kmers are present. |
| 31 | touppercase=f (tuc) Change all bases upper-case. |
| 32 | interleaved=auto (int) t/f overrides interleaved autodetection. |
| 33 | Must be set mainually when streaming fastq input. |
| 34 | qin=auto Input quality offset: 33 (Sanger), 64, or auto. |
| 35 | reads=-1 If positive, quit after processing X reads or pairs. |
| 36 | copyundefined=f (cu) Process non-AGCT IUPAC reference bases by making all |
| 37 | possible unambiguous copies. Intended for short motifs |
| 38 | or adapter barcodes, as time/memory use is exponential. |
| 39 | samplerate=1 Set lower to only process a fraction of input reads. |
| 40 | samref=<file> Optional reference fasta for processing sam files. |
| 41 | |
| 42 | Output parameters: |
| 43 | out=<file> (outnonmatch) Write reads here that do not contain |
| 44 | kmers matching the database. 'out=stdout.fq' will pipe |
| 45 | to standard out. |
| 46 | out2=<file> (outnonmatch2) Use this to write 2nd read of pairs to a |
| 47 | different file. |
| 48 | outm=<file> (outmatch) Write reads here that fail filters. In default |
| 49 | kfilter mode, this means any read with a matching kmer. |
| 50 | In any mode, it also includes reads that fail filters such |
| 51 | as minlength, mingc, maxgc, entropy, etc. In other words, |
| 52 | it includes all reads that do not go to 'out'. |
| 53 | outm2=<file> (outmatch2) Use this to write 2nd read of pairs to a |
| 54 | different file. |
| 55 | outs=<file> (outsingle) Use this to write singleton reads whose mate |
| 56 | was trimmed shorter than minlen. |
| 57 | stats=<file> Write statistics about which contamininants were detected. |
| 58 | refstats=<file> Write statistics on a per-reference-file basis. |
| 59 | rpkm=<file> Write RPKM for each reference sequence (for RNA-seq). |
| 60 | dump=<file> Dump kmer tables to a file, in fasta format. |
| 61 | duk=<file> Write statistics in duk's format. *DEPRECATED* |
| 62 | nzo=t Only write statistics about ref sequences with nonzero hits. |
| 63 | overwrite=t (ow) Grant permission to overwrite files. |
| 64 | showspeed=t (ss) 'f' suppresses display of processing speed. |
| 65 | ziplevel=2 (zl) Compression level; 1 (min) through 9 (max). |
| 66 | fastawrap=70 Length of lines in fasta output. |
| 67 | qout=auto Output quality offset: 33 (Sanger), 64, or auto. |
| 68 | statscolumns=3 (cols) Number of columns for stats output, 3 or 5. |
| 69 | 5 includes base counts. |
| 70 | rename=f Rename reads to indicate which sequences they matched. |
| 71 | refnames=f Use names of reference files rather than scaffold IDs. |
| 72 | trd=f Truncate read and ref names at the first whitespace. |
| 73 | ordered=f Set to true to output reads in same order as input. |
| 74 | maxbasesout=-1 If positive, quit after writing approximately this many |
| 75 | bases to out (outu/outnonmatch). |
| 76 | maxbasesoutm=-1 If positive, quit after writing approximately this many |
| 77 | bases to outm (outmatch). |
| 78 | json=f Print to screen in json format. |
| 79 | |
| 80 | Histogram output parameters: |
| 81 | bhist=<file> Base composition histogram by position. |
| 82 | qhist=<file> Quality histogram by position. |
| 83 | qchist=<file> Count of bases with each quality value. |
| 84 | aqhist=<file> Histogram of average read quality. |
| 85 | bqhist=<file> Quality histogram designed for box plots. |
| 86 | lhist=<file> Read length histogram. |
| 87 | phist=<file> Polymer length histogram. |
| 88 | gchist=<file> Read GC content histogram. |
| 89 | enthist=<file> Read entropy histogram. |
| 90 | ihist=<file> Insert size histogram, for paired reads in mapped sam. |
| 91 | gcbins=100 Number gchist bins. Set to 'auto' to use read length. |
| 92 | maxhistlen=6000 Set an upper bound for histogram lengths; higher uses |
| 93 | more memory. The default is 6000 for some histograms |
| 94 | and 80000 for others. |
| 95 | |
| 96 | Histograms for mapped sam/bam files only: |
| 97 | histbefore=t Calculate histograms from reads before processing. |
| 98 | ehist=<file> Errors-per-read histogram. |
| 99 | qahist=<file> Quality accuracy histogram of error rates versus quality |
| 100 | score. |
| 101 | indelhist=<file> Indel length histogram. |
| 102 | mhist=<file> Histogram of match, sub, del, and ins rates by position. |
| 103 | idhist=<file> Histogram of read count versus percent identity. |
| 104 | idbins=100 Number idhist bins. Set to 'auto' to use read length. |
| 105 | varfile=<file> Ignore substitution errors listed in this file when |
| 106 | calculating error rates. Can be generated with |
| 107 | CallVariants. |
| 108 | vcf=<file> Ignore substitution errors listed in this VCF file |
| 109 | when calculating error rates. |
| 110 | ignorevcfindels=t Also ignore indels listed in the VCF. |
| 111 | |
| 112 | Processing parameters: |
| 113 | k=27 Kmer length used for finding contaminants. Contaminants |
| 114 | shorter than k will not be found. k must be at least 1. |
| 115 | rcomp=t Look for reverse-complements of kmers in addition to |
| 116 | forward kmers. |
| 117 | maskmiddle=t (mm) Treat the middle base of a kmer as a wildcard, to |
| 118 | increase sensitivity in the presence of errors. This may |
| 119 | also be set to a number, e.g. mm=3, to mask that many bp. |
| 120 | The default mm=t corresponds to mm=1 for odd-length kmers |
| 121 | and mm=2 for even-length kmers (as of v39.04), while |
| 122 | mm=f is always equivalent to mm=0. |
| 123 | minkmerhits=1 (mkh) Reads need at least this many matching kmers |
| 124 | to be considered as matching the reference. |
| 125 | minkmerfraction=0.0 (mkf) A reads needs at least this fraction of its total |
| 126 | kmers to hit a ref, in order to be considered a match. |
| 127 | If this and minkmerhits are set, the greater is used. |
| 128 | mincovfraction=0.0 (mcf) A reads needs at least this fraction of its total |
| 129 | bases to be covered by ref kmers to be considered a match. |
| 130 | If specified, mcf overrides mkh and mkf. |
| 131 | hammingdistance=0 (hdist) Maximum Hamming distance for ref kmers (subs only). |
| 132 | Memory use is proportional to (3*K)^hdist. |
| 133 | qhdist=0 Hamming distance for query kmers; impacts speed, not memory. |
| 134 | editdistance=0 (edist) Maximum edit distance from ref kmers (subs |
| 135 | and indels). Memory use is proportional to (8*K)^edist. |
| 136 | hammingdistance2=0 (hdist2) Sets hdist for short kmers, when using mink. |
| 137 | qhdist2=0 Sets qhdist for short kmers, when using mink. |
| 138 | editdistance2=0 (edist2) Sets edist for short kmers, when using mink. |
| 139 | forbidn=f (fn) Forbids matching of read kmers containing N. |
| 140 | By default, these will match a reference 'A' if |
| 141 | hdist>0 or edist>0, to increase sensitivity. |
| 142 | removeifeitherbad=t (rieb) Paired reads get sent to 'outmatch' if either is |
| 143 | match (or either is trimmed shorter than minlen). |
| 144 | Set to false to require both. |
| 145 | trimfailures=f Instead of discarding failed reads, trim them to 1bp. |
| 146 | This makes the statistics a bit odd. |
| 147 | findbestmatch=f (fbm) If multiple matches, associate read with sequence |
| 148 | sharing most kmers. Reduces speed. |
| 149 | skipr1=f Don't do kmer-based operations on read 1. |
| 150 | skipr2=f Don't do kmer-based operations on read 2. |
| 151 | ecco=f For overlapping paired reads only. Performs error- |
| 152 | correction with BBMerge prior to kmer operations. |
| 153 | recalibrate=f (recal) Recalibrate quality scores. Requires calibration |
| 154 | matrices generated by CalcTrueQuality. |
| 155 | sam=<file,file> If recalibration is desired, and matrices have not already |
| 156 | been generated, BBDuk will create them from the sam file. |
| 157 | amino=f Run in amino acid mode. Some features have not been |
| 158 | tested, but kmer-matching works fine. Maximum k is 12. |
| 159 | |
| 160 | Speed and Memory parameters: |
| 161 | threads=auto (t) Set number of threads to use; default is number of |
| 162 | logical processors. |
| 163 | prealloc=f Preallocate memory in table. Allows faster table loading |
| 164 | and more efficient memory usage, for a large reference. |
| 165 | monitor=f Kill this process if it crashes. monitor=600,0.01 would |
| 166 | kill after 600 seconds under 1% usage. |
| 167 | minrskip=1 (mns) Force minimal skip interval when indexing reference |
| 168 | kmers. 1 means use all, 2 means use every other kmer, etc. |
| 169 | maxrskip=1 (mxs) Restrict maximal skip interval when indexing |
| 170 | reference kmers. Normally all are used for scaffolds<100kb, |
| 171 | but with longer scaffolds, up to maxrskip-1 are skipped. |
| 172 | rskip= Set both minrskip and maxrskip to the same value. |
| 173 | If not set, rskip will vary based on sequence length. |
| 174 | qskip=1 Skip query kmers to increase speed. 1 means use all. |
| 175 | speed=0 Ignore this fraction of kmer space (0-15 out of 16) in both |
| 176 | reads and reference. Increases speed and reduces memory. |
| 177 | Note: Do not use more than one of 'speed', 'qskip', and 'rskip'. |
| 178 | |
| 179 | Trimming/Filtering/Masking parameters: |
| 180 | Note - if ktrim, kmask, and ksplit are unset, the default behavior is kfilter. |
| 181 | All kmer processing modes are mutually exclusive. |
| 182 | Reads only get sent to 'outm' purely based on kmer matches in kfilter mode. |
| 183 | |
| 184 | ktrim=f Trim reads to remove bases matching reference kmers, plus |
| 185 | all bases to the left or right. |
| 186 | Values: |
| 187 | f (don't trim), |
| 188 | r (trim to the right), |
| 189 | l (trim to the left) |
| 190 | ktrimtips=0 Set this to a positive number to perform ktrim on both |
| 191 | ends, examining only the outermost X bases. |
| 192 | kmask= Replace bases matching ref kmers with another symbol. |
| 193 | Allows any non-whitespace character, and processes short |
| 194 | kmers on both ends if mink is set. 'kmask=lc' will |
| 195 | convert masked bases to lowercase. |
| 196 | maskfullycovered=f (mfc) Only mask bases that are fully covered by kmers. |
| 197 | ksplit=f For single-ended reads only. Reads will be split into |
| 198 | pairs around the kmer. If the kmer is at the end of the |
| 199 | read, it will be trimmed instead. Singletons will go to |
| 200 | out, and pairs will go to outm. Do not use ksplit with |
| 201 | other operations such as quality-trimming or filtering. |
| 202 | mink=0 Look for shorter kmers at read tips down to this length, |
| 203 | when k-trimming or masking. 0 means disabled. Enabling |
| 204 | this will disable maskmiddle. |
| 205 | qtrim=f Trim read ends to remove bases with quality below trimq. |
| 206 | Performed AFTER looking for kmers. Values: |
| 207 | rl (trim both ends), |
| 208 | f (neither end), |
| 209 | r (right end only), |
| 210 | l (left end only), |
| 211 | w (sliding window). |
| 212 | trimq=6 Regions with average quality BELOW this will be trimmed, |
| 213 | if qtrim is set to something other than f. Can be a |
| 214 | floating-point number like 7.3. |
| 215 | quantize Bin quality scores to reduce file size. quantize=2 will |
| 216 | eliminate all odd quality scores, while quantize=0,10,37 |
| 217 | will only allow qualty scores of 0, 10, or 37. |
| 218 | trimclip=f Trim soft-clipped bases from sam files. |
| 219 | minlength=10 (ml) Reads shorter than this after trimming will be |
| 220 | discarded. Pairs will be discarded if both are shorter. |
| 221 | mlf=0 (minlengthfraction) Reads shorter than this fraction of |
| 222 | original length after trimming will be discarded. |
| 223 | maxlength= Reads longer than this after trimming will be discarded. |
| 224 | minavgquality=0 (maq) Reads with average quality (after trimming) below |
| 225 | this will be discarded. |
| 226 | maqb=0 If positive, calculate maq from this many initial bases. |
| 227 | minbasequality=0 (mbq) Reads with any base below this quality (after |
| 228 | trimming) will be discarded. |
| 229 | maxns=-1 If non-negative, reads with more Ns than this |
| 230 | (after trimming) will be discarded. |
| 231 | mcb=0 (minconsecutivebases) Discard reads without at least |
| 232 | this many consecutive called bases. |
| 233 | ottm=f (outputtrimmedtomatch) Output reads trimmed to shorter |
| 234 | than minlength to outm rather than discarding. |
| 235 | tp=0 (trimpad) Trim this much extra around matching kmers. |
| 236 | tbo=f (trimbyoverlap) Trim adapters based on where paired |
| 237 | reads overlap. |
| 238 | strictoverlap=t Adjust sensitivity for trimbyoverlap mode. |
| 239 | minoverlap=14 Require this many bases of overlap for detection. |
| 240 | mininsert=40 Require insert size of at least this for overlap. |
| 241 | Should be reduced to 16 for small RNA sequencing. |
| 242 | tpe=f (trimpairsevenly) When kmer right-trimming, trim both |
| 243 | reads to the minimum length of either. |
| 244 | forcetrimleft=0 (ftl) If positive, trim bases to the left of this position |
| 245 | (exclusive, 0-based). |
| 246 | forcetrimright=0 (ftr) If positive, trim bases to the right of this position |
| 247 | (exclusive, 0-based). |
| 248 | forcetrimright2=0 (ftr2) If positive, trim this many bases on the right end. |
| 249 | forcetrimmod=0 (ftm) If positive, right-trim length to be equal to zero, |
| 250 | modulo this number. |
| 251 | restrictleft=0 If positive, only look for kmer matches in the |
| 252 | leftmost X bases. |
| 253 | restrictright=0 If positive, only look for kmer matches in the |
| 254 | rightmost X bases. |
| 255 | NOTE: restrictleft and restrictright are mutually exclusive. If trimming |
| 256 | both ends is desired, use ktrimtips. |
| 257 | mingc=0 Discard reads with GC content below this. |
| 258 | maxgc=1 Discard reads with GC content above this. |
| 259 | gcpairs=t Use average GC of paired reads. |
| 260 | Also affects gchist. |
| 261 | tossjunk=f Discard reads with invalid characters as bases. |
| 262 | swift=f Trim Swift sequences: Trailing C/T/N R1, leading G/A/N R2. |
| 263 | |
| 264 | Header-parsing parameters - these require Illumina headers: |
| 265 | chastityfilter=f (cf) Discard reads with id containing ' 1:Y:' or ' 2:Y:'. |
| 266 | barcodefilter=f Remove reads with unexpected barcodes if barcodes is set, |
| 267 | or barcodes containing 'N' otherwise. A barcode must be |
| 268 | the last part of the read header. Values: |
| 269 | t: Remove reads with bad barcodes. |
| 270 | f: Ignore barcodes. |
| 271 | crash: Crash upon encountering bad barcodes. |
| 272 | barcodes= Comma-delimited list of barcodes or files of barcodes. |
| 273 | xmin=-1 If positive, discard reads with a lesser X coordinate. |
| 274 | ymin=-1 If positive, discard reads with a lesser Y coordinate. |
| 275 | xmax=-1 If positive, discard reads with a greater X coordinate. |
| 276 | ymax=-1 If positive, discard reads with a greater Y coordinate. |
| 277 | |
| 278 | Polymer trimming: |
| 279 | trimpolya=0 If greater than 0, trim poly-A or poly-T tails of |
| 280 | at least this length on either end of reads. |
| 281 | trimpolygleft=0 If greater than 0, trim poly-G prefixes of at least this |
| 282 | length on the left end of reads. Does not trim poly-C. |
| 283 | trimpolygright=0 If greater than 0, trim poly-G tails of at least this |
| 284 | length on the right end of reads. Does not trim poly-C. |
| 285 | trimpolyg=0 This sets both left and right at once. |
| 286 | filterpolyg=0 If greater than 0, remove reads with a poly-G prefix of |
| 287 | at least this length (on the left). |
| 288 | Note: there are also equivalent poly-C flags. |
| 289 | |
| 290 | Polymer tracking: |
| 291 | pratio=base,base 'pratio=G,C' will print the ratio of G to C polymers. |
| 292 | plen=20 Length of homopolymers to count. |
| 293 | |
| 294 | Entropy/Complexity parameters: |
| 295 | entropy=-1 Set between 0 and 1 to filter reads with entropy below |
| 296 | that value. Higher is more stringent. |
| 297 | entropywindow=50 Calculate entropy using a sliding window of this length. |
| 298 | entropyk=5 Calculate entropy using kmers of this length. |
| 299 | minbasefrequency=0 Discard reads with a minimum base frequency below this. |
| 300 | entropytrim=f Values: |
| 301 | f: (false) Do not entropy-trim. |
| 302 | r: (right) Trim low entropy on the right end only. |
| 303 | l: (left) Trim low entropy on the left end only. |
| 304 | rl: (both) Trim low entropy on both ends. |
| 305 | entropymask=f Values: |
| 306 | f: (filter) Discard low-entropy sequences. |
| 307 | t: (true) Mask low-entropy parts of sequences with N. |
| 308 | lc: Change low-entropy parts of sequences to lowercase. |
| 309 | entropymark=f Mark each base with its entropy value. This is on a scale |
| 310 | of 0-41 and is reported as quality scores, so the output |
| 311 | should be fastq or fasta+qual. |
| 312 | NOTE: If set, entropytrim overrides entropymask. |
| 313 | |
| 314 | Cardinality estimation: |
| 315 | cardinality=f (loglog) Count unique kmers using the LogLog algorithm. |
| 316 | cardinalityout=f (loglogout) Count unique kmers in output reads. |
| 317 | loglogk=31 Use this kmer length for counting. |
| 318 | loglogbuckets=2048 Use this many buckets for counting. |
| 319 | khist=<file> Kmer frequency histogram; plots number of kmers versus |
| 320 | kmer depth. This is approximate. |
| 321 | khistout=<file> Kmer frequency histogram for output reads. |
| 322 | |
| 323 | Side Channel: |
| 324 | sideout=<file> Output for aligned reads. |
| 325 | sideref=phix Reference for side-channel alignment; must be a single |
| 326 | sequence and virtually repeat-free at selected k. |
| 327 | sidek1=17 Kmer length for seeding alignment to reference. |
| 328 | sidek2=13 Kmer length for seeding alignment of unaligned reads |
| 329 | with an aligned mate. |
| 330 | sideminid1=0.66 Minimum identity to accept individual alignments. |
| 331 | sideminid2=0.58 Minimum identity for aligning reads with aligned mates. |
| 332 | sidemm1=1 Middle mask length for sidek1. |
| 333 | sidemm2=1 Middle mask length for sidek2. |
| 334 | Note: The side channel is a special additional output that allows alignment |
| 335 | to a secondary reference while also doing trimming. Alignment does not affect |
| 336 | whether reads go to the normal outputs (out, outm). The main purpose is to |
| 337 | simplify pipelines that need trimmed, aligned phiX reads for recalibration. |
| 338 | |
| 339 | |
| 340 | Java Parameters: |
| 341 | |
| 342 | -Xmx This will set Java's memory usage, overriding autodetection. |
| 343 | -Xmx20g will |
| 344 | specify 20 gigs of RAM, and -Xmx200m will specify 200 megs. |
| 345 | The max is typically 85% of physical memory. |
| 346 | -eoom This flag will cause the process to exit if an |
| 347 | out-of-memory exception occurs. Requires Java 8u92+. |
| 348 | -da Disable assertions. |
| 349 | |
| 350 | Please contact Brian Bushnell at [email protected] if you encounter any problems. |
| 351 | " |
| 352 | } |
| 353 | |
| 354 | #This block allows symlinked shellscripts to correctly set classpath. |
| 355 | pushd . > /dev/null |
| 356 | DIR="${BASH_SOURCE[0]}" |
| 357 | while [ -h "$DIR" ]; do |
| 358 | cd "$(dirname "$DIR")" |
| 359 | DIR="$(readlink "$(basename "$DIR")")" |
| 360 | done |
| 361 | cd "$(dirname "$DIR")" |
| 362 | DIR="$(pwd)/" |
| 363 | popd > /dev/null |
| 364 | |
| 365 | #DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/" |
| 366 | CP="$DIR""current/" |
| 367 | JNI="-Djava.library.path=""$DIR""jni/" |
| 368 | JNI="" |
| 369 | |
| 370 | z="-Xmx1g" |
| 371 | z2="-Xms1g" |
| 372 | set=0 |
| 373 | silent=0 |
| 374 | json=0 |
| 375 | |
| 376 | if [ -z "$1" ] || [[ $1 == -h ]] || [[ $1 == --help ]]; then |
| 377 | usage |
| 378 | exit |
| 379 | fi |
| 380 | |
| 381 | calcXmx () { |
| 382 | source "$DIR""/calcmem.sh" |
| 383 | setEnvironment |
| 384 | parseXmx "$@" |
| 385 | if [[ $set == 1 ]]; then |
| 386 | return |
| 387 | fi |
| 388 | freeRam 1400m 42 |
| 389 | z="-Xmx${RAM}m" |
| 390 | z2="-Xms${RAM}m" |
| 391 | } |
| 392 | calcXmx "$@" |
| 393 | |
| 394 | bbduk() { |
| 395 | local CMD="java $EA $EOOM $z $z2 $JNI -cp $CP jgi.BBDuk $@" |
| 396 | if [[ $silent == 0 ]] && [[ $json == 0 ]]; then |
| 397 | echo $CMD >&2 |
| 398 | fi |
| 399 | eval $CMD |
| 400 | } |
| 401 | |
| 402 | bbduk "$@" |