| 1 | #!/bin/bash |
| 2 | |
| 3 | usage(){ |
| 4 | echo " |
| 5 | Written by Brian Bushnell |
| 6 | Last modified March 19, 2025 |
| 7 | |
| 8 | Description: Estimates the strandedness of a library without alignment; |
| 9 | intended for RNA-seq data. Only the reads are required input to determine |
| 10 | strandedness, so this works when run with just a fastq file. If sam/bam |
| 11 | input is used, additional alignment-based metrics will be reported. |
| 12 | If an assembly and gff file are provided, the affinity of reads to the plus |
| 13 | or minus (sense or antisense) strand will also be calculated. If a genome |
| 14 | is specified with no gff file, the genes will be called automatically with a |
| 15 | prokaryotic gene caller. Strandedness and P/(P+M) ratios are similar but |
| 16 | calculated in different ways, and thus will not exactly agree, but should be |
| 17 | close. For most calculations, only read 1 is used, or the merge of read 1 |
| 18 | and read 2 if the merge flag is enabled and they overlap. |
| 19 | |
| 20 | |
| 21 | Output meaning: |
| 22 | |
| 23 | Depth_Analysis: Based on comparing the fraction of kmers in forward and |
| 24 | reverse orientation to a binomial distribution. |
| 25 | Strandedness: Percent of reads that came from the majority strand, based |
| 26 | on kmer depth. |
| 27 | StrandednessN: Depth-normalized strandedness, where each unique kmer |
| 28 | contributes equally regardless of depth. |
| 29 | AvgKmerDepth: Average depth of kmers; typically higher than read depth. |
| 30 | Kmers>Depth1: Fraction of kmers with depth over 1. Singleton kmers cannot |
| 31 | be used to calculate strandedness from depth. |
| 32 | |
| 33 | Stop_Codon_Analysis: Based on counting stop codons. |
| 34 | MajorStrandORF: Predicted major strand based on stop-codon analysis. |
| 35 | AvgReadLen: Average length of R1, or the merged read pair. |
| 36 | AvgORFLen+: Average ORF length in the best frame on the plus side of R1. |
| 37 | AvgORFLen-: Average ORF length in the best frame on the minus side of R1. |
| 38 | AvgStopCount+: Average number of stop codons in the best frame on plus side. |
| 39 | AvgStopCount-: Average number of stop codons in the best frame on minus side. |
| 40 | GC_Content: GC content of reads, which affects stop codon frequency. |
| 41 | |
| 42 | PolyA_Analysis: Based on counting poly-A or poly-T tails. |
| 43 | MajorStrandPA: Predicted major strand based on poly-A tail analysis. |
| 44 | This is unreliable if the poly-A fraction is low. |
| 45 | PolyA/(PA+PT): Ratio of (reads with poly-A tails)/(poly-A + poly-T tails). |
| 46 | PolyAFraction: Fraction of reads ending in poly-A or poly-T. |
| 47 | |
| 48 | Ref_Analysis: Compares read kmer frequencies to a reference (if present). |
| 49 | The reference can be a transcriptome or genome, and a gff file |
| 50 | will be used if provided. |
| 51 | MajorStrandREF: The strand containing a majority of forward read kmers. |
| 52 | P/(P+M)_Ratio: P is the sum of counts of plus kmers, and M is minus kmers. |
| 53 | GeneCoverage: Fraction of transcriptome kmers represented in reads. |
| 54 | GenePrecision: Fraction of read kmers found in the transcriptome. |
| 55 | |
| 56 | Read_Gene_Calling_Analysis: Uses gene-calling on reads to calculate which |
| 57 | strand better fits a prokaryotic gene model. |
| 58 | MajorStrandRGC: Predicted major strand based on read gene calling. |
| 59 | P/(P+M)_Ratio: P is the read count best matching the plus strand; M is minus. |
| 60 | AvgScorePlus: Average score of called plus-strand genes. |
| 61 | AvgScoreMinus: Average score of called plus-strand genes. |
| 62 | UsedFraction: Fraction of reads with any called genes (or partial genes); |
| 63 | this can be increased by merging the reads for longer frames. |
| 64 | |
| 65 | Alignment_Results: Requires sam/bam input. The reads must have been |
| 66 | mapped to a transcriptome or RNA-seq assembly, or to a |
| 67 | specified genome, or a gff file must be provided. |
| 68 | StrandednessAL: Percent of reads aligned to the dominant strand. More |
| 69 | accurate for transcriptome-mapped than genome-mapped reads. |
| 70 | StrandednessAN: Depth-normalized strandedness, where each feature or |
| 71 | contig contributes equally. |
| 72 | MajorStrandAL: Strand to which a majority of reads aligned. |
| 73 | P/(P+M)_Ratio: P is the number of plus-mapped reads, M is minus. |
| 74 | P/(P+M)_RatioN: Depth-normalized plus/total ratio. |
| 75 | PlusFeatures: Fraction of features with majority plus-mapped reads. |
| 76 | AlignmentRate: Fraction of reads that aligned. |
| 77 | Feature-Mapped: Fraction of reads that aligned to a feature in the gff. |
| 78 | |
| 79 | |
| 80 | Usage: checkstrand.sh in=<input file> |
| 81 | |
| 82 | Running on a fastq is simple, but there are multiple ways to run CheckStrand |
| 83 | on aligned data (in=, ref=, and gff= flags are not needed if the files have |
| 84 | proper extensions): |
| 85 | |
| 86 | #1) This won't give alignment results, just kmer results |
| 87 | checkstrand.sh mapped.sam |
| 88 | |
| 89 | #2) This will do gene-calling and the alignment strandedness will be based |
| 90 | on gene sense strand, but only works for prokaryotes/viruses |
| 91 | checkstrand.sh mapped.sam contigs.fa |
| 92 | |
| 93 | #3) This will use the annotation and the alignment strandedness will be based |
| 94 | on gene sense strand, works for proks, and should work for eukaryotes |
| 95 | (there are lots of ways to annotate multi-exon genes though) |
| 96 | checkstrand.sh mapped.sam genes.gff |
| 97 | |
| 98 | #4) This will assume that the reference was a sense-strand transcriptome, |
| 99 | and the alignment strandedness will be based on contig plus strand |
| 100 | checkstrand.sh mapped.sam transcriptome |
| 101 | |
| 102 | #5) This will assume that the reference was unstranded contigs assembled |
| 103 | from RNA-seq data, so the alignment strandedness will be based on |
| 104 | contig majority strand |
| 105 | checkstrand.sh mapped.sam rnacontigs |
| 106 | |
| 107 | |
| 108 | Standard parameters: |
| 109 | in=<file> Primary input (a fastq, fasta, sam or bam file). |
| 110 | in2=<file> Secondary input for paired fastq in twin files. Read 2 is |
| 111 | ignored unless merge=t. |
| 112 | out=<file> Optional destination to redirect results instead of stdout. |
| 113 | outp=<file> Optional output for plus-mapped reads. |
| 114 | outm=<file> Optional output for minus-mapped reads. |
| 115 | *Note: outp/outm require sam/bam input and either transcriptome mode or a gff. |
| 116 | The destination of plus-mapped r1 would be outp, but outm for plus-mapped r2. |
| 117 | |
| 118 | Processing parameters: |
| 119 | ref=<file> Optional reference (assembly) input. |
| 120 | gff=<file> Optional gene annotation file. |
| 121 | transcriptome=f Set this to 't' if the reference is a sense-strand |
| 122 | transcriptome (rather than a genome assembly). This applies |
| 123 | to either a reference specified by 'ref' or the reference |
| 124 | used for alignment, fo sam/bam input. |
| 125 | rnacontigs=f Set this to 't' if the reference is contigs assembled from |
| 126 | RNA-seq data, but with unknown orientation. Only affects |
| 127 | alignment results. |
| 128 | size=80000 Sketch size; larger may be more precise. |
| 129 | merge=f Attempt to merge paired reads, and use the merged read when |
| 130 | successful. If unsuccessful only R1 is used. This has a |
| 131 | performance impact on CPUs with few cores. |
| 132 | orf=t Analyze stop codons and open reading frames. Usually this |
| 133 | will allow major strand determination without a reference, |
| 134 | unless GC is very high. |
| 135 | callreads=t Perform gene-calling on reads using a prokarotic gene model. |
| 136 | Not very relevant to eukaryotes. |
| 137 | passes=2 Two passes refines the gene model for better gene-calling on |
| 138 | reads. Only used if there is a reference. |
| 139 | samplerate=1.0 Set to a lower number to subsample the input reads; increases |
| 140 | speed on CPUs with few cores. |
| 141 | sampleseed=17 Positive numbers are deterministic; negative use random seeds. |
| 142 | reads=-1 If positive, quit after processing this many reads. |
| 143 | |
| 144 | Java Parameters: |
| 145 | -Xmx This will set Java's memory usage, overriding autodetection. |
| 146 | -Xmx20g will specify 20 gigs of RAM, and -Xmx200m will |
| 147 | specify 200 megs. The max is typically 85% of physical memory. |
| 148 | -eoom This flag will cause the process to exit if an out-of-memory |
| 149 | exception occurs. Requires Java 8u92+. |
| 150 | -da Disable assertions. |
| 151 | |
| 152 | Please contact Brian Bushnell at [email protected] if you encounter any problems. |
| 153 | " |
| 154 | } |
| 155 | |
| 156 | #This block allows symlinked shellscripts to correctly set classpath. |
| 157 | pushd . > /dev/null |
| 158 | DIR="${BASH_SOURCE[0]}" |
| 159 | while [ -h "$DIR" ]; do |
| 160 | cd "$(dirname "$DIR")" |
| 161 | DIR="$(readlink "$(basename "$DIR")")" |
| 162 | done |
| 163 | cd "$(dirname "$DIR")" |
| 164 | DIR="$(pwd)/" |
| 165 | popd > /dev/null |
| 166 | |
| 167 | #DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/" |
| 168 | CP="$DIR""current/" |
| 169 | |
| 170 | z="-Xmx2g" |
| 171 | #z2="-Xms2g" |
| 172 | set=0 |
| 173 | |
| 174 | if [ -z "$1" ] || [[ $1 == -h ]] || [[ $1 == --help ]]; then |
| 175 | usage |
| 176 | exit |
| 177 | fi |
| 178 | |
| 179 | calcXmx () { |
| 180 | source "$DIR""/calcmem.sh" |
| 181 | setEnvironment |
| 182 | parseXmx "$@" |
| 183 | } |
| 184 | calcXmx "$@" |
| 185 | |
| 186 | checkstrand() { |
| 187 | local CMD="java $EA $EOOM $z -cp $CP jgi.CheckStrand2 $@" |
| 188 | echo $CMD >&2 |
| 189 | eval $CMD |
| 190 | } |
| 191 | |
| 192 | checkstrand "$@" |