-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBas.pm
119 lines (95 loc) · 2.8 KB
/
Bas.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
package PCAP::Bam::Bas;
##########LICENCE##########
# PCAP - NGS reference implementations and helper code for the ICGC/TCGA Pan-Cancer Analysis Project
# Copyright (C) 2014 ICGC PanCancer Project
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not see:
# http://www.gnu.org/licenses/gpl-2.0.html
##########LICENCE##########
use PCAP;
our $VERSION = PCAP->VERSION;
use strict;
use English qw( -no_match_vars );
use warnings FATAL=>'all';
use autodie qw( :all );
use Carp qw(croak carp);
sub new {
my ($class, $bas) = @_;
my $self = { };
bless $self, $class;
$self->_init($bas);
return $self;
}
sub _init {
my ($self, $bas) = @_;
croak "No bas file defined" if(!defined $bas);
die "*.bas file: $bas does not exist" unless(-e $bas);
die "*.bas file: $bas is empty" unless(-s $bas);
open my $IN, '<', $bas;
$self->bas_keys($IN);
$self->_import_data($IN);
close $IN;
return 1;
}
sub _import_data {
my ($self, $fh) = @_;
while(my $line = <$fh>) {
chomp $line;
my @bits = split /\t/, $line;
my %rg;
for my $key(@{$self->bas_keys}) {
$rg{$key} = $bits[$self->{'key_pos_map'}->{$key}];
}
$self->{'_data'}->{$rg{'readgroup'}} = \%rg;
}
return 1;
}
sub bas_keys {
my ($self, $key_fh) = @_;
croak "bas_keys should only be initialised once\n" if(exists $self->{'keys'} && defined $key_fh);
if(defined $key_fh) {
my $line = <$key_fh>;
chomp $line;
my @head = split /\t/, $line;
my %key_pos_map;
my $pos=0;
for my $key(@head) {
$key_pos_map{$key} = $pos++;
}
$self->{'keys'} = \@head;
$self->{'key_pos_map'} = \%key_pos_map;
}
return $self->{'keys'};
}
sub get {
my ($self, $rg, $key) = @_;
die qq{Readgroup '$rg' does not exist\n} unless(exists $self->{'_data'}->{$rg});
return exists $self->{'_data'}->{$rg}->{$key} ? $self->{'_data'}->{$rg}->{$key} : undef;
}
1;
__END__
=head1 PCAP::Bam::Bas
Convenience class for accessing data in a *.bas file.
=head2 METHODS
=over 2
=item new
Construct an access object for BAM statistics file.
my $bas_ob = PCAP::Bam::Bas->new($bas);
=item bas_keys
Returns the list of available keys for this BAS file.
=item get
Retrieve a value by its readgroup and key:
$bas->($rg, 'median_insert_size');
NOTE: Returns undef if a key is not available.
=back