-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_vcf.sh
executable file
·89 lines (75 loc) · 2.44 KB
/
merge_vcf.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
if [ $# -ne 2 ] ; then
echo "Error !!! Requires 2 arguments. $# argument(s) provided. Exiting ... (ERR_CODE: 1000)"
exit 1000
fi
declare bcftools_path
declare bgzip_path
declare script_path
declare input_folder
declare input_count
declare output_folder
declare -a files
declare -a input_ids
script_path="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )/"
source $script_path"config.sh"
input_folder=$1
output_folder=$2
source $script_path"config.sh"
if [ ! -d "$input_folder" ] ; then
echo "Error !!! Invalid 'input_folder' given. Exiting ... (ERR_CODE: 1001)"
exit 1001
fi
if [ ! -d "$output_folder" ] ; then
echo "Error !!! Invalid 'output_folder' given. Exiting ... (ERR_CODE: 1002)"
exit 1002
fi
if [ "${input_folder[@]: -1: 1}" != "/" ] ; then
input_folder="$input_folder/"
fi
if [ "${output_folder[@]: -1: 1}" != "/" ] ; then
output_folder="$output_folder/"
fi
# files=(`ls $input_folder*.vcf`)
files=(`find $input_folder -maxdepth 1 -type f -name "*.vcf"`)
for ((i=0,j=0; i<${#files[@]}; ++i)) ; do
declare temp
temp=${files[$i]##*/}
temp=${temp%%.*}
temp=${temp%_*}
if [[ ! " ${input_ids[*]} " =~ " $temp " ]]; then
input_ids[$j]=$temp
((j=j+1))
fi
# echo $j" => "$temp" => "${input_ids[j-1]}
unset temp
done
input_count=${#input_ids[@]}
if [[ $input_count -lt 1 ]] ; then
echo "Error !!! No VCF files found in the input_folder ($input_folder). Exiting ... (ERR_CODE: 1001)"
exit 1001
elif [[ $input_count -eq 1 ]] ; then
echo "Error !!! Only one VCF file {${input_ids[@]}} found in the input_folder ($input_folder). Cannot merge single VCF. Exiting ... (ERR_CODE: 1001)"
exit 1001
fi
echo "Running with following arguments:"
echo "script_path = $script_path"
echo "input_folder = $input_folder"
echo "Sample IDs = {"${input_ids[@]}"}"
echo "output_folder = $output_folder"
echo "bgzip_path = $bgzip_path"
echo "bcftools_path = $bcftools_path"
echo ""
echo "Doing zip and index ..."
for ((i=0; i<${#input_ids[@]}; ++i)) ; do
$bgzip_path -c "$input_folder${input_ids[$i]}.vcf" > $output_folder${input_ids[$i]}.vcf.gz && $bcftools_path index $output_folder${input_ids[$i]}.vcf.gz
done
echo "Done zip and index. Exit status $?"
echo ""
echo "Doing VCF merge ..."
declare -a zipped_vcf_files
for ((i=0; i<${#input_ids[@]}; ++i)) ; do
zipped_vcf_files[$i]=$output_folder${input_ids[$i]}".vcf.gz"
done
# echo ${zipped_vcf_files[@]}
$bcftools_path merge -m all ${zipped_vcf_files[@]} > $output_folder"merged.vcf"
echo "Done VCF merge. Exit status $?"