| 1 | #!/bin/bash |
| 2 | |
| 3 | usage(){ |
| 4 | echo " |
| 5 | Written by Brian Bushnell |
| 6 | Last modified March 2, 2024 |
| 7 | |
| 8 | Description: Demultiplexes sequences into multiple files based on their |
| 9 | names, substrings of their names, or prefixes or suffixes of their names. |
| 10 | Allows unlimited output files while maintaining only a small number of |
| 11 | open file handles. |
| 12 | |
| 13 | Usage: |
| 14 | demuxbyname.sh in=<file> in2=<file> out=<file> out2=<file> names=<string,string,...> |
| 15 | |
| 16 | Alternately: |
| 17 | demuxbyname.sh in=<file> out=<file> outu=<file> names=<file> barcode |
| 18 | This will parse the barcode from Illumina reads with a header like this: |
| 19 | @A00178:73:HH7H3DSXX:4:1101:13666:1047 1:N:0:ACGTTGGT+TGACGCAT |
| 20 | |
| 21 | demuxbyname.sh in=<file> out=<file> delimiter=whitespace prefixmode=f |
| 22 | This will demultiplex by the substring after the last whitespace. |
| 23 | |
| 24 | demuxbyname.sh in=<file> out=<file> length=8 prefixmode=t |
| 25 | This will demultiplex by the first 8 characters of read names. |
| 26 | |
| 27 | demuxbyname.sh in=<file> out=<file> delimiter=: prefixmode=f |
| 28 | This will split on colons, and use the last substring as the name; |
| 29 | so for Illumina reads in the above format it would demux by barcode. |
| 30 | |
| 31 | in2 and out2 are for paired reads in twin files and are optional. |
| 32 | If input is paired and there is only one output file, it will be written interleaved. |
| 33 | |
| 34 | File Parameters: |
| 35 | in=<file> Input file. |
| 36 | in2=<file> If input reads are paired in twin files, use in2 for the |
| 37 | second file. |
| 38 | out=<file> Output files for reads with matched headers (must contain % |
| 39 | symbol). For example, out=out_%.fq with names XX and YY |
| 40 | would create out_XX.fq and out_YY.fq. If twin files for |
| 41 | paired reads are desired, use the # symbol. For example, |
| 42 | out=out_%_#.fq in this case would create out_XX_1.fq, |
| 43 | out_XX_2.fq, out_YY_1.fq, and out_YY_2.fq. |
| 44 | outu=<file> Output file for reads with unmatched headers. |
| 45 | stats=<file> Print statistics about how many reads went to each file. |
| 46 | names= List of strings (or files containing strings) to parse from |
| 47 | read names. Files should contain one name per line. This is |
| 48 | optional. If a list of names is provided, files will only be |
| 49 | created for those names. For example, 'prefixmode=t length=5' |
| 50 | would create a file for every unique first 5 characters in |
| 51 | read names, and every read would be written to one of those |
| 52 | files. But if there was addionally 'names=ABCDE,FGHIJ' then |
| 53 | at most 2 files would be created, and anything not matching |
| 54 | those names would go to outu. |
| 55 | |
| 56 | Processing Modes (determines how to convert a read into a name): |
| 57 | prefixmode=t (pm) Match prefix of read header. If false, match suffix of |
| 58 | read header. prefixmode=f is equivalent to suffixmode=t. |
| 59 | barcode=f Parse barcodes from Illumina headers. |
| 60 | tile=f Parse tile numbers from Illumina headers. |
| 61 | chrom=f For mapped sam files, make one file per chromosome (scaffold) |
| 62 | using the rname. |
| 63 | header=f Use the entire sequence header. |
| 64 | delimiter= For prefix or suffix mode, specifying a delimiter will allow |
| 65 | exact matches even if the length is variable. This allows |
| 66 | demultiplexing based on names that are found without |
| 67 | specifying a list of names. In suffix mode, everything after |
| 68 | the last delimiter will be used. Normally the delimiter will |
| 69 | be used as a literal string (a Java regular expression); for |
| 70 | example, ':' or 'HISEQ'. But there are some special |
| 71 | delimiters which will be replaced by the symbol they name, |
| 72 | because they can cause problems. |
| 73 | These are provided for convenience due to OS conflicts: |
| 74 | space, tab, whitespace, pound, greaterthan, lessthan, |
| 75 | equals, colon, semicolon, bang, and, quote, singlequote |
| 76 | These are provided because they interfere with Java regular |
| 77 | expression syntax: |
| 78 | backslash, hat, dollar, dot, pipe, questionmark, star, |
| 79 | plus, openparen, closeparen, opensquare, opencurly |
| 80 | In other words, to match '.', you should set 'delimiter=dot'. |
| 81 | substring=f Names can be substrings of read headers. Substring mode is |
| 82 | slow if the list of names is large. Requires a list of names. |
| 83 | |
| 84 | Other Processing Parameters: |
| 85 | column=-1 If positive, split the header on a delimiter and match that |
| 86 | column (1-based). For example, using this header: |
| 87 | NB501886:61:HL3GMAFXX:1:11101:10717:1140 1:N:0:ACTGAGC+ATTAGAC |
| 88 | You could demux by tile (11101) using 'delimiter=: column=5' |
| 89 | Column is 1-based (first column is 1). |
| 90 | If column is omitted when a delimiter is present, prefixmode |
| 91 | will use the first substring, and suffixmode will use the |
| 92 | last substring. |
| 93 | length=0 If positive, use a suffix or prefix of this length from read |
| 94 | name instead of or in addition to the list of names. |
| 95 | For example, you could create files based on the first 8 |
| 96 | characters of read names. |
| 97 | hdist=0 Allow a hamming distance for demultiplexing barcodes. This |
| 98 | requires a list of names (barcodes). It is unrelated to |
| 99 | probabiilty mode's hdist3. |
| 100 | replace= Replace some characters in the output filenames. For example, |
| 101 | replace=+- would replace the + symbol in headers with the - |
| 102 | symbol in output filenames. So you could match the barcode |
| 103 | ACTGAGC+ATTAGAC, but write to file ACTGAGC-ATTAGAC. |
| 104 | |
| 105 | Buffering Parameters |
| 106 | streams=8 Allow at most this many active streams. The actual number |
| 107 | of open files will be 1 greater than this if outu is set, |
| 108 | and doubled if output is paired and written in twin files |
| 109 | instead of interleaved. Setting this to at least the number |
| 110 | of expected output files can make things go much faster. |
| 111 | minreads=0 Don't create a file for fewer than this many reads; instead, |
| 112 | send them to unknown. This option will incur additional |
| 113 | memory usage. |
| 114 | rpb=8000 Dump buffers to files when they fill with this many reads. |
| 115 | Higher can be faster; lower uses less memory. |
| 116 | bpb=8000000 Dump buffers to files when they contain this many bytes. |
| 117 | Higher can be faster; lower uses less memory. |
| 118 | |
| 119 | Common parameters: |
| 120 | ow=t (overwrite) Overwrites files that already exist. |
| 121 | zl=4 (ziplevel) Set compression level, 1 (low) to 9 (max). |
| 122 | int=auto (interleaved) Determines whether INPUT file is considered |
| 123 | interleaved. |
| 124 | qin=auto ASCII offset for input quality. All modern platforms use 33. |
| 125 | qout=auto ASCII offset for output quality. |
| 126 | |
| 127 | Java Parameters: |
| 128 | -Xmx This will set Java's memory usage, overriding autodetection. |
| 129 | -Xmx20g will specify 20 gigs of RAM, and -Xmx200m will specify |
| 130 | 200 megs. The max is typically 85% of physical memory. |
| 131 | -eoom This flag will cause the process to exit if an out-of-memory |
| 132 | exception occurs. Requires Java 8u92+. |
| 133 | -da Disable assertions. |
| 134 | |
| 135 | Please contact Brian Bushnell at [email protected] if you encounter any problems. |
| 136 | " |
| 137 | } |
| 138 | |
| 139 | #This block allows symlinked shellscripts to correctly set classpath. |
| 140 | pushd . > /dev/null |
| 141 | DIR="${BASH_SOURCE[0]}" |
| 142 | while [ -h "$DIR" ]; do |
| 143 | cd "$(dirname "$DIR")" |
| 144 | DIR="$(readlink "$(basename "$DIR")")" |
| 145 | done |
| 146 | cd "$(dirname "$DIR")" |
| 147 | DIR="$(pwd)/" |
| 148 | popd > /dev/null |
| 149 | |
| 150 | #DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/" |
| 151 | CP="$DIR""current/" |
| 152 | |
| 153 | z="-Xmx2g" |
| 154 | z2="-Xms2g" |
| 155 | set=0 |
| 156 | |
| 157 | if [ -z "$1" ] || [[ $1 == -h ]] || [[ $1 == --help ]]; then |
| 158 | usage |
| 159 | exit |
| 160 | fi |
| 161 | |
| 162 | calcXmx () { |
| 163 | source "$DIR""/calcmem.sh" |
| 164 | setEnvironment |
| 165 | parseXmx "$@" |
| 166 | if [[ $set == 1 ]]; then |
| 167 | return |
| 168 | fi |
| 169 | freeRam 3200m 84 |
| 170 | z="-Xmx${RAM}m" |
| 171 | z2="-Xms${RAM}m" |
| 172 | } |
| 173 | calcXmx "$@" |
| 174 | |
| 175 | function demuxbyname() { |
| 176 | local CMD="java $EA $EOOM $z $z2 -cp $CP jgi.DemuxByName2 $@" |
| 177 | echo $CMD >&2 |
| 178 | eval $CMD |
| 179 | } |
| 180 | |
| 181 | demuxbyname "$@" |