| 1 | #!/bin/bash |
| 2 | |
| 3 | usage(){ |
| 4 | echo " |
| 5 | Written by Brian Bushnell |
| 6 | Last modified November 18, 2024 |
| 7 | |
| 8 | Description: Demultiplexes sequencer reads into multiple files based on |
| 9 | their barcodes. Uses statistical analysis to ensure optimal yield and |
| 10 | minimal crosstalk in the presence of errors. Barcodes (indexes) must be |
| 11 | embedded in read headers, and the expected barcodes must be provided |
| 12 | as a text file with one barcode (or barcode pair) per line. |
| 13 | |
| 14 | Usage example for single-ended or interleaved files: |
| 15 | |
| 16 | novademux.sh in=reads.fq out=out_%.fq outu=unknown.fq expected=barcodes.txt |
| 17 | |
| 18 | For twin files: |
| 19 | novademux.sh in=in_#.fq out=out_%_#.fq.gz outu=unk_#.fq expected=barcodes.txt |
| 20 | |
| 21 | |
| 22 | File Parameters: |
| 23 | in=<file> Input file. |
| 24 | in2=<file> If input reads are paired in twin files, use in2 for the |
| 25 | second file. You can alternatively use the # symbol, |
| 26 | e.g. 'in=read_#.fastq.gz', which is equivalent to |
| 27 | 'in1=read_1.fastq.gz in2=read_2.fastq.gz'. |
| 28 | out=<file> Output files for reads with matched headers (must contain % |
| 29 | symbol). For example, out=out_%.fq with indexes XX and YY |
| 30 | would create out_XX.fq and out_YY.fq. If twin files for |
| 31 | paired reads are desired, use the # symbol. For example, |
| 32 | out=out_%_#.fq in this case would create out_XX_1.fq, |
| 33 | out_XX_2.fq, out_YY_1.fq, and out_YY_2.fq. |
| 34 | outu=<file> Output file for reads with unmatched headers. |
| 35 | stats=<file> Print statistics about how many reads went to each file. |
| 36 | expected= List of barcodes (or files containing containing barcodes) |
| 37 | to parse from read headers. Files should contain one barcode |
| 38 | per line. For example, 'expected=barcodes.txt' or |
| 39 | 'expected=ACGTACGT,GGTTAACC,AACCGGTT'. This list must be |
| 40 | contain all pooled barcodes to ensure accuracy, including |
| 41 | PhiX if present. |
| 42 | writeempty=t Write empty files for expected but absent barcodes. |
| 43 | subset= Optional list of barcodes when only some output files are |
| 44 | desired; only demultiplex these libraries. |
| 45 | nosplit=f When true, dump all reads to outu instead of individual files. |
| 46 | rename=f When true, append the assigned barcode (or 'unknown') to |
| 47 | each read header, after a tab. Can be used in conjunction |
| 48 | with nosplit to simply label reads. |
| 49 | rc1=f Reverse-complement index1 from expected and samplemap. |
| 50 | rc2=f Reverse-complement index2 from expected and samplemap. |
| 51 | addpolyg=f It is recommended to set this to true on a platform where |
| 52 | no signal is read as G. This will add poly-G as a dummy |
| 53 | expected barcode. If no signal yields a different base call, |
| 54 | use the appropriate flag (addpolyc, etc). |
| 55 | remap= Change symbols for output filenames. For example, remap=+- |
| 56 | would output barcode ACGT+TGCA to file ACGT-TCGA.fq.gz. |
| 57 | |
| 58 | Legacy Output Stats File Support: |
| 59 | legacy= Set this to a path like '.' to output legacy stats files. |
| 60 | samplemap= An input csv or tsv containing barcodes and sample names, |
| 61 | for legacy stats. If present 'expected' can be omitted. |
| 62 | lane=0 Set this to a number to print the lane in legacy files. |
| 63 | |
| 64 | Barcode Parsing Modes (choose one): |
| 65 | barcode Parse the barcode automatically, assuming the standard |
| 66 | Illumina header format. This is the default. |
| 67 | header Match the entire read header. |
| 68 | prefix Match the prefix of the read header (length must be set). |
| 69 | suffix Match the suffix of the read header (length must be set). |
| 70 | hdelimiter= (headerdelimiter) Split the header using this delimiter, |
| 71 | then select a term (column must be set). Normally the |
| 72 | delimiter will be used as a literal string (a Java regular |
| 73 | expression); for example, ':' or 'HISEQ'. But there are |
| 74 | some special delimiters which will be replaced by the symbol |
| 75 | they name, because they can cause problems. |
| 76 | These are provided for convenience due to OS conflicts: |
| 77 | space, tab, whitespace, pound, greaterthan, lessthan, |
| 78 | equals, colon, semicolon, bang, and, quote, singlequote |
| 79 | These are provided because they interfere with Java regular |
| 80 | expression syntax: |
| 81 | backslash, hat, dollar, dot, pipe, questionmark, star, |
| 82 | plus, openparen, closeparen, opensquare, opencurly |
| 83 | In other words, to match '.', you should set 'hdelimiter=dot'. |
| 84 | |
| 85 | length=0 For prefix or suffix mode, use this many characters from |
| 86 | the read header. Must be positive in these modes. |
| 87 | column=0 Select the term when using a header delimiter. This is |
| 88 | 1-based (first term is column 1) so it must be positive. |
| 89 | |
| 90 | Barcode Assignment Mode (choose one): |
| 91 | mode=prob prob: Default mode. Assigns reads to the bin where they most |
| 92 | likely belong, from gathering statistics across the pool. |
| 93 | tile: Similar to prob, but calculates statistics on a per-tile |
| 94 | basis for higher precision. This mode is recommended as |
| 95 | long as the tile numbers are in the read headers. |
| 96 | hdist: Demultiplex reads to the bin with the fewest |
| 97 | mismatches. This is the fastest and least accurate mode. |
| 98 | Here, 'hdist' stands for 'Hamming distance'. |
| 99 | Note: prob and tile mode may require a license. |
| 100 | |
| 101 | Server Parameters (for prob Mode only): |
| 102 | server=auto true: Barcode counts are sent to a remote server for |
| 103 | processing, and barcode assignments are sent back. |
| 104 | false: Barcode counts are processed locally. |
| 105 | auto: Sets flag to false unless the local machine contains |
| 106 | proprietary probabilistic processing code. |
| 107 | |
| 108 | Sensitivity Cutoffs for Prob/Tile Mode: |
| 109 | maxhdist=6 Maximum Hamming distance (number of mismatches) allowed. |
| 110 | Lower values will reduce yield with little benefit. |
| 111 | pairhdist=f When true, maxhdist will apply to the Hamming distance of |
| 112 | both barcodes combined (if using dual indexes). When false, |
| 113 | maxhdist will apply to each barcode individually. |
| 114 | minratio=1m Minimum ratio of probabilities allowed; k/m/b suffixes are |
| 115 | allowed. ratio=1m will only assign a barcode to a bin if |
| 116 | it is at least 1 million times more likely to belong in that |
| 117 | bin than in all others combined. Lower values will increase |
| 118 | yield but may increase crosstalk. |
| 119 | minprob=-5.6 Discard reads with a lower probability than this of belonging |
| 120 | to any bin. This is log10-scale, so -5 means 10^-5=0.00001. |
| 121 | Lower values will increase yield but increase crosstalk. |
| 122 | E.g., -6 would be lower than -5. |
| 123 | matrixthreads=1 More threads is faster but adds nondeterminism. |
| 124 | Note: These cutoffs are optimized for dual 10bp indexes. For single 10bp |
| 125 | indexes, 'minratio=5000 minprob=-3.2' is recommended. |
| 126 | |
| 127 | Sensitivity Cutoffs for HDist Mode |
| 128 | maxhdist=1 Maximum Hamming distance (number of mismatches) allowed. |
| 129 | Lower values will reduce yield and decrease crosstalk. |
| 130 | Setting maxhdist=0 will allow exact matches only. |
| 131 | pairhdist=f When true, maxhdist will apply to the Hamming distance of |
| 132 | both barcodes combined (if using dual indexes). When false, |
| 133 | maxhdist will apply to each barcode individually. |
| 134 | clearzone=1 (cz) Minimum difference between the closest and second-closest |
| 135 | Hamming distances. For example, AAAG is 1 mismatch from |
| 136 | AAAA and 3 mismatches away from GGGG, for a margin of 3-1=2. |
| 137 | This would be demultiplexed into AAAA as long as the |
| 138 | clearzone is set to at most 2. Lower values increase both |
| 139 | yield and crosstalk. |
| 140 | |
| 141 | Buffering Parameters |
| 142 | streams=8 Allow at most this many active streams. The actual number |
| 143 | of open files will be 1 greater than this if outu is set, |
| 144 | and doubled if output is paired and written in twin files |
| 145 | instead of interleaved. Setting this to at least the number |
| 146 | of expected output files can make things go much faster. |
| 147 | minreads=0 Don't create a file for fewer than this many reads; instead, |
| 148 | send them to unknown. This option will incur additional |
| 149 | memory usage. |
| 150 | rpb=8000 Dump buffers to files when they fill with this many reads. |
| 151 | Higher can be faster; lower uses less memory. |
| 152 | bpb=8000000 Dump buffers to files when they contain this many bytes. |
| 153 | Higher can be faster; lower uses less memory. |
| 154 | |
| 155 | Special Processing of Spike-ins (particularly for spike-ins with no barcodes) |
| 156 | spikelabel= If and only if a spike-in label is set here, reads will be |
| 157 | aligned to a reference, and matching reads will be sent to |
| 158 | the file with this label. May be a barcode or other string. |
| 159 | refpath=phix Override this with a file path for a custom reference. |
| 160 | kspike=27 Use this kmer length to map reads. |
| 161 | minid=0.7 Identity cutoff for matching the reference. |
| 162 | mapall=f Map all reads to the reference, instead of just unassigned |
| 163 | reads. |
| 164 | |
| 165 | Common parameters: |
| 166 | ow=t (overwrite) Overwrites files that already exist. |
| 167 | zl=4 (ziplevel) Set compression level, 1 (low) to 9 (max). |
| 168 | int=auto (interleaved) Determines whether INPUT file is considered |
| 169 | interleaved. |
| 170 | |
| 171 | Java Parameters: |
| 172 | -Xmx This will set Java's memory usage, overriding autodetection. |
| 173 | -Xmx20g will specify 20 gigs of RAM, and -Xmx200m will specify |
| 174 | 200 megs. The max is typically 85% of physical memory. |
| 175 | -eoom This flag will cause the process to exit if an out-of-memory |
| 176 | exception occurs. Requires Java 8u92+. |
| 177 | -da Disable assertions. |
| 178 | |
| 179 | Please contact Brian Bushnell at [email protected] if you encounter any problems. |
| 180 | " |
| 181 | } |
| 182 | |
| 183 | #This block allows symlinked shellscripts to correctly set classpath. |
| 184 | pushd . > /dev/null |
| 185 | DIR="${BASH_SOURCE[0]}" |
| 186 | while [ -h "$DIR" ]; do |
| 187 | cd "$(dirname "$DIR")" |
| 188 | DIR="$(readlink "$(basename "$DIR")")" |
| 189 | done |
| 190 | cd "$(dirname "$DIR")" |
| 191 | DIR="$(pwd)/" |
| 192 | popd > /dev/null |
| 193 | |
| 194 | #DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/" |
| 195 | CP="$DIR""current/" |
| 196 | |
| 197 | z="-Xmx16g" |
| 198 | z2="-Xms16g" |
| 199 | set=0 |
| 200 | |
| 201 | if [ -z "$1" ] || [[ $1 == -h ]] || [[ $1 == --help ]]; then |
| 202 | usage |
| 203 | exit |
| 204 | fi |
| 205 | |
| 206 | calcXmx () { |
| 207 | source "$DIR""/calcmem.sh" |
| 208 | setEnvironment |
| 209 | parseXmx "$@" |
| 210 | if [[ $set == 1 ]]; then |
| 211 | return |
| 212 | fi |
| 213 | freeRam 16000m 84 |
| 214 | z="-Xmx${RAM}m" |
| 215 | z2="-Xms${RAM}m" |
| 216 | } |
| 217 | calcXmx "$@" |
| 218 | |
| 219 | function demux() { |
| 220 | local CMD="java $EA $EOOM $z $z2 -cp $CP barcode.NovaDemux $@" |
| 221 | echo $CMD >&2 |
| 222 | eval $CMD |
| 223 | } |
| 224 | |
| 225 | demux "$@" |