| 1 | #!/bin/bash |
| 2 | |
| 3 | usage(){ |
| 4 | echo " |
| 5 | Written by Brian Bushnell |
| 6 | Last modified September 28, 2024 |
| 7 | |
| 8 | Description: Calculates per-scaffold or per-base coverage information from an unsorted sam or bam file. |
| 9 | Supports SAM/BAM format for reads and FASTA for reference. |
| 10 | Sorting is not needed, so output may be streamed directly from a mapping program. |
| 11 | Requires a minimum of 1 bit per reference base plus 100 bytes per scaffold (even if no reference is specified). |
| 12 | If per-base coverage is needed (including for stdev and median), at least 4 bytes per base is needed. |
| 13 | |
| 14 | Usage: pileup.sh in=<input> out=<output> |
| 15 | |
| 16 | Input Parameters: |
| 17 | in=<file> The input sam file; this is the only required parameter. |
| 18 | ref=<file> Scans a reference fasta for per-scaffold GC counts, not otherwise needed. |
| 19 | fastaorf=<file> An optional fasta file with ORF header information in PRODIGAL's output format. Must also specify 'outorf'. |
| 20 | unpigz=t Decompress with pigz for faster decompression. |
| 21 | addfromref=t Allow ref scaffolds not present in sam header to be added from the reference. |
| 22 | addfromreads=f Allow ref scaffolds not present in sam header to be added from the reads. |
| 23 | Note that in this case the ref scaffold lengths will be inaccurate. |
| 24 | |
| 25 | Output Parameters: |
| 26 | out=<file> (covstats) Per-scaffold coverage info. |
| 27 | rpkm=<file> Per-scaffold RPKM/FPKM counts. |
| 28 | twocolumn=f Change to true to print only ID and Avg_fold instead of all 6 columns. |
| 29 | countgc=t Enable/disable counting of read GC content. |
| 30 | outorf=<file> Per-orf coverage info to this file (only if 'fastaorf' is specified). |
| 31 | outsam=<file> Print the input sam stream to this file (or stdout). Useful for piping data. |
| 32 | hist=<file> Histogram of # occurrences of each depth level. |
| 33 | basecov=<file> Coverage per base location. |
| 34 | rangecov=<file> Concise ranges where coverage depth is at least mincov. |
| 35 | mincov=1 When calculating percent covered, ignore bases under this depth. |
| 36 | Also used as threshold for rangecov. |
| 37 | bincov=<file> Binned coverage per location (one line per X bases). |
| 38 | binsize=1000 Binsize for binned coverage output. |
| 39 | keepshortbins=t (ksb) Keep residual bins shorter than binsize. |
| 40 | normcov=<file> Normalized coverage by normalized location (X lines per scaffold). |
| 41 | normcovo=<file> Overall normalized coverage by normalized location (X lines for the entire assembly). |
| 42 | normb=-1 If positive, use a fixed number of bins per scaffold; affects 'normcov' and 'normcovo'. |
| 43 | normc=f Normalize coverage to fraction of max per scaffold; affects 'normcov' and 'normcovo'. |
| 44 | delta=f Only print base coverage lines when the coverage differs from the previous base. |
| 45 | nzo=f Only print scaffolds with nonzero coverage. |
| 46 | concise=f Write 'basecov' in a more concise format. |
| 47 | header=t (hdr) Include headers in output files. |
| 48 | headerpound=t (#) Prepend header lines with '#' symbol. |
| 49 | stdev=t Calculate coverage standard deviation. |
| 50 | covminscaf=0 (minscaf) Don't print coverage for scaffolds shorter than this. |
| 51 | covwindow=0 Calculate how many bases are in windows of this size with |
| 52 | low average coverage. Produces an extra stats column. |
| 53 | covwindowavg=5 Average coverage below this will be classified as low. |
| 54 | k=0 If positive, calculate kmer coverage statstics for this kmer length. |
| 55 | keyvalue=f Output statistics to screen as key=value pairs. |
| 56 | |
| 57 | Processing Parameters: |
| 58 | strandedcov=f Track coverage for plus and minus strand independently. |
| 59 | startcov=f Only track start positions of reads. |
| 60 | stopcov=f Only track stop positions of reads. |
| 61 | secondary=t Use secondary alignments, if present. |
| 62 | softclip=f Include soft-clipped bases in coverage. |
| 63 | minmapq=0 (minq) Ignore alignments with mapq below this. |
| 64 | physical=f (physcov) Calculate physical coverage for paired reads. This includes the unsequenced bases. |
| 65 | tlen=t Track physical coverage from the tlen field rather than recalculating it. |
| 66 | arrays=auto Set to t/f to manually force the use of coverage arrays. Arrays and bitsets are mutually exclusive. |
| 67 | bitsets=auto Set to t/f to manually force the use of coverage bitsets. |
| 68 | 32bit=f Set to true if you need per-base coverage over 64k; does not affect per-scaffold coverage precision. |
| 69 | This option will double RAM usage (when calculating per-base coverage). |
| 70 | delcoverage=t (delcov) Count bases covered by deletions or introns as covered. |
| 71 | True is faster than false. |
| 72 | dupecoverage=t (dupes) Include reads flagged as duplicates in coverage. |
| 73 | samstreamer=t (ss) Load reads multithreaded to increase speed. |
| 74 | |
| 75 | Trimming Parameters: |
| 76 | ** NOTE: These are applied before adding coverage, to allow mimicking ** |
| 77 | ** tools like CallVariants, which uses 'qtrim=r trimq=10 border=5' ** |
| 78 | qtrim=f Quality-trim. May be set to: |
| 79 | f (false): Don't quality-trim. |
| 80 | r (right): Trim right (3') end only. |
| 81 | l (left): Trim right (5') end only. |
| 82 | rl (both): Trim both ends. |
| 83 | trimq=-1 If positive, quality-trim to this threshold. |
| 84 | border=0 Ignore this many bases on the left and right end. |
| 85 | |
| 86 | Output format (tab-delimited): |
| 87 | ID, Avg_fold, Length, Ref_GC, Covered_percent, Covered_bases, Plus_reads, Minus_reads, Read_GC, Median_fold, Std_Dev |
| 88 | |
| 89 | ID: Scaffold ID |
| 90 | Length: Scaffold length |
| 91 | Ref_GC: GC ratio of reference |
| 92 | Avg_fold: Average fold coverage of this scaffold |
| 93 | Covered_percent: Percent of scaffold with any coverage (only if arrays or bitsets are used) |
| 94 | Covered_bases: Number of bases with any coverage (only if arrays or bitsets are used) |
| 95 | Plus_reads: Number of reads mapped to plus strand |
| 96 | Minus_reads: Number of reads mapped to minus strand |
| 97 | Read_GC: Average GC ratio of reads mapped to this scaffold |
| 98 | Median_fold: Median fold coverage of this scaffold (only if arrays are used) |
| 99 | Std_Dev: Standard deviation of coverage (only if arrays are used) |
| 100 | |
| 101 | Java Parameters: |
| 102 | |
| 103 | -Xmx This will set Java's memory usage, overriding |
| 104 | autodetection. -Xmx20g will |
| 105 | specify 20 gigs of RAM, and -Xmx200m will specify 200 megs. |
| 106 | The max is typically 85% of physical memory. |
| 107 | -eoom This flag will cause the process to exit if an out-of-memory |
| 108 | exception occurs. Requires Java 8u92+. |
| 109 | -da Disable assertions. |
| 110 | |
| 111 | Please contact Brian Bushnell at [email protected] if you encounter any problems. |
| 112 | " |
| 113 | } |
| 114 | |
| 115 | #This block allows symlinked shellscripts to correctly set classpath. |
| 116 | pushd . > /dev/null |
| 117 | DIR="${BASH_SOURCE[0]}" |
| 118 | while [ -h "$DIR" ]; do |
| 119 | cd "$(dirname "$DIR")" |
| 120 | DIR="$(readlink "$(basename "$DIR")")" |
| 121 | done |
| 122 | cd "$(dirname "$DIR")" |
| 123 | DIR="$(pwd)/" |
| 124 | popd > /dev/null |
| 125 | |
| 126 | #DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/" |
| 127 | CP="$DIR""current/" |
| 128 | |
| 129 | z="-Xmx1g" |
| 130 | z2="-Xms1g" |
| 131 | set=0 |
| 132 | |
| 133 | if [ -z "$1" ] || [[ $1 == -h ]] || [[ $1 == --help ]]; then |
| 134 | usage |
| 135 | exit |
| 136 | fi |
| 137 | |
| 138 | calcXmx () { |
| 139 | source "$DIR""/calcmem.sh" |
| 140 | setEnvironment |
| 141 | parseXmx "$@" |
| 142 | if [[ $set == 1 ]]; then |
| 143 | return |
| 144 | fi |
| 145 | freeRam 3200m 84 |
| 146 | z="-Xmx${RAM}m" |
| 147 | z2="-Xms${RAM}m" |
| 148 | } |
| 149 | calcXmx "$@" |
| 150 | |
| 151 | pileup() { |
| 152 | local CMD="java $EA $EOOM $z -cp $CP jgi.CoveragePileup $@" |
| 153 | echo $CMD >&2 |
| 154 | eval $CMD |
| 155 | } |
| 156 | |
| 157 | pileup "$@" |