| 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 AUTHOR |
| 19 |
|
| 20 |
Rishi Nag E<lt>[email protected]<gt> |
| 21 |
|
| 22 |
=head1 NAME |
| 23 |
|
| 24 |
Bio::DB::HTS::Query -- Object representing the query portion of a BAM/SAM alignment |
| 25 |
|
| 26 |
=head1 SYNOPSIS |
| 27 |
|
| 28 |
Given an alignment retrieved from a Bio::DB::HTS database, |
| 29 |
|
| 30 |
my $query = $alignment->query; |
| 31 |
|
| 32 |
my $name = $query->display_name; |
| 33 |
my $start = $query->start; |
| 34 |
my $end = $query->end; |
| 35 |
my $dna = $query->dna; # dna string |
| 36 |
my $seq = $query->seq; # Bio::PrimarySeq object |
| 37 |
my @scores = $query->qscore; # quality score |
| 38 |
|
| 39 |
=head1 DESCRIPTION |
| 40 |
|
| 41 |
This is a simple Bio::SeqFeatureI object that represents the query |
| 42 |
part of a SAM alignment. |
| 43 |
|
| 44 |
=head2 Methods |
| 45 |
|
| 46 |
=over 4 |
| 47 |
|
| 48 |
=cut |
| 49 |
|
| 50 |
package Bio::DB::HTS::Query; |
| 51 |
$Bio::DB::HTS::Query::VERSION = '3.02'; |
| 52 |
|
| 53 |
use strict; |
| 54 |
use warnings; |
| 55 |
|
| 56 |
use Bio::DB::HTS; |
| 57 |
use Bio::DB::HTS::Constants |
| 58 |
qw(CIGAR_SYMBOLS BAM_CREF_SKIP BAM_CSOFT_CLIP BAM_CHARD_CLIP); |
| 59 |
|
| 60 |
use constant CIGAR_SKIP => { CIGAR_SYMBOLS->[BAM_CREF_SKIP] => 1, |
| 61 |
CIGAR_SYMBOLS->[BAM_CSOFT_CLIP] => 1, |
| 62 |
CIGAR_SYMBOLS->[BAM_CHARD_CLIP] => 1 }; |
| 63 |
|
| 64 |
sub new { |
| 65 |
my $self = shift; |
| 66 |
my $alignment = shift; |
| 67 |
bless \$alignment, ref $self || $self; |
| 68 |
} |
| 69 |
|
| 70 |
=item $seqid = $query->seq_id |
| 71 |
|
| 72 |
The name of the read. |
| 73 |
|
| 74 |
=cut |
| 75 |
|
| 76 |
sub seq_id { |
| 77 |
my $self = shift; |
| 78 |
$$self->qname; |
| 79 |
} |
| 80 |
|
| 81 |
=item $name = $query->name |
| 82 |
|
| 83 |
The read name (same as seq_id in this case). |
| 84 |
|
| 85 |
=cut |
| 86 |
|
| 87 |
sub name { |
| 88 |
my $self = shift; |
| 89 |
$$self->qname; |
| 90 |
} |
| 91 |
|
| 92 |
=item $name = $query->display_name |
| 93 |
|
| 94 |
The read display_name (same as seq_id in this case). |
| 95 |
|
| 96 |
=cut |
| 97 |
|
| 98 |
sub display_name { shift->name } |
| 99 |
|
| 100 |
=item $tag = $query->primary_tag |
| 101 |
|
| 102 |
The string "match". |
| 103 |
|
| 104 |
=cut |
| 105 |
|
| 106 |
sub primary_tag { ${ shift() }->primary_tag } |
| 107 |
|
| 108 |
=item $tag = $query->source_tag |
| 109 |
|
| 110 |
The string "sam/bam". |
| 111 |
|
| 112 |
=cut |
| 113 |
|
| 114 |
sub source_tag { ${ shift() }->source_tag } |
| 115 |
|
| 116 |
=item $start = $query->start |
| 117 |
|
| 118 |
The start of the match in read coordinates. |
| 119 |
|
| 120 |
=cut |
| 121 |
|
| 122 |
sub start { |
| 123 |
my $self = shift; |
| 124 |
return $self->low; |
| 125 |
} |
| 126 |
|
| 127 |
=item $end = $query->end |
| 128 |
|
| 129 |
The end of the match in read coordinates; |
| 130 |
|
| 131 |
=cut |
| 132 |
|
| 133 |
sub end { |
| 134 |
my $self = shift; |
| 135 |
return $self->high; |
| 136 |
} |
| 137 |
|
| 138 |
sub low { |
| 139 |
my $self = shift; |
| 140 |
my $cigar_arry = $$self->cigar_array; |
| 141 |
my $start = 1; |
| 142 |
for my $c (@$cigar_arry) { |
| 143 |
next if CIGAR_SYMBOLS->[BAM_CHARD_CLIP] eq $c->[0]; |
| 144 |
last unless CIGAR_SKIP->{ $c->[0] }; |
| 145 |
$start += $c->[1]; |
| 146 |
} |
| 147 |
$start; |
| 148 |
} |
| 149 |
|
| 150 |
sub high { |
| 151 |
my $self = shift; |
| 152 |
my $len = $$self->cigar2qlen; |
| 153 |
my $cigar_arry = $$self->cigar_array; |
| 154 |
|
| 155 |
# alignment stops at first non-clip CIGAR position |
| 156 |
my $i = $len - 1; |
| 157 |
for my $c ( reverse @$cigar_arry ) { |
| 158 |
next if CIGAR_SYMBOLS->[BAM_CHARD_CLIP] eq $c->[0]; |
| 159 |
last unless CIGAR_SKIP->{ $c->[0] }; |
| 160 |
$len -= $c->[1]; |
| 161 |
} |
| 162 |
return $len; |
| 163 |
} |
| 164 |
|
| 165 |
=item $len = $query->length |
| 166 |
|
| 167 |
The length of the read. |
| 168 |
|
| 169 |
=cut |
| 170 |
|
| 171 |
sub length { |
| 172 |
my $self = shift; |
| 173 |
$self->high - $self->low + 1; |
| 174 |
# $$self->cigar2qlen; |
| 175 |
} |
| 176 |
|
| 177 |
=item $seq = $query->seq |
| 178 |
|
| 179 |
A Bio::PrimarySeq representing the read sequence in REFERENCE |
| 180 |
orientation. |
| 181 |
|
| 182 |
=cut |
| 183 |
|
| 184 |
sub seq { |
| 185 |
my $self = shift; |
| 186 |
my $dna = $self->dna; |
| 187 |
return Bio::PrimarySeq->new( -seq => $dna, -id => $$self->qname ); |
| 188 |
} |
| 189 |
|
| 190 |
=item $scores = $query->qscore |
| 191 |
|
| 192 |
The read quality scores. In a list context, a list of integers equal |
| 193 |
in length to the read sequence length. In a scalar context, an array |
| 194 |
ref. The qscores are in REFERENCE sequence orientation. |
| 195 |
|
| 196 |
=cut |
| 197 |
|
| 198 |
sub qscore { |
| 199 |
my $self = shift; |
| 200 |
my @qscore = $$self->qscore; |
| 201 |
return wantarray ? @qscore : \@qscore; |
| 202 |
} |
| 203 |
|
| 204 |
=item $dna = $query->dna |
| 205 |
|
| 206 |
The DNA string in reference sequence orientation. |
| 207 |
|
| 208 |
=cut |
| 209 |
|
| 210 |
sub dna { |
| 211 |
my $self = shift; |
| 212 |
return $$self->qseq || ( 'N' x $self->length ); |
| 213 |
} |
| 214 |
|
| 215 |
=item $strand = $query->strand |
| 216 |
|
| 217 |
If the query was reversed to align it, -1. Otherwise +1. |
| 218 |
|
| 219 |
=cut |
| 220 |
|
| 221 |
sub strand { |
| 222 |
my $self = shift; |
| 223 |
return $$self->reversed ? -1 : 1; |
| 224 |
} |
| 225 |
|
| 226 |
=item $seq = $query->subseq($start,$end) |
| 227 |
|
| 228 |
Return a Bio::PrimarySeq object representing the requested subsequence |
| 229 |
on the read. |
| 230 |
|
| 231 |
=cut |
| 232 |
|
| 233 |
sub subseq { |
| 234 |
my $self = shift; |
| 235 |
my ( $start, $end ) = @_; |
| 236 |
$start = 1 if $start < 1; |
| 237 |
$end = $self->high if $end > $self->high; |
| 238 |
( $end, $start ) = ( $start, $end ) if $start > $end; |
| 239 |
return Bio::PrimarySeq->new( |
| 240 |
-seq => substr( $self->dna, $start - 1, $end - $start + 1 ) ); |
| 241 |
} |
| 242 |
|
| 243 |
1; |
| 244 |
|
| 245 |
=back |
| 246 |
|
| 247 |
=head1 SEE ALSO |
| 248 |
|
| 249 |
L<Bio::Perl>, L<Bio::DB::HTS>, L<Bio::DB::HTS::Alignment>, L<Bio::DB::HTS::Constants> |
| 250 |
|
| 251 |
=cut |