forked from rnnh/bioinfo-notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnp_calling.sh
244 lines (205 loc) · 8 KB
/
snp_calling.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#! /bin/bash
# https://github.com/rnnh/bioinfo-notebook.git
# Help/usage text
usage="$(basename "$0") [-h|--help] [-1|--one -2|--two -r|--reference] \n
[-d|--demo] [-o|--output -l|--log -p|--processors n] \n
\n
This script aligns sequencing reads to a reference genome, and finds genetic \n
variants (SNPs/indels) based on this alignment, which are written to a variant\n
call format (VCF) file.\n
\n
Calling this script with the argument '-d' or '--demo' will run this script \n
using Saccharomyces cerevisiae FASTQ sequencing reads and a Saccharomyces \n
cerevisiae reference genome, which will be downloaded from NCBI. \n
\n
This script should be called from the 'bioinfo-notebook/' directory.The \n
programs required for this script are in the 'bioinfo-notebook' conda \n
environment (bioinfo-notebook/envs/bioinfo-notebook.yml or \n
bioinfo-notebook/envs/bioinfo-notebook.txt). \n
If the input files are not in the 'bioinfo-notebook/data/' directory, the full \n
file paths should be given.\n\n
\n
arguments: \n
\t -h | --help\t\t show this help text and exit \n
\t -1 | --one\t\t forward reads to align with reference sequence \n
\t\t\t\t (FASTQ: .fastq or .fastq.gz) \n
\t -2 | --two\t\t reverse reads to align with reference sequence \n
\t\t\t\t (FASTQ: .fastq or .fastq.gz) \n
\t -r | --reference\t reference sequence to align reads against \n
\t\t\t\t (FASTA nucleotide file: .fna) \n
\t -d | --demo\t\t run the script with demonstration inputs\n
\n
optional arguments: \n
\t -o | --output\t\t optional: name of final output file \n
\t\t\t\t (default: 'reference_seq_vs_reads_var.vcf', or \n
\t\t\t\t 'S_cere_DRR237290_var.vcf' if demo is used). \n
\t -l | --log\t\t redirect terminal output to a log file in the \n
\t\t\t\t directory bioinfo-notebook/results/ \n
\t -p | --processors\t optional: set the number (n) of processors to \n
\t\t\t\t use (default: 1) \n
"
MAKELOG=false
PROCESSORS=1
DEMO=false
ONE=""
TWO=""
REFERENCE=""
OUTPUT=""
# Iterating through the input arguments with a while loop
while (( "$#" )); do
case "$1" in
-h|--help)
echo -e $usage
exit
;;
-1|--one)
ONE=$2
shift 2
;;
-2|--two)
TWO=$2
shift 2
;;
-r|--reference)
REFERENCE=$2
shift 2
;;
-o|--output)
OUTPUT=$2
shift 2
;;
-d|--demo)
DEMO=true
shift 1
;;
-l|--log)
MAKELOG=true
shift 1
;;
-p|--processors)
PROCESSORS=$2
shift 2
;;
--) # end argument parsing
shift
break
;;
-*|--*) # unsupported flags
echo -e "ERROR: $1 is an invalid option. \n" >&2
echo -e $usage
exit 1
;;
esac
done
cd ~/bioinfo-notebook/data/
if $MAKELOG ; then
# Creating results directory, if it does not already exist
if [ ! -d ../results ]; then
mkdir ../results
fi
# CREATING LOG FILE
# Terminal output directed to the file 'snp_calling_[date]_[time].log'
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>../results/snp_calling_$(date +%Y%m%d_%H%M).log 2>&1
fi
echo "$(date +%Y/%m/%d\ %H:%M) Beginning SNP calling script."
if $DEMO ; then
echo Downloading reads...
until fastq-dump --gzip --skip-technical --readids --read-filter pass \
--dumpbase --split-files --clip DRR237290; do
echo fastq-dump failed, retrying in 10 seconds...
sleep 10s
done
echo Downloading reference sequence...
curl -s --remote-name --remote-time ftp://ftp.ncbi.nlm.nih.gov/genomes/all/\
GCF/000/146/045/GCF_000146045.2_R64/GCF_000146045.2_R64_genomic.fna.gz
echo Decompressing reference sequence...
gunzip GCF_000146045.2_R64_genomic.fna.gz
echo Indexing reference sequence for bowtie2...
bowtie2-build GCF_000146045.2_R64_genomic.fna S_cere_ref_seq
echo Aligning reads to the reference genome...
bowtie2 --no-unal -p $PROCESSORS -x S_cere_ref_seq \
-1 DRR237290_pass_1.fastq.gz -2 DRR237290_pass_2.fastq.gz \
-S S_cere_DRR237290_alignment.sam
echo Converting SAM alignment to sorted BAM alignment...
samtools view -@ $PROCESSORS -Sb \
-o S_cere_DRR237290_alignment_unsorted.bam S_cere_DRR237290_alignment.sam
samtools sort -@ $PROCESSORS -O bam -l 9 -o S_cere_DRR237290_alignment.bam \
S_cere_DRR237290_alignment_unsorted.bam
echo Removing redundant alignment files...
rm -v S_cere_DRR237290_alignment.sam S_cere_DRR237290_alignment_unsorted.bam
echo Indexing reference sequence for SAMtools...
samtools faidx GCF_000146045.2_R64_genomic.fna
echo Generating genotype variant likelihoods with BCFtools...
bcftools mpileup --max-depth 10000 --threads $PROCESSORS \
-f GCF_000146045.2_R64_genomic.fna \
-o S_cere_DRR237290_full.bcf S_cere_DRR237290_alignment.bam
echo Variant calling with BCFtools...
bcftools call -O b --threads $PROCESSORS -vc --ploidy 1 -p 0.05 \
-o S_cere_DRR237290_var_unfiltered.bcf S_cere_DRR237290_full.bcf
echo Removing redundant BCF file...
rm -v S_cere_DRR237290_full.bcf
if [ -z $OUTPUT ]; then
echo Variant filtering with BCFtools filter...
bcftools filter --threads $PROCESSORS -i '%QUAL>=20' -O v \
-o S_cere_DRR237290_var.vcf S_cere_DRR237290_var_unfiltered.bcf
echo Head of VCF file...
head S_cere_DRR237290_var.vcf
echo Tail of VCF file...
tail S_cere_DRR237290_var.vcf
echo "$(date +%Y/%m/%d\ %H:%M) Script finished."
else
echo Variant filtering with BCFtools filter...
bcftools filter --threads $PROCESSORS -i '%QUAL>=20' -O v \
-o $OUTPUT.vcf S_cere_DRR237290_var_unfiltered.bcf
echo Head of VCF file...
head $OUTPUT.vcf
echo Tail of VCF file...
tail $OUTPUT.vcf
fi
fi
echo Indexing reference sequence for bowtie2...
bowtie2-build $REFERENCE reference_seq
echo Aligning reads to the reference genome...
bowtie2 --no-unal -p $PROCESSORS -x reference_seq \
-1 $ONE -2 $TWO -S reference_seq_vs_reads_alignment.sam
echo Converting SAM alignment to sorted BAM alignment...
samtools view -@ $PROCESSORS -Sb \
-o reference_seq_vs_reads_alignment_unsorted.bam \
reference_seq_vs_reads_alignment.sam
samtools sort -@ $PROCESSORS -O bam -l 9 \
-o reference_seq_vs_reads_alignment.bam \
reference_seq_vs_reads_alignment_unsorted.bam
echo Removing redundant alignment files...
rm -v reference_seq_vs_reads_alignment.sam \
reference_seq_vs_reads_alignment_unsorted.bam
echo Indexing reference sequence for SAMtools...
samtools faidx $REFERENCE
echo Generating genotype variant likelihoods with BCFtools...
bcftools mpileup --max-depth 10000 --threads $PROCESSORS \
-f $REFERENCE -o reference_seq_vs_reads_full.bcf \
reference_seq_vs_reads_alignment.bam
echo Variant calling with BCFtools...
bcftools call -O b --threads $PROCESSORS -vc --ploidy 1 -p 0.05 \
-o reference_seq_vs_reads_var_unfiltered.bcf reference_seq_vs_reads_full.bcf
echo Removing redundant BCF file...
rm reference_seq_vs_reads_full.bcf
if [ -z $OUTPUT ]; then
echo Variant filtering with BCFtools filter...
bcftools filter --threads $PROCESSORS -i '%QUAL>=20' -O v \
-o reference_seq_vs_reads_var.vcf reference_seq_vs_reads_var_unfiltered.bcf
echo Head of VCF file...
head reference_seq_vs_reads_var.vcf
echo Tail of VCF file...
tail reference_seq_vs_reads_var.vcf
else
echo Variant filtering with BCFtools filter...
bcftools filter --threads $PROCESSORS -i '%QUAL>=20' -O v \
-o $OUTPUT.vcf reference_seq_vs_reads_var_unfiltered.bcf
echo Head of VCF file...
head $OUTPUT.vcf
echo Tail of VCF file...
tail $OUTPUT.vcf
fi
echo "$(date +%Y/%m/%d\ %H:%M) Script finished."