-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_info_VCF.pl
160 lines (120 loc) · 2.96 KB
/
extract_info_VCF.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/perl
#######################
#
# Extract imputation info from Michigan VCF HRC imputation files (.gz)
#
# (c) Alexander Teumer 2021 ateumer@uni-greifswald.de
#
#######################
$sep = "\t"; # separator in output file
# get filenames
$dirname = $ARGV[0];
if ($dirname eq "") {
print STDERR "Input directory required!\n";
exit 1;
}
$prefix = $ARGV[1];
if ($prefix eq "") {
print STDERR "Prefix required (e.g. SHIP-0_R4)!\n";
exit 1;
}
$filemask = $prefix.".*.vcf.gz\$";
opendir(DIR, "$dirname");
@files = grep(/$filemask/,readdir(DIR));
closedir(DIR);
# Get output file name
$outname = $dirname."/".$prefix.".all.info";
print "output name: ".$outname."\n";
open (OUT,">$outname.txt");
print OUT "SNPID".$sep."REF".$sep."ALT".$sep."Genotyped".$sep."MAF".$sep."R2".$sep."AVG_CS".$sep."ER2".$sep."ID"."\n";
$col_chr = 0;
$col_position = 1;
$col_id = 2;
$col_alleleA = 3;
$col_alleleB = 4;
$col_format = 7; # INFO
foreach $filename (@files) {
print "Processing: $filename";
$cnt=-1;
open (F,"zcat $dirname/$filename|");
while(<F>) {
my $line = $_;
#$line=~s/[\n\r]//g;
if ((($line) =~ /^#CHROM\t/) || ($cnt>=0)) {
$cnt++;
}
if ($cnt <= 0) {
next;
}
my @vals = split("\t",$line,9);
# generate modout values
my $chr = $vals[$col_chr];
$chr=~s/^0+//g; # remove leading zeros
my $pos = $vals[$col_position];
my $format = $vals[$col_format];
my $id = $vals[$col_id];
my $allele_a = $vals[$col_alleleA];
my $allele_b = $vals[$col_alleleB];
my $type = "SNP"; # HRC includes SNPs only so far
my $snpid = $chr.':'.$pos.':'.$allele_a.':'.$allele_b;
# effallele, freq, n
$ref_all = $vals[$col_alleleA];
$alt_all = $vals[$col_alleleB];
# imputation info
$oevar_imp = "NA";
my @buf = split(";",$format);
for ($i=scalar @buf - 1; $i>=0; $i--) {
($key,$val) = split("=",$buf[$i],2);
if ($key eq "R2") {
$oevar_imp = $val;
last;
}
}
# imputation infoe
$oevar_impe = "NA";
my @buf = split(";",$format);
for ($i=scalar @buf - 1; $i>=0; $i--) {
($key,$val) = split("=",$buf[$i],2);
if ($key eq "ER2") {
$oevar_impe = $val;
last;
}
}
# Average call score
$oevar_cs = "NA";
my @buf = split(";",$format);
for ($i=scalar @buf - 1; $i>1; $i--) {
($key,$val) = split("=",$buf[$i],2);
if ($key eq "AVG_CS") {
$oevar_cs = $val;
last;
}
}
# Minor allele frequency
$oevar_MAF = "NA";
my @buf = split(";",$format);
for ($i=scalar @buf - 1; $i>1; $i--) {
($key,$val) = split("=",$buf[$i],2);
if ($key eq "MAF") {
$oevar_MAF = $val;
last;
}
}
# Genotyped
$genotype = "Genotyped";
my @buf = split(";",$format);
for ($i=scalar @buf; $i>1; $i--) {
$key = $buf[1];
if ($key eq "IMPUTED") {
$genotype = "Imputed";
last;
}
}
# output
print OUT $snpid.$sep.$ref_all.$sep.$alt_all.$sep.$genotype.$sep.$oevar_MAF.$sep.$oevar_imp.$sep.$oevar_cs.$sep.$oevar_impe.$sep.$id."\n";
}
close(F);
print " $cnt result lines processed.\n";
}
close(OUT);
print "Done.\n";