| 1 |
|
| 2 |
=head1 LICENSE |
| 3 |
|
| 4 |
Copyright [2015-2024] EMBL-European Bioinformatics Institute |
| 5 |
|
| 6 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
you may not use this file except in compliance with the License. |
| 8 |
You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
|
| 18 |
=head1 NAME |
| 19 |
|
| 20 |
Bio::DB::HTS::AlignWrapper -- Add high-level methods to Bio::DB::HTS::Alignment |
| 21 |
|
| 22 |
=head1 SYNOPSIS |
| 23 |
|
| 24 |
See L<Bio::DB::HTS::Alignment>. |
| 25 |
|
| 26 |
=head1 DESCRIPTION |
| 27 |
|
| 28 |
This is a wrapper around Bio::DB::HTS::Alignment that adds the |
| 29 |
following high-level methods. These are described in detail in |
| 30 |
L<Bio::DB::HTS::Alignment/High-level Bio::DB::HTS::Alignment methods>. |
| 31 |
|
| 32 |
add_segment() add a new subfeature to split alignments |
| 33 |
get_SeqFeatures() fetch subfeatures from split alignments |
| 34 |
split_splices() process cigar strings to produce split alignments |
| 35 |
expand_flags() return true if flags should be expanded into tags |
| 36 |
seq_id() return human-readable reference sequence name |
| 37 |
seq() return Bio::PrimarySeq object for reference sequence |
| 38 |
subseq() return a subsequence across the indicated range |
| 39 |
mate_seq_id() return human-readable mate reference sequence name |
| 40 |
dna() return the DNA of the reference sequence |
| 41 |
tam_line() return the text representation of the alignment |
| 42 |
attributes() synonym for get_tag_values() |
| 43 |
get_all_tags() return all tag names |
| 44 |
get_tag_values() return the values of the given tag |
| 45 |
has_tag() return true if the given tag is defined |
| 46 |
|
| 47 |
=head1 SEE ALSO |
| 48 |
|
| 49 |
L<Bio::Perl>, L<Bio::DB::HTS>, L<Bio::DB::HTS::Constants> |
| 50 |
|
| 51 |
=head1 AUTHOR |
| 52 |
|
| 53 |
Rishi Nag E<lt>[email protected]<gt> |
| 54 |
|
| 55 |
|
| 56 |
=cut |
| 57 |
|
| 58 |
package Bio::DB::HTS::AlignWrapper; |
| 59 |
$Bio::DB::HTS::AlignWrapper::VERSION = '3.02'; |
| 60 |
|
| 61 |
use strict; |
| 62 |
use warnings; |
| 63 |
|
| 64 |
use Bio::DB::HTS::Constants; |
| 65 |
|
| 66 |
our $AUTOLOAD; |
| 67 |
use Carp 'croak'; |
| 68 |
|
| 69 |
sub new { |
| 70 |
my $package = shift; |
| 71 |
my ( $align, $sam ) = @_; |
| 72 |
|
| 73 |
my $self = bless { sam => $sam, align => $align }, ref $package || $package; |
| 74 |
|
| 75 |
$self->add_segment( $self->split_splices ) |
| 76 |
if $sam->split_splices && $align->cigar_str =~ /N/; |
| 77 |
|
| 78 |
return $self; |
| 79 |
} |
| 80 |
|
| 81 |
sub AUTOLOAD { |
| 82 |
my ( $pack, $func_name ) = $AUTOLOAD =~ /(.+)::([^:]+)$/; |
| 83 |
return if $func_name eq 'DESTROY'; |
| 84 |
|
| 85 |
no strict 'refs'; |
| 86 |
$_[0] or die "autoload called for non-object symbol $func_name"; |
| 87 |
croak qq(Can't locate object method "$func_name" via package "$pack") |
| 88 |
unless $_[0]->{align}->can($func_name); |
| 89 |
|
| 90 |
*{"${pack}::${func_name}"} = sub { shift->{align}->$func_name(@_) }; |
| 91 |
|
| 92 |
shift->$func_name(@_); |
| 93 |
} |
| 94 |
|
| 95 |
sub score { shift->{align}->qual } |
| 96 |
|
| 97 |
sub can { |
| 98 |
my $self = shift; |
| 99 |
return 1 if $self->SUPER::can(@_); |
| 100 |
return ref $self && $self->{align}->can(@_); |
| 101 |
} |
| 102 |
|
| 103 |
sub add_segment { |
| 104 |
my $self = shift; |
| 105 |
my @subfeat = @_; |
| 106 |
$self->{segments} ||= []; |
| 107 |
push @{ $self->{segments} }, @subfeat; |
| 108 |
} |
| 109 |
|
| 110 |
sub get_SeqFeatures { |
| 111 |
my $self = shift; |
| 112 |
return unless $self->{segments}; |
| 113 |
return @{ $self->{segments} }; |
| 114 |
} |
| 115 |
|
| 116 |
sub split_splices { |
| 117 |
my $self = shift; |
| 118 |
my $cigar = $self->cigar_array; |
| 119 |
my @results; |
| 120 |
|
| 121 |
my $start = 0; |
| 122 |
my $end = 0; |
| 123 |
my $skip = 0; |
| 124 |
my $partial_cigar = ''; |
| 125 |
|
| 126 |
# in case sequence is missing? |
| 127 |
my $qseq = $self->qseq; |
| 128 |
$qseq ||= 'N' x $self->length; |
| 129 |
|
| 130 |
for my $op ( @$cigar, [ 'N', 0 ] ) { |
| 131 |
my ( $operation, $count ) = @$op; |
| 132 |
|
| 133 |
if ( $operation eq 'N' ) { |
| 134 |
my $s = $self->start + $start + $skip; |
| 135 |
my $e = $self->start + $end - 1 + $skip; |
| 136 |
my $f = Bio::DB::HTS::SplitAlignmentPart->new( |
| 137 |
-name => $self->display_name, |
| 138 |
-start => $s, |
| 139 |
-end => $e, |
| 140 |
-seq_id => $self->seq_id, |
| 141 |
-strand => $self->strand, |
| 142 |
-seq => [ $self, $start + $skip, $end - $start ] |
| 143 |
, # deferred rendering |
| 144 |
-type => $self->type ); |
| 145 |
|
| 146 |
$f->hit( -name => $self->display_name, |
| 147 |
-seq_id => $self->display_name, |
| 148 |
-start => $start + 1, |
| 149 |
-end => $end, |
| 150 |
-strand => $self->strand, |
| 151 |
-seq => substr( $qseq, $start, $end - $start ), ); |
| 152 |
$f->cigar_str($partial_cigar); |
| 153 |
$partial_cigar = ''; |
| 154 |
|
| 155 |
push @results, $f; |
| 156 |
$start += $end - $start; |
| 157 |
} ## end if ( $operation eq 'N') |
| 158 |
else { |
| 159 |
$partial_cigar .= "$operation$count"; |
| 160 |
} |
| 161 |
$end += $count if $operation =~ /^[MDSHP=X]/i; |
| 162 |
$skip += $count if $operation eq 'N'; |
| 163 |
if ( $operation eq 'H' and $start == 0 ) { |
| 164 |
$qseq = 'N' x $count . $qseq; |
| 165 |
} |
| 166 |
} ## end for my $op ( @$cigar, [...]) |
| 167 |
return @results; |
| 168 |
} ## end sub split_splices |
| 169 |
|
| 170 |
sub expand_flags { |
| 171 |
shift->{sam}->expand_flags(@_); |
| 172 |
} |
| 173 |
|
| 174 |
sub query { |
| 175 |
my $self = shift; |
| 176 |
return Bio::DB::HTS::Query->new($self); |
| 177 |
} |
| 178 |
|
| 179 |
sub target { |
| 180 |
my $self = shift; |
| 181 |
return Bio::DB::HTS::Target->new($self); |
| 182 |
} |
| 183 |
|
| 184 |
sub hit { shift->target(@_); } |
| 185 |
|
| 186 |
sub seq_id { |
| 187 |
my $self = shift; |
| 188 |
my $tid = $self->tid; |
| 189 |
return if $tid < 0 || $self->unmapped; |
| 190 |
$self->{sam}->target_name($tid); |
| 191 |
} |
| 192 |
|
| 193 |
sub mate_seq_id { |
| 194 |
my $self = shift; |
| 195 |
my $tid = $self->mtid; |
| 196 |
return if $tid < 0 || $self->munmapped; |
| 197 |
$self->{sam}->target_name($tid); |
| 198 |
} |
| 199 |
|
| 200 |
sub abs_ref { shift->seq_id } |
| 201 |
sub abs_start { shift->start } |
| 202 |
sub abs_end { shift->end } |
| 203 |
sub low { shift->start } |
| 204 |
sub high { shift->end } |
| 205 |
sub type { shift->primary_tag } |
| 206 |
sub method { shift->primary_tag } |
| 207 |
sub source { return shift->source_tag; } |
| 208 |
sub name { shift->qname } |
| 209 |
sub class { shift->primary_tag } |
| 210 |
|
| 211 |
sub seq { |
| 212 |
my $self = shift; |
| 213 |
my $dna = $self->dna; |
| 214 |
return Bio::PrimarySeq->new( -seq => $dna, -id => $self->seq_id ); |
| 215 |
} |
| 216 |
|
| 217 |
sub subseq { |
| 218 |
my $self = shift; |
| 219 |
my ( $start, $end ) = @_; |
| 220 |
$start = 1 if $start < 1; |
| 221 |
$end = $self->high if $end > $self->high; |
| 222 |
my $dna = $self->dna; |
| 223 |
return Bio::PrimarySeq->new( |
| 224 |
-seq => substr( $dna, $start - 1, $end - $start + 1 ) ); |
| 225 |
} |
| 226 |
|
| 227 |
sub padded_alignment { |
| 228 |
my $self = shift; |
| 229 |
my $cigar = $self->cigar_array; |
| 230 |
my $real_ref = 0; |
| 231 |
$real_ref = 1 if ( $self->{sam}->force_refseq || !$self->has_tag('MD') ); |
| 232 |
|
| 233 |
my $sdna = $self->dna; |
| 234 |
my $tdna = $self->query->dna; |
| 235 |
|
| 236 |
my ( $pad_source, $pad_target, $pad_match, $char_source, $char_target ); |
| 237 |
for my $event (@$cigar) { |
| 238 |
my ( $op, $count ) = @$event; |
| 239 |
if ( $op eq 'I' || $op eq 'S' ) { |
| 240 |
$pad_source .= '-' x $count; |
| 241 |
$pad_target .= substr( $tdna, 0, $count, '' ); |
| 242 |
$pad_match .= ' ' x $count; |
| 243 |
} |
| 244 |
elsif ( $op eq 'D' ) { |
| 245 |
$pad_source .= substr( $sdna, 0, $count, '' ); |
| 246 |
$pad_target .= '-' x $count; |
| 247 |
$pad_match .= ' ' x $count; |
| 248 |
} |
| 249 |
elsif ( $op eq 'N' ) { |
| 250 |
if ($real_ref) { |
| 251 |
$pad_source .= substr( $sdna, 0, $count, '' ); |
| 252 |
} |
| 253 |
else { |
| 254 |
$pad_source .= '-' x $count; |
| 255 |
} |
| 256 |
$pad_target .= '-' x $count; |
| 257 |
$pad_match .= ' ' x $count; |
| 258 |
} |
| 259 |
elsif ( $op eq 'P' ) { |
| 260 |
$pad_source .= '*' x $count; |
| 261 |
$pad_target .= '*' x $count; |
| 262 |
$pad_match .= ' ' x $count; |
| 263 |
} |
| 264 |
elsif ( $op eq 'H' ) { |
| 265 |
# nothing needs to be done in this case |
| 266 |
} |
| 267 |
else { # everything else is assumed to be a match |
| 268 |
while ( $count-- > 0 ) { |
| 269 |
$char_source = substr( $sdna, 0, 1, '' ); |
| 270 |
$char_target = substr( $tdna, 0, 1, '' ); |
| 271 |
$pad_source .= $char_source; |
| 272 |
$pad_target .= $char_target; |
| 273 |
$pad_match .= $char_source eq $char_target ? '|' : ' '; |
| 274 |
} |
| 275 |
} |
| 276 |
} ## end for my $event (@$cigar) |
| 277 |
return ( $pad_source, $pad_match, $pad_target ); |
| 278 |
} ## end sub padded_alignment |
| 279 |
|
| 280 |
sub dna { |
| 281 |
my $self = shift; |
| 282 |
|
| 283 |
my $sam = $self->{sam}; |
| 284 |
my $force = $sam->force_refseq; |
| 285 |
if ( !$force && ( my $md = $self->get_tag_values('MD') ) ) |
| 286 |
{ # try to use MD string |
| 287 |
my $qseq = $self->qseq; |
| 288 |
|
| 289 |
#preprocess qseq using cigar array |
| 290 |
my $cigar = $self->cigar_array; |
| 291 |
my $seq = ''; |
| 292 |
for my $op (@$cigar) { |
| 293 |
my ( $operation, $count ) = @$op; |
| 294 |
if ( $operation eq 'M' || $operation eq '=' || $operation eq 'X' ) { |
| 295 |
$seq .= substr( $qseq, 0, $count, '' ); # include these residues |
| 296 |
} |
| 297 |
elsif ( $operation eq 'S' or $operation eq 'I' ) { |
| 298 |
substr( $qseq, 0, $count, '' ) |
| 299 |
; # skip soft clipped and inserted residues |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
my $start = 0; |
| 304 |
my $result; |
| 305 |
while ( $md =~ /(\d+)|\^([gatcn]+)|([gatcn]+)/ig ) { |
| 306 |
if ( defined $1 ) { |
| 307 |
$result .= substr( $seq, $start, $1 ); |
| 308 |
$start += $1; |
| 309 |
} |
| 310 |
elsif ($2) { |
| 311 |
$result .= $2; |
| 312 |
} |
| 313 |
elsif ($3) { |
| 314 |
$result .= $3; |
| 315 |
$start += length $3; |
| 316 |
} |
| 317 |
} |
| 318 |
return $result; |
| 319 |
} ## end if ( !$force && ( my $md...)) |
| 320 |
|
| 321 |
else { |
| 322 |
return $self->{sam}->seq( $self->seq_id, $self->start, $self->end ); |
| 323 |
} |
| 324 |
} ## end sub dna |
| 325 |
|
| 326 |
sub tseq { |
| 327 |
shift->dna(@_); |
| 328 |
} |
| 329 |
|
| 330 |
sub attributes { |
| 331 |
my $self = shift; |
| 332 |
my $tag = shift; |
| 333 |
if ( defined $tag ) { |
| 334 |
return $self->get_tag_values($tag); |
| 335 |
} |
| 336 |
else { |
| 337 |
return map { $_ => $self->get_tag_values($_) } $self->get_all_tags; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
sub get_all_tags { |
| 342 |
my $self = shift; |
| 343 |
return $self->{align}->get_all_tags(@_) if $self->expand_flags; |
| 344 |
return ( $self->aux_keys, 'FLAGS' ); |
| 345 |
} |
| 346 |
|
| 347 |
sub get_tag_values { |
| 348 |
my $self = shift; |
| 349 |
my $tag = shift; |
| 350 |
defined $tag or return; |
| 351 |
|
| 352 |
return $self->{align}->get_tag_values($tag) if $self->expand_flags; |
| 353 |
if ( $tag eq 'FLAGS' ) { |
| 354 |
$self->flag_str; |
| 355 |
} |
| 356 |
else { |
| 357 |
$self->aux_get($tag); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
sub has_tag { |
| 362 |
my $self = shift; |
| 363 |
my $tag = shift; |
| 364 |
defined $tag or return; |
| 365 |
$self->{align}->get_tag_values($tag) if $self->expand_flags; |
| 366 |
if ( $tag eq 'FLAGS' ) { |
| 367 |
return 1; |
| 368 |
} |
| 369 |
else { |
| 370 |
my %keys = map { $_ => 1 } $self->aux_keys; |
| 371 |
return exists $keys{ uc $tag }; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
sub gff_string { shift->gff3_string(@_) } |
| 376 |
|
| 377 |
sub gff3_string { |
| 378 |
my $self = shift; |
| 379 |
my $recurse = shift; |
| 380 |
my $parent_id = shift; |
| 381 |
|
| 382 |
my $group = $self->format_attributes($parent_id); |
| 383 |
my $name = $self->name; |
| 384 |
my $id = $self->primary_id; |
| 385 |
|
| 386 |
my $class = $self->class; |
| 387 |
my $strand = ( '-', '.', '+' )[ $self->strand + 1 ]; |
| 388 |
my $p = join( "\t", |
| 389 |
$self->seq_id || '.', |
| 390 |
$self->source || '.', |
| 391 |
$self->method || '.', |
| 392 |
$self->start || '.', |
| 393 |
$self->stop || '.', |
| 394 |
defined( $self->score ) ? $self->score : '.', |
| 395 |
$strand || '.', |
| 396 |
defined( $self->phase ) ? $self->phase : '.', |
| 397 |
$group || '' ); |
| 398 |
my @rsf = $self->get_SeqFeatures; |
| 399 |
return join( "\n", $p, map { $_->gff3_string($id) } @rsf ); |
| 400 |
} |
| 401 |
|
| 402 |
sub phase { return } |
| 403 |
|
| 404 |
sub escape { |
| 405 |
my $self = shift; |
| 406 |
my $toencode = shift; |
| 407 |
$toencode =~ |
| 408 |
s/([^a-zA-Z0-9_.:?^*\(\)\[\]@!+-])/uc sprintf("%%%02x",ord($1))/eg; |
| 409 |
$toencode; |
| 410 |
} |
| 411 |
|
| 412 |
sub format_attributes { |
| 413 |
my $self = shift; |
| 414 |
my $parent_id = shift; |
| 415 |
|
| 416 |
my @tags = $self->get_all_tags; |
| 417 |
my @result; |
| 418 |
for my $t (@tags) { |
| 419 |
my @values = $self->each_tag_value($t); |
| 420 |
push @result, join '=', $self->escape($t), |
| 421 |
join( ',', map { $self->escape($_) } @values ) |
| 422 |
if @values; |
| 423 |
} |
| 424 |
my $id = $self->escape( $self->primary_id ); |
| 425 |
|
| 426 |
my $name = $self->display_name; |
| 427 |
unshift @result, "ID=" . $id if defined $id; |
| 428 |
unshift @result, "Parent=" . $parent_id if defined $parent_id; |
| 429 |
unshift @result, "Name=" . $self->escape($name) if defined $name; |
| 430 |
return join ';', @result; |
| 431 |
} |
| 432 |
|
| 433 |
sub tam_line { |
| 434 |
my $self = shift; |
| 435 |
return |
| 436 |
join( "\t", |
| 437 |
$self->qname, |
| 438 |
$self->flag, |
| 439 |
$self->tid >= 0 ? $self->{sam}->target_name( $self->tid ) : '*', |
| 440 |
$self->pos + 1, |
| 441 |
$self->qual, |
| 442 |
$self->cigar_str || '*', |
| 443 |
$self->mtid >= 0 ? ( $self->mtid == $self->tid ? '=' : |
| 444 |
$self->{sam}->target_name( $self->mtid ) ) : |
| 445 |
'*', |
| 446 |
$self->mpos + 1, |
| 447 |
$self->isize, |
| 448 |
$self->qseq, |
| 449 |
join( '', map { chr( $_ + 33 ) } $self->qscore ), |
| 450 |
$self->aux || () ); |
| 451 |
} |
| 452 |
|
| 453 |
package Bio::DB::HTS::SplitAlignmentPart; |
| 454 |
|
| 455 |
$Bio::DB::HTS::SplitAlignmentPart::VERSION = '3.02'; |
| 456 |
|
| 457 |
use base 'Bio::SeqFeature::Lite'; |
| 458 |
|
| 459 |
sub hit { |
| 460 |
my $self = shift; |
| 461 |
my $d = $self->{hit}; |
| 462 |
$self->{hit} = Bio::SeqFeature::Lite->new(@_) if @_; |
| 463 |
return $d; |
| 464 |
} |
| 465 |
|
| 466 |
sub seq { |
| 467 |
my $self = shift; |
| 468 |
my $seq = $self->{seq}; |
| 469 |
return $self->SUPER::seq() unless ref $seq; |
| 470 |
return substr( $seq->[0]->dna, $seq->[1], $seq->[2] ); |
| 471 |
} |
| 472 |
|
| 473 |
sub Bio::SeqFeature::Lite::subseq { |
| 474 |
my $self = shift; |
| 475 |
my ( $start, $end ) = @_; |
| 476 |
$start = 1 if $start < 1; |
| 477 |
$end = $self->high if $end > $self->high; |
| 478 |
return Bio::PrimarySeq->new( |
| 479 |
-seq => substr( $self->dna, $start - 1, $end - $start + 1 ) ); |
| 480 |
} |
| 481 |
|
| 482 |
sub cigar_str { |
| 483 |
my $self = shift; |
| 484 |
my $d = $self->{cigar_str}; |
| 485 |
$self->{cigar_str} = shift if @_; |
| 486 |
$d; |
| 487 |
} |
| 488 |
|
| 489 |
1; |