| 1 | #!/bin/bash |
| 2 | |
| 3 | ## Written by Brian Bushnell |
| 4 | ## Last modified April 11, 2020 |
| 5 | ## Description: Calls SARS-CoV-2 variants from Illumina amplicon data. |
| 6 | ## This script assumes input data is paired-end. |
| 7 | |
| 8 | ## Usage: processCorona.sh <prefix> |
| 9 | ## For example, "processCorona.sh Sample1" if the data is in Sample1.fq.gz |
| 10 | |
| 11 | ## Grab the sample name from the command line. |
| 12 | NAME="$1" |
| 13 | |
| 14 | ## Set minimum coverage for genotype calls. |
| 15 | ## Areas below this depth will be set to N in the consensus genome. |
| 16 | MINCOV=5 |
| 17 | |
| 18 | ## Specify the viral reference file. |
| 19 | ## NC_045512.fasta contains the SARS-CoV-2 genome, equivalent to bbmap/resources/Covid19_ref.fa |
| 20 | REF="NC_045512.fasta" |
| 21 | |
| 22 | ## PCR primers |
| 23 | ## artic3 primers are in /bbmap/resources/artic3.fasta |
| 24 | PRIMERS="artic3.fasta" |
| 25 | |
| 26 | ## This line is in case the script is being re-run, to clear the old output. |
| 27 | rm "$NAME"*.sam.gz "$NAME"*.bam "$NAME"*.bai "$NAME"*.txt "$NAME"*.fa "$NAME"*.vcf |
| 28 | |
| 29 | ## If data is paired in twin files, interleave it into a single file. |
| 30 | ## Otherwise, skip this step. |
| 31 | ## In this case, the files are assumed to be named "Sample1_R1.fq.gz" and "Sample1_R2.fq.gz" |
| 32 | #reformat.sh in="$NAME"_R#.fq.gz out="$NAME".fq.gz |
| 33 | |
| 34 | ## Split into Covid and non-Covid reads if this has not already been done. |
| 35 | ## This step can be skipped if non-Covid was already removed. |
| 36 | bbduk.sh ow -Xmx1g in="$NAME".fq.gz ref="$REF" outm="$NAME"_viral.fq.gz outu="$NAME"_nonviral.fq.gz k=25 |
| 37 | |
| 38 | ## Recalibrate quality scores prior to any trimming. |
| 39 | ## *** Requires a recalibration matrix in the working directory (see recal.sh for details). *** |
| 40 | ## This step is optional but useful for Illumina binned quality scores. |
| 41 | bbduk.sh in="$NAME"_viral.fq.gz out="$NAME"_recal.fq.gz recalibrate -Xmx1g ow |
| 42 | |
| 43 | ## Discover adapter sequence for this library based on read overlap. |
| 44 | ## You can examine the adapters output file afterward if desired; |
| 45 | ## If there were too few short-insert pairs this step will fail (and you can just use the default Illumina adapters). |
| 46 | bbmerge.sh in="$NAME"_recal.fq.gz outa="$NAME"_adapters.fa ow reads=1m |
| 47 | |
| 48 | ## Remove duplicates by sequence similarity. |
| 49 | ## This is more memory-efficient than dedupebymapping. |
| 50 | clumpify.sh in="$NAME"_recal.fq.gz out="$NAME"_clumped.fq.gz zl=9 dedupe s=2 passes=4 -Xmx31g |
| 51 | |
| 52 | ## Perform adapter-trimming on the reads. |
| 53 | ## Also do quality trimming and filtering. |
| 54 | ## If desired, also do primer-trimming here by adding, e.g., 'ftl=20' to to trim the leftmost 20 bases. |
| 55 | ## If the prior adapter-detection step failed, use "ref=adapters" |
| 56 | bbduk.sh in="$NAME"_clumped.fq.gz out="$NAME"_trimmed.fq.gz minlen=60 ktrim=r k=21 mink=9 hdist=2 hdist2=1 ref="$NAME"_adapters.fa altref=adapters maq=14 qtrim=r trimq=10 maxns=0 tbo tpe ow -Xmx1g ftm=5 |
| 57 | |
| 58 | ## Trim artic3 primers, if present. |
| 59 | ## Disable this line if you are not amplifying with primers. |
| 60 | bbduk.sh in="$NAME"_trimmed.fq.gz out="$NAME"_trimmed2.fq.gz ref="$PRIMERS" ktrim=l restrictleft=30 k=22 hdist=3 qhdist=1 rcomp=f mm=f |
| 61 | |
| 62 | ## Align reads to the reference. |
| 63 | ## Local flag is due to primer-amplification-induced anomalies at read ends; |
| 64 | ## for randomly-sheared, unamplified data, "local" should be omitted. |
| 65 | bbmap.sh ref="$REF" in="$NAME"_trimmed2.fq.gz outm="$NAME"_mapped.sam.gz nodisk local maxindel=500 -Xmx4g ow k=12 |
| 66 | |
| 67 | ## Deduplicate based on mapping coordinates. |
| 68 | ## Note that if you use single-ended amplicon data, you will lose most of your data here. |
| 69 | dedupebymapping.sh in="$NAME"_mapped.sam.gz out="$NAME"_deduped.sam.gz -Xmx31g ow |
| 70 | |
| 71 | ## Remove junk reads with unsupported unique deletions; these are often chimeric. |
| 72 | filtersam.sh ref="$REF" ow in="$NAME"_deduped.sam.gz out="$NAME"_filtered.sam.gz mbad=1 del sub=f mbv=0 -Xmx4g |
| 73 | |
| 74 | ## Remove junk reads with multiple unsupported unique substitutions; these are often junk, particularly on Novaseq. |
| 75 | ## This step is not essential but reduces noise. |
| 76 | filtersam.sh ref="$REF" ow in="$NAME"_filtered.sam.gz out="$NAME"_filtered2.sam.gz mbad=1 sub mbv=2 -Xmx4g |
| 77 | |
| 78 | ## Trim soft-clipped bases. |
| 79 | bbduk.sh in="$NAME"_filtered2.sam.gz trimclip out="$NAME"_trimclip.sam.gz -Xmx1g ow |
| 80 | |
| 81 | ## Call variants from the sam files. |
| 82 | ## The usebias=f/minstrandratio=0 flags are necessary due to amplicon-induced strand bias, |
| 83 | ## and should be removed if the data is exclusively shotgun/metagenomic or otherwise randomly fragmented. |
| 84 | callvariants.sh in="$NAME"_trimclip.sam.gz ref="$REF" out="$NAME"_vars.vcf -Xmx4g ow strandedcov usebias=f minstrandratio=0 maf=0.6 minreads="$MINCOV" mincov="$MINCOV" minedistmax=30 minedist=16 flagnearby |
| 85 | |
| 86 | ## Calculate reduced coverage as per CallVariants defaults (ignoring outermost 5bp of reads). |
| 87 | pileup.sh in="$NAME"_trimclip.sam.gz basecov="$NAME"_basecov_border5.txt -Xmx4g ow border=5 |
| 88 | |
| 89 | ## Generate a mutant reference by applying the detected variants to the reference. |
| 90 | ## This is essentially the reference-guided assembly of the strain. |
| 91 | ## Also changes anything below depth MINCOV to N (via the mindepth flag). |
| 92 | ## Does not apply indels below MINCOV |
| 93 | applyvariants.sh in="$REF" out="$NAME"_genome.fa vcf="$NAME"_vars.vcf basecov="$NAME"_basecov_border5.txt ow mindepth="$MINCOV" |
| 94 | |
| 95 | ## Make bam/bai files; requires samtools to be installed. |
| 96 | ## This step is only necessary for visualization, not variant-calling. |
| 97 | #samtools view -bShu "$NAME"_trimclip.sam.gz | samtools sort -m 2G -@ 3 - -o "$NAME"_sorted.bam |
| 98 | #samtools index "$NAME"_sorted.bam |
| 99 | |
| 100 | ## At this point, "$NAME"_sorted.bam, "$NAME"_sorted.bam.bai, "$REF", and "$NAME"_vars.vcf can be used for visualization in IGV. |
| 101 |