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
|
package ReadCoverageNode;
use strict;
use warnings;
use Carp;
use base qw (GenericNode);
## static vars
my %READ_TRACKER;
my $READ_COUNTER = 0;
sub new {
my $packagename = shift;
my ($node_name, $read_acc) = @_;
my $self = $packagename->SUPER::new($node_name); # sets _value
$self->{_reads} = {};
$self->{_count} = 0;
bless ($self, $packagename);
$self->add_read($read_acc);
return($self);
}
####
sub get_count {
my $self = shift;
return($self->{_count});
}
sub set_count {
my $self = shift;
my $count = shift;
$self->{_count} = $count;
return;
}
####
sub add_read {
my $self = shift;
my $read_acc = shift;
my $node_name = $self->get_value();
unless ((defined $read_acc) && $read_acc =~ /\w/) {
confess "Error, need read accession";
}
my $read_tracking_no = $self->_get_or_create_tracking_number($read_acc);
if (exists $self->{_reads}->{$read_tracking_no}) {
#print "Already got read $read_acc for $node_name, count:" . $self->get_count() . "\n";
}
else {
$self->{_reads}->{$read_tracking_no} = 1;
$self->{_count}++;
#print "-incrementing count for $read_acc for $node_name => $self->{_count}\n";
}
return;
}
####
sub get_reads {
my $self = shift;
my @reads = keys %{$self->{_reads}};
return(@reads);
}
###
sub has_read {
my $self = shift;
my $read_acc = shift;
if (exists $self->{_reads}->{$read_acc}) {
return(1);
}
else {
return(0);
}
}
####
sub count_reads_in_common {
my $self = shift;
my $other_node = shift;
my $count = 0;
foreach my $read ($self->get_reads()) {
if ($other_node->has_read($read)) {
$count++;
}
}
return($count);
}
####
sub toString {
my $self = shift;
my @prev_nodes = $self->get_all_prev_nodes();
my @next_nodes = $self->get_all_next_nodes();
my $text = "";
foreach my $prev_node (@prev_nodes) {
$text .= "P " . $prev_node->get_value() . "(" . $prev_node->get_count() . ")\n";
}
$text .= "X " . $self->get_value() . "(" . $self->get_count() . ")\n";
foreach my $next_node (@next_nodes) {
$text .= "N " . $next_node->get_value() . "(" . $self->get_count() . ")\n";
}
return($text);
}
#### Private read tracking ## this should probably be a separate class at some point, with a singleton class object.
sub _read_is_tracked {
my $self = shift;
my $read_acc = shift;
if (exists $READ_TRACKER{$read_acc}) {
return(1);
}
else {
return(0);
}
}
sub _get_read_tracking_number {
my $self = shift;
my $read_acc = shift;
if (! $self->_read_is_tracked($read_acc)) {
die "Error, read $read_acc is not tracked";
}
my $tracking_number = $READ_TRACKER{$read_acc};
return($tracking_number);
}
sub _get_or_create_tracking_number {
my $self = shift;
my $read_acc = shift;
if ($self->_read_is_tracked($read_acc)) {
return($self->_get_read_tracking_number($read_acc));
}
else {
## track this new read:
$READ_COUNTER++;
$READ_TRACKER{$read_acc} = $READ_COUNTER;
return($self->_get_read_tracking_number($read_acc));
}
}
1; # EOM
|