| 1 | package Bio::ToolBox::db_helper::bigbed; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | use English qw(-no_match_vars); | ||
| 7 | use List::Util qw(min max sum); | ||
| 8 | use Statistics::Lite qw(median); | ||
| 9 | use Bio::ToolBox::db_helper::constants; | ||
| 10 | use Bio::DB::BigBed; | ||
| 11 | require Exporter; | ||
| 12 | |||
| 13 | our $VERSION = '2.00'; | ||
| 14 | |||
| 15 | # Exported names | ||
| 16 | our @ISA = qw(Exporter); | ||
| 17 | |||
| 18 | ## no critic | ||
| 19 | ## this is never intended to be used directly by end users | ||
| 20 | ## and exporting everything is required | ||
| 21 | our @EXPORT = qw( | ||
| 22 | collect_bigbed_scores | ||
| 23 | collect_bigbed_position_scores | ||
| 24 | open_bigbed_db | ||
| 25 | sum_total_bigbed_features | ||
| 26 | ); | ||
| 27 | ## use critic | ||
| 28 | |||
| 29 | # Hash of Bigfile chromosomes | ||
| 30 | my %BIGBED_CHROMOS; | ||
| 31 | |||
| 32 | # sometimes user may request a chromosome that's not in the bigfile | ||
| 33 | # that could lead to an exception | ||
| 34 | # we will record the chromosomes list in this hash | ||
| 35 | # $BIGBED_CHROMOS{bigfile}{chromos} | ||
| 36 | # we also record the chromosome name variant with or without chr prefix | ||
| 37 | # to accommodate different naming conventions | ||
| 38 | |||
| 39 | # Opened bigBed db objects | ||
| 40 | my %OPENED_BB; | ||
| 41 | |||
| 42 | # a cache for opened BigBed databases, primarily for collecting scores | ||
| 43 | # caching here is only for local purposes of collecting scores | ||
| 44 | # db_helper also provides caching of db objects but with option to force open in | ||
| 45 | # the case of forking processes - we don't have that here | ||
| 46 | |||
| 47 | ### Collect BigBed scores only | ||
| 48 | sub collect_bigbed_scores { | ||
| 49 | |||
| 50 | # passed parameters as array ref | ||
| 51 | # chromosome, start, stop, strand, strandedness, method, db, dataset | ||
| 52 | my $param = shift; | ||
| 53 | |||
| 54 | # look at each bedfile | ||
| 55 | # usually there is only one, but for stranded data there may be | ||
| 56 | # two bedfiles (+ and -), so we'll check each bed file for strand info | ||
| 57 | my @scores; | ||
| 58 | for ( my $d = DATA; $d < scalar @{$param}; $d++ ) { | ||
| 59 | |||
| 60 | # open the bedfile | ||
| 61 | my $bb = _get_bb( $param->[$d] ); | ||
| 62 | |||
| 63 | # first check that the chromosome is present | ||
| 64 | my $chromo = $BIGBED_CHROMOS{ $param->[$d] }{ $param->[CHR] } or next; | ||
| 65 | |||
| 66 | # collect the features overlapping the region | ||
| 67 | # we are using the high level API rather than the low-level | ||
| 68 | # since we getting the individual scores from each bed element | ||
| 69 | my $bb_stream = $bb->features( | ||
| 70 | -seq_id => $chromo, | ||
| 71 | -start => $param->[STRT], | ||
| 72 | -end => $param->[STOP], | ||
| 73 | -iterator => 1, | ||
| 74 | ); | ||
| 75 | |||
| 76 | # process each feature | ||
| 77 | while ( my $bed = $bb_stream->next_seq ) { | ||
| 78 | |||
| 79 | # First check whether the strand is acceptable | ||
| 80 | if ( | ||
| 81 | $param->[STND] eq 'all' # all data is requested | ||
| 82 | or $bed->strand == 0 # unstranded data | ||
| 83 | or ( | ||
| 84 | # sense data | ||
| 85 | $param->[STR] == $bed->strand and $param->[STND] eq 'sense' | ||
| 86 | ) | ||
| 87 | or ( | ||
| 88 | # antisense data | ||
| 89 | $param->[STR] != $bed->strand and $param->[STND] eq 'antisense' | ||
| 90 | ) | ||
| 91 | ) | ||
| 92 | { | ||
| 93 | # we have acceptable data to collect | ||
| 94 | |||
| 95 | # store the appropriate datapoint | ||
| 96 | if ( $param->[METH] eq 'count' ) { | ||
| 97 | push @scores, 1; | ||
| 98 | } | ||
| 99 | elsif ( $param->[METH] eq 'pcount' ) { | ||
| 100 | push @scores, 1 | ||
| 101 | if ( $bed->start >= $param->[STRT] | ||
| 102 | and $bed->end <= $param->[STOP] ); | ||
| 103 | } | ||
| 104 | elsif ( $param->[METH] eq 'ncount' ) { | ||
| 105 | push @scores, $bed->display_name || $bed->primary_id; | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | push @scores, $bed->score; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | # return collected data | ||
| 115 | |||
| 116 | return wantarray ? @scores : \@scores; | ||
| 117 | } | ||
| 118 | |||
| 119 | ### Collect positioned BigBed scores | ||
| 120 | sub collect_bigbed_position_scores { | ||
| 121 | |||
| 122 | # passed parameters as array ref | ||
| 123 | # chromosome, start, stop, strand, strandedness, method, db, dataset | ||
| 124 | my $param = shift; | ||
| 125 | |||
| 126 | # look at each bedfile | ||
| 127 | # usually there is only one, but there may be more | ||
| 128 | my %pos2data; | ||
| 129 | for ( my $i = DATA; $i < scalar @{$param}; $i++ ) { | ||
| 130 | |||
| 131 | # open the bedfile | ||
| 132 | my $bb = _get_bb( $param->[$i] ); | ||
| 133 | |||
| 134 | # first check that the chromosome is present | ||
| 135 | my $chromo = $BIGBED_CHROMOS{ $param->[$i] }{ $param->[CHR] } or next; | ||
| 136 | |||
| 137 | # collect the features overlapping the region | ||
| 138 | my $bb_stream = $bb->features( | ||
| 139 | -seq_id => $chromo, | ||
| 140 | -start => $param->[STRT], | ||
| 141 | -end => $param->[STOP], | ||
| 142 | -iterator => 1, | ||
| 143 | ); | ||
| 144 | |||
| 145 | # process each feature | ||
| 146 | while ( my $bed = $bb_stream->next_seq ) { | ||
| 147 | |||
| 148 | # First check whether the strand is acceptable | ||
| 149 | if ( | ||
| 150 | $param->[STND] eq 'all' # all data is requested | ||
| 151 | or $bed->strand == 0 # unstranded data | ||
| 152 | or ( | ||
| 153 | # sense data | ||
| 154 | $param->[STR] == $bed->strand and $param->[STND] eq 'sense' | ||
| 155 | ) | ||
| 156 | or ( | ||
| 157 | # antisense data | ||
| 158 | $param->[STR] != $bed->strand and $param->[STND] eq 'antisense' | ||
| 159 | ) | ||
| 160 | ) | ||
| 161 | { | ||
| 162 | # we have acceptable data to collect | ||
| 163 | |||
| 164 | # determine position to record | ||
| 165 | my $position; | ||
| 166 | if ( $bed->start == $bed->end ) { | ||
| 167 | |||
| 168 | # just one position recorded | ||
| 169 | $position = $bed->start; | ||
| 170 | } | ||
| 171 | else { | ||
| 172 | # calculate the midpoint | ||
| 173 | $position = int( ( ( $bed->start + $bed->end ) / 2 ) + 0.5 ); | ||
| 174 | } | ||
| 175 | |||
| 176 | # check the position | ||
| 177 | next | ||
| 178 | unless ( | ||
| 179 | # want to avoid those whose midpoint are not technically | ||
| 180 | # within the region of interest | ||
| 181 | $position >= $param->[STRT] and $position <= $param->[STOP] | ||
| 182 | ); | ||
| 183 | |||
| 184 | # store the appropriate datapoint | ||
| 185 | # for score and length, we're putting these into an array | ||
| 186 | if ( $param->[METH] eq 'count' ) { | ||
| 187 | $pos2data{$position} += 1; | ||
| 188 | } | ||
| 189 | elsif ( $param->[METH] eq 'pcount' ) { | ||
| 190 | $pos2data{$position} += 1 | ||
| 191 | if ( $bed->start <= $param->[STRT] | ||
| 192 | and $bed->end <= $param->[STOP] ); | ||
| 193 | } | ||
| 194 | elsif ( $param->[METH] eq 'ncount' ) { | ||
| 195 | $pos2data{$position} ||= []; | ||
| 196 | push @{ $pos2data{$position} }, $bed->display_name | ||
| 197 | || $bed->primary_id; | ||
| 198 | } | ||
| 199 | else { | ||
| 200 | # everything else we just take the score | ||
| 201 | push @{ $pos2data{$position} }, $bed->score + 0; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | # combine multiple datapoints at the same position | ||
| 208 | if ( $param->[METH] eq 'ncount' ) { | ||
| 209 | foreach my $position ( keys %pos2data ) { | ||
| 210 | my %name2count; | ||
| 211 | foreach ( @{ $pos2data{$position} } ) { $name2count{$_} += 1 } | ||
| 212 | $pos2data{$position} = scalar( keys %name2count ); | ||
| 213 | } | ||
| 214 | } | ||
| 215 | elsif ( $param->[METH] eq 'count' or $param->[METH] eq 'pcount' ) { | ||
| 216 | |||
| 217 | # do nothing, these aren't arrays | ||
| 218 | } | ||
| 219 | elsif ( $param->[METH] eq 'mean' ) { | ||
| 220 | foreach my $position ( keys %pos2data ) { | ||
| 221 | $pos2data{$position} = | ||
| 222 | sum( @{ $pos2data{$position} } ) / scalar( @{ $pos2data{$position} } ); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | elsif ( $param->[METH] eq 'median' ) { | ||
| 226 | foreach my $position ( keys %pos2data ) { | ||
| 227 | $pos2data{$position} = median( @{ $pos2data{$position} } ); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | elsif ( $param->[METH] eq 'min' ) { | ||
| 231 | foreach my $position ( keys %pos2data ) { | ||
| 232 | $pos2data{$position} = min( @{ $pos2data{$position} } ); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | elsif ( $param->[METH] eq 'max' ) { | ||
| 236 | foreach my $position ( keys %pos2data ) { | ||
| 237 | $pos2data{$position} = max( @{ $pos2data{$position} } ); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | elsif ( $param->[METH] eq 'sum' ) { | ||
| 241 | foreach my $position ( keys %pos2data ) { | ||
| 242 | $pos2data{$position} = sum( @{ $pos2data{$position} } ); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | else { | ||
| 246 | # just take the mean for everything else | ||
| 247 | foreach my $position ( keys %pos2data ) { | ||
| 248 | $pos2data{$position} = | ||
| 249 | sum( @{ $pos2data{$position} } ) / scalar( @{ $pos2data{$position} } ); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | # return collected data | ||
| 254 | return wantarray ? %pos2data : \%pos2data; | ||
| 255 | } | ||
| 256 | |||
| 257 | ### Open a bigBed database connection | ||
| 258 | sub open_bigbed_db { | ||
| 259 | |||
| 260 | # check path | ||
| 261 | my $bedfile = shift; | ||
| 262 | my $path = $bedfile; | ||
| 263 | $path =~ s/^file://; # clean up file prefix if present | ||
| 264 | |||
| 265 | # open | ||
| 266 | my $bb; | ||
| 267 | eval { $bb = Bio::DB::BigBed->new($path); }; | ||
| 268 | if ($bb) { | ||
| 269 | return $bb; | ||
| 270 | } | ||
| 271 | elsif ( $EVAL_ERROR or $OS_ERROR ) { | ||
| 272 | carp $EVAL_ERROR; | ||
| 273 | carp $OS_ERROR; | ||
| 274 | return; | ||
| 275 | } | ||
| 276 | else { | ||
| 277 | return; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | ### Sum the total number of features in the bigBed file | ||
| 282 | sub sum_total_bigbed_features { | ||
| 283 | |||
| 284 | # Passed arguments; | ||
| 285 | my $bb_file = shift; | ||
| 286 | unless ($bb_file) { | ||
| 287 | carp "ERROR: no BigBed file or BigBed db object passed!"; | ||
| 288 | return; | ||
| 289 | } | ||
| 290 | |||
| 291 | # Open BigBed file if necessary | ||
| 292 | my $bb; | ||
| 293 | my $bb_ref = ref $bb_file; | ||
| 294 | if ( $bb_ref =~ /Bio::DB::BigBed/x ) { | ||
| 295 | |||
| 296 | # we have an opened bigbed db object | ||
| 297 | $bb = $bb_file; | ||
| 298 | } | ||
| 299 | else { | ||
| 300 | # we have a name of a bigbed file | ||
| 301 | # open it but do not remember it | ||
| 302 | $bb = open_bigbed_db($bb_file); | ||
| 303 | return unless ($bb); | ||
| 304 | } | ||
| 305 | |||
| 306 | # Return the item count for the bigBed | ||
| 307 | # wow, this is easy! | ||
| 308 | return $bb->bf->bigBedItemCount; | ||
| 309 | |||
| 310 | # this is the itemCount available to the low level bigFile object | ||
| 311 | # the validCount method from summary or bin features simple records | ||
| 312 | # the number of covered bases, not entirely useful here | ||
| 313 | } | ||
| 314 | |||
| 315 | ### Internal subroutine for getting the cached bigbed object | ||
| 316 | sub _get_bb { | ||
| 317 | my $bbfile = shift; | ||
| 318 | |||
| 319 | return $OPENED_BB{$bbfile} if exists $OPENED_BB{$bbfile}; | ||
| 320 | |||
| 321 | # open and cache the bigWig object | ||
| 322 | my $bb = open_bigbed_db($bbfile) | ||
| 323 | or croak "FATAL: Unable to open bigBed file '$bbfile'!"; | ||
| 324 | $OPENED_BB{$bbfile} = $bb; | ||
| 325 | |||
| 326 | # record the chromosomes and possible variants | ||
| 327 | $BIGBED_CHROMOS{$bbfile} = {}; | ||
| 328 | foreach my $s ( $bb->seq_ids ) { | ||
| 329 | $BIGBED_CHROMOS{$bbfile}{$s} = $s; | ||
| 330 | if ( $s =~ /^chr(.+)$/ ) { | ||
| 331 | $BIGBED_CHROMOS{$bbfile}{$1} = $s; | ||
| 332 | } | ||
| 333 | else { | ||
| 334 | $BIGBED_CHROMOS{$bbfile}{"chr$s"} = $s; | ||
| 335 | } | ||
| 336 | } | ||
| 337 | return $bb; | ||
| 338 | } | ||
| 339 | |||
| 340 | 1; | ||
| 341 | |||
| 342 | __END__ |