-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPoolSNP.sh
More file actions
executable file
·348 lines (269 loc) · 10.3 KB
/
PoolSNP.sh
File metadata and controls
executable file
·348 lines (269 loc) · 10.3 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/bin/bash
## PoolSNP
## Author: Martin Kapun
## test if Shell is indeed BASH
# Specify the scripts/ directory
SCRIPTS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPTS_DIR="$SCRIPTS_DIR/scripts"
###############################################
######### READ COMMANDLINE ARGUMENTS ##########
###############################################
for i in "$@"
do
case $i in
mpileup=*)
mpileup="${i#*=}"
;;
reference=*)
ref="${i#*=}"
;;
min-cov=*)
mic="${i#*=}"
;;
max-cov=*)
mac="${i#*=}"
;;
min-count=*)
mico="${i#*=}"
;;
min-freq=*)
mif="${i#*=}"
;;
miss-frac=*)
mis="${i#*=}"
;;
base-quality=*)
bq="${i#*=}"
;;
names=*)
names="${i#*=}"
;;
jobs=*)
jobs="${i#*=}"
;;
output=*)
out="${i#*=}"
;;
badsites=*)
BS="${i#*=}"
;;
allsites=*)
AS="${i#*=}"
;;
*)
# unknown option
;;
esac
done
###############################################
######## TEST IF GNU PARALLEL INSTALLED #######
###############################################
echo "Test if GNU parallel installed"
echo "*********************"
echo ""
command -v parallel >/dev/null 2>&1 || { echo >&2 "PoolSNP requires GNU parallel but it's not installed. Please check here: https://www.gnu.org/software/parallel/ for more details. Aborting."; exit 1; }
echo "done"
echo ""
###############################################
######## TEST IF ALL PARAMETERS ARE SET #######
###############################################
mpileup=${mpileup}
out=${out}
ref=${ref}
names=${names}
help='''
********************************
************ HELP **************
********************************
PoolSNP v. 1.05 - 13/11/2017
A typcial command line looks like this:
sh ~/PoolSNP.sh \
mpileup=~/input.mpileup \ ## The input mpileup
output=~/output \ ## The output prefix
reference=~/reference.fa \ ## The reference FASTA file
names=sample1,sample2,sample3 \ ## A comma separated list of samples names according to the order in the mpileup file
min-cov=10 \ ## sample-wise minimum coverage
max-cov=0.95 \ ## Either the maximum coverage percentile to be computed or an input file
min-count=20 \ ## minimum alternative allele count across all populations pooled
min-freq=0.001 \ ## minimum alternative allele frequency across all populations pooled
miss-frac=0.1 \ ## maximum allowed fraction of samples not fullfilling all parameters
base-quality 15 \ ## minimum base quality for every nucleotide
jobs=10 ## number of parallel jobs/cores used for the SNP calling
Please see below, which parameter is missing:
*******************************
'''
if [ -z "$mpileup" ]; then echo "$help\nmpileup=~/input.mpileup is missing: The full path to the input mpileup needs to be specified"; exit 1 ; fi
if [ -z "$out" ]; then echo "$help\noutput=~/output is missing: The full path to the output file prefix needs to be defined. The script will automatically append file extensions"; exit 2 ; fi
if [ -z "$names" ]; then echo "$help\nComma separated list of names correpsonding to the samples in the mpileup file"; exit 3; fi
if [ -z "$ref" ]; then echo "$help\nThe full path to the reference FASTA genome is missing"; exit 3; fi
if [ -z "$mac" ]; then echo "$help\nEither provide an upper quantile cutoff (e.g. 0.95 to exclude all reads lager than the 95% coverage percentile) or input file"; exit 4; fi
if [ -z "$mic" ]; then mic=10; fi
if [ -z "$mis" ]; then mis=0.1; fi
if [ -z "$mif" ]; then mif=0.01; fi
if [ -z "$mico" ]; then mico=20; fi
if [ -z "$bq" ]; then bq=15; fi
if [ -z "$jobs" ]; then jobs=1; fi
if [ -z "$BS" ]; then BS=1; fi
if [ -z "$AS" ]; then AS=0; fi
## change to home directory of scripts
BASEDIR=$(dirname $0)
cd $BASEDIR
## replace commas with tabs of name list
new=$(echo $names | tr ',' '\t')
## create temporary directory
mkdir -p $out/temp
###############################################
############### CREATE VCF HEADER #############
###############################################
## create VCF header
echo -e """##fileformat=VCFv4.2
##fileDate=$(date +%d'/'%m'/'%y)
##Source=PoolSnp-1.05
##Parameters=<ID=MinCov,Number=$mic,Type=Integer,Description=\"Minimum coverage per sample\">
##Parameters=<ID=MaxCov,Number=$mac,Type=Integer,Description=\"Maximum chromosome- and sample-specific maximum coverage; Either a precomuted file or the maximum percentile cutoff, eg. 0.95 to consider only reads within the 95% coverage percentile\">
##Parameters=<ID=MinCount,Number=$mico,Type=Integer,Description=\"Minimum alternative allele count across all samples pooled\">
##Parameters=<ID=MinFreq,Number=$mif,Type=Float,Description=\"Minimum alternative allele frequency across all samples pooled\">
##Parameters=<ID=MaximumMissingFraction,Number=$mis,Type=Float,Description=\"Maximum fraction of samples allowed that are not fullfilling all parameters\">
##Parameters=<ID=BaseQual,Number=$bq,Type=Integer,Description=\"Minimum PHRED scaled base quality\">
##Reference=$ref
##INFO=<ID=ADP,Number=1,Type=Integer,Description=\"Average per-sample depth of bases with Phred score >=$bq\">
##INFO=<ID=NC,Number=1,Type=Integer,Description=\"Number of samples not called\">
##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">
##FORMAT=<ID=RD,Number=1,Type=Integer,Description=\"Reference Counts\">
##FORMAT=<ID=AD,Number=1,Type=Integer,Description=\"Alternative Counts\">
##FORMAT=<ID=DP,Number=1,Type=Integer,Description=\"Total Depth\">
##FORMAT=<ID=FREQ,Number=1,Type=Float,Description=\"Variant allele frequency\">
#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t$new""" > $out/temp/header.txt
###############################################
#### calculate max coverage when necessary ####
###############################################
# test if $mac is not a file else continue
if ! [ -f $mac ]
then
# if $mac is a decimal, calculate the coverage threshold for each chromsomal arm and sample smaller than the cutoff percentile, for example, if you provide 0.95, the maximum coverage of the 95% coverage percentile will be calculated. This is REALLY computationally demanding. Thus, to parallelize to the extend possible, I split the mpileup by contigs and execute these in parallel.
mkdir -p $out/temp/cov/mpileups
echo "Splitting Mpileup by contigs"
echo "*********************"
echo ""
# test if mpileup is gzipped and split the mpileup by contig name. AWK is the fastest, I tried a lot!!
if [[ $mpileup =~ \.gz$ ]]
then
gunzip -c $mpileup | awk -v out="$out/temp/cov/mpileups/" -v file="" -v i="" '{if (i!=$1) {close(file); close(command); if (i!="") {print i" done"}} else {file=out$1".mpileup.gz" ; command= "gzip > "file ; print | command; }; i=$1 }'
else
awk -v out="$out/temp/cov/mpileups/" -v file="" -v i="" '{if (i!=$1) {close(file); close(command); if (i!="") {print i" done"}} else {file=out$1".mpileup.gz" ; command= "gzip > "file ; print | command; }; i=$1 }' $mpileup
fi
mkdir -p $out/temp/cov/cutoffs
## read FASTA contig names and run Python script "max-cov.py" to calculate coverage thresholds
## Note that ALL contigs with special characters (|/\,) will be ignored
# test if Reference FASTA is gzipped
if [[ $ref =~ \.gz$ ]]
then
gunzip -c $ref | grep '^>' | awk '$1!~/\|/ && $1!~/\// && $1!~/\\/ && $1!~/,/' | awk -v inp="$out" -v cut="$mac" -v script_dir="$SCRIPTS_DIR" '{print "python3 " script_dir "/max-cov.py --mpileup " inp "/temp/cov/mpileups/" substr($1,2) ".mpileup.gz --cutoff " cut " --contig " substr($1,2) " --out " inp "/temp/cov/cutoffs/" substr($1,2) ".txt"}' > $out/temp/contignames.txt
else
grep '^>' $ref | awk '$1!~/\|/ && $1!~/\// && $1!~/\\/ && $1!~/,/' | awk -v inp="$out" -v cut="$mac" -v script_dir="$SCRIPTS_DIR" '{print "python3 " script_dir "/max-cov.py --mpileup " inp "/temp/cov/mpileups/" substr($1,2) ".mpileup.gz --cutoff " cut " --contig " substr($1,2) " --out " inp "/temp/cov/cutoffs/" substr($1,2) ".txt"}' > $out/temp/contignames.txt
fi
echo "done"
echo ""
echo "Max coverage percentile $mac calculation started"
echo "*********************"
echo ""
## start parallel calulation of maximum coverage
cat $out/temp/contignames.txt | parallel --no-notice --jobs $jobs
# merge files into single coverage file
for f in $out/temp/cov/cutoffs/*
do
cat "$f" >> $out-cov-$mac.txt
done
echo "done"
echo ""
## once the calulations are done, point to the new coverage cutoff file
mac=$out-cov-$mac.txt
fi
###############################################
################## RUN POOLSNP ################
###############################################
echo "PoolSNP started"
echo "*********************"
echo ""
# test if Mpileup is comressed
if [[ $mpileup =~ \.gz$ ]]
then
## run PoolSNP with GNU parallel
gunzip -c $mpileup | parallel \
-k \
--pipe \
-j $jobs \
--no-notice \
--cat python3 "$SCRIPTS_DIR/PoolSnp.py" \
--mpileup {} \
--min-cov $mic \
--max-cov $mac \
--min-freq $mif \
--miss-frac $mis \
--min-count $mico \
--base-quality $bq \
--allsites $AS \
> $out/temp/SNPs.txt
else
parallel \
-k \
-j $jobs \
--pipepart \
--no-notice \
-a $mpileup \
--cat python3 "$SCRIPTS_DIR/PoolSnp.py" \
--mpileup {} \
--min-cov $mic \
--max-cov $mac \
--min-freq $mif \
--miss-frac $mis \
--min-count $mico \
--base-quality $bq \
--allsites $AS \
> $out/temp/SNPs.txt
fi
echo "done"
echo ""
###############################################
##### concatenate and remove temp files #######
###############################################
cat $out/temp/header.txt $out/temp/SNPs.txt | gzip > $out.vcf.gz
rm -r $out/temp
###############################################
## export file containing sites with bad cov ##
###############################################
if [[ $BS = 1 ]]
echo "BadSites caluclation started"
echo "*********************"
echo ""
then
if [[ $mpileup =~ \.gz$ ]]
then
gunzip -c $mpileup | parallel \
-k \
--pipe \
-j $jobs \
--no-notice \
--cat python3 "$SCRIPTS_DIR/bad-sites.py" \
--mpileup {} \
--min-cov $mic \
--max-cov $mac \
--base-quality $bq \
| gzip > $out\_BS.txt.gz
else
parallel \
-k \
-j $jobs \
--pipepart \
--no-notice \
-a $mpileup \
--cat python3 "$SCRIPTS_DIR/bad-sites.py" \
--mpileup {} \
--min-cov $mic \
--max-cov $mac \
--base-quality $bq \
| gzip > $out\_BS.txt.gz
fi
echo "done"
echo ""
fi