| 1 | #!/bin/bash |
| 2 | |
| 3 | usage(){ |
| 4 | echo " |
| 5 | Written by Brian Bushnell |
| 6 | Last modified November 22, 2023 |
| 7 | |
| 8 | Description: Reformats reads to change ASCII quality encoding, interleaving, file format, or compression format. |
| 9 | Optionally performs additional functions such as quality trimming, subsetting, and subsampling. |
| 10 | Supports fastq, fasta, fasta+qual, scarf, oneline, sam, bam, gzip, bz2. |
| 11 | Please read bbmap/docs/guides/ReformatGuide.txt for more information. |
| 12 | |
| 13 | Usage: reformat.sh in=<file> in2=<file2> out=<outfile> out2=<outfile2> |
| 14 | |
| 15 | in2 and out2 are for paired reads and are optional. |
| 16 | If input is paired and there is only one output file, it will be written interleaved. |
| 17 | |
| 18 | Parameters and their defaults: |
| 19 | |
| 20 | ow=f (overwrite) Overwrites files that already exist. |
| 21 | app=f (append) Append to files that already exist. |
| 22 | zl=4 (ziplevel) Set compression level, 1 (low) to 9 (max). |
| 23 | int=f (interleaved) Determines whether INPUT file is considered interleaved. |
| 24 | fastawrap=70 Length of lines in fasta output. |
| 25 | fastareadlen=0 Set to a non-zero number to break fasta files into reads of at most this length. |
| 26 | fastaminlen=1 Ignore fasta reads shorter than this. |
| 27 | qin=auto ASCII offset for input quality. May be 33 (Sanger), 64 (Illumina), or auto. |
| 28 | qout=auto ASCII offset for output quality. May be 33 (Sanger), 64 (Illumina), or auto (same as input). |
| 29 | qfake=30 Quality value used for fasta to fastq reformatting. |
| 30 | qfin=<.qual file> Read qualities from this qual file, for the reads coming from 'in=<fasta file>' |
| 31 | qfin2=<.qual file> Read qualities from this qual file, for the reads coming from 'in2=<fasta file>' |
| 32 | qfout=<.qual file> Write qualities from this qual file, for the reads going to 'out=<fasta file>' |
| 33 | qfout2=<.qual file> Write qualities from this qual file, for the reads coming from 'out2=<fasta file>' |
| 34 | outsingle=<file> (outs) If a read is longer than minlength and its mate is shorter, the longer one goes here. |
| 35 | deleteinput=f Delete input upon successful completion. |
| 36 | ref=<file> Optional reference fasta for sam processing. |
| 37 | |
| 38 | Processing Parameters: |
| 39 | |
| 40 | verifypaired=f (vpair) When true, checks reads to see if the names look paired. Prints an error message if not. |
| 41 | verifyinterleaved=f (vint) sets 'vpair' to true and 'interleaved' to true. |
| 42 | allowidenticalnames=f (ain) When verifying pair names, allows identical names, instead of requiring /1 and /2 or 1: and 2: |
| 43 | tossbrokenreads=f (tbr) Discard reads that have different numbers of bases and qualities. By default this will be detected and cause a crash. |
| 44 | ignorebadquality=f (ibq) Fix out-of-range quality values instead of crashing with a warning. |
| 45 | addslash=f Append ' /1' and ' /2' to read names, if not already present. Please include the flag 'int=t' if the reads are interleaved. |
| 46 | spaceslash=t Put a space before the slash in addslash mode. |
| 47 | addcolon=f Append ' 1:' and ' 2:' to read names, if not already present. Please include the flag 'int=t' if the reads are interleaved. |
| 48 | underscore=f Change whitespace in read names to underscores. |
| 49 | rcomp=f (rc) Reverse-complement reads. |
| 50 | rcompmate=f (rcm) Reverse-complement read 2 only. |
| 51 | comp=f (complement) Reverse-complement reads. |
| 52 | changequality=t (cq) N bases always get a quality of 0 and ACGT bases get a min quality of 2. |
| 53 | quantize=f Quantize qualities to a subset of values like NextSeq. Can also be used with comma-delimited list, like quantize=0,8,13,22,27,32,37 |
| 54 | tuc=f (touppercase) Change lowercase letters in reads to uppercase. |
| 55 | uniquenames=f Make duplicate names unique by appending _<number>. |
| 56 | remap= A set of pairs: remap=CTGN will transform C>T and G>N. |
| 57 | Use remap1 and remap2 to specify read 1 or 2. |
| 58 | iupacToN=f (itn) Convert non-ACGTN symbols to N. |
| 59 | monitor=f Kill this process if it crashes. monitor=600,0.01 would kill after 600 seconds under 1% usage. |
| 60 | crashjunk=t Crash when encountering reads with invalid bases. |
| 61 | tossjunk=f Discard reads with invalid characters as bases. |
| 62 | fixjunk=f Convert invalid bases to N (or X for amino acids). |
| 63 | dotdashxton=f Specifically convert . - and X to N (or X for amino acids). |
| 64 | fixheaders=f Convert nonstandard header characters to standard ASCII. |
| 65 | recalibrate=f (recal) Recalibrate quality scores. Must first generate matrices with CalcTrueQuality. |
| 66 | maxcalledquality=41 Quality scores capped at this upper bound. |
| 67 | mincalledquality=2 Quality scores of ACGT bases will be capped at lower bound. |
| 68 | trimreaddescription=f (trd) Trim the names of reads after the first whitespace. |
| 69 | trimrname=f For sam/bam files, trim rname/rnext fields after the first space. |
| 70 | fixheaders=f Replace characters in headers such as space, *, and | to make them valid file names. |
| 71 | warnifnosequence=t For fasta, issue a warning if a sequenceless header is encountered. |
| 72 | warnfirsttimeonly=t Issue a warning for only the first sequenceless header. |
| 73 | utot=f Convert U to T (for RNA -> DNA translation). |
| 74 | padleft=0 Pad the left end of sequences with this many symbols. |
| 75 | padright=0 Pad the right end of sequences with this many symbols. |
| 76 | pad=0 Set padleft and padright to the same value. |
| 77 | padsymbol=N Symbol to use for padding. |
| 78 | |
| 79 | Histogram output parameters: |
| 80 | |
| 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 | gchist=<file> Read GC content histogram. |
| 88 | gcbins=100 Number gchist bins. Set to 'auto' to use read length. |
| 89 | gcplot=f Add a graphical representation to the gchist. |
| 90 | maxhistlen=6000 Set an upper bound for histogram lengths; higher uses more memory. |
| 91 | The default is 6000 for some histograms and 80000 for others. |
| 92 | |
| 93 | Histograms for sam files only (requires sam format 1.4 or higher): |
| 94 | |
| 95 | ehist=<file> Errors-per-read histogram. |
| 96 | qahist=<file> Quality accuracy histogram of error rates versus quality score. |
| 97 | indelhist=<file> Indel length histogram. |
| 98 | mhist=<file> Histogram of match, sub, del, and ins rates by read location. |
| 99 | ihist=<file> Insert size histograms. Requires paired reads in a sam file. |
| 100 | idhist=<file> Histogram of read count versus percent identity. |
| 101 | idbins=100 Number idhist bins. Set to 'auto' to use read length. |
| 102 | |
| 103 | Sampling parameters: |
| 104 | |
| 105 | reads=-1 Set to a positive number to only process this many INPUT reads (or pairs), then quit. |
| 106 | skipreads=-1 Skip (discard) this many INPUT reads before processing the rest. |
| 107 | samplerate=1 Randomly output only this fraction of reads; 1 means sampling is disabled. |
| 108 | sampleseed=-1 Set to a positive number to use that prng seed for sampling (allowing deterministic sampling). |
| 109 | samplereadstarget=0 (srt) Exact number of OUTPUT reads (or pairs) desired. |
| 110 | samplebasestarget=0 (sbt) Exact number of OUTPUT bases desired. |
| 111 | Important: srt/sbt flags should not be used with stdin, samplerate, qtrim, minlength, or minavgquality. |
| 112 | upsample=f Allow srt/sbt to upsample (duplicate reads) when the target is greater than input. |
| 113 | prioritizelength=f If true, calculate a length threshold to reach the target, and retain all reads of at least that length (must set srt or sbt). |
| 114 | |
| 115 | Trimming and filtering parameters: |
| 116 | |
| 117 | qtrim=f Trim read ends to remove bases with quality below trimq. |
| 118 | Values: t (trim both ends), f (neither end), r (right end only), l (left end only), w (sliding window). |
| 119 | trimq=6 Regions with average quality BELOW this will be trimmed. Can be a floating-point number like 7.3. |
| 120 | minlength=0 (ml) Reads shorter than this after trimming will be discarded. Pairs will be discarded only if both are shorter. |
| 121 | mlf=0 (mlf) Reads shorter than this fraction of original length after trimming will be discarded. |
| 122 | maxlength=0 If nonzero, reads longer than this after trimming will be discarded. |
| 123 | breaklength=0 If nonzero, reads longer than this will be broken into multiple reads of this length. Does not work for paired reads. |
| 124 | requirebothbad=t (rbb) Only discard pairs if both reads are shorter than minlen. |
| 125 | invertfilters=f (invert) Output failing reads instead of passing reads. |
| 126 | minavgquality=0 (maq) Reads with average quality (after trimming) below this will be discarded. |
| 127 | maqb=0 If positive, calculate maq from this many initial bases. |
| 128 | chastityfilter=f (cf) Reads with names containing ' 1:Y:' or ' 2:Y:' will be discarded. |
| 129 | barcodefilter=f Remove reads with unexpected barcodes if barcodes is set, or barcodes containing 'N' otherwise. |
| 130 | A barcode must be the last part of the read header. |
| 131 | barcodes= Comma-delimited list of barcodes or files of barcodes. |
| 132 | maxns=-1 If 0 or greater, reads with more Ns than this (after trimming) will be discarded. |
| 133 | minconsecutivebases=0 (mcb) Discard reads without at least this many consecutive called bases. |
| 134 | forcetrimleft=0 (ftl) If nonzero, trim left bases of the read to this position (exclusive, 0-based). |
| 135 | forcetrimright=-1 (ftr) If nonnegative, trim right bases of the read after this position (exclusive, 0-based). |
| 136 | forcetrimright2=0 (ftr2) If positive, trim this many bases on the right end. |
| 137 | forcetrimmod=5 (ftm) If positive, trim length to be equal to zero modulo this number. |
| 138 | mingc=0 Discard reads with GC content below this. |
| 139 | maxgc=1 Discard reads with GC content above this. |
| 140 | gcpairs=t Use average GC of paired reads. |
| 141 | Also affects gchist. |
| 142 | |
| 143 | Tag-filtering parameters: |
| 144 | |
| 145 | tag= Look for this tag in the header to filter by the next value. To filter reads |
| 146 | with a header like 'foo,depth=5.5,bar' where you only want depths |
| 147 | of at least 3, the necessary flags would be 'tag=depth= minvalue=3 delimiter=,' |
| 148 | delimiter= Character after the end of the value, such as delimiter=X. Control and |
| 149 | whitespace symbols may be spelled out, like delimiter=tab or delimiter=pipe. |
| 150 | The tag may contain the delimiter. If the value is the last term in the header, |
| 151 | the delimiter doesn't matter but is still required. |
| 152 | minvalue= If set, only accept a numeric value of at least this. |
| 153 | maxvalue= If set, only accept a numeric value of at most this. |
| 154 | value= If set, only accept a string value of exactly this. |
| 155 | |
| 156 | Illumina-specific parameters: |
| 157 | top=true Include reads from the top of the flowcell. |
| 158 | bottom=true Include reads from the bottom of the flowcell. |
| 159 | |
| 160 | Sam and bam processing options: |
| 161 | |
| 162 | mappedonly=f Toss unmapped reads. |
| 163 | unmappedonly=f Toss mapped reads. |
| 164 | pairedonly=f Toss reads that are not mapped as proper pairs. |
| 165 | unpairedonly=f Toss reads that are mapped as proper pairs. |
| 166 | primaryonly=f Toss secondary alignments. Set this to true for sam to fastq conversion. |
| 167 | minmapq=-1 If non-negative, toss reads with mapq under this. |
| 168 | maxmapq=-1 If non-negative, toss reads with mapq over this. |
| 169 | requiredbits=0 (rbits) Toss sam lines with any of these flag bits unset. Similar to samtools -f. |
| 170 | filterbits=0 (fbits) Toss sam lines with any of these flag bits set. Similar to samtools -F. |
| 171 | stoptag=f Set to true to write a tag indicating read stop location, prefixed by YS:i: |
| 172 | sam= Set to 'sam=1.3' to convert '=' and 'X' cigar symbols (from sam 1.4+ format) to 'M'. |
| 173 | Set to 'sam=1.4' to convert 'M' to '=' and 'X' (sam=1.4 requires MD tags to be present, or ref to be specified). |
| 174 | |
| 175 | Sam and bam alignment filtering options: |
| 176 | These require = and X symbols in cigar strings, or MD tags, or a reference fasta. |
| 177 | -1 means disabled; to filter reads with any of a symbol type, set to 0. |
| 178 | |
| 179 | subfilter=-1 Discard reads with more than this many substitutions. |
| 180 | minsubs=-1 Discard reads with fewer than this many substitutions. |
| 181 | insfilter=-1 Discard reads with more than this many insertions. |
| 182 | delfilter=-1 Discard reads with more than this many deletions. |
| 183 | indelfilter=-1 Discard reads with more than this many indels. |
| 184 | editfilter=-1 Discard reads with more than this many edits. |
| 185 | inslenfilter=-1 Discard reads with an insertion longer than this. |
| 186 | dellenfilter=-1 Discard reads with a deletion longer than this. |
| 187 | minidfilter=-1.0 Discard reads with identity below this (0-1). |
| 188 | maxidfilter=1.0 Discard reads with identity above this (0-1). |
| 189 | clipfilter=-1 Discard reads with more than this many soft-clipped bases. |
| 190 | |
| 191 | Kmer counting and cardinality estimation: |
| 192 | k=0 If positive, count the total number of kmers. |
| 193 | cardinality=f (loglog) Count unique kmers using the LogLog algorithm. |
| 194 | loglogbuckets=1999 Use this many buckets for cardinality estimation. |
| 195 | |
| 196 | Shortcuts: |
| 197 | The # symbol will be substituted for 1 and 2. The % symbol in out will be substituted for input name minus extensions. |
| 198 | For example: |
| 199 | reformat.sh in=read#.fq out=%.fa |
| 200 | ...is equivalent to: |
| 201 | reformat.sh in1=read1.fq in2=read2.fq out1=read1.fa out2=read2.fa |
| 202 | |
| 203 | Java Parameters: |
| 204 | -Xmx This will set Java's memory usage, overriding autodetection. |
| 205 | -Xmx20g will specify 20 gigs of RAM, and -Xmx200m will specify 200 megs. |
| 206 | The max is typically 85% of physical memory. |
| 207 | -eoom This flag will cause the process to exit if an out-of-memory exception occurs. Requires Java 8u92+. |
| 208 | -da Disable assertions. |
| 209 | |
| 210 | Please contact Brian Bushnell at [email protected] if you encounter any problems. |
| 211 | " |
| 212 | } |
| 213 | |
| 214 | #This block allows symlinked shellscripts to correctly set classpath. |
| 215 | pushd . > /dev/null |
| 216 | DIR="${BASH_SOURCE[0]}" |
| 217 | while [ -h "$DIR" ]; do |
| 218 | cd "$(dirname "$DIR")" |
| 219 | DIR="$(readlink "$(basename "$DIR")")" |
| 220 | done |
| 221 | cd "$(dirname "$DIR")" |
| 222 | DIR="$(pwd)/" |
| 223 | popd > /dev/null |
| 224 | |
| 225 | #DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/" |
| 226 | CP="$DIR""current/" |
| 227 | |
| 228 | z="-Xmx300m" |
| 229 | z2="-Xms300m" |
| 230 | set=0 |
| 231 | |
| 232 | if [ -z "$1" ] || [[ $1 == -h ]] || [[ $1 == --help ]]; then |
| 233 | usage |
| 234 | exit |
| 235 | fi |
| 236 | |
| 237 | calcXmx () { |
| 238 | source "$DIR""/calcmem.sh" |
| 239 | setEnvironment |
| 240 | parseXmx "$@" |
| 241 | } |
| 242 | calcXmx "$@" |
| 243 | |
| 244 | function reformat() { |
| 245 | local CMD="java $EA $EOOM $z $z2 -cp $CP jgi.ReformatReads $@" |
| 246 | echo $CMD >&2 |
| 247 | eval $CMD |
| 248 | } |
| 249 | |
| 250 | reformat "$@" |