blib/lib/Bio/Gonzales/Var/IO/VCF.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 158 CC 13 SCAR 25.8
CC
13
Cov
0%
_parse_header
30.0
format_header
20.0
next_var
12.0
CRAP 13.2
1 package Bio::Gonzales::Var::IO::VCF;
2  
3 use Mouse;
4  
5 use warnings;
6 use strict;
7 use Carp;
8  
9 use 5.010;
10 use List::Util qw/any/;
11  
12 our $VERSION = 0.01_01;
13  
14 with 'Bio::Gonzales::Util::Role::FileIO';
15  
16 has meta => ( is => 'rw', default => sub { {} } );
17 has sample_ids => ( is => 'rw', default => sub { [] } );
18 has _wrote_sth_before => ( is => 'rw' );
19  
20 # stay consistent with GFF3 io
21 sub pragmas { shift->meta(@_) }
22  
23 sub BUILD {
24 my ($self) = @_;
25  
26 $self->_parse_header if ( $self->mode eq '<' );
27 }
28  
29 sub format_header {
30 my $self = shift;
31  
32 my $res = '';
33  
34 my $meta = $self->meta;
35 $res .= "##fileformat=" . ( $meta->{fileformat}[0] // 'VCF4.2' ) . "\n";
36 for my $kw (qw/FILTER FORMAT INFO/) {
37 next unless ( $meta->{$kw} && @{ $meta->{$kw} } > 0 );
38 for my $v ( @{ $meta->{$kw} } ) {
39 $res .= "##$kw=" . $v . "\n";
40 }
41 }
42  
43 for my $kw ( keys %$meta ) {
44 next if ( any { $kw eq $_ } qw/fileformat FILTER FORMAT INFO/ );
45 next unless ( @{ $meta->{$kw} } > 0 );
46 for my $v ( @{ $meta->{$kw} } ) {
47 $res .= "##$kw=" . $v . "\n";
48 }
49 }
50 $res .= "#"
51 . join( "\t", qw/CHROM POS ID REF ALT QUAL FILTER INFO FORMAT/, @{ $self->sample_ids } ) . "\n";
52 return $res;
53 }
54  
55 sub _write_header {
56 my ($self) = @_;
57  
58 $self->_wrote_sth_before(1);
59  
60 my $fh = $self->fh;
61  
62 print $fh $self->format_header;
63 return;
64 }
65  
66 sub _parse_header {
67 my $self = shift;
68 my $fhi = $self->_fhi;
69  
70 my @sample_ids;
71 my %meta;
72 my $l;
73 while ( defined( $l = $fhi->() ) ) {
74 next if ( !$l || $l =~ /^\s*$/ );
75 #looks like the header is over!
76 last unless $l =~ /^\#/;
77 if ( $l =~ /^\s*#CHROM/ ) {
78  
79 ( undef, undef, undef, undef, undef, undef, undef, undef, undef, @sample_ids ) = split /\t/, $l;
80 } elsif ( $l =~ s/^##// ) {
81 my ( $k, $v ) = split /=/, $l, 2;
82 $meta{$k} //= [];
83 push @{ $meta{$k} }, $v;
84 } else {
85 next;
86 }
87 }
88 push @{ $self->_cached_records }, $l;
89  
90 $self->meta( \%meta );
91 $self->sample_ids( \@sample_ids );
92  
93 return;
94 }
95  
96 sub next_var {
97 my ($self) = @_;
98  
99 my $fhi = $self->_fhi;
100  
101 my $l;
102 while ( defined( $l = $fhi->() ) ) {
103 if ( $l =~ /^\#/ || $l =~ /^\s*$/ ) {
104 next;
105 } else {
106 last;
107 }
108 }
109 return unless $l;
110  
111 my ( $chr, $pos, $id, $ref, $alt, $qual, $filter, $info, $format, @variants ) = split /\t/, $l;
112 return {
113 seq_id => $chr,
114 pos => $pos + 0,
115 var_id => $id,
116 alleles => [ grep { $_ ne '.' } ( $ref, split( /,/, $alt ) ) ],
117 qual => $qual,
118 filter => $filter,
119 info => $info,
120 format => $format,
121 genotypes => \@variants,
122 };
123 }
124  
125 sub write_var {
126 my ( $self, $var ) = @_;
127  
128 my $fh = $self->fh;
129  
130 $self->_write_header
131 unless ( $self->_wrote_sth_before );
132  
133 my $ref = ( shift @{ $var->{alleles} } ) // '.';
134 my $alt = @{ $var->{alleles} } > 0 ? join( ",", @{ $var->{alleles} } ) : '.';
135 say $fh join( "\t",
136 @{$var}{qw(seq_id pos var_id)},
137 $ref, $alt,
138 @{$var}{qw(qual filter info format)},
139 @{ $var->{genotypes} } );
140 }
141  
142 1;
143  
144 __END__
145  
146 =head1 NAME
147  
148 Bio::Gonzales::Var::IO::VCF - parse VCF files
149  
150 =head1 SYNOPSIS
151  
152 =head1 DESCRIPTION
153  
154 =head1 OPTIONS
155  
156 =head1 SUBROUTINES
157 =head1 METHODS
158  
159 =head1 SEE ALSO
160  
161 =head1 AUTHOR
162  
163 jw bargsten, C<< <jwb at cpan dot org> >>
164  
165 =cut
166  
167