| 1 | #!/bin/bash |
| 2 | |
| 3 | usage(){ |
| 4 | echo " |
| 5 | Written by Brian Bushnell and Jonathan Rood |
| 6 | Last modified October 8, 2024 |
| 7 | |
| 8 | Description: Merges paired reads into single reads by overlap detection. |
| 9 | With sufficient coverage, can merge nonoverlapping reads by kmer extension. |
| 10 | Kmer modes (Tadpole or Bloom Filter) require much more memory, and should |
| 11 | be used with the bbmerge-auto.sh script rather than bbmerge.sh. |
| 12 | Please read bbmap/docs/guides/BBMergeGuide.txt for more information. |
| 13 | |
| 14 | Usage for interleaved files: bbmerge.sh in=<reads> out=<merged reads> outu=<unmerged reads> |
| 15 | Usage for paired files: bbmerge.sh in1=<read1> in2=<read2> out=<merged reads> outu1=<unmerged1> outu2=<unmerged2> |
| 16 | |
| 17 | Input may be stdin or a file, fasta or fastq, raw or gzipped. |
| 18 | |
| 19 | Input parameters: |
| 20 | in=null Primary input. 'in2' will specify a second file. |
| 21 | interleaved=auto May be set to true or false to override autodetection of |
| 22 | whether the input file as interleaved. |
| 23 | reads=-1 Quit after this many read pairs (-1 means all). |
| 24 | |
| 25 | Output parameters: |
| 26 | out=<file> File for merged reads. 'out2' will specify a second file. |
| 27 | outu=<file> File for unmerged reads. 'outu2' will specify a second file. |
| 28 | outinsert=<file> (outi) File to write read names and insert sizes. |
| 29 | outadapter=<file> (outa) File to write consensus adapter sequences. |
| 30 | outc=<file> File to write input read kmer cardinality estimate. |
| 31 | ihist=<file> (hist) Insert length histogram output file. |
| 32 | nzo=t Only print histogram bins with nonzero values. |
| 33 | showhiststats=t Print extra header lines with statistical information. |
| 34 | ziplevel=2 Set to 1 (lowest) through 9 (max) to change compression |
| 35 | level; lower compression is faster. |
| 36 | ordered=f Output reads in same order as input. |
| 37 | mix=f Output both the merged (or mergable) and unmerged reads |
| 38 | in the same file (out=). Useful for ecco mode. |
| 39 | |
| 40 | Trimming/Filtering parameters: |
| 41 | qtrim=f Trim read ends to remove bases with quality below minq. |
| 42 | Trims BEFORE merging. |
| 43 | Values: t (trim both ends), |
| 44 | f (neither end), |
| 45 | r (right end only), |
| 46 | l (left end only). |
| 47 | qtrim2=f May be specified instead of qtrim to perform trimming |
| 48 | only if merging is unsuccessful, then retry merging. |
| 49 | trimq=10 Trim quality threshold. This may be a comma-delimited |
| 50 | list (ascending) to try multiple values. |
| 51 | minlength=1 (ml) Reads shorter than this after trimming, but before |
| 52 | merging, will be discarded. Pairs will be discarded only |
| 53 | if both are shorter. |
| 54 | maxlength=-1 Reads with longer insert sizes will be discarded. |
| 55 | tbo=f (trimbyoverlap) Trim overlapping reads to remove |
| 56 | rightmost (3') non-overlapping portion, instead of joining. |
| 57 | minavgquality=0 (maq) Reads with average quality below this, after |
| 58 | trimming, will not be attempted to be merged. |
| 59 | maxexpectederrors=0 (mee) If positive, reads with more combined expected |
| 60 | errors than this will not be attempted to be merged. |
| 61 | forcetrimleft=0 (ftl) If nonzero, trim left bases of the read to |
| 62 | this position (exclusive, 0-based). |
| 63 | forcetrimright=0 (ftr) If nonzero, trim right bases of the read |
| 64 | after this position (exclusive, 0-based). |
| 65 | forcetrimright2=0 (ftr2) If positive, trim this many bases on the right end. |
| 66 | forcetrimmod=5 (ftm) If positive, trim length to be equal to |
| 67 | zero modulo this number. |
| 68 | ooi=f Output only incorrectly merged reads, for testing. |
| 69 | trimpolya=t Trim trailing poly-A tail from adapter output. Only |
| 70 | affects outadapter. This also trims poly-A followed |
| 71 | by poly-G, which occurs on NextSeq. |
| 72 | |
| 73 | Processing Parameters: |
| 74 | usejni=f (jni) Do overlapping in C code, which is faster. Requires |
| 75 | compiling the C code; details are in /jni/README.txt. |
| 76 | However, the jni path is currently disabled. |
| 77 | merge=t Create merged reads. If set to false, you can still |
| 78 | generate an insert histogram. |
| 79 | ecco=f Error-correct the overlapping part, but don't merge. |
| 80 | trimnonoverlapping=f (tno) Trim all non-overlapping portions, leaving only |
| 81 | consensus sequence. By default, only sequence to the |
| 82 | right of the overlap (adapter sequence) is trimmed. |
| 83 | useoverlap=t Attempt find the insert size using read overlap. |
| 84 | mininsert=15 Minimum insert size to merge reads. |
| 85 | mininsert0=12 Insert sizes less than this will not be considered. |
| 86 | Must be less than or equal to mininsert. |
| 87 | minoverlap=12 Minimum number of overlapping bases to allow merging. |
| 88 | minoverlap0=8 Overlaps shorter than this will not be considered. |
| 89 | Must be less than or equal to minoverlap. |
| 90 | minq=9 Ignore bases with quality below this. |
| 91 | maxq=41 Cap output quality scores at this. |
| 92 | quantize=1 Set to a higher number to eliminate some quality scores |
| 93 | for a lower output filesize. |
| 94 | entropy=t Increase the minimum overlap requirement for low- |
| 95 | complexity reads. |
| 96 | efilter=6 Ban overlaps with over this many times the expected |
| 97 | number of errors. Lower is more strict. -1 disables. |
| 98 | pfilter=0.00004 Ban improbable overlaps. Higher is more strict. 0 will |
| 99 | disable the filter; 1 will allow only perfect overlaps. |
| 100 | kfilter=0 Ban overlaps that create kmers with count below |
| 101 | this value (0 disables). If this is used minprob should |
| 102 | probably be set to 0. Requires good coverage. |
| 103 | ouq=f Calculate best overlap using quality values. |
| 104 | owq=t Calculate best overlap without using quality values. |
| 105 | usequality=t If disabled, quality values are completely ignored, |
| 106 | both for overlap detection and filtering. May be useful |
| 107 | for data with inaccurate quality values. |
| 108 | iupacton=f (itn) Change ambiguous IUPAC symbols to N. |
| 109 | adapter= Specify the adapter sequences used for these reads, if |
| 110 | known; this can be a fasta file or a literal sequence. |
| 111 | Read 1 and 2 can have adapters specified independently |
| 112 | with the adapter1 and adapter2 flags. adapter=default |
| 113 | will use a list of common adapter sequences. |
| 114 | |
| 115 | Neural Network Mode: |
| 116 | nn=t Use a neural network for increased merging accuracy. |
| 117 | This is highly recommended, but will conflict with |
| 118 | strictness and ratiomode flags. Stringency in nn mode |
| 119 | should be adjusted via the cutoff flag instead. |
| 120 | cutoff=0.872857 Merge reads with nn score above this value. Lower will |
| 121 | increase the merge rate at the cost of false positives. |
| 122 | net=<file> Optional network to specify (for developer use); the |
| 123 | default is bbmap/resources/bbmerge.bbnet. |
| 124 | |
| 125 | Ratio Mode: |
| 126 | ratiomode=t Score overlaps based on the ratio of matching to |
| 127 | mismatching bases. |
| 128 | maxratio=0.09 Max error rate; higher increases merge rate. |
| 129 | ratiomargin=5.5 Lower increases merge rate; min is 1. |
| 130 | ratiooffset=0.55 Lower increases merge rate; min is 0. |
| 131 | maxmismatches=20 Maximum mismatches allowed in overlapping region. |
| 132 | ratiominoverlapreduction=3 This is the difference between minoverlap in |
| 133 | flat mode and minoverlap in ratio mode; generally, |
| 134 | minoverlap should be lower in ratio mode. |
| 135 | minsecondratio=0.1 Cutoff for second-best overlap ratio. |
| 136 | forcemerge=f Disable all filters and just merge everything |
| 137 | (not recommended). |
| 138 | |
| 139 | Strictness (these are mutually exclusive macros that set other parameters): |
| 140 | strict=f Decrease false positive rate and merging rate. |
| 141 | verystrict=f (vstrict) Greatly decrease FP and merging rate. |
| 142 | ultrastrict=f (ustrict) Decrease FP and merging rate even more. |
| 143 | maxstrict=f (xstrict) Maximally decrease FP and merging rate. |
| 144 | loose=f Increase false positive rate and merging rate. |
| 145 | veryloose=f (vloose) Greatly increase FP and merging rate. |
| 146 | ultraloose=f (uloose) Increase FP and merging rate even more. |
| 147 | maxloose=f (xloose) Maximally decrease FP and merging rate. |
| 148 | fast=f Fastest possible mode; less accurate. |
| 149 | |
| 150 | Tadpole Parameters (for read extension and error-correction): |
| 151 | *Note: These require more memory and should be run with bbmerge-auto.sh.* |
| 152 | k=31 Kmer length. 31 (or less) is fastest and uses the least |
| 153 | memory, but higher values may be more accurate. |
| 154 | 60 tends to work well for 150bp reads. |
| 155 | extend=0 Extend reads to the right this much before merging. |
| 156 | Requires sufficient (>5x) kmer coverage. |
| 157 | extend2=0 Extend reads this much only after a failed merge attempt, |
| 158 | or in rem/rsem mode. |
| 159 | iterations=1 (ei) Iteratively attempt to extend by extend2 distance |
| 160 | and merge up to this many times. |
| 161 | rem=f (requireextensionmatch) Do not merge if the predicted |
| 162 | insert size differs before and after extension. |
| 163 | However, if only the extended reads overlap, then that |
| 164 | insert will be used. Requires setting extend2. |
| 165 | rsem=f (requirestrictextensionmatch) Similar to rem but stricter. |
| 166 | Reads will only merge if the predicted insert size before |
| 167 | and after extension match. Requires setting extend2. |
| 168 | Enables the lowest possible false-positive rate. |
| 169 | ecctadpole=f (ecct) If reads fail to merge, error-correct with Tadpole |
| 170 | and try again. This happens prior to extend2. |
| 171 | reassemble=t If ecct is enabled, use Tadpole's reassemble mode for |
| 172 | error correction. Alternatives are pincer and tail. |
| 173 | removedeadends (shave) Remove kmers leading to dead ends. |
| 174 | removebubbles (rinse) Remove kmers in error bubbles. |
| 175 | mindepthseed=3 (mds) Minimum kmer depth to begin extension. |
| 176 | mindepthextend=2 (mde) Minimum kmer depth continue extension. |
| 177 | branchmult1=20 Min ratio of 1st to 2nd-greatest path depth at high depth. |
| 178 | branchmult2=3 Min ratio of 1st to 2nd-greatest path depth at low depth. |
| 179 | branchlower=3 Max value of 2nd-greatest path depth to be considered low. |
| 180 | ibb=t Ignore backward branches when extending. |
| 181 | extra=<file> A file or comma-delimited list of files of reads to use |
| 182 | for kmer counting, but not for merging or output. |
| 183 | prealloc=f Pre-allocate memory rather than dynamically growing; |
| 184 | faster and more memory-efficient for large datasets. |
| 185 | A float fraction (0-1) may be specified, default 1. |
| 186 | prefilter=0 If set to a positive integer, use a countmin sketch to |
| 187 | ignore kmers with depth of that value or lower, to |
| 188 | reduce memory usage. |
| 189 | filtermem=0 Allows manually specifying prefilter memory in bytes, for |
| 190 | deterministic runs. 0 will set it automatically. |
| 191 | minprob=0.5 Ignore kmers with overall probability of correctness |
| 192 | below this, to reduce memory usage. |
| 193 | minapproxoverlap=26 For rem mode, do not merge reads if the extended reads |
| 194 | indicate that the raw reads should have overlapped by |
| 195 | at least this much, but no overlap was found. |
| 196 | |
| 197 | |
| 198 | Bloom Filter Parameters (for kmer operations with less memory than Tadpole) |
| 199 | *Note: These require more memory and should be run with bbmerge-auto.sh.* |
| 200 | eccbloom=f (eccb) If reads fail to merge, error-correct with bbcms |
| 201 | and try again. |
| 202 | testmerge=f Test kmer counts around the read merge junctions. If |
| 203 | it appears that the merge created new errors, undo it. |
| 204 | This reduces the false-positive rate, but not as much as |
| 205 | rem or rsem. |
| 206 | |
| 207 | Java Parameters: |
| 208 | -Xmx This will set Java's memory usage, |
| 209 | overriding autodetection. |
| 210 | For example, -Xmx400m will specify 400 MB RAM. |
| 211 | -eoom This flag will cause the process to exit if an |
| 212 | out-of-memory exception occurs. Requires Java 8u92+. |
| 213 | -da Disable assertions. |
| 214 | |
| 215 | Please contact Brian Bushnell at [email protected] if you encounter any problems. |
| 216 | " |
| 217 | } |
| 218 | |
| 219 | #This block allows symlinked shellscripts to correctly set classpath. |
| 220 | pushd . > /dev/null |
| 221 | DIR="${BASH_SOURCE[0]}" |
| 222 | while [ -h "$DIR" ]; do |
| 223 | cd "$(dirname "$DIR")" |
| 224 | DIR="$(readlink "$(basename "$DIR")")" |
| 225 | done |
| 226 | cd "$(dirname "$DIR")" |
| 227 | DIR="$(pwd)/" |
| 228 | popd > /dev/null |
| 229 | |
| 230 | #DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/" |
| 231 | CP="$DIR""current/" |
| 232 | JNI="-Djava.library.path=""$DIR""jni/" |
| 233 | #JNI="" |
| 234 | |
| 235 | z="-Xmx1000m" |
| 236 | z2="-Xms1000m" |
| 237 | set=0 |
| 238 | |
| 239 | if [ -z "$1" ] || [[ $1 == -h ]] || [[ $1 == --help ]]; then |
| 240 | usage |
| 241 | exit |
| 242 | fi |
| 243 | |
| 244 | calcXmx () { |
| 245 | source "$DIR""/calcmem.sh" |
| 246 | setEnvironment |
| 247 | parseXmx "$@" |
| 248 | } |
| 249 | calcXmx "$@" |
| 250 | |
| 251 | function merge() { |
| 252 | local CMD="java $EA $EOOM $z $z2 $JNI $SIMD -cp $CP jgi.BBMerge $@" |
| 253 | echo $CMD >&2 |
| 254 | eval $CMD |
| 255 | } |
| 256 | |
| 257 | merge "$@" |