-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPGGSV.024.vcf2cnvr.pl
executable file
·143 lines (138 loc) · 3.61 KB
/
PGGSV.024.vcf2cnvr.pl
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
#!/usr/bin/env perl
## specially for convert into CNVnator genotype input format
use File::Basename;
die("Argument: .genotype.vcf[.gz]\n") if (@ARGV != 1);
my $vcf = shift @ARGV;
my ($fn, $dir, undef) = fileparse($vcf, qw/.vcf.gz .vcf/);
my ($chr_col, $pos_col, $id_col, $ref_col, $alt_col, $info_col, $format_col, $sample_col) = (1-1, 2-1, 3-1, 4-1, 5-1, 8-1, 9-1, 10-1);
if($vcf =~ /\.vcf\.gz$/){
open VCF, "zcat -c $vcf|" or die("Cannot open $vcf!\n");
}elsif($vcf =~ /\.vcf$/){
open VCF, "<$vcf" or die("Cannot open $vcf!\n");
}else{
die("Please give .vcf(.gz) as first argument!\n");
}
my $header;
while($line=<VCF>){
next if ($line=~/^##/);
$header = $line;
last;
}
my @header = split /\s/, $header;
while($line=<VCF>){
my $cn_num_flag = 0;
my @line = split /\s+/, $line;
my $chr = $line[$chr_col];
my $pos = $line[$pos_col];
my $id = $line[$id_col];
my $ref = $line[$ref_col];
if ($ref =~ /CN(\d+)/){
$cn_num_flag = 1;
$ref = $1;
}
my @alt = split /,/, $line[$alt_col];
for $i (0..$#alt){
if($alt[$i] =~ /CN(\d+)/){
$cn_num_flag = 1;
$alt[$i] = $1;
}else{
die("Check $line") if ($cn_num_flag);
}
}
my @alleles = ($ref,@alt);
my @info = split /;/, $line[$info_col];
my ($svtype, $end, $svlen);
for $item (@info){
if($item =~ /^SVTYPE=/){
$svtype = $';
}elsif($item =~ /^END=/){
$end = $';
}elsif($item =~ /^SVLEN=/){
$svlen = $';
}else{
}
}
die("1.Check $line") if (length($svtype)==0);
if(length($end)*length($svlen)!=0){
# print "2. Check $line" if (($pos - $end + 1)!=$svlen)
}
my @formats = split /:/, $line[$format_col];
my $GT_flag;
my $genotype_index;
for $i (0..$#formats){
if($formats[$i] eq 'GT'){ # for vcf genotype format ./. or .|.
$GT_flag = 1;
$genotype_index = $i;
}
if($formats[$i] eq 'CN'){ # for single copynumber format .
$GT_flag = 0;
$genotype_index = $i;
last; # 'CN' has higher priority
}
}
die("Unknown genotype format in $vcf!\n") if (length($GT_flag)<=0);
if(length($end)!=0){
# push @output, "$id\t$chr\t$pos\t$end\t$ref\t$line[$alt_col]\t$svtype";
$chr =~ s/^23$/X/;
$chr =~ s/^24$/Y/;
push @output, "${chr}:${pos}-${end}\n";
}elsif(length($svlen)!=0){
my $sv_end = $pos + $svlen - 1;
# push @output, "$id\t$chr\t$pos\t$sv_end\t$ref\t$line[$alt_col]\t$svtype";
$chr =~ s/^23$/X/;
$chr =~ s/^24$/Y/;
push @output, "${chr}:${pos}-${sv_end}\n";
}else{
die("Check $line!");
}
# for $i ($sample_col..$#line){
# my $gt = (split /:/, $line[$i])[$genotype_index];
# if ($gt =~ /\./){
# ## Missing data
# $output[-1] .= "\tNA";
# next;
# }
# if($GT_flag){
# ## SNP-like genotype format: ./. or .|.
# ## convert into copy number
# my @gt = (split /\/|\|/, $gt);
# my $cn = 0;
# foreach $a (@gt){
# if($line[$alt_col] =~ /del/i){
# ## DEL, GenomeSTRiP deletion vcf
# if($a == 0){
# $cn += 1;
# }elsif($a == 1){
# $cn += 0;
# }else{
# die("Unknown genotype in $line");
# }
# }elsif($cn_num_flag){
# ## Copy Numbers, like <CN#>
# if($a == 0){
# $cn += $ref;
# }elsif($a == 1){
# $cn += $alt[0];
# }else{
# die("Unknown genotype in $line");
# }
# }else{
# die("Check $line in $vcf!\n");
# }
# }
# $output[-1] .= "\t$cn";
# }else{
# ## CNV, GenomeSTRiP CNV vcf
# $output[-1] .= "\t$gt";
# }
# }
# $output[-1] .= "\n";
}
close VCF;
$calls_file = $dir . $fn . '.region.txt';
open CALL, ">$calls_file" or die("Cannot open $calls_file!\n");
#print CALL (join "\t",('ID','Chr','Pos','End','Ref','Alt','SVtype',@header[$sample_col..$#header]));
#print CALL "\n";
print CALL @output;
close CALL;
print "Finish $vcf!\n";