| 1 | #!/bin/bash |
| 2 | |
| 3 | usage(){ |
| 4 | echo " |
| 5 | Written by Brian Bushnell |
| 6 | Last modified October 10, 2022 |
| 7 | |
| 8 | Description: Sorts reads by name or other keys such as length, |
| 9 | quality, mapping position, flowcell coordinates, or taxonomy. |
| 10 | Writes temp files if memory is exceeded. |
| 11 | |
| 12 | Usage: bbsort.sh in=<file> out=<file> |
| 13 | |
| 14 | Input may be fasta, fastq, or sam, compressed or uncompressed. |
| 15 | Temp files will use the same format as the output. Pairs are |
| 16 | kept together if reads are paired, and in2/out2 may be used for that. |
| 17 | |
| 18 | Example 1 - sort by name: |
| 19 | bbsort.sh in=raw.fq out=sorted.fq |
| 20 | Example 2 - sort by sequence: |
| 21 | bbsort.sh in=raw.fq out=sorted.fq sequence |
| 22 | Example 3 - sort by mapping position: |
| 23 | bbsort.sh in=mapped.sam out=sorted.sam position |
| 24 | |
| 25 | Parameters: |
| 26 | |
| 27 | in=<file> Input file. |
| 28 | out=<file> Output file. |
| 29 | name=t Sort reads by name. |
| 30 | length=f Sort reads by length. |
| 31 | quality=f Sort reads by quality. |
| 32 | position=f Sort reads by position (for mapped reads). |
| 33 | taxa=f Sort reads by taxonomy (for NCBI naming convention). |
| 34 | sequence=f Sort reads by sequence, alphabetically. |
| 35 | clump=f Sort reads by shared kmers, like Clumpify. |
| 36 | flowcell=f Sort reads by flowcell coordinates. |
| 37 | shuffle=f Shuffle reads randomly (untested). |
| 38 | list=<file> Sort reads according to this list of names. |
| 39 | ascending=t Sort ascending. |
| 40 | |
| 41 | Memory parameters (you might reduce these if you experience a crash) |
| 42 | memmult=0.30 Write a temp file when used memory exceeds this fraction |
| 43 | of available memory. |
| 44 | memlimit=0.65 Wait for temp files to finish writing until used memory |
| 45 | drops below this fraction of available memory. |
| 46 | delete=t Delete temporary files. |
| 47 | allowtemp=t Allow writing temporary files. |
| 48 | |
| 49 | Taxonomy-sorting parameters (for taxa mode only): |
| 50 | tree= Specify a taxtree file. On Genepool, use 'auto'. |
| 51 | gi= Specify a gitable file. On Genepool, use 'auto'. |
| 52 | accession= Specify one or more comma-delimited NCBI accession to |
| 53 | taxid files. On Genepool, use 'auto'. |
| 54 | |
| 55 | Note: name, length, and quality are mutually exclusive. |
| 56 | Sorting by quality actually sorts by average expected error rate, |
| 57 | so ascending will place the highest-quality reads first. |
| 58 | |
| 59 | Java Parameters: |
| 60 | -Xmx This will set Java's memory usage, overriding |
| 61 | autodetection. -Xmx20g will specify 20 gigs of RAM, and -Xmx200m will |
| 62 | specify 200 megs. The max is typically 85% of physical memory. |
| 63 | -eoom This flag will cause the process to exit if an |
| 64 | out-of-memory exception occurs. Requires Java 8u92+. |
| 65 | -da Disable assertions. |
| 66 | |
| 67 | Please contact Brian Bushnell at [email protected] if you encounter any problems. |
| 68 | " |
| 69 | } |
| 70 | |
| 71 | #This block allows symlinked shellscripts to correctly set classpath. |
| 72 | pushd . > /dev/null |
| 73 | DIR="${BASH_SOURCE[0]}" |
| 74 | while [ -h "$DIR" ]; do |
| 75 | cd "$(dirname "$DIR")" |
| 76 | DIR="$(readlink "$(basename "$DIR")")" |
| 77 | done |
| 78 | cd "$(dirname "$DIR")" |
| 79 | DIR="$(pwd)/" |
| 80 | popd > /dev/null |
| 81 | |
| 82 | #DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/" |
| 83 | CP="$DIR""current/" |
| 84 | |
| 85 | z="-Xmx2g" |
| 86 | z2="-Xms2g" |
| 87 | set=0 |
| 88 | |
| 89 | if [ -z "$1" ] || [[ $1 == -h ]] || [[ $1 == --help ]]; then |
| 90 | usage |
| 91 | exit |
| 92 | fi |
| 93 | |
| 94 | calcXmx () { |
| 95 | source "$DIR""/calcmem.sh" |
| 96 | setEnvironment |
| 97 | parseXmx "$@" |
| 98 | if [[ $set == 1 ]]; then |
| 99 | return |
| 100 | fi |
| 101 | freeRam 2000m 84 |
| 102 | z="-Xmx${RAM}m" |
| 103 | z2="-Xms${RAM}m" |
| 104 | } |
| 105 | calcXmx "$@" |
| 106 | |
| 107 | bbsort() { |
| 108 | local CMD="java $EA $EOOM $z $z2 -cp $CP sort.SortByName $@" |
| 109 | echo $CMD >&2 |
| 110 | eval $CMD |
| 111 | } |
| 112 | |
| 113 | bbsort "$@" |