forked from chaolinzhanglab/czplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVcf.pm
350 lines (269 loc) · 6.19 KB
/
Vcf.pm
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
#
#===============================================================================
#
# FILE: Vcf.pm
#
# DESCRIPTION: Package to handle Vcf file
# BUGS: ---
# NOTES:
# AUTHOR: Chaolin Zhang (cz), [email protected]
# COMPANY: Columbia University
# VERSION: 1.0
# CREATED: 12/07/13
# REVISION: ---
#===============================================================================
package Vcf;
require Exporter;
our $VERSION = 1.01;
@ISA = qw (Exporter);
@EXPORT = qw(
assignAltBase
generateVcfHeader
lineToVcf
readVcfFile
splitVcfFileByChrom
vcfToLine
writeVcfFile
);
=head1 Bed
Subroutines to handle BED file
=cut
use strict;
use warnings;
use Data::Dumper;
use Carp;
use Common;
#
#object-oriented interface will be added later
#
my $debug = 0;
my @vcfInfoField = (
['DP4', 0,'s'],
['BC', 1, 'i'],
['snp', 2,'s'],
['refStrandP', 3,'f'],
['altStrandP', 4,'f'],
['altP', 5,'f'],
['strand', 6,'s'],
['gene', 7,'s'],
['region',8,'s']
);
my %vcfInfoFieldHash = (
'DP4'=>[0,'s'],
'BC'=>[1,'i'],
'snp'=> [2,'s'],
'refStrandP'=>[3,'f'],
'altStrandP'=>[4,'f'],
'altP'=>[5,'f'],
'strand'=>[6,'s'],
'gene'=>[7,'s'],
'region'=>[8,'s']
);
sub readVcfFile
{
my ($inFile, $verbose, $msgio) = @_;
$msgio = *STDOUT unless $msgio;
my $validateFields = 0; #prefixed for now.
my $fin;
my @ret;
my $iter = 0;
open ($fin, "<$inFile") || Carp::croak "cannot open file $inFile to read\n";
while (my $line =<$fin>)
{
chomp $line;
next if $line =~/^\#/;
next if $line =~/^\s*$/;
print $msgio "$iter ...\n" if $verbose && $iter % 500000 == 0;
$iter++;
my $snv = lineToVcf ($line, $validateFields);
push @ret, $snv;
}
close($fin);
return \@ret;
}
sub generateVcfHeader
{
my $ret = "#" . join ("\t", qw(CHROM POS ID REF ALT QUAL FILTER INFO));
return $ret;
}
sub writeVcfFile
{
my ($sites, $outFile, $verbose, $msgio) = @_;
$msgio = *STDOUT unless $msgio;
my $fout;
open ($fout, ">$outFile") || Carp::croak "cannot open file $outFile to write\n";
print $fout generateVcfHeader (), "\n";
for (my $i = 0; $i < @$sites; $i++)
{
print $msgio "$i ...\n" if $verbose && $i % 500000 == 0;
print $fout vcfToLine ($sites->[$i]), "\n";
}
close ($fout);
}
#outDir has to be an existing dir
sub splitVcfFileByChrom
{
my ($inFile, $outDir, $verbose, $msgio) = @_;
if ($inFile ne '-')
{
Carp::croak "$inFile does not exist\n" unless -f $inFile;
}
Carp::croak "$outDir does not exist\n" unless -d $outDir;
$msgio = *STDOUT unless $msgio;
my %siteCount;
my $fin;
#print "reading tags from $inBedFile ...\n" if $verbose;
if ($inFile eq '-')
{
$fin = *STDIN;
}
else
{
if ($inFile =~/\.gz$/)
{
open ($fin, "gunzip -c $inFile |") || Carp::croak "can not open file $inFile to read\n";
}
elsif ($inFile =~/\.bz2$/)
{
open ($fin, "bunzip2 -c $inFile |") || Carp::croak "can not open file $inFile to read\n";
}
else
{
open ($fin, "<$inFile") || Carp::croak "can not open file $inFile to read\n";
}
}
#my %tagCount;
#write tags according to chromosomes
my $i = 0;
my %fhHash;
while (my $line = <$fin>)
{
chomp $line;
next if $line=~/^\s*$/;
next if $line=~/^\#/;
$i++;
print $msgio "$i ...\n" if $i % 500000 == 0 && $verbose;
$line =~/(\S+)\s/;
my $chrom = $1;
my $tmpFile = "$outDir/$chrom.vcf";
if (not exists $fhHash{$chrom})
{
my $fout;
open ($fout, ">>$tmpFile") || Carp::croak "can not open file $tmpFile to write: $?\n";
$fhHash{$chrom} = $fout;
}
#open ($fout, ">>$tmpFile") || Carp::croak "can not open file $tmpFile to write\n";
my $fout = $fhHash{$chrom};
print $fout $line, "\n";
#close ($fout);
if (exists $siteCount{$chrom})
{
$siteCount{$chrom}->{"n"} ++;
}
else
{
$siteCount{$chrom} = {f=>"$outDir/$chrom.vcf", n=> 1};
}
}
close ($fin) if $inFile ne '-';
#close all file handles
foreach my $chrom (sort keys %fhHash)
{
close ($fhHash{$chrom});
}
return \%siteCount;
}
=head2 lineToVcf
Pass a line of Vcf file
my $vcf = lineToVcf ($line;
=cut
sub lineToVcf
{
my ($line, $validateFields) = @_;
my ($chrom, $position, $id, $refBase, $altBase, $qual, $filter, $info, @ignored) = split ("\t", $line);
my $infoHash = parseVcfInfo ($info, $validateFields);
my %vcfHash = (chrom=>$chrom, position=>$position-1, id=>$id, refBase=>$refBase, altBase=>$altBase, qual=>$qual, filter=>$filter, info=>$infoHash);
return \%vcfHash;
}
sub vcfToLine
{
my $vcf = $_[0];
my $validateInfoFields = 0;
my $infoStr = encodeVcfInfo ($vcf->{'info'});
return join ("\t", $vcf->{'chrom'}, $vcf->{'position'}+1, $vcf->{'id'}, $vcf->{'refBase'}, $vcf->{'altBase'}, $vcf->{'qual'}, $vcf->{'filter'}, $infoStr);
}
sub parseVcfInfo
{
my ($infoStr, $validateFields) = @_;
my %infoHash;
my @pairs = split (";", $infoStr);
foreach my $pair (@pairs)
{
next if $pair eq '.';
my $name = $pair;
if ($pair=~/^(.*?)\=(.*?)$/)
{
#a pair
$infoHash{$1} = $2;
$name = $1;
}
else
{
#not a pair
$infoHash{$pair} = 1;
}
if (defined $validateFields && $validateFields == 1)
{
Carp::croak "$name is not a valid VCF info field\n" unless exists $vcfInfoFieldHash{$name};
}
}
return \%infoHash;
}
sub encodeVcfInfo
{
my ($info, $validateFields) = @_;
if (defined $validateFields && $validateFields == 1)
{
#make sure every field is considered
foreach my $name (keys %$info)
{
Carp::croak "$name is not a valid VCF info field\n" unless exists $vcfInfoFieldHash{$name};
}
}
my $ret = "";
foreach my $f (@vcfInfoField)
{
my $name = $f->[0];
next unless exists $info->{$name};
if ($f->[2] ne '')
{
$ret .= join ("=", $name, $info->{$name}) . ";";
}
else
{
$ret .= $name . ";";
}
}
chop $ret if $ret ne '';
return $ret;
}
#
sub assignAltBase
{
my ($readBaseHash, $refBase) = @_;
my %readBaseHash = %$readBaseHash;
#add a small number to resolve ties
map {$readBaseHash{$_} += rand(1) * 0.01} qw(A C G T);
my @readBaseSort = sort {$readBaseHash{$b} <=> $readBaseHash{$a}} qw(A C G T);
my $altBase = '';
foreach my $b (@readBaseSort)
{
if ($b ne $refBase)
{
$altBase = $b;
last;
}
}
return $altBase;
}
1;