| 1 | #!/bin/bash |
| 2 | |
| 3 | usage(){ |
| 4 | echo " |
| 5 | Written by Brian Bushnell |
| 6 | Last modified January 10, 2025 |
| 7 | |
| 8 | Description: Sorts sequences to put similar reads near each other. |
| 9 | Can be used for increased compression or error correction. |
| 10 | Please read bbmap/docs/guides/ClumpifyGuide.txt for more information. |
| 11 | |
| 12 | Usage: clumpify.sh in=<file> out=<file> reorder |
| 13 | |
| 14 | Input may be fasta or fastq, compressed or uncompressed. Cannot accept sam. |
| 15 | |
| 16 | Parameters and their defaults: |
| 17 | in=<file> Input file. |
| 18 | in2=<file> Optional input for read 2 of twin paired files. |
| 19 | out=<file> Output file. May not be standard out. |
| 20 | out2=<file> Optional output for read 2 of twin paired files. |
| 21 | groups=auto Use this many intermediate files (to save memory). |
| 22 | 1 group is fastest. Auto will estimate the number |
| 23 | of groups needed based on the file size, so it should |
| 24 | not ever run out of memory. |
| 25 | lowcomplexity=f For compressed low-complexity libraries such as RNA-seq, |
| 26 | this will more conservatively estimate how much memory |
| 27 | is needed to automatically decide the number of groups. |
| 28 | rcomp=f Give read clumps the same orientation to increase |
| 29 | compression. Should be disabled for paired reads. |
| 30 | overwrite=f (ow) Set to false to force the program to abort rather |
| 31 | than overwrite an existing file. |
| 32 | qin=auto Auto-detect input quality encoding. May be set to: |
| 33 | 33: ASCII-33 (Sanger) encoding. |
| 34 | 64: ASCII-64 (old Illumina) encoding. |
| 35 | All modern sequence is encoded as ASCII-33. |
| 36 | qout=auto Use input quality encoding as output quality encoding. |
| 37 | changequality=f (cq) If true, fix broken quality scores such as Ns with |
| 38 | Q>0. Default is false to ensure lossless compression. |
| 39 | fastawrap=70 Set to a higher number like 4000 for longer lines in |
| 40 | fasta format, which increases compression. |
| 41 | |
| 42 | Compression parameters: |
| 43 | ziplevel=6 (zl) Gzip compression level (1-11). Higher is slower. |
| 44 | Level 11 is only available if pigz is installed and is |
| 45 | extremely slow to compress, but faster to decompress. |
| 46 | Naming the output file to *.bz2 will use bzip2 instead of |
| 47 | gzip for ~9% additional compression, which requires |
| 48 | bzip2, pbzip2, or lbzip2 in the path. |
| 49 | blocksize=128 Size of blocks for pigz, in kb. Higher gives slightly |
| 50 | better compression. |
| 51 | shortname=f Make the names as short as possible. 'shortname=shrink' |
| 52 | will shorten the names where possible, but retain the |
| 53 | flowcell and barcode information. |
| 54 | reorder=f Reorder clumps for additional compression. Only valid |
| 55 | when groups=1, passes=1, and ecc=f. Possible modes: |
| 56 | f: Do not reorder clumps. |
| 57 | c: Reorder using consensus reads. Uses additional |
| 58 | time and memory. |
| 59 | p: Reorder using pair information. Requires paired |
| 60 | reads. Yields the highest compression. |
| 61 | a: Automatically choose between 'c' and 'p'. The |
| 62 | flag reorder with no argument will set 'reorder=a'. |
| 63 | quantize=f Bin the quality scores, like NextSeq. This greatly |
| 64 | increases compression, but information is lost. |
| 65 | |
| 66 | Temp file parameters: |
| 67 | compresstemp=auto (ct) Gzip temporary files. By default temp files will be |
| 68 | compressed if the output file is compressed. |
| 69 | deletetemp=t Delete temporary files. |
| 70 | deleteinput=f Delete input upon successful completion. |
| 71 | usetmpdir=f Use tmpdir for temp files. |
| 72 | tmpdir= By default, this is the environment variable TMPDIR. |
| 73 | |
| 74 | Hashing parameters: |
| 75 | k=31 Use kmers of this length (1-31). Shorter kmers may |
| 76 | increase compression, but 31 is recommended for error |
| 77 | correction. |
| 78 | mincount=0 Don't use pivot kmers with count less than this. |
| 79 | Setting mincount=2 can increase compression. |
| 80 | Increases time and memory usage. |
| 81 | seed=1 Random number generator seed for hashing. |
| 82 | Set to a negative number to use a random seed. |
| 83 | hashes=4 Use this many masks when hashing. 0 uses raw kmers. |
| 84 | Often hashes=0 increases compression, but it should |
| 85 | not be used with error-correction. |
| 86 | border=1 Do not use kmers within this many bases of read ends. |
| 87 | |
| 88 | Deduplication parameters: |
| 89 | dedupe=f Remove duplicate reads. For pairs, both must match. |
| 90 | By default, deduplication does not occur. |
| 91 | If dedupe and markduplicates are both false, none of |
| 92 | the other duplicate-related flags will have any effect. |
| 93 | markduplicates=f Don't remove; just append ' duplicate' to the name. |
| 94 | allduplicates=f Mark or remove all copies of duplicates, instead of |
| 95 | keeping the highest-quality copy. |
| 96 | addcount=f Append the number of copies to the read name. |
| 97 | Mutually exclusive with markduplicates or allduplicates. |
| 98 | entryfilter=f This assists in removing exact duplicates, which saves |
| 99 | memory in libraries that split unevenly due to huge |
| 100 | numbers of duplicates. Enabled automatically as needed. |
| 101 | subs=2 (s) Maximum substitutions allowed between duplicates. |
| 102 | subrate=0.0 (dsr) If set, the number of substitutions allowed will be |
| 103 | max(subs, subrate*min(length1, length2)) for 2 sequences. |
| 104 | allowns=t No-called bases will not be considered substitutions. |
| 105 | scanlimit=5 (scan) Continue for this many reads after encountering a |
| 106 | nonduplicate. Improves detection of inexact duplicates. |
| 107 | umi=f If reads have UMIs in the headers, require them to match |
| 108 | to consider the reads duplicates. |
| 109 | umisubs=0 Consider UMIs as matching if they have up to this many |
| 110 | mismatches. |
| 111 | containment=f Allow containments (where one sequence is shorter). |
| 112 | affix=f For containments, require one sequence to be an affix |
| 113 | (prefix or suffix) of the other. |
| 114 | optical=f If true, mark or remove optical duplicates only. |
| 115 | This means they are Illumina reads within a certain |
| 116 | distance on the flowcell. Normal Illumina names needed. |
| 117 | Also for tile-edge and well duplicates. |
| 118 | dupedist=40 (dist) Max distance to consider for optical duplicates. |
| 119 | Higher removes more duplicates but is more likely to |
| 120 | remove PCR rather than optical duplicates. |
| 121 | This is platform-specific; recommendations: |
| 122 | NextSeq 40 (and spany=t) |
| 123 | HiSeq 1T 40 |
| 124 | HiSeq 2500 40 |
| 125 | HiSeq 3k/4k 2500 |
| 126 | Novaseq6000 12000 |
| 127 | NovaseqX+ 50 |
| 128 | spany=f Allow reads to be considered optical duplicates if they |
| 129 | are on different tiles, but are within dupedist in the |
| 130 | y-axis. Should only be enabled when looking for |
| 131 | tile-edge duplicates (as in NextSeq). |
| 132 | spanx=f Like spany, but for the x-axis. Not necessary |
| 133 | for NextSeq. |
| 134 | spantiles=f Set both spanx and spany. |
| 135 | adjacent=f Limit tile-spanning to adjacent tiles (those with |
| 136 | consecutive numbers). |
| 137 | *** Thus, for NextSeq, the recommended deduplication flags are: *** |
| 138 | dedupe optical spany adjacent |
| 139 | |
| 140 | Pairing/ordering parameters (for use with error-correction): |
| 141 | unpair=f For paired reads, clump all of them rather than just |
| 142 | read 1. Destroys pairing. Without this flag, for paired |
| 143 | reads, only read 1 will be error-corrected. |
| 144 | repair=f After clumping and error-correction, restore pairing. |
| 145 | If groups>1 this will sort by name which will destroy |
| 146 | clump ordering; with a single group, clumping will |
| 147 | be retained. |
| 148 | |
| 149 | Error-correction parameters: |
| 150 | ecc=f Error-correct reads. Requires multiple passes for |
| 151 | complete correction. |
| 152 | ecco=f Error-correct paired reads via overlap before clumping. |
| 153 | passes=1 Use this many error-correction passes. 6 passes are |
| 154 | suggested, though more will be more through. |
| 155 | conservative=f Only correct the highest-confidence errors, to minimize |
| 156 | chances of eliminating a minor allele or inexact repeat. |
| 157 | aggressive=f Maximize the number of errors corrected. |
| 158 | consensus=f Output consensus sequence instead of clumps. |
| 159 | |
| 160 | Advanced error-correction parameters: |
| 161 | mincc=4 (mincountcorrect) Do not correct to alleles occuring less |
| 162 | often than this. |
| 163 | minss=4 (minsizesplit) Do not split into new clumps smaller than |
| 164 | this. |
| 165 | minsfs=0.17 (minsizefractionsplit) Do not split on pivot alleles in |
| 166 | areas with local depth less than this fraction of clump size. |
| 167 | minsfc=0.20 (minsizefractioncorrect) Do not correct in areas with local |
| 168 | depth less than this. |
| 169 | minr=30.0 (minratio) Correct to the consensus if the ratio of the |
| 170 | consensus allele to second-most-common allele is >=minr, |
| 171 | for high depth. Actual ratio used is: |
| 172 | min(minr, minro+minorCount*minrm+quality*minrqm). |
| 173 | minro=1.9 (minratiooffset) Base ratio. |
| 174 | minrm=1.8 (minratiomult) Ratio multiplier for secondary allele count. |
| 175 | minrqm=0.08 (minratioqmult) Ratio multiplier for base quality. |
| 176 | minqr=2.8 (minqratio) Do not correct bases when cq*minqr>rqsum. |
| 177 | minaqr=0.70 (minaqratio) Do not correct bases when cq*minaqr>5+rqavg. |
| 178 | minid=0.97 (minidentity) Do not correct reads with identity to |
| 179 | consensus less than this. |
| 180 | maxqadjust=0 Adjust quality scores by at most maxqadjust per pass. |
| 181 | maxqi=-1 (maxqualityincorrect) Do not correct bases with quality |
| 182 | above this (if positive). |
| 183 | maxci=-1 (maxcountincorrect) Do not correct alleles with count |
| 184 | above this (if positive). |
| 185 | findcorrelations=t Look for correlated SNPs in clumps to split into alleles. |
| 186 | maxcorrelations=12 Maximum number of eligible SNPs per clump to consider for |
| 187 | correlations. Increasing this number can reduce false- |
| 188 | positive corrections at the possible expense of speed. |
| 189 | |
| 190 | Java Parameters: |
| 191 | -Xmx This will set Java's memory usage, overriding autodetection. |
| 192 | -Xmx20g will specify 20 gigs of RAM, and -Xmx200m will specify 200 megs. |
| 193 | The max is typically 85% of physical memory. |
| 194 | -eoom This flag will cause the process to exit if an |
| 195 | out-of-memory exception occurs. Requires Java 8u92+. |
| 196 | -da Disable assertions. |
| 197 | |
| 198 | Please contact Brian Bushnell at [email protected] if you encounter any problems. |
| 199 | " |
| 200 | } |
| 201 | |
| 202 | #This block allows symlinked shellscripts to correctly set classpath. |
| 203 | pushd . > /dev/null |
| 204 | DIR="${BASH_SOURCE[0]}" |
| 205 | while [ -h "$DIR" ]; do |
| 206 | cd "$(dirname "$DIR")" |
| 207 | DIR="$(readlink "$(basename "$DIR")")" |
| 208 | done |
| 209 | cd "$(dirname "$DIR")" |
| 210 | DIR="$(pwd)/" |
| 211 | popd > /dev/null |
| 212 | |
| 213 | #DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/" |
| 214 | CP="$DIR""current/" |
| 215 | |
| 216 | z="-Xmx2g" |
| 217 | z2="-Xms2g" |
| 218 | set=0 |
| 219 | |
| 220 | if [ -z "$1" ] || [[ $1 == -h ]] || [[ $1 == --help ]]; then |
| 221 | usage |
| 222 | exit |
| 223 | fi |
| 224 | |
| 225 | calcXmx () { |
| 226 | source "$DIR""/calcmem.sh" |
| 227 | setEnvironment |
| 228 | parseXmx "$@" |
| 229 | if [[ $set == 1 ]]; then |
| 230 | return |
| 231 | fi |
| 232 | #Note that this uses slighly less (82%) of memory than normal to account for multiple pigz instances. |
| 233 | freeRam 2000m 82 |
| 234 | z="-Xmx${RAM}m" |
| 235 | z2="-Xms${RAM}m" |
| 236 | } |
| 237 | calcXmx "$@" |
| 238 | |
| 239 | clumpify() { |
| 240 | local CMD="java $EA $EOOM $z $z2 -cp $CP clump.Clumpify $@" |
| 241 | echo $CMD >&2 |
| 242 | eval $CMD |
| 243 | } |
| 244 | |
| 245 | clumpify "$@" |