| 1 | package Bio::ToolBox::db_helper::alignment_callbacks; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | use Bio::ToolBox::db_helper::constants; | ||
| 7 | require Exporter; | ||
| 8 | |||
| 9 | our $VERSION = '2.03'; | ||
| 10 | |||
| 11 | # Exported names | ||
| 12 | our @ISA = qw(Exporter); | ||
| 13 | |||
| 14 | ## no critic | ||
| 15 | ## this is never intended to be used directly by end users | ||
| 16 | ## and exporting everything is required | ||
| 17 | our @EXPORT = qw( | ||
| 18 | assign_callback | ||
| 19 | _all_count_indexed | ||
| 20 | _all_precise_count_indexed | ||
| 21 | _all_name_indexed | ||
| 22 | _all_count_array | ||
| 23 | _all_precise_count_array | ||
| 24 | _all_name_array | ||
| 25 | _forward_count_indexed | ||
| 26 | _forward_precise_count_indexed | ||
| 27 | _forward_name_indexed | ||
| 28 | _forward_count_array | ||
| 29 | _forward_precise_count_array | ||
| 30 | _forward_name_array | ||
| 31 | _reverse_count_indexed | ||
| 32 | _reverse_precise_count_indexed | ||
| 33 | _reverse_name_indexed | ||
| 34 | _reverse_count_array | ||
| 35 | _reverse_precise_count_array | ||
| 36 | _reverse_name_array | ||
| 37 | ); | ||
| 38 | ## use critic | ||
| 39 | |||
| 40 | # Lookup hash for caching callback methods | ||
| 41 | my %CALLBACKS; | ||
| 42 | |||
| 43 | # skip all unwanted flags: SECONDARY,QCFAIL,DUP,SUPPLEMENTARY | ||
| 44 | my $UNWANTED_FLAGS = 0xf00; # 0x100 + 0x200 + 0x400 + 0x800 | ||
| 45 | |||
| 46 | ### Generate callback subroutine for walking through Bam alignments | ||
| 47 | sub assign_callback { | ||
| 48 | |||
| 49 | # generate the callback code depending on whether we want to look at | ||
| 50 | # stranded data, collecting counts or length, or whether indexed data | ||
| 51 | # is wanted. | ||
| 52 | |||
| 53 | # we perform a check of whether the alignment midpoint is within the | ||
| 54 | # search region for indexed data only | ||
| 55 | |||
| 56 | # these subroutines are designed to work with the low level fetch API | ||
| 57 | |||
| 58 | # there are so many different subroutines because I want to increase | ||
| 59 | # efficiency by limiting the number of conditional tests in one generic subroutine | ||
| 60 | |||
| 61 | # passed parameters as array ref | ||
| 62 | # chromosome, start, stop, strand, strandedness, method, value, db, dataset | ||
| 63 | my $param = shift; | ||
| 64 | |||
| 65 | # check the current list of calculated callbacks | ||
| 66 | # cache the calculated callback method to speed up subsequent data | ||
| 67 | # collections it's likely only one method is ever employed in an | ||
| 68 | # execution, but just in case we will cache all that we calculate | ||
| 69 | my $string = sprintf "%s_%s_%s_%d", $param->[STND], $param->[STR], | ||
| 70 | $param->[METH], $param->[RETT]; | ||
| 71 | return $CALLBACKS{$string} if exists $CALLBACKS{$string}; | ||
| 72 | |||
| 73 | # determine the callback method based on requested criteria | ||
| 74 | my $callback; | ||
| 75 | |||
| 76 | # all reads, either strand | ||
| 77 | if ( $param->[STND] eq 'all' | ||
| 78 | and $param->[METH] eq 'count' | ||
| 79 | and $param->[RETT] == 2 ) | ||
| 80 | { | ||
| 81 | $callback = \&_all_count_indexed; | ||
| 82 | } | ||
| 83 | elsif ( $param->[STND] eq 'all' | ||
| 84 | and $param->[METH] eq 'pcount' | ||
| 85 | and $param->[RETT] == 2 ) | ||
| 86 | { | ||
| 87 | $callback = \&_all_precise_count_indexed; | ||
| 88 | } | ||
| 89 | elsif ( $param->[STND] eq 'all' | ||
| 90 | and $param->[METH] eq 'count' | ||
| 91 | and $param->[RETT] != 2 ) | ||
| 92 | { | ||
| 93 | $callback = \&_all_count_array; | ||
| 94 | } | ||
| 95 | elsif ( $param->[STND] eq 'all' | ||
| 96 | and $param->[METH] eq 'pcount' | ||
| 97 | and $param->[RETT] != 2 ) | ||
| 98 | { | ||
| 99 | $callback = \&_all_precise_count_array; | ||
| 100 | } | ||
| 101 | elsif ( $param->[STND] eq 'all' | ||
| 102 | and $param->[METH] eq 'ncount' | ||
| 103 | and $param->[RETT] != 2 ) | ||
| 104 | { | ||
| 105 | $callback = \&_all_name_array; | ||
| 106 | } | ||
| 107 | elsif ( $param->[STND] eq 'all' | ||
| 108 | and $param->[METH] eq 'ncount' | ||
| 109 | and $param->[RETT] == 2 ) | ||
| 110 | { | ||
| 111 | $callback = \&_all_name_indexed; | ||
| 112 | } | ||
| 113 | |||
| 114 | # sense, forward strand | ||
| 115 | elsif ( $param->[STND] eq 'sense' | ||
| 116 | and $param->[STR] >= 0 | ||
| 117 | and $param->[METH] eq 'count' | ||
| 118 | and $param->[RETT] == 2 ) | ||
| 119 | { | ||
| 120 | $callback = \&_forward_count_indexed; | ||
| 121 | } | ||
| 122 | elsif ( $param->[STND] eq 'sense' | ||
| 123 | and $param->[STR] >= 0 | ||
| 124 | and $param->[METH] eq 'pcount' | ||
| 125 | and $param->[RETT] == 2 ) | ||
| 126 | { | ||
| 127 | $callback = \&_forward_precise_count_indexed; | ||
| 128 | } | ||
| 129 | elsif ( $param->[STND] eq 'sense' | ||
| 130 | and $param->[STR] >= 0 | ||
| 131 | and $param->[METH] eq 'count' | ||
| 132 | and $param->[RETT] != 2 ) | ||
| 133 | { | ||
| 134 | $callback = \&_forward_count_array; | ||
| 135 | } | ||
| 136 | elsif ( $param->[STND] eq 'sense' | ||
| 137 | and $param->[STR] >= 0 | ||
| 138 | and $param->[METH] eq 'pcount' | ||
| 139 | and $param->[RETT] != 2 ) | ||
| 140 | { | ||
| 141 | $callback = \&_forward_precise_count_array; | ||
| 142 | } | ||
| 143 | elsif ( $param->[STND] eq 'sense' | ||
| 144 | and $param->[STR] >= 0 | ||
| 145 | and $param->[METH] eq 'ncount' | ||
| 146 | and $param->[RETT] != 2 ) | ||
| 147 | { | ||
| 148 | $callback = \&_forward_name_array; | ||
| 149 | } | ||
| 150 | elsif ( $param->[STND] eq 'sense' | ||
| 151 | and $param->[STR] >= 0 | ||
| 152 | and $param->[METH] eq 'ncount' | ||
| 153 | and $param->[RETT] == 2 ) | ||
| 154 | { | ||
| 155 | $callback = \&_forward_name_indexed; | ||
| 156 | } | ||
| 157 | |||
| 158 | # sense, reverse strand | ||
| 159 | elsif ( $param->[STND] eq 'sense' | ||
| 160 | and $param->[STR] == -1 | ||
| 161 | and $param->[METH] eq 'count' | ||
| 162 | and $param->[RETT] == 2 ) | ||
| 163 | { | ||
| 164 | $callback = \&_reverse_count_indexed; | ||
| 165 | } | ||
| 166 | elsif ( $param->[STND] eq 'sense' | ||
| 167 | and $param->[STR] == -1 | ||
| 168 | and $param->[METH] eq 'pcount' | ||
| 169 | and $param->[RETT] == 2 ) | ||
| 170 | { | ||
| 171 | $callback = \&_reverse_precise_count_indexed; | ||
| 172 | } | ||
| 173 | elsif ( $param->[STND] eq 'sense' | ||
| 174 | and $param->[STR] == -1 | ||
| 175 | and $param->[METH] eq 'ncount' | ||
| 176 | and $param->[RETT] == 2 ) | ||
| 177 | { | ||
| 178 | $callback = \&_reverse_name_indexed; | ||
| 179 | } | ||
| 180 | elsif ( $param->[STND] eq 'sense' | ||
| 181 | and $param->[STR] == -1 | ||
| 182 | and $param->[METH] eq 'count' | ||
| 183 | and $param->[RETT] != 2 ) | ||
| 184 | { | ||
| 185 | $callback = \&_reverse_count_array; | ||
| 186 | } | ||
| 187 | elsif ( $param->[STND] eq 'sense' | ||
| 188 | and $param->[STR] == -1 | ||
| 189 | and $param->[METH] eq 'pcount' | ||
| 190 | and $param->[RETT] != 2 ) | ||
| 191 | { | ||
| 192 | $callback = \&_reverse_precise_count_array; | ||
| 193 | } | ||
| 194 | elsif ( $param->[STND] eq 'sense' | ||
| 195 | and $param->[STR] == -1 | ||
| 196 | and $param->[METH] eq 'ncount' | ||
| 197 | and $param->[RETT] != 2 ) | ||
| 198 | { | ||
| 199 | $callback = \&_reverse_name_array; | ||
| 200 | } | ||
| 201 | |||
| 202 | # anti-sense, forward strand | ||
| 203 | elsif ( $param->[STND] eq 'antisense' | ||
| 204 | and $param->[STR] >= 0 | ||
| 205 | and $param->[METH] eq 'count' | ||
| 206 | and $param->[RETT] == 2 ) | ||
| 207 | { | ||
| 208 | $callback = \&_reverse_count_indexed; | ||
| 209 | } | ||
| 210 | elsif ( $param->[STND] eq 'antisense' | ||
| 211 | and $param->[STR] >= 0 | ||
| 212 | and $param->[METH] eq 'pcount' | ||
| 213 | and $param->[RETT] == 2 ) | ||
| 214 | { | ||
| 215 | $callback = \&_reverse_precise_count_indexed; | ||
| 216 | } | ||
| 217 | elsif ( $param->[STND] eq 'antisense' | ||
| 218 | and $param->[STR] >= 0 | ||
| 219 | and $param->[METH] eq 'ncount' | ||
| 220 | and $param->[RETT] == 2 ) | ||
| 221 | { | ||
| 222 | $callback = \&_reverse_name_indexed; | ||
| 223 | } | ||
| 224 | elsif ( $param->[STND] eq 'antisense' | ||
| 225 | and $param->[STR] >= 0 | ||
| 226 | and $param->[METH] eq 'count' | ||
| 227 | and $param->[RETT] != 2 ) | ||
| 228 | { | ||
| 229 | $callback = \&_reverse_count_array; | ||
| 230 | } | ||
| 231 | elsif ( $param->[STND] eq 'antisense' | ||
| 232 | and $param->[STR] >= 0 | ||
| 233 | and $param->[METH] eq 'pcount' | ||
| 234 | and $param->[RETT] != 2 ) | ||
| 235 | { | ||
| 236 | $callback = \&_reverse_precise_count_array; | ||
| 237 | } | ||
| 238 | elsif ( $param->[STND] eq 'antisense' | ||
| 239 | and $param->[STR] >= 0 | ||
| 240 | and $param->[METH] eq 'ncount' | ||
| 241 | and $param->[RETT] != 2 ) | ||
| 242 | { | ||
| 243 | $callback = \&_reverse_name_array; | ||
| 244 | } | ||
| 245 | |||
| 246 | # anti-sense, reverse strand | ||
| 247 | elsif ( $param->[STND] eq 'antisense' | ||
| 248 | and $param->[STR] == -1 | ||
| 249 | and $param->[METH] eq 'count' | ||
| 250 | and $param->[RETT] == 2 ) | ||
| 251 | { | ||
| 252 | $callback = \&_forward_count_indexed; | ||
| 253 | } | ||
| 254 | elsif ( $param->[STND] eq 'antisense' | ||
| 255 | and $param->[STR] == -1 | ||
| 256 | and $param->[METH] eq 'pcount' | ||
| 257 | and $param->[RETT] == 2 ) | ||
| 258 | { | ||
| 259 | $callback = \&_forward_precise_count_indexed; | ||
| 260 | } | ||
| 261 | elsif ( $param->[STND] eq 'antisense' | ||
| 262 | and $param->[STR] == -1 | ||
| 263 | and $param->[METH] eq 'ncount' | ||
| 264 | and $param->[RETT] == 2 ) | ||
| 265 | { | ||
| 266 | $callback = \&_forward_name_indexed; | ||
| 267 | } | ||
| 268 | elsif ( $param->[STND] eq 'antisense' | ||
| 269 | and $param->[STR] == -1 | ||
| 270 | and $param->[METH] eq 'count' | ||
| 271 | and $param->[RETT] != 2 ) | ||
| 272 | { | ||
| 273 | $callback = \&_forward_count_array; | ||
| 274 | } | ||
| 275 | elsif ( $param->[STND] eq 'antisense' | ||
| 276 | and $param->[STR] == -1 | ||
| 277 | and $param->[METH] eq 'pcount' | ||
| 278 | and $param->[RETT] != 2 ) | ||
| 279 | { | ||
| 280 | $callback = \&_forward_precise_count_array; | ||
| 281 | } | ||
| 282 | elsif ( $param->[STND] eq 'antisense' | ||
| 283 | and $param->[STR] == -1 | ||
| 284 | and $param->[METH] eq 'ncount' | ||
| 285 | and $param->[RETT] != 2 ) | ||
| 286 | { | ||
| 287 | $callback = \&_forward_name_array; | ||
| 288 | } | ||
| 289 | |||
| 290 | # I goofed | ||
| 291 | else { | ||
| 292 | confess sprintf | ||
| 293 | "FATAL Programmer error: stranded %s, strand %s, method %s, do_index %d", | ||
| 294 | $param->[STND], $param->[STR], $param->[METH], $param->[RETT]; | ||
| 295 | } | ||
| 296 | |||
| 297 | # remember next time | ||
| 298 | $CALLBACKS{$string} = $callback; | ||
| 299 | |||
| 300 | return $callback; | ||
| 301 | } | ||
| 302 | |||
| 303 | #### Callback subroutines | ||
| 304 | # the following are all of the callback subroutines | ||
| 305 | |||
| 306 | # we explicitly check that alignment start and/or stop overlap the search coordinates | ||
| 307 | # to avoid e.g. weird alignments with massive splice junctions that would nevertheless | ||
| 308 | # be pulled out by the bam index search | ||
| 309 | |||
| 310 | sub _all_count_indexed { | ||
| 311 | my ( $a, $data ) = @_; | ||
| 312 | my $flag = $a->flag; | ||
| 313 | return if $flag & $UNWANTED_FLAGS; | ||
| 314 | return if $a->qual < $MAPQ; | ||
| 315 | my $s = $a->pos + 1; | ||
| 316 | my $e = $a->calend; | ||
| 317 | return | ||
| 318 | unless ( ( $s >= $data->{start} and $s <= $data->{stop} ) | ||
| 319 | or ( $e >= $data->{start} and $e <= $data->{stop} ) ); | ||
| 320 | |||
| 321 | if ( $flag & 0x10 ) { | ||
| 322 | |||
| 323 | # reversed | ||
| 324 | $data->{'index'}{$e}++; | ||
| 325 | } | ||
| 326 | else { | ||
| 327 | $data->{'index'}{$s}++; | ||
| 328 | } | ||
| 329 | } | ||
| 330 | |||
| 331 | sub _all_precise_count_indexed { | ||
| 332 | my ( $a, $data ) = @_; | ||
| 333 | my $flag = $a->flag; | ||
| 334 | return if $flag & $UNWANTED_FLAGS; | ||
| 335 | return if $a->qual < $MAPQ; | ||
| 336 | my $s = $a->pos + 1; | ||
| 337 | return unless ( $s >= $data->{start} and $a->calend <= $data->{stop} ); | ||
| 338 | if ( $flag & 0x10 ) { | ||
| 339 | |||
| 340 | # reversed | ||
| 341 | $data->{'index'}{$s}++; | ||
| 342 | } | ||
| 343 | else { | ||
| 344 | $data->{'index'}{ $a->calend }++; | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | sub _all_name_indexed { | ||
| 349 | my ( $a, $data ) = @_; | ||
| 350 | my $flag = $a->flag; | ||
| 351 | return if $flag & $UNWANTED_FLAGS; | ||
| 352 | return if $a->qual < $MAPQ; | ||
| 353 | my $s = $a->pos + 1; | ||
| 354 | my $e = $a->calend; | ||
| 355 | return | ||
| 356 | unless ( ( $s >= $data->{start} and $s <= $data->{stop} ) | ||
| 357 | or ( $e >= $data->{start} and $e <= $data->{stop} ) ); | ||
| 358 | |||
| 359 | if ( $flag & 0x10 ) { | ||
| 360 | |||
| 361 | # reversed | ||
| 362 | # since we're working with names, only record the 5' end of fragment | ||
| 363 | return if ( ( $flag & 0x1 ) and ( $flag & 0x2 ) ); | ||
| 364 | |||
| 365 | # paired and proper | ||
| 366 | # not the end we're looking for | ||
| 367 | $data->{'index'}{$e} ||= []; | ||
| 368 | push @{ $data->{'index'}{$e} }, $a->qname; | ||
| 369 | } | ||
| 370 | else { | ||
| 371 | $data->{'index'}{$s} ||= []; | ||
| 372 | push @{ $data->{'index'}{$s} }, $a->qname; | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | sub _all_count_array { | ||
| 377 | my ( $a, $data ) = @_; | ||
| 378 | my $flag = $a->flag; | ||
| 379 | return if $flag & $UNWANTED_FLAGS; | ||
| 380 | return if $a->qual < $MAPQ; | ||
| 381 | my $s = $a->pos + 1; | ||
| 382 | my $e = $a->calend; | ||
| 383 | return | ||
| 384 | unless ( ( $s >= $data->{start} and $s <= $data->{stop} ) | ||
| 385 | or ( $e >= $data->{start} and $e <= $data->{stop} ) ); | ||
| 386 | push @{ $data->{scores} }, 1; | ||
| 387 | } | ||
| 388 | |||
| 389 | sub _all_precise_count_array { | ||
| 390 | my ( $a, $data ) = @_; | ||
| 391 | my $flag = $a->flag; | ||
| 392 | return if $flag & $UNWANTED_FLAGS; | ||
| 393 | return if $a->qual < $MAPQ; | ||
| 394 | return unless ( $a->pos + 1 >= $data->{start} and $a->calend <= $data->{stop} ); | ||
| 395 | push @{ $data->{scores} }, 1; | ||
| 396 | } | ||
| 397 | |||
| 398 | sub _all_name_array { | ||
| 399 | my ( $a, $data ) = @_; | ||
| 400 | my $flag = $a->flag; | ||
| 401 | return if $flag & $UNWANTED_FLAGS; | ||
| 402 | return if $a->qual < $MAPQ; | ||
| 403 | my $s = $a->pos + 1; | ||
| 404 | my $e = $a->calend; | ||
| 405 | return | ||
| 406 | unless ( ( $s >= $data->{start} and $s <= $data->{stop} ) | ||
| 407 | or ( $e >= $data->{start} and $e <= $data->{stop} ) ); | ||
| 408 | push @{ $data->{scores} }, $a->qname; | ||
| 409 | } | ||
| 410 | |||
| 411 | sub _forward_count_indexed { | ||
| 412 | my ( $a, $data ) = @_; | ||
| 413 | my $flag = $a->flag; | ||
| 414 | return if $flag & $UNWANTED_FLAGS; | ||
| 415 | return if $a->qual < $MAPQ; | ||
| 416 | my $reversed = $flag & 0x10; # reversed | ||
| 417 | if ( $flag & 0x1 ) { | ||
| 418 | |||
| 419 | # paired | ||
| 420 | my $first = $flag & 0x40; # true if FIRST_MATE | ||
| 421 | return if ( $first and $reversed ); | ||
| 422 | return if ( not $first and not $reversed ); | ||
| 423 | } | ||
| 424 | else { | ||
| 425 | return if $reversed; | ||
| 426 | } | ||
| 427 | my $s = $a->pos + 1; | ||
| 428 | my $e = $a->calend; | ||
| 429 | return | ||
| 430 | unless ( ( $s >= $data->{start} and $s <= $data->{stop} ) | ||
| 431 | or ( $e >= $data->{start} and $e <= $data->{stop} ) ); | ||
| 432 | if ($reversed) { | ||
| 433 | $data->{'index'}{$e}++; | ||
| 434 | } | ||
| 435 | else { | ||
| 436 | $data->{'index'}{$s}++; | ||
| 437 | } | ||
| 438 | } | ||
| 439 | |||
| 440 | sub _forward_precise_count_indexed { | ||
| 441 | my ( $a, $data ) = @_; | ||
| 442 | my $flag = $a->flag; | ||
| 443 | return if $flag & $UNWANTED_FLAGS; | ||
| 444 | return if $a->qual < $MAPQ; | ||
| 445 | my $reversed = $flag & 0x10; # reversed; | ||
| 446 | if ( $flag & 0x1 ) { | ||
| 447 | |||
| 448 | # paired | ||
| 449 | my $first = $flag & 0x40; # true if FIRST_MATE | ||
| 450 | return if ( $first and $reversed ); | ||
| 451 | return if ( not $first and not $reversed ); | ||
| 452 | } | ||
| 453 | else { | ||
| 454 | return if $reversed; | ||
| 455 | } | ||
| 456 | my $s = $a->pos + 1; | ||
| 457 | return unless ( $s >= $data->{start} and $a->calend <= $data->{stop} ); | ||
| 458 | if ($reversed) { | ||
| 459 | $data->{'index'}{$s}++; | ||
| 460 | } | ||
| 461 | else { | ||
| 462 | $data->{'index'}{ $a->calend }++; | ||
| 463 | } | ||
| 464 | } | ||
| 465 | |||
| 466 | sub _forward_name_indexed { | ||
| 467 | my ( $a, $data ) = @_; | ||
| 468 | my $flag = $a->flag; | ||
| 469 | return if $flag & $UNWANTED_FLAGS; | ||
| 470 | return if $a->qual < $MAPQ; | ||
| 471 | my $reversed = $flag & 0x10; # reversed; | ||
| 472 | if ( $flag & 0x1 ) { | ||
| 473 | |||
| 474 | # paired | ||
| 475 | my $first = $a->flag & 0x40; # true if FIRST_MATE | ||
| 476 | return if ( $first and $reversed ); | ||
| 477 | return if ( not $first and not $reversed ); | ||
| 478 | } | ||
| 479 | else { | ||
| 480 | return if $reversed; | ||
| 481 | } | ||
| 482 | my $s = $a->pos + 1; | ||
| 483 | my $e = $a->calend; | ||
| 484 | return | ||
| 485 | unless ( ( $s >= $data->{start} and $s <= $data->{stop} ) | ||
| 486 | or ( $e >= $data->{start} and $e <= $data->{stop} ) ); | ||
| 487 | if ($reversed) { | ||
| 488 | $data->{'index'}{$e} ||= []; | ||
| 489 | push @{ $data->{'index'}{$e} }, $a->qname; | ||
| 490 | } | ||
| 491 | else { | ||
| 492 | $data->{'index'}{$s} ||= []; | ||
| 493 | push @{ $data->{'index'}{$s} }, $a->qname; | ||
| 494 | } | ||
| 495 | } | ||
| 496 | |||
| 497 | sub _forward_count_array { | ||
| 498 | my ( $a, $data ) = @_; | ||
| 499 | my $flag = $a->flag; | ||
| 500 | return if $flag & $UNWANTED_FLAGS; | ||
| 501 | return if $a->qual < $MAPQ; | ||
| 502 | my $reversed = $flag & 0x10; # reversed; | ||
| 503 | if ( $flag & 0x1 ) { | ||
| 504 | |||
| 505 | # paired | ||
| 506 | my $first = $a->flag & 0x40; # true if FIRST_MATE | ||
| 507 | return if ( $first and $reversed ); | ||
| 508 | return if ( not $first and not $reversed ); | ||
| 509 | } | ||
| 510 | else { | ||
| 511 | return if $reversed; | ||
| 512 | } | ||
| 513 | my $s = $a->pos + 1; | ||
| 514 | my $e = $a->calend; | ||
| 515 | return | ||
| 516 | unless ( ( $s >= $data->{start} and $s <= $data->{stop} ) | ||
| 517 | or ( $e >= $data->{start} and $e <= $data->{stop} ) ); | ||
| 518 | push @{ $data->{scores} }, 1; | ||
| 519 | } | ||
| 520 | |||
| 521 | sub _forward_precise_count_array { | ||
| 522 | my ( $a, $data ) = @_; | ||
| 523 | my $flag = $a->flag; | ||
| 524 | return if $flag & $UNWANTED_FLAGS; | ||
| 525 | return if $a->qual < $MAPQ; | ||
| 526 | my $reversed = $flag & 0x10; # reversed; | ||
| 527 | if ( $flag & 0x1 ) { | ||
| 528 | |||
| 529 | # paired | ||
| 530 | my $first = $a->flag & 0x40; # true if FIRST_MATE | ||
| 531 | return if ( $first and $reversed ); | ||
| 532 | return if ( not $first and not $reversed ); | ||
| 533 | } | ||
| 534 | else { | ||
| 535 | return if $reversed; | ||
| 536 | } | ||
| 537 | return unless ( $a->pos + 1 >= $data->{start} and $a->calend <= $data->{stop} ); | ||
| 538 | push @{ $data->{scores} }, 1; | ||
| 539 | } | ||
| 540 | |||
| 541 | sub _forward_name_array { | ||
| 542 | my ( $a, $data ) = @_; | ||
| 543 | my $flag = $a->flag; | ||
| 544 | return if $flag & $UNWANTED_FLAGS; | ||
| 545 | return if $a->qual < $MAPQ; | ||
| 546 | my $reversed = $flag & 0x10; # reversed; | ||
| 547 | if ( $flag & 0x1 ) { | ||
| 548 | |||
| 549 | # paired | ||
| 550 | my $first = $a->flag & 0x40; # true if FIRST_MATE | ||
| 551 | return if ( $first and $reversed ); | ||
| 552 | return if ( not $first and not $reversed ); | ||
| 553 | } | ||
| 554 | else { | ||
| 555 | return if $reversed; | ||
| 556 | } | ||
| 557 | my $s = $a->pos + 1; | ||
| 558 | my $e = $a->calend; | ||
| 559 | return | ||
| 560 | unless ( ( $s >= $data->{start} and $s <= $data->{stop} ) | ||
| 561 | or ( $e >= $data->{start} and $e <= $data->{stop} ) ); | ||
| 562 | push @{ $data->{scores} }, $a->qname; | ||
| 563 | } | ||
| 564 | |||
| 565 | sub _reverse_count_indexed { | ||
| 566 | my ( $a, $data ) = @_; | ||
| 567 | my $flag = $a->flag; | ||
| 568 | return if $flag & $UNWANTED_FLAGS; | ||
| 569 | return if $a->qual < $MAPQ; | ||
| 570 | my $reversed = $flag & 0x10; # reversed; | ||
| 571 | if ( $flag & 0x1 ) { | ||
| 572 | |||
| 573 | # paired | ||
| 574 | my $first = $a->flag & 0x40; # true if FIRST_MATE | ||
| 575 | return if ( $first and not $reversed ); | ||
| 576 | return if ( not $first and $reversed ); | ||
| 577 | } | ||
| 578 | else { | ||
| 579 | return unless $reversed; | ||
| 580 | } | ||
| 581 | my $s = $a->pos + 1; | ||
| 582 | my $e = $a->calend; | ||
| 583 | return | ||
| 584 | unless ( ( $s >= $data->{start} and $s <= $data->{stop} ) | ||
| 585 | or ( $e >= $data->{start} and $e <= $data->{stop} ) ); | ||
| 586 | if ($reversed) { | ||
| 587 | $data->{'index'}{$e}++; | ||
| 588 | } | ||
| 589 | else { | ||
| 590 | $data->{'index'}{$s}++; | ||
| 591 | } | ||
| 592 | } | ||
| 593 | |||
| 594 | sub _reverse_precise_count_indexed { | ||
| 595 | my ( $a, $data ) = @_; | ||
| 596 | my $flag = $a->flag; | ||
| 597 | return if $flag & $UNWANTED_FLAGS; | ||
| 598 | return if $a->qual < $MAPQ; | ||
| 599 | my $reversed = $flag & 0x10; # reversed; | ||
| 600 | if ( $flag & 0x1 ) { | ||
| 601 | |||
| 602 | # paired | ||
| 603 | my $first = $a->flag & 0x40; # true if FIRST_MATE | ||
| 604 | return if ( $first and not $reversed ); | ||
| 605 | return if ( not $first and $reversed ); | ||
| 606 | } | ||
| 607 | else { | ||
| 608 | return unless $reversed; | ||
| 609 | } | ||
| 610 | my $s = $a->pos + 1; | ||
| 611 | return unless ( $s >= $data->{start} and $a->calend <= $data->{stop} ); | ||
| 612 | if ($reversed) { | ||
| 613 | $data->{'index'}{$s}++; | ||
| 614 | } | ||
| 615 | else { | ||
| 616 | $data->{'index'}{ $a->calend }++; | ||
| 617 | } | ||
| 618 | } | ||
| 619 | |||
| 620 | sub _reverse_name_indexed { | ||
| 621 | my ( $a, $data ) = @_; | ||
| 622 | my $flag = $a->flag; | ||
| 623 | return if $flag & $UNWANTED_FLAGS; | ||
| 624 | return if $a->qual < $MAPQ; | ||
| 625 | my $reversed = $flag & 0x10; # reversed; | ||
| 626 | if ( $flag & 0x1 ) { | ||
| 627 | |||
| 628 | # paired | ||
| 629 | my $first = $a->flag & 0x40; # true if FIRST_MATE | ||
| 630 | return if ( $first and not $reversed ); | ||
| 631 | return if ( not $first and $reversed ); | ||
| 632 | } | ||
| 633 | else { | ||
| 634 | return unless $reversed; | ||
| 635 | } | ||
| 636 | my $s = $a->pos + 1; | ||
| 637 | my $e = $a->calend; | ||
| 638 | return | ||
| 639 | unless ( ( $s >= $data->{start} and $s <= $data->{stop} ) | ||
| 640 | or ( $e >= $data->{start} and $e <= $data->{stop} ) ); | ||
| 641 | if ($reversed) { | ||
| 642 | $data->{'index'}{$e} ||= []; | ||
| 643 | push @{ $data->{'index'}{$e} }, $a->qname; | ||
| 644 | } | ||
| 645 | else { | ||
| 646 | $data->{'index'}{$s} ||= []; | ||
| 647 | push @{ $data->{'index'}{$s} }, $a->qname; | ||
| 648 | } | ||
| 649 | } | ||
| 650 | |||
| 651 | sub _reverse_count_array { | ||
| 652 | my ( $a, $data ) = @_; | ||
| 653 | my $flag = $a->flag; | ||
| 654 | return if $flag & $UNWANTED_FLAGS; | ||
| 655 | return if $a->qual < $MAPQ; | ||
| 656 | my $reversed = $flag & 0x10; # reversed; | ||
| 657 | if ( $flag & 0x1 ) { | ||
| 658 | |||
| 659 | # paired | ||
| 660 | my $first = $a->flag & 0x40; # true if FIRST_MATE | ||
| 661 | return if ( $first and not $reversed ); | ||
| 662 | return if ( not $first and $reversed ); | ||
| 663 | } | ||
| 664 | else { | ||
| 665 | return unless $reversed; | ||
| 666 | } | ||
| 667 | my $s = $a->pos + 1; | ||
| 668 | my $e = $a->calend; | ||
| 669 | return | ||
| 670 | unless ( ( $s >= $data->{start} and $s <= $data->{stop} ) | ||
| 671 | or ( $e >= $data->{start} and $e <= $data->{stop} ) ); | ||
| 672 | push @{ $data->{scores} }, 1; | ||
| 673 | } | ||
| 674 | |||
| 675 | sub _reverse_precise_count_array { | ||
| 676 | my ( $a, $data ) = @_; | ||
| 677 | my $flag = $a->flag; | ||
| 678 | return if $flag & $UNWANTED_FLAGS; | ||
| 679 | return if $a->qual < $MAPQ; | ||
| 680 | my $reversed = $flag & 0x10; # reversed; | ||
| 681 | if ( $flag & 0x1 ) { | ||
| 682 | |||
| 683 | # paired | ||
| 684 | my $first = $a->flag & 0x40; # true if FIRST_MATE | ||
| 685 | return if ( $first and not $reversed ); | ||
| 686 | return if ( not $first and $reversed ); | ||
| 687 | } | ||
| 688 | else { | ||
| 689 | return unless $reversed; | ||
| 690 | } | ||
| 691 | return unless ( $a->pos + 1 >= $data->{start} and $a->calend <= $data->{stop} ); | ||
| 692 | push @{ $data->{scores} }, 1; | ||
| 693 | } | ||
| 694 | |||
| 695 | sub _reverse_name_array { | ||
| 696 | my ( $a, $data ) = @_; | ||
| 697 | my $flag = $a->flag; | ||
| 698 | return if $flag & $UNWANTED_FLAGS; | ||
| 699 | return if $a->qual < $MAPQ; | ||
| 700 | my $reversed = $flag & 0x10; # reversed; | ||
| 701 | if ( $flag & 0x1 ) { | ||
| 702 | |||
| 703 | # paired | ||
| 704 | my $first = $a->flag & 0x40; # true if FIRST_MATE | ||
| 705 | return if ( $first and not $reversed ); | ||
| 706 | return if ( not $first and $reversed ); | ||
| 707 | } | ||
| 708 | else { | ||
| 709 | return unless $reversed; | ||
| 710 | } | ||
| 711 | my $s = $a->pos + 1; | ||
| 712 | my $e = $a->calend; | ||
| 713 | return | ||
| 714 | unless ( ( $s >= $data->{start} and $s <= $data->{stop} ) | ||
| 715 | or ( $e >= $data->{start} and $e <= $data->{stop} ) ); | ||
| 716 | push @{ $data->{scores} }, $a->qname; | ||
| 717 | } | ||
| 718 | |||
| 719 | 1; | ||
| 720 | |||
| 721 | __END__ |