| 1 | # -*-CPerl-*- | ||||||||||||||||||||||||||||
| 2 | # Last changed Time-stamp: <2018-07-03 12:20:53 mtw> | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Bio::ViennaNGS::Peak; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Bio::ViennaNGS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Path::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use List::Util qw(sum sum0 min max first); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Bio::ViennaNGS::Util qw(sortbed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use File::Temp qw(tempfile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use version; our $VERSION = version->declare("$Bio::ViennaNGS::VERSION"); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | has 'data' => ( | ||||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||
| 19 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 20 | predicate => 'has_data', | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 22 | 1 | ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | has 'region' => ( | ||||||||||||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||||||||||
| 26 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 27 | predicate => 'has_region', | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 29 | 1 | ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | has 'peaks' => ( | ||||||||||||||||||||||||||||
| 32 | is => 'ro', | ||||||||||||||||||||||||||||
| 33 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 34 | predicate => 'has_peaks', | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 36 | 1 | ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | has 'winsize' => ( | |||||||||||||||||||||||||||
| 39 | is => 'ro', | ||||||||||||||||||||||||||||
| 40 | isa => 'Int', | ||||||||||||||||||||||||||||
| 41 | predicate => 'has_winsize', | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | has 'interval' => ( | |||||||||||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||||||||||
| 46 | isa => 'Int', | ||||||||||||||||||||||||||||
| 47 | predicate => 'has_interval', | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | has 'mincov' => ( | |||||||||||||||||||||||||||
| 51 | is => 'ro', | ||||||||||||||||||||||||||||
| 52 | isa => 'Int', | ||||||||||||||||||||||||||||
| 53 | predicate => 'has_mincov', | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | has 'length' => ( | |||||||||||||||||||||||||||
| 57 | is => 'ro', | ||||||||||||||||||||||||||||
| 58 | isa => 'Int', | ||||||||||||||||||||||||||||
| 59 | predicate => 'has_length', | ||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | has 'threshold' => ( | |||||||||||||||||||||||||||
| 63 | is => 'ro', | ||||||||||||||||||||||||||||
| 64 | isa => 'Value', | ||||||||||||||||||||||||||||
| 65 | predicate => 'has_threshold', | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub populate_data { | ||||||||||||||||||||||||||
|
Subroutine
populate_data: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 0 | my ($self,$filep,$filen) = @_; | |||||||||||||||||||||||||||
| 70 | 0 | my $this_function = (caller(0))[3]; | |||||||||||||||||||||||||||
| 71 | 0 | my ($i,$element,$chr,$start,$end,$val,$lastend); | |||||||||||||||||||||||||||
| 72 | 0 | my $have_lastend = 0; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # parse [+] strand | ||||||||||||||||||||||||||||
| 75 | 0 | foreach $element (@{$filep->data}){ | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | unless (exists ${$self->region}{$element->chromosome}{pos}{start}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | ${$self->region}{$element->chromosome}{pos}{start} = $element->start; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if($have_lastend == 1 && $element->start>$lastend){ # fill the gap with 0s | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$have_lastend == 1 and $element->start > $lastend
MC/DC0%0/2
$have_lastend == 1 and $element->start > $lastend
$have_lastend == 1
$element->start > $lastend
Truth table0%0/3
A and B
A= $have_lastend == 1B= $element->start > $lastend
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | for($i=$lastend;$i<$element->start;$i++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | ${$self->data}{$element->chromosome}{pos}[$i] = 0.; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | for ($i=$element->start;$i<$element->end;$i++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | ${$self->data}{$element->chromosome}{pos}[$i]=$element->dataValue; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 0 | $lastend = $element->end; | |||||||||||||||||||||||||||
| 88 | 0 | $have_lastend = 1; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | $lastend = 0; # reset lastend before parsing [-] strand | |||||||||||||||||||||||||||
| 92 | 0 | $have_lastend = 1; # needed to get the values parsed correctly below | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # parse [-] strand | ||||||||||||||||||||||||||||
| 95 | 0 | foreach $element (@{$filen->data}){ | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ($element->{dataValue} <= 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | $element->{dataValue} *= -1; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | unless (exists ${$self->region}{$element->chromosome}{neg}{start}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | ${$self->region}{$element->chromosome}{neg}{start} = $element->start; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if($have_lastend == 1 && $element->start>$lastend){ # fill the gap with 0s | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$have_lastend == 1 and $element->start > $lastend
MC/DC0%0/2
$have_lastend == 1 and $element->start > $lastend
$have_lastend == 1
$element->start > $lastend
Truth table0%0/3
A and B
A= $have_lastend == 1B= $element->start > $lastend
| |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | for($i=$lastend;$i<$element->start;$i++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | ${$self->data}{$element->chromosome}{neg}[$i] = 0.; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | for ($i=$element->start;$i<$element->end;$i++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | ${$self->data}{$element->chromosome}{neg}[$i]=$element->dataValue; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 0 | $lastend = $element->end; | |||||||||||||||||||||||||||
| 111 | 0 | $have_lastend = 1; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | return; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub raw_peaks { | ||||||||||||||||||||||||||
|
Subroutine
raw_peaks: not called CC=30 SCAR=68.4
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 0 | my ($self,$dest,$prefix,$log) = @_; | |||||||||||||||||||||||||||
| 118 | 0 | my ($fn,$tfn,$tfh,$maxidx,$have_pstart,$pstart,$pend,$chr); | |||||||||||||||||||||||||||
| 119 | 0 | my ($winstart, $winend, $winsum, $mean, $lastmax, $from, $to); | |||||||||||||||||||||||||||
| 120 | 0 | my ($index_of_maxwin); | |||||||||||||||||||||||||||
| 121 | 0 | my $suffix = "rawpeaks.bed"; | |||||||||||||||||||||||||||
| 122 | 0 | my $this_function = (caller(0))[3]; | |||||||||||||||||||||||||||
| 123 | 0 | my $flex = 10; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | croak "ERROR [$this_function]: $dest does not exist\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | unless (-d $dest); | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if (defined $log){open(LOG, ">", $log) or croak "$!"} | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 129 | 0 | else {croak "ERROR [$this_function] \$log not defined";} | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | $fn = $prefix.".".$suffix; # filename of final, sorted RAWPEAKS bed file | |||||||||||||||||||||||||||
| 132 | 0 | (undef,$tfn) = tempfile('RAWPEAKS_XXXXXXX',UNLINK=>0); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | croak "ERROR [$this_function]: $self->data not available\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | unless ($self->has_data); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | croak "ERROR [$this_function]: $self->region not available\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | unless ($self->has_region); | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | open ($tfh, ">", $tfn) or croak $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | $lastmax = 0; | |||||||||||||||||||||||||||
| 142 | 0 | foreach $chr (keys (%{$self->data})){ | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # pos strand | ||||||||||||||||||||||||||||
| 145 | 0 | $maxidx = $#{$self->data->{$chr}{pos}} ; # largest index in array | |||||||||||||||||||||||||||
| 146 | 0 | $have_pstart = 0; | |||||||||||||||||||||||||||
| 147 | 0 | $pstart = -1; | |||||||||||||||||||||||||||
| 148 | 0 | $pend = -1; | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | if (defined $log){print LOG "processing chr $chr [+] strand $maxidx\n"} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | croak "$chr has not been parsed in input data" | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | unless (exists $self->region->{$chr}{pos}{start}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | for ($winstart=$self->region->{$chr}{pos}{start};$winstart<($maxidx-$self->winsize);$winstart+=$self->interval){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | # NOTE: does not handle the last window properly - > ignore this | ||||||||||||||||||||||||||||
| 155 | 0 | $winend = $winstart + $self->winsize - 1; | |||||||||||||||||||||||||||
| 156 | 0 | $winsum = sum0 @{$self->data->{$chr}{pos}}[$winstart..$winend]; | |||||||||||||||||||||||||||
| 157 | 0 | $mean = $winsum/$self->winsize; | |||||||||||||||||||||||||||
| 158 | #print LOG "**processing $winstart - $winend sum=$winsum mean=$mean lastmax=$lastmax\n"; | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | if ($mean > $lastmax){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | $lastmax = $mean; | |||||||||||||||||||||||||||
| 162 | 0 | $index_of_maxwin = $winstart; | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | unless ($have_pstart == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | $pstart = $winstart; | |||||||||||||||||||||||||||
| 165 | 0 | $have_pstart = 1; | |||||||||||||||||||||||||||
| 166 | #print LOG "Peak start $chr:$winstart -- "; | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||
| 170 | 0 | ▶ | if ($mean > 0. && $mean <= $lastmax*$self->threshold ){ # peak stop criterion | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$mean > 0 and $mean <= $lastmax * $self->threshold
MC/DC0%0/2
$mean > 0 and $mean <= $lastmax * $self->threshold
$mean > 0
$mean <= $lastmax * $self->threshold
Truth table0%0/3
A and B
A= $mean > 0B= $mean <= $lastmax * $self->threshold
| |||||||||||||||||||||||||||||
| 171 | 0 | $pend = $winend; | |||||||||||||||||||||||||||
| 172 | 0 | $have_pstart = 0; | |||||||||||||||||||||||||||
| 173 | 0 | $from = min($pstart,$pend); | |||||||||||||||||||||||||||
| 174 | 0 | $to = max($pstart, $pend); | |||||||||||||||||||||||||||
| 175 | 0 | ▶ | next if ($from<0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | next if ($to<0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | # now add +- $flex nt to raw peaks, required fro better peak boundary determination below | ||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if ($from-$flex > 0){$from = $from - $flex;} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | $to= $to+$flex; # we dont know the chromsize here, hence override it in case | |||||||||||||||||||||||||||
| 180 | #print LOG "$pend end\n"; | ||||||||||||||||||||||||||||
| 181 | 0 | my %pk = ( | |||||||||||||||||||||||||||
| 182 | chr => $chr, | ||||||||||||||||||||||||||||
| 183 | start => $from, | ||||||||||||||||||||||||||||
| 184 | end => $to, | ||||||||||||||||||||||||||||
| 185 | strand => 'pos', | ||||||||||||||||||||||||||||
| 186 | maxindex => $index_of_maxwin, | ||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||
| 188 | 0 | push (@{ $self->peaks->{$chr} }, \%pk); | |||||||||||||||||||||||||||
| 189 | 0 | ▶ | if (defined $log){print LOG "**PEAK $chr [+] $from-$to max at $index_of_maxwin\n"} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 0 | print $tfh "$chr\t$from\t$to\tRAW\t0\t+\n"; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # reset parameters | ||||||||||||||||||||||||||||
| 193 | 0 | $lastmax = 0; | |||||||||||||||||||||||||||
| 194 | 0 | $pstart = -1; | |||||||||||||||||||||||||||
| 195 | 0 | $pend = -1; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | } # end for | ||||||||||||||||||||||||||||
| 199 | 0 | $lastmax = 0; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # neg strand | ||||||||||||||||||||||||||||
| 202 | 0 | $maxidx = $#{$self->data->{$chr}{neg}}; # largest index in array | |||||||||||||||||||||||||||
| 203 | 0 | $have_pstart = 0; | |||||||||||||||||||||||||||
| 204 | 0 | $pstart = -1; | |||||||||||||||||||||||||||
| 205 | 0 | $pend = -1; | |||||||||||||||||||||||||||
| 206 | 0 | ▶ | if (defined $log){print LOG "processing chr $chr [-] strand $maxidx\n"} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | croak "$chr has not been parsed in input data" | ||||||||||||||||||||||||||||
| 208 | 0 | ▶ | unless (exists $self->region->{$chr}{neg}{start}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | ▶ | for ($winstart=$maxidx;$winstart>($self->region->{$chr}{neg}{start}+$self->winsize);$winstart-=$self->interval){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | # NOTE: does not handle the last window properly - > ignore this | ||||||||||||||||||||||||||||
| 212 | 0 | $winend = $winstart - $self->winsize + 1; | |||||||||||||||||||||||||||
| 213 | 0 | $winsum = sum0 @{$self->data->{$chr}{neg}}[$winend..$winstart]; | |||||||||||||||||||||||||||
| 214 | 0 | $mean = $winsum/$self->winsize; | |||||||||||||||||||||||||||
| 215 | #print "processing $winstart - $winend sum=$winsum mean=$mean lastmax=$lastmax\n"; | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if ($mean > $lastmax){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | $lastmax = $mean; | |||||||||||||||||||||||||||
| 219 | 0 | $index_of_maxwin = $winend; | |||||||||||||||||||||||||||
| 220 | 0 | ▶ | unless ($have_pstart == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 0 | $pstart = $winend; | |||||||||||||||||||||||||||
| 222 | 0 | $have_pstart = 1; | |||||||||||||||||||||||||||
| 223 | #print "Peak start $chr:$pstart -- "; | ||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | else { | ||||||||||||||||||||||||||||
| 227 | 0 | ▶ | if ($mean > 0. && $mean <= $lastmax*$self->threshold ){ # peak stop criterion | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$mean > 0 and $mean <= $lastmax * $self->threshold
MC/DC0%0/2
$mean > 0 and $mean <= $lastmax * $self->threshold
$mean > 0
$mean <= $lastmax * $self->threshold
Truth table0%0/3
A and B
A= $mean > 0B= $mean <= $lastmax * $self->threshold
| |||||||||||||||||||||||||||||
| 228 | 0 | $pend = $winstart; | |||||||||||||||||||||||||||
| 229 | 0 | $have_pstart = 0; | |||||||||||||||||||||||||||
| 230 | 0 | $from = min($pstart,$pend); | |||||||||||||||||||||||||||
| 231 | 0 | $to = max($pstart, $pend); | |||||||||||||||||||||||||||
| 232 | 0 | ▶ | next if ($from<0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 0 | ▶ | next if ($to<0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | # now add +- $flex nt to raw peaks, required fro better peak boundary determination below | ||||||||||||||||||||||||||||
| 235 | 0 | ▶ | if ($from-$flex > 0){$from = $from - $flex;} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 0 | $to = $to+$flex; # we dont know the chromsize here, hence override it in case | |||||||||||||||||||||||||||
| 237 | #print "$pend end\n"; | ||||||||||||||||||||||||||||
| 238 | 0 | my %pk = ( | |||||||||||||||||||||||||||
| 239 | chr => $chr, | ||||||||||||||||||||||||||||
| 240 | start => $from, | ||||||||||||||||||||||||||||
| 241 | end => $to, | ||||||||||||||||||||||||||||
| 242 | strand => 'neg', | ||||||||||||||||||||||||||||
| 243 | maxindex => $index_of_maxwin, | ||||||||||||||||||||||||||||
| 244 | ); | ||||||||||||||||||||||||||||
| 245 | 0 | push (@{ $self->peaks->{$chr} }, \%pk); | |||||||||||||||||||||||||||
| 246 | 0 | ▶ | if (defined $log){print LOG "**PEAK $chr [-] $from-$to max at $index_of_maxwin\n"} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | 0 | print $tfh "$chr\t$from\t$to\tRAW\t0\t-\n"; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # reset parameters | ||||||||||||||||||||||||||||
| 250 | 0 | $lastmax = 0; | |||||||||||||||||||||||||||
| 251 | 0 | $pstart = -1; | |||||||||||||||||||||||||||
| 252 | 0 | $pend = -1; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | } # end for | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | #print LOG Dumper($self->peaks); | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | } # end foreach | ||||||||||||||||||||||||||||
| 260 | 0 | close($tfh); | |||||||||||||||||||||||||||
| 261 | 0 | close(LOG); | |||||||||||||||||||||||||||
| 262 | 0 | sortbed($tfn,$dest,$fn,0,$log); | |||||||||||||||||||||||||||
| 263 | 0 | unlink($tfn); | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | sub final_peaks { | ||||||||||||||||||||||||||
|
Subroutine
final_peaks: not called CC=32 SCAR=69.6
Pod
documented
| |||||||||||||||||||||||||||||
| 267 | 0 | my ($self,$dest,$prefix,$log) = @_; | |||||||||||||||||||||||||||
| 268 | 0 | my ($fn,$tfn,$tfh,$strand,$max,$idx,$val,$peak,$position,$pleft,$pright,$str); | |||||||||||||||||||||||||||
| 269 | 0 | my $suffix = "candidatepeaks.bed"; | |||||||||||||||||||||||||||
| 270 | 0 | my $this_function = (caller(0))[3]; | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 0 | ▶ | croak "ERROR [$this_function]: $dest does not exist\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | unless (-d $dest); | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | ▶ | if (defined $log){open(LOG, ">>", $log) or croak "$!";} | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 276 | 0 | else {croak "ERROR [$this_function] \$log not defined";} | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 0 | $fn = $prefix.".".$suffix; | |||||||||||||||||||||||||||
| 279 | 0 | (undef,$tfn) = tempfile('CANDIDATEPEAKS_XXXXXXX',UNLINK=>0); | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 0 | ▶ | croak "ERROR [$this_function]: $self->data not available\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | unless ($self->has_data); | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 0 | ▶ | croak "ERROR [$this_function]: $self->peaks not available\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | unless ($self->has_peaks); | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 0 | ▶ | open($tfh, ">", $tfn) or croak $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 0 | foreach my $chr (keys (%{$self->peaks})){ | |||||||||||||||||||||||||||
| 289 | 0 | ▶ | if (defined $log){print LOG "filtering candidate peaks in $chr\n";} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 0 | foreach $peak ( @{$self->peaks->{$chr}}){ | |||||||||||||||||||||||||||
| 292 | 0 | my $have_pleft = 0; # we have found a proper left end | |||||||||||||||||||||||||||
| 293 | 0 | my $have_pright = 0; # we have found a proper right end | |||||||||||||||||||||||||||
| 294 | 0 | $strand = $$peak{strand}; | |||||||||||||||||||||||||||
| 295 | 0 | ▶ | if (defined $log){print LOG ">>processing peak $$peak{start}-$$peak{end} [$strand] ";} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 0 | $max = max @{$self->data->{$chr}{$strand}}[$$peak{start}..$$peak{end}]; # max peak elevation | |||||||||||||||||||||||||||
| 297 | 0 | ▶ | if (defined $log){print LOG "max is $max";} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | ▶ | if ($max < $self->mincov){print LOG " ..SKIPPING\n";next;} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 0 | ▶ | $idx = first { @{$self->data->{$chr}{$strand}}[$_] eq $max } $$peak{start}..$$peak{end}; # coordinate | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 300 | #print LOG Dumper(\$peak); | ||||||||||||||||||||||||||||
| 301 | 0 | ▶ | if (defined $log){print LOG " at $idx ..KEEPING\n";} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | # process regions left/right of the maximum to identify peak boundaries | ||||||||||||||||||||||||||||
| 304 | # validity check first | ||||||||||||||||||||||||||||
| 305 | die "maximum at $idx is not within peak region" | ||||||||||||||||||||||||||||
| 306 | 0 | ▶ | if ( $idx<$$peak{start} || $idx>$$peak{end} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$idx < $peak->{"start"} or $idx > $peak->{"end"}
MC/DC0%0/2
$idx < $peak->{"start"} or $idx > $peak->{"end"}
$idx < $peak->{"start"}
$idx > $peak->{"end"}
Truth table0%0/3
A or B
A= $idx < $peak->{"start"}B= $idx > $peak->{"end"}
| |||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # find left end | ||||||||||||||||||||||||||||
| 309 | 0 | ▶ | for ($position = $idx; $position>$$peak{start}; $position--){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 0 | $val = ${$self->data->{$chr}{$strand}}[$position]; | |||||||||||||||||||||||||||
| 311 | 0 | ▶ | if (defined $log){print LOG "** VAL_L at pos $position $val - \$have_pleft=$have_pleft ";} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 0 | ▶ | if ( $val < $max*$self->threshold){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | 0 | $have_pleft = 1; | |||||||||||||||||||||||||||
| 314 | 0 | ▶ | if (defined $log){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 0 | print LOG "exiting 'cause $val < ".$max*$self->threshold." - \$have_pleft=$have_pleft\n"; | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | 0 | last; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | 0 | ▶ | if (defined $log){print LOG "\n";} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | 0 | $pleft = $position; | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | # find right end | ||||||||||||||||||||||||||||
| 324 | 0 | ▶ | for ($position = $idx; $position<=$$peak{end}; $position++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 0 | $val = ${$self->data->{$chr}{$strand}}[$position]; | |||||||||||||||||||||||||||
| 326 | 0 | ▶ | if (defined $log){print LOG "** VAL_R at pos $position $val - \$have_pright=$have_pright ";} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 0 | ▶ | if ( $val < $max*$self->threshold){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 0 | $have_pright = 1; | |||||||||||||||||||||||||||
| 329 | 0 | ▶ | if (defined $log){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 0 | print LOG "exiting 'cause $val < ".$max*$self->threshold."- \$have_pright=$have_pright\n"; | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | 0 | last; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | 0 | ▶ | if (defined $log){print LOG "\n";} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | 0 | $pright = $position; | |||||||||||||||||||||||||||
| 337 | 0 | ▶ | if ($strand eq "neg"){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | 0 | $str = '-'; | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | else{ | ||||||||||||||||||||||||||||
| 341 | 0 | $str = '+'; | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | 0 | ▶ | if (defined $log){print LOG "# PEAK characterized at $chr:$pleft-$pright\n";} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 0 | ▶ | if ($have_pleft == 1 && $have_pright == 1){ # print only if its a proper peak | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$have_pleft == 1 and $have_pright == 1
MC/DC0%0/2
$have_pleft == 1 and $have_pright == 1
$have_pleft == 1
$have_pright == 1
Truth table0%0/3
A and B
A= $have_pleft == 1B= $have_pright == 1
| |||||||||||||||||||||||||||||
| 346 | 0 | ▶ | if ($pright-$pleft<=$self->length){ # filter peak length | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | 0 | ▶ | if (defined $log){print $tfh "$chr\t$pleft\t$pright\tCANDIDATE\t$max\t$str\n";} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | } # end foreach $peak | ||||||||||||||||||||||||||||
| 351 | } # end foreach $chr | ||||||||||||||||||||||||||||
| 352 | 0 | close($tfh); | |||||||||||||||||||||||||||
| 353 | 0 | sortbed($tfn,$dest,$fn,0,$log); | |||||||||||||||||||||||||||
| 354 | 0 | unlink($tfn); | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 1 | 1; | |||||||||||||||||||||||||||
| 359 | __END__ | ||||||||||||||||||||||||||||