VCF.pm

filter: Statementstmt 30.4% 117 / 384 Branchbran 18.6% 41 / 220 Conditioncond 11.9% 10 / 84 MC/DCmcdc 1.9% 1 / 52 Subroutinesub 47.3% 18 / 38 Podpod 80.0% 16 / 20 totaltotal 25.4% 203 / 798 CC 143 SCAR 42.1
CC
143
Cov
24%
_open
146.6
_open
146.6
set_samples
90.0
CRAP 67.3
« VCF/V4_1.pm ↑ Summary VCF/V3_2.pm »
1 package VCF;
2 1 $VCF::VERSION = '1.003';
3 # http://vcftools.sourceforge.net/specs.html
4 # http://samtools.github.io/hts-specs/
5 #
6 # Authors: petr.danecek@sanger
7 # for VCF v3.2, v3.3, v4.0, v4.1, v4.2
8 #
9  
10 # ABSTRACT: Module for validation, parsing and creating VCF files.
11  
12 =head1 NAME
13  
14 VCF.pm. Module for validation, parsing and creating VCF files.
15 Supported versions: 3.2, 3.3, 4.0, 4.1, 4.2
16  
17 =head1 SYNOPSIS
18  
19 From the command line:
20 perl -MVCF -e validate example.vcf
21 perl -I/path/to/the/module/ -MVCF -e validate_v32 example.vcf
22  
23 From a script:
24 use VCF;
25  
26 my $vcf = VCF->new(file=>'example.vcf.gz',region=>'1:1000-2000');
27 $vcf->parse_header();
28  
29 # Do some simple parsing. Most thorough but slowest way how to get the data.
30 while (my $x=$vcf->next_data_hash())
31 {
32 for my $gt (keys %{$$x{gtypes}})
33 {
34 my ($al1,$sep,$al2) = $vcf->parse_alleles($x,$gt);
35 print "\t$gt: $al1$sep$al2\n";
36 }
37 print "\n";
38 }
39  
40 # This will split the fields and print a list of CHR:POS
41 while (my $x=$vcf->next_data_array())
42 {
43 print "$$x[0]:$$x[1]\n";
44 }
45  
46 # This will return the lines as they were read, including the newline at the end
47 while (my $x=$vcf->next_line())
48 {
49 print $x;
50 }
51  
52 # Only the columns NA00001, NA00002 and NA00003 will be printed.
53 my @columns = qw(NA00001 NA00002 NA00003);
54 print $vcf->format_header(\@columns);
55 while (my $x=$vcf->next_data_array())
56 {
57 # this will recalculate AC and AN counts, unless $vcf->recalc_ac_an was set to 0
58 print $vcf->format_line($x,\@columns);
59 }
60  
61 $vcf->close();
62  
63 =cut
64  
65 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 1 ▶use vars qw/@ISA @EXPORT/;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 @ISA = qw/Exporter/;
74 1 @EXPORT = qw/validate validate_v32/;
75  
76 1 ▶use VCF::V3_2;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 1 ▶use VCF::V3_3;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use VCF::V4_0;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 ▶use VCF::V4_1;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1 ▶use VCF::V4_2;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 =head2 validate
83  
84 About : Validates the VCF file.
85 Usage : perl -MVCF -e validate example.vcf.gz # (from the command line)
86 validate('example.vcf.gz'); # (from a script)
87 validate(\*STDIN);
88 Args : File name or file handle. When no argument given, the first command line
89 argument is interpreted as the file name.
90  
91 =cut
92  
93 sub validate
94 0 ▶{
Subroutine validate: not called CC=5 SCAR=34.0
Pod documented
95 0 ▶ my ($fh) = @_;
Subroutine validate: not called CC=5 SCAR=34.0
Pod documented
96  
97 0 ▶ if ( !$fh && @ARGV ) { $fh = $ARGV[0]; }
Branch: 2 branches
BranchTrueFalseTotal
if (not $fh and @ARGV) 0 0 0
if (not $fh and @ARGV) 0 0 0
Condition0%0/6
not $fh and @ARGV
!ll&&!rl&&r
000
not $fh and @ARGV
!ll&&!rl&&r
000
MC/DC0%0/2
not $fh and @ARGV
not $fh @ARGV
MC/DC0%0/2
not $fh and @ARGV
not $fh @ARGV
Truth table0%0/3
not(A) and B
A= not $fhB= @ARGV
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not $fhB= @ARGV
ABresult
0X0
100
111
98  
99 0 my $vcf;
100 0 ▶ if ( $fh ) { $vcf = fileno($fh) ? VCF->new(fh=>$fh) : VCF->new(file=>$fh); }
Branch: 4 branches
BranchTrueFalseTotal
if ($fh) { } 0 0 0
fileno $fh ? : 0 0 0
if ($fh) { } 0 0 0
fileno $fh ? : 0 0 0
101 0 else { $vcf = VCF->new(fh=>\*STDIN); }
102  
103 0 $vcf->run_validation();
104 }
105  
106  
107 =head2 validate_v32
108  
109 About : Same as validate, but assumes v3.2 VCF version.
110 Usage : perl -MVCF -e validate_v32 example.vcf.gz # (from the command line)
111 Args : File name or file handle. When no argument given, the first command line
112 argument is interpreted as the file name.
113  
114 =cut
115  
116 sub validate_v32
117 0 ▶{
Subroutine validate_v32: not called CC=6 SCAR=37.4
Pod documented
118 0 ▶ my ($fh) = @_;
Subroutine validate_v32: not called CC=6 SCAR=37.4
Pod documented
119  
120 0 ▶ if ( !$fh && @ARGV && -e $ARGV[0] ) { $fh = $ARGV[0]; }
Branch: 2 branches
BranchTrueFalseTotal
if (not $fh and @ARGV and -e $ARGV[0]) 0 0 0
if (not $fh and @ARGV and -e $ARGV[0]) 0 0 0
Condition0%0/12
not $fh and @ARGV and -e $ARGV[0]
!ll&&!rl&&r
000
not $fh and @ARGV
!ll&&!rl&&r
000
not $fh and @ARGV and -e $ARGV[0]
!ll&&!rl&&r
000
not $fh and @ARGV
!ll&&!rl&&r
000
MC/DC0%0/3
not $fh and @ARGV and -e $ARGV[0]
not $fh @ARGV -e $ARGV[0]
MC/DC0%0/3
not $fh and @ARGV and -e $ARGV[0]
not $fh @ARGV -e $ARGV[0]
Truth table0%0/4
not(A) and B and C
A= not $fhB= @ARGVC= -e $ARGV[0]
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
not(A) and B and C
A= not $fhB= @ARGVC= -e $ARGV[0]
ABCresult
0XX0
10X0
1100
1111
121  
122 0 my %params = ( version=>'3.2' );
123  
124 0 my $vcf;
125 0 ▶ if ( $fh ) { $vcf = fileno($fh) ? VCF->new(%params, fh=>$fh) : VCF->new(%params, file=>$fh); }
Branch: 4 branches
BranchTrueFalseTotal
if ($fh) { } 0 0 0
fileno $fh ? : 0 0 0
if ($fh) { } 0 0 0
fileno $fh ? : 0 0 0
126 0 else { $vcf = VCF->new(%params, fh=>\*STDIN); }
127  
128 0 $vcf->run_validation();
129 }
130  
131  
132 =head2 new
133  
134 About : Creates new VCF reader/writer.
135 Usage : my $vcf = VCF->new(file=>'my.vcf', version=>'3.2');
136 Args :
137 fh .. Open file handle. If neither file nor fh is given, open in write mode.
138 file .. The file name. If neither file nor fh is given, open in write mode.
139 region .. Optional region to parse (requires tabix indexed VCF file)
140 silent .. Unless set to 0, warning messages may be printed.
141 strict .. Unless set to 0, the reader will die when the file violates the specification.
142 version .. If not given, '4.0' is assumed. The header information overrides this setting.
143  
144 =cut
145  
146 sub new
147 10 ▶{
Subroutine new: called CC=12 SCAR=41.1
Pod documented
148 0 ▶ my ($class,@args) = @_;
Subroutine new: not called CC=12 SCAR=41.1
Pod documented
149 0 my $self = {@args};
150 0 ▶ bless $self, ref($class) || $class;
Condition16%1/6
ref $class || $class
l!l&&r!l&&!r
000
ref $class || $class
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $class || $class
ref $class $class
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
151  
152 0 ▶ $$self{silent} = 0 unless exists($$self{silent});
Branch: 2 branches
BranchTrueFalseTotal
unless exists $self->{"silent"} 0 0 0
unless exists $self->{"silent"} 10 0 10
153 0 ▶ $$self{strict} = 0 unless exists($$self{strict});
Branch: 2 branches
BranchTrueFalseTotal
unless exists $self->{"strict"} 0 0 0
unless exists $self->{"strict"} 10 0 10
154 0 $$self{buffer} = []; # buffer stores the lines in the reverse order
155 0 $$self{columns} = undef; # column names
156 0 ▶ $$self{mandatory} = ['CHROM','POS','ID','REF','ALT','QUAL','FILTER','INFO'] unless exists($$self{mandatory});
Branch: 2 branches
BranchTrueFalseTotal
unless exists $self->{"mandatory"} 0 0 0
unless exists $self->{"mandatory"} 10 0 10
157 0 ▶ $$self{reserved}{cols} = {CHROM=>1,POS=>1,ID=>1,REF=>1,ALT=>1,QUAL=>1,FILTER=>1,INFO=>1,FORMAT=>1} unless exists($$self{reserved_cols});
Branch: 2 branches
BranchTrueFalseTotal
unless exists $self->{"reserved_cols"} 0 0 0
unless exists $self->{"reserved_cols"} 10 0 10
158 0 $$self{recalc_ac_an} = 1;
159 0 $$self{has_header} = 0;
160 0 $$self{default_version} = '4.2';
161 0 $$self{versions} = [ qw(Vcf3_2 Vcf3_3 Vcf4_0 Vcf4_1 Vcf4_2) ];
162 0 ▶ if ( !exists($$self{max_line_len}) && exists($ENV{MAX_VCF_LINE_LEN}) ) { $$self{max_line_len} = $ENV{MAX_VCF_LINE_LEN} }
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $self->{"max_line_len"} and exists $ENV{"MAX_VCF_LINE_LEN"}) 0 0 0
if (not exists $self->{"max_line_len"} and exists $ENV{"MAX_VCF_LINE_LEN"}) 0 10 10
Condition16%1/6
not exists $self->{"max_line_len"} and exists $ENV{"MAX_VCF_LINE_LEN"}
!ll&&!rl&&r
000
not exists $self->{"max_line_len"} and exists $ENV{"MAX_VCF_LINE_LEN"}
!ll&&!rl&&r
0100
MC/DC0%0/2
not exists $self->{"max_line_len"} and exists $ENV{"MAX_VCF_LINE_LEN"}
not exists $self->{"max_line_len"} exists $ENV{"MAX_VCF_LINE_LEN"}
MC/DC0%0/2
not exists $self->{"max_line_len"} and exists $ENV{"MAX_VCF_LINE_LEN"}
not exists $self->{"max_line_len"} exists $ENV{"MAX_VCF_LINE_LEN"}
Truth table0%0/3
not(A) and B
A= not exists $self->{"max_line_len"}B= exists $ENV{"MAX_VCF_LINE_LEN"}
ABresult
0X0
100
111
Truth table33%1/3
not(A) and B
A= not exists $self->{"max_line_len"}B= exists $ENV{"MAX_VCF_LINE_LEN"}
ABresult
0X0
100
111
163 0 ▶ $$self{fix_v40_AGtags} = $ENV{DONT_FIX_VCF40_AG_TAGS} ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
$ENV{'DONT_FIX_VCF40_AG_TAGS'} ? : 0 0 0
$ENV{'DONT_FIX_VCF40_AG_TAGS'} ? : 0 10 10
164 0 my %open_args = ();
165 0 ▶ if ( exists($$self{region}) )
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{"region"}) 0 0 0
if (exists $self->{"region"}) 0 10 10
166 {
167 0 $open_args{region}=$$self{region};
168 0 ▶ if ( !exists($$self{print_header}) ) { $$self{print_header}=1; }
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"print_header"}) 0 0 0
unless (exists $self->{"print_header"}) 0 0 0
169 }
170 0 ▶ if ( exists($$self{print_header}) ) { $open_args{print_header}=$$self{print_header}; }
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{"print_header"}) 0 0 0
if (exists $self->{"print_header"}) 0 10 10
171 0 return $self->_open(%open_args);
172 }
173  
174 sub throw
175 0 ▶{
Subroutine throw: not called CC=1 SCAR=6.9
Pod undocumented
176 0 ▶ my ($self,@msg) = @_;
Subroutine throw: not called CC=1 SCAR=6.9
Pod undocumented
177 0 confess @msg,"\n";
178 }
179  
180 sub warn
181 0 ▶{
Subroutine warn: not called CC=3 SCAR=24.8
Pod undocumented
182 0 ▶ my ($self,@msg) = @_;
Subroutine warn: not called CC=3 SCAR=24.8
Pod undocumented
183 0 ▶ if ( $$self{silent} ) { return; }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{"silent"}) 0 0 0
if ($self->{"silent"}) 0 0 0
184 0 ▶ if ( $$self{strict} ) { $self->throw(@msg); }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{"strict"}) 0 0 0
if ($self->{"strict"}) 0 0 0
185 0 warn @msg;
186 }
187  
188 sub _open
189 10 ▶{
Subroutine _open: called CC=18 SCAR=49.9
190 0 ▶ my ($self,%args) = @_;
Subroutine _open: not called CC=18 SCAR=49.9
191  
192 0 ▶ if ( !exists($$self{fh}) && !exists($$self{file}) )
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"fh"} or exists $self->{"file"}) 0 0 0
unless (exists $self->{"fh"} or exists $self->{"file"}) 9 1 10
Condition33%2/6
exists $self->{"fh"} or exists $self->{"file"}
l!l&&r!l&&!r
000
exists $self->{"fh"} or exists $self->{"file"}
l!l&&r!l&&!r
019
MC/DC0%0/2
exists $self->{"fh"} or exists $self->{"file"}
exists $self->{"fh"} exists $self->{"file"}
MC/DC50%1/2
exists $self->{"fh"} or exists $self->{"file"}
exists $self->{"fh"} exists $self->{"file"}
Truth table0%0/3
A or B
A= exists $self->{"fh"}B= exists $self->{"file"}
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= exists $self->{"fh"}B= exists $self->{"file"}
ABresult
1X1
011
000
193 {
194 # Write mode, the version must be supplied by the user
195 0 ▶ return $self->_set_version(exists($$self{version}) ? $$self{version} : $$self{default_version});
Branch: 2 branches
BranchTrueFalseTotal
exists $self->{'version'} ? : 0 0 0
exists $self->{'version'} ? : 7 2 9
196 }
197  
198 # Open the file unless filehandle is provided
199 0 ▶ if ( !exists($$self{fh}) )
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"fh"}) 0 0 0
unless (exists $self->{"fh"}) 1 0 1
200 {
201 0 ▶ if ( !defined $$self{file} ) { $self->throw("Undefined value passed to VCF->new(file=>undef)."); }
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $self->{"file"}) 0 0 0
unless (defined $self->{"file"}) 0 1 1
202 0 my $cmd = "<$$self{file}";
203  
204 0 my $tabix_args = '';
205 0 ▶ if ( exists($args{print_header}) && $args{print_header} ) { $tabix_args .= ' -h '; }
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{"print_header"} and $args{"print_header"}) 0 0 0
if (exists $args{"print_header"} and $args{"print_header"}) 0 1 1
Condition16%1/6
exists $args{"print_header"} and $args{"print_header"}
!ll&&!rl&&r
000
exists $args{"print_header"} and $args{"print_header"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $args{"print_header"} and $args{"print_header"}
exists $args{"print_header"} $args{"print_header"}
MC/DC0%0/2
exists $args{"print_header"} and $args{"print_header"}
exists $args{"print_header"} $args{"print_header"}
Truth table0%0/3
A and B
A= exists $args{"print_header"}B= $args{"print_header"}
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= exists $args{"print_header"}B= $args{"print_header"}
ABresult
0X0
100
111
206 0 $tabix_args .= qq['$$self{file}'];
207 0 ▶ if ( exists($args{region}) && defined($args{region}) ) { $tabix_args .= qq[ '$args{region}']; }
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{"region"} and defined $args{"region"}) 0 0 0
if (exists $args{"region"} and defined $args{"region"}) 0 1 1
Condition16%1/6
exists $args{"region"} and defined $args{"region"}
!ll&&!rl&&r
000
exists $args{"region"} and defined $args{"region"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $args{"region"} and defined $args{"region"}
exists $args{"region"} defined $args{"region"}
MC/DC0%0/2
exists $args{"region"} and defined $args{"region"}
exists $args{"region"} defined $args{"region"}
Truth table0%0/3
A and B
A= exists $args{"region"}B= defined $args{"region"}
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= exists $args{"region"}B= defined $args{"region"}
ABresult
0X0
100
111
208  
209 0 ▶ if ( -e $$self{file} && $$self{file}=~/\.gz/i )
Branch: 4 branches
BranchTrueFalseTotal
if (-e $self->{'file'} and $self->{'file'} =~ /\.gz/i) { } 0 0 0
elsif ($self->{'file'} =~ m[^(?:http|ftp)://]) { } 0 0 0
if (-e $self->{'file'} and $self->{'file'} =~ /\.gz/i) { } 0 1 1
elsif ($self->{'file'} =~ m[^(?:http|ftp)://]) { } 0 1 1
Condition16%1/6
-e $self->{"file"} and $self->{"file"} =~ /\.gz/i
!ll&&!rl&&r
000
-e $self->{"file"} and $self->{"file"} =~ /\.gz/i
!ll&&!rl&&r
010
MC/DC0%0/2
-e $self->{"file"} and $self->{"file"} =~ /\.gz/i
-e $self->{"file"} $self->{"file"} =~ /\.gz/i
MC/DC0%0/2
-e $self->{"file"} and $self->{"file"} =~ /\.gz/i
-e $self->{"file"} $self->{"file"} =~ /\.gz/i
Truth table0%0/3
A and B
A= -e $self->{"file"}B= $self->{"file"} =~ /\.gz/i
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= -e $self->{"file"}B= $self->{"file"} =~ /\.gz/i
ABresult
0X0
100
111
210 {
211 0 ▶ if ( exists($args{region}) && defined($args{region}) )
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'region'} and defined $args{'region'}) { } 0 0 0
if (exists $args{'region'} and defined $args{'region'}) { } 0 0 0
Condition0%0/6
exists $args{"region"} and defined $args{"region"}
!ll&&!rl&&r
000
exists $args{"region"} and defined $args{"region"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"region"} and defined $args{"region"}
exists $args{"region"} defined $args{"region"}
MC/DC0%0/2
exists $args{"region"} and defined $args{"region"}
exists $args{"region"} defined $args{"region"}
Truth table0%0/3
A and B
A= exists $args{"region"}B= defined $args{"region"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= exists $args{"region"}B= defined $args{"region"}
ABresult
0X0
100
111
212 {
213 0 $cmd = "tabix $tabix_args |";
214 }
215 0 else { $cmd = "gunzip -c '$$self{file}' |"; }
216 }
217 elsif ( $$self{file}=~m{^(?:http|ftp)://} )
218 {
219 0 ▶ if ( !exists($args{region}) ) { $tabix_args .= ' .'; }
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $args{"region"}) 0 0 0
unless (exists $args{"region"}) 0 0 0
220 0 $cmd = "tabix $tabix_args |";
221 }
222 0 ▶ open($$self{fh},$cmd) or $self->throw("$cmd: $!");
Branch: 2 branches
BranchTrueFalseTotal
CORE::open $self->{"fh"}, $cmd or $self->throw("${cmd}: $!") 0 0 0
CORE::open $self->{"fh"}, $cmd or $self->throw("${cmd}: $!") 1 0 1
223 }
224  
225 # Set the correct VCF version, but only when called for the first time
226 0 my $vcf = $self;
227 0 ▶ if ( !$$self{_version_set} )
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->{"_version_set"}) 0 0 0
unless ($self->{"_version_set"}) 1 0 1
228 {
229 0 my $first_line = $self->next_line();
230 0 $vcf = $self->_set_version($first_line);
231 0 $self->_unread_line($first_line);
232 }
233 0 return $vcf;
234 }
235  
236  
237  
238 =head2 open
239  
240 About : (Re)Open file. No need to call this explicitly unless reading from a different
241 region is requested.
242 Usage : $vcf->open(); # Read from the start
243 $vcf->open(region=>'1:12345-92345');
244 Args : region .. Supported only for tabix indexed files
245  
246 =cut
247  
248 sub open
249 0 ▶{
Subroutine open: not called CC=1 SCAR=6.9
Pod documented
250 0 ▶ my ($self,%args) = @_;
Subroutine open: not called CC=1 SCAR=6.9
Pod documented
251 0 $self->close();
252 0 $self->_open(%args);
253 }
254  
255  
256 =head2 close
257  
258 About : Close the filehandle
259 Usage : $vcf->close();
260 Args : none
261 Returns : close exit status
262  
263 =cut
264  
265 sub close
266 0 ▶{
Subroutine close: not called CC=2 SCAR=17.9
Pod documented
267 0 ▶ my ($self) = @_;
Subroutine close: not called CC=2 SCAR=17.9
Pod documented
268 0 ▶ if ( !$$self{fh} ) { return; }
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->{"fh"}) 0 0 0
unless ($self->{"fh"}) 0 0 0
269 0 my $ret = close($$self{fh});
270 0 delete($$self{fh});
271 0 $$self{buffer} = [];
272 0 return $ret;
273 }
274  
275  
276 =head2 next_line
277  
278 About : Reads next VCF line.
279 Usage : my $vcf = VCF->new();
280 my $x = $vcf->next_line();
281 Args : none
282  
283 =cut
284  
285 sub next_line
286 8 ▶{
Subroutine next_line: called CC=7 SCAR=35.2
Pod documented
287 0 ▶ my ($self) = @_;
Subroutine next_line: not called CC=7 SCAR=35.2
Pod documented
288 0 ▶ if ( @{$$self{buffer}} ) { return shift(@{$$self{buffer}}); }
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->{"buffer"};}) 0 0 0
if (@{$self->{"buffer"};}) 2 6 8
289  
290 0 my $line;
291 0 ▶ if ( !exists($$self{max_line_len}) )
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $self->{'max_line_len'}) { } 0 0 0
if (not exists $self->{'max_line_len'}) { } 6 0 6
292 {
293 0 $line = readline($$self{fh});
294 }
295 else
296 {
297 0 while (1)
298 {
299 0 $line = readline($$self{fh});
300 0 ▶ if ( !defined $line ) { last; }
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $line) 0 0 0
unless (defined $line) 0 0 0
301  
302 0 my $len = length($line);
303 0 ▶ if ( $len>$$self{max_line_len} && !($line=~/^#/) )
Branch: 2 branches
BranchTrueFalseTotal
if ($len > $self->{"max_line_len"} and not $line =~ /^#/) 0 0 0
if ($len > $self->{"max_line_len"} and not $line =~ /^#/) 0 0 0
Condition0%0/6
$len > $self->{"max_line_len"} and not $line =~ /^#/
!ll&&!rl&&r
000
$len > $self->{"max_line_len"} and not $line =~ /^#/
!ll&&!rl&&r
000
MC/DC0%0/2
$len > $self->{"max_line_len"} and not $line =~ /^#/
$len > $self->{"max_line_len"} not $line =~ /^#/
MC/DC0%0/2
$len > $self->{"max_line_len"} and not $line =~ /^#/
$len > $self->{"max_line_len"} not $line =~ /^#/
Truth table0%0/3
A and not(B)
A= $len > $self->{"max_line_len"}B= not $line =~ /^#/
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $len > $self->{"max_line_len"}B= not $line =~ /^#/
ABresult
0X0
100
111
304 {
305 0 ▶ if ( !($line=~/^([^\t]+)\t([^\t]+)/) ) { $self->throw("Could not parse the line: $line"); }
Branch: 2 branches
BranchTrueFalseTotal
unless ($line =~ /^([^\t]+)\t([^\t]+)/) 0 0 0
unless ($line =~ /^([^\t]+)\t([^\t]+)/) 0 0 0
306 0 $self->warn("The VCF line too long, ignoring: $1 $2 .. len=$len\n");
307 0 next;
308 }
309 0 last;
310 }
311 }
312 0 return $line;
313 }
314  
315 sub _unread_line
316 2 ▶{
Subroutine _unread_line: called CC=1 SCAR=1.2
317 0 ▶ my ($self,$line) = @_;
Subroutine _unread_line: not called CC=1 SCAR=1.2
318 0 unshift @{$$self{buffer}}, $line;
319 0 return;
320 }
321  
322  
323 =head2 next_data_array
324  
325 About : Reads next VCF line and splits it into an array. The last element is chomped.
326 Usage : my $vcf = VCF->new();
327 $vcf->parse_header();
328 my $x = $vcf->next_data_array();
329 Args : Optional line to parse
330  
331 =cut
332  
333 sub next_data_array
334 4 ▶{
Subroutine next_data_array: called CC=5 SCAR=25.8
Pod documented
335 0 ▶ my ($self,$line) = @_;
Subroutine next_data_array: not called CC=5 SCAR=25.8
Pod documented
336 0 ▶ if ( !$line ) { $line = $self->next_line(); }
Branch: 2 branches
BranchTrueFalseTotal
unless ($line) 0 0 0
unless ($line) 4 0 4
337 0 ▶ if ( !$line ) { return undef; }
Branch: 2 branches
BranchTrueFalseTotal
unless ($line) 0 0 0
unless ($line) 0 4 4
338 0 ▶ if ( ref($line) eq 'ARRAY' ) { return $line; }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $line eq "ARRAY") 0 0 0
if (ref $line eq "ARRAY") 0 4 4
339 0 my @items = split(/\t/,$line);
340 0 ▶ if ( @items<8 ) { $line=~s/\n/\\n/g; $self->throw("Could not parse the line, wrong number of columns: [$line]"); }
Branch: 2 branches
BranchTrueFalseTotal
if (@items < 8) 0 0 0
if (@items < 8) 0 4 4
341 0 chomp($items[-1]);
342 0 return \@items;
343 }
344  
345  
346 =head2 set_samples
347  
348 About : Parsing big VCF files with many sample columns is slow, not parsing unwanted samples may speed things a bit.
349 Usage : my $vcf = VCF->new();
350 $vcf->set_samples(include=>['NA0001']); # Exclude all but this sample. When the array is empty, all samples will be excluded.
351 $vcf->set_samples(exclude=>['NA0003']); # Include only this sample. When the array is empty, all samples will be included.
352 my $x = $vcf->next_data_hash();
353 Args : Optional line to parse
354  
355 =cut
356  
357 sub set_samples
358 0 ▶{
Subroutine set_samples: not called CC=9 SCAR=45.0
Pod documented
359 0 ▶ my ($self,%args) = @_;
Subroutine set_samples: not called CC=9 SCAR=45.0
Pod documented
360  
361 0 ▶ if ( exists($args{include}) )
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{"include"}) 0 0 0
if (exists $args{"include"}) 0 0 0
362 {
363 0 ▶ for (my $i=0; $i<@{$$self{columns}}; $i++) { $$self{samples_to_parse}[$i] = 0; }
Branch: 2 branches
BranchTrueFalseTotal
if ($i < @{$self->{"columns"};}) 0 0 0
if ($i < @{$self->{"columns"};}) 0 0 0
364 0 for my $sample (@{$args{include}})
365 {
366 0 ▶ if ( !exists($$self{has_column}{$sample}) ) { $self->throw("The sample not present in the VCF file: [$sample]\n"); }
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"has_column"}{$sample}) 0 0 0
unless (exists $self->{"has_column"}{$sample}) 0 0 0
367 0 my $idx = $$self{has_column}{$sample} - 1;
368 0 $$self{samples_to_parse}[$idx] = 1;
369 }
370 }
371  
372 0 ▶ if ( exists($args{exclude}) )
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{"exclude"}) 0 0 0
if (exists $args{"exclude"}) 0 0 0
373 {
374 0 ▶ for (my $i=0; $i<@{$$self{columns}}; $i++) { $$self{samples_to_parse}[$i] = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if ($i < @{$self->{"columns"};}) 0 0 0
if ($i < @{$self->{"columns"};}) 0 0 0
375 0 for my $sample (@{$args{exclude}})
376 {
377 0 ▶ if ( !exists($$self{has_column}{$sample}) ) { $self->throw("The sample not present in the VCF file: [$sample]\n"); }
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"has_column"}{$sample}) 0 0 0
unless (exists $self->{"has_column"}{$sample}) 0 0 0
378 0 my $idx = $$self{has_column}{$sample} - 1;
379 0 $$self{samples_to_parse}[$idx] = 0;
380 }
381 }
382 }
383  
384  
385 sub _set_version
386 10 ▶{
Subroutine _set_version: called CC=14 SCAR=43.7
387 0 ▶ my ($self,$version_line) = @_;
Subroutine _set_version: not called CC=14 SCAR=43.7
388  
389 0 ▶ if ( $$self{_version_set} ) { return $self; }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{"_version_set"}) 0 0 0
if ($self->{"_version_set"}) 0 10 10
390 0 $$self{_version_set} = 1;
391  
392 0 $$self{version} = $$self{default_version};
393 0 ▶ if ( $version_line )
Branch: 2 branches
BranchTrueFalseTotal
if ($version_line) 0 0 0
if ($version_line) 10 0 10
394 {
395 0 ▶ if ( $version_line=~/^(\d+(?:\.\d+)?)$/ )
Branch: 4 branches
BranchTrueFalseTotal
if ($version_line =~ /^(\d+(?:\.\d+)?)$/) { } 0 0 0
elsif (not $version_line =~ /^##fileformat=/i && $version_line =~ /(\d+(?:\.\d+)?)\s*$/i) { } 0 0 0
if ($version_line =~ /^(\d+(?:\.\d+)?)$/) { } 9 1 10
elsif (not $version_line =~ /^##fileformat=/i && $version_line =~ /(\d+(?:\.\d+)?)\s*$/i) { } 0 1 1
396 {
397 0 $$self{version} = $1;
398 0 undef $version_line;
399 }
400 ▶ elsif ( !($version_line=~/^##fileformat=/i) or !($version_line=~/(\d+(?:\.\d+)?)\s*$/i) )
Condition16%1/6
$version_line =~ /^##fileformat=/i && $version_line =~ /(\d+(?:\.\d+)?)\s*$/i
!ll&&!rl&&r
000
$version_line =~ /^##fileformat=/i && $version_line =~ /(\d+(?:\.\d+)?)\s*$/i
!ll&&!rl&&r
001
MC/DC0%0/2
$version_line =~ /^##fileformat=/i && $version_line =~ /(\d+(?:\.\d+)?)\s*$/i
$version_line =~ /^##fileformat=/i $version_line =~ /(\d+(?:\.\d+)?)\s*$/i
MC/DC0%0/2
$version_line =~ /^##fileformat=/i && $version_line =~ /(\d+(?:\.\d+)?)\s*$/i
$version_line =~ /^##fileformat=/i $version_line =~ /(\d+(?:\.\d+)?)\s*$/i
Truth table0%0/3
A && B
A= $version_line =~ /^##fileformat=/iB= $version_line =~ /(\d+(?:\.\d+)?)\s*$/i
ABresult
0X0
100
111
Truth table33%1/3
A && B
A= $version_line =~ /^##fileformat=/iB= $version_line =~ /(\d+(?:\.\d+)?)\s*$/i
ABresult
0X0
100
111
401 {
402 0 chomp($version_line);
403 0 $self->warn("Could not parse the fileformat version string [$version_line], assuming VCFv$$self{default_version}\n");
404 0 undef $version_line;
405 }
406 else
407 {
408 0 $$self{version} = $1;
409 }
410 }
411  
412 0 my $reader;
413 0 ▶ if ( $$self{version} eq '3.2' ) { $reader=VCF::V3_2->new(%$self); }
Branch: 10 branches
BranchTrueFalseTotal
if ($self->{'version'} eq '3.2') { } 0 0 0
elsif ($self->{'version'} eq '3.3') { } 0 0 0
elsif ($self->{'version'} eq '4.0') { } 0 0 0
elsif ($self->{'version'} eq '4.1') { } 0 0 0
elsif ($self->{'version'} eq '4.2') { } 0 0 0
if ($self->{'version'} eq '3.2') { } 0 10 10
elsif ($self->{'version'} eq '3.3') { } 1 9 10
elsif ($self->{'version'} eq '4.0') { } 5 4 9
elsif ($self->{'version'} eq '4.1') { } 2 2 4
elsif ($self->{'version'} eq '4.2') { } 2 0 2
414 0 elsif ( $$self{version} eq '3.3' ) { $reader=VCF::V3_3->new(%$self); }
415 0 elsif ( $$self{version} eq '4.0' ) { $reader=VCF::V4_0->new(%$self); }
416 0 elsif ( $$self{version} eq '4.1' ) { $reader=VCF::V4_1->new(%$self); }
417 0 elsif ( $$self{version} eq '4.2' ) { $reader=VCF::V4_2->new(%$self); }
418 else
419 {
420 0 $self->warn(qq[The version "$$self{version}" not supported, assuming VCFv$$self{default_version}\n]);
421 0 $$self{version} = '4.2';
422 0 $reader = VCF::V4_2->new(%$self);
423 }
424  
425 0 $self = $reader;
426 # When changing version, change also the fileformat header line
427 0 ▶ if ( exists($$self{header_lines}) && exists($$self{header_lines}[0]{key}) && $$self{header_lines}[0]{key} eq 'fileformat' )
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{"header_lines"} and exists $self->{"header_lines"}[0]{"key"} and $self->{"header_lines"}[0]{"key"} eq "fileformat") 0 0 0
if (exists $self->{"header_lines"} and exists $self->{"header_lines"}[0]{"key"} and $self->{"header_lines"}[0]{"key"} eq "fileformat") 0 10 10
Condition16%2/12
exists $self->{"header_lines"} and exists $self->{"header_lines"}[0]{"key"} and $self->{"header_lines"}[0]{"key"} eq "fileformat"
!ll&&!rl&&r
000
exists $self->{"header_lines"} and exists $self->{"header_lines"}[0]{"key"}
!ll&&!rl&&r
000
exists $self->{"header_lines"} and exists $self->{"header_lines"}[0]{"key"} and $self->{"header_lines"}[0]{"key"} eq "fileformat"
!ll&&!rl&&r
1000
exists $self->{"header_lines"} and exists $self->{"header_lines"}[0]{"key"}
!ll&&!rl&&r
1000
MC/DC0%0/3
exists $self->{"header_lines"} and exists $self->{"header_lines"}[0]{"key"} and $self->{"header_lines"}[0]{"key"} eq "fileformat"
exists $self->{"header_lines"} exists $self->{"header_lines"}[0]{"key"} $self->{"header_lines"}[0]{"key"} eq "fileformat"
MC/DC0%0/3
exists $self->{"header_lines"} and exists $self->{"header_lines"}[0]{"key"} and $self->{"header_lines"}[0]{"key"} eq "fileformat"
exists $self->{"header_lines"} exists $self->{"header_lines"}[0]{"key"} $self->{"header_lines"}[0]{"key"} eq "fileformat"
Truth table0%0/4
A and B and C
A= exists $self->{"header_lines"}B= exists $self->{"header_lines"}[0]{"key"}C= $self->{"header_lines"}[0]{"key"} eq "fileformat"
ABCresult
0XX0
10X0
1100
1111
Truth table25%1/4
A and B and C
A= exists $self->{"header_lines"}B= exists $self->{"header_lines"}[0]{"key"}C= $self->{"header_lines"}[0]{"key"} eq "fileformat"
ABCresult
0XX0
10X0
1100
1111
428 {
429 0 shift(@{$$self{header_lines}});
430 }
431  
432 0 return $self;
433 }
434  
435 1 1;
« VCF/V4_1.pm ↑ Summary VCF/V3_2.pm »