blib/lib/Bio/Cigar.pm

filter: Statementstmt 98.8% 89 / 90 Branchbran 86.9% 40 / 46 Conditioncond 76.4% 13 / 17 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 17 / 17 Podpod 80.0% 4 / 5 totaltotal 90.8% 169 / 186 CC 32 SCAR 25.8
CC
32
Cov
93%
_map_position
24.4
__ANON__
3.2
__ANON__
3.0
CRAP 13.2
↑ Summary
1 package Bio::Cigar;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =encoding utf-8
8  
9 =head1 NAME
10  
11 Bio::Cigar - Parse CIGAR strings and translate coordinates to/from reference/query
12  
13 =head1 SYNOPSIS
14  
15 use 5.014;
16 use Bio::Cigar;
17 my $cigar = Bio::Cigar->new("2M1D1M1I4M");
18 say "Query length is ", $cigar->query_length;
19 say "Reference length is ", $cigar->reference_length;
20
21 my ($qpos, $op) = $cigar->rpos_to_qpos(3);
22 say "Alignment operation at reference position 3 is $op";
23  
24 =head1 DESCRIPTION
25  
26 Bio::Cigar is a small library to parse CIGAR strings ("Compact Idiosyncratic
27 Gapped Alignment Report"), such as those used in the SAM file format. CIGAR
28 strings minimally describe the alignment of a query sequence to an (often
29 longer) reference sequence.
30  
31 Parsing follows the L<SAM v1 spec|http://samtools.github.io/hts-specs/SAMv1.pdf>
32 for the C<CIGAR> column.
33  
34 Parsed strings are represented by an object that provides a few utility
35 methods.
36  
37 =head1 ATTRIBUTES
38  
39 All attributes are read-only.
40  
41 =head2 string
42  
43 The CIGAR string for this object.
44  
45 =head2 reference_length
46  
47 The length of the reference sequence segment aligned with the query sequence
48 described by the CIGAR string.
49  
50 =head2 query_length
51  
52 The length of the query sequence described by the CIGAR string.
53  
54 =head2 ops
55  
56 An arrayref of C<[length, operation]> tuples describing the CIGAR string.
57 Lengths are integers, L<possible operations are below|/"CIGAR operations">.
58  
59 =cut
60  
61 1 our $VERSION = '1.00';
62  
63 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use Types::Standard qw< ArrayRef Tuple Int Enum StrMatch >;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use List::Util qw< sum >;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use Carp qw< croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 =head3 CIGAR operations
70  
71 The CIGAR operations are given in the following table, taken from the SAM v1
72 spec:
73
74 Op Description
75 ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
76 M alignment match (can be a sequence match or mismatch)
77 I insertion to the reference
78 D deletion from the reference
79 N skipped region from the reference
80 S soft clipping (clipped sequences present in SEQ)
81 H hard clipping (clipped sequences NOT present in SEQ)
82 P padding (silent deletion from padded reference)
83 = sequence match
84 X sequence mismatch
85  
86 • H can only be present as the first and/or last operation.
87 • S may only have H operations between them and the ends of the string.
88 • For mRNA-to-genome alignment, an N operation represents an intron.
89 For other types of alignments, the interpretation of N is not defined.
90 • Sum of the lengths of the M/I/S/=/X operations shall equal the length of SEQ.
91  
92 =cut
93  
94 1 our $CIGAR_REGEX = qr/^
95 (\d*H)?(\d*S)?
96 (?<OP>\d*[MIDNP=X])*
97 (\d*S)?(\d*H)?
98 $/xa;
99  
100 1 has 'string',
101 is => 'ro',
102 isa => StrMatch[ $CIGAR_REGEX ],
103 required => 1;
104  
105 has 'query_length',
106 lazy => 1,
107 is => 'ro',
108 isa => Int,
109 3 ▶ default => sub { sum(map { $_ || 1 } $_[0]->string =~ /(\d*)[MIS=X]/ga) || 0 };
Subroutine __ANON__: called CC=3 SCAR=11.7
Branch: 2 branches
BranchTrueFalseTotal
&sum(map({$_ or 1;} $_[0]->string =~ /(\d*)[MIS=X]/ag)) or 0 0 3 3
$_ or 1 0 9 9
110  
111 has 'reference_length',
112 lazy => 1,
113 is => 'ro',
114 isa => Int,
115 3 ▶ default => sub { sum(map { $_ || 1 } $_[0]->string =~ /(\d*)[MDN=X]/ga) || 0 };
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 2 branches
BranchTrueFalseTotal
&sum(map({$_ or 1;} $_[0]->string =~ /(\d*)[MDN=X]/ag)) or 0 0 3 3
$_ or 1 1 7 8
116  
117 1 has 'ops',
118 lazy => 1,
119 is => 'ro',
120 isa => ArrayRef[ Tuple[ Int, Enum[split '', 'MIDNSHP=X'] ] ],
121 builder => '_parse';
122  
123 sub BUILDARGS {
124 13 ▶ my $class = shift;
Subroutine BUILDARGS: called CC=3 SCAR=11.0
Pod undocumented
125 13 ▶ if (@_ == 1 and not ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 12 1 13
Condition66%2/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
1012
MC/DC50%1/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
126 12 return { string => $_[0] };
127 } else {
128 1 croak sprintf "%s->new must be called with a string as the sole argument", __PACKAGE__;
129 }
130 }
131  
132 sub _parse {
133 3 ▶ my $self = shift;
Subroutine _parse: called CC=3 SCAR=11.0
134 3 my $cigar = $self->string;
135 3 my @ops;
136 3 for my $op (grep defined, $cigar =~ /(\d*[MIDNSHP=X])/g) {
137 12 my ($len, $type) = $op =~ /(\d*)(\D*)/a;
138 12 ▶ push @ops, [ $len || 1, uc $type ];
Condition100%2/2
$len || 1
l!l
111
MC/DC100%1/1
$len || 1
$len
Truth table100%2/2
A
A= $len
Aresult
11
00
139 }
140 3 return \@ops;
141 }
142  
143 =head1 CONSTRUCTOR
144  
145 =head2 new
146  
147 Takes a CIGAR string as the sole argument and returns a new Bio::Cigar object.
148  
149 =head1 METHODS
150  
151 =head2 rpos_to_qpos
152  
153 Takes a reference position (origin 1, base-numbered) and returns the
154 corresponding position (origin 1, base-numbered) on the query sequence. Indels
155 affect how the numbering maps from reference to query.
156  
157 In list context returns a tuple of C<[query position, operation at position]>.
158 Operation is a single-character string. See the
159 L<table of CIGAR operations|/"CIGAR operations">.
160  
161 If the reference position does not map to the query sequence (as with a
162 deletion, for example), returns C<undef> or C<[undef, operation]>.
163  
164 =head2 qpos_to_rpos
165  
166 Takes a query position (origin 1, base-numbered) and returns the corresponding
167 position (origin 1, base-numbered) on the reference sequence. Indels affect
168 how the numbering maps from query to reference.
169  
170 In list context returns a tuple of C<[references position, operation at position]>.
171 Operation is a single-character string. See the
172 L<table of CIGAR operations|/"CIGAR operations">.
173  
174 If the query position does not map to the reference sequence (as with an
175 insertion, for example), returns C<undef> or C<[undef, operation]>.
176  
177 =head2 op_at_rpos
178  
179 Takes a reference position and returns the operation at that position. Simply
180 a shortcut for calling L</rpos_to_qpos> in list context and discarding the
181 first return value.
182  
183 =head2 op_at_qpos
184  
185 Takes a query position and returns the operation at that position. Simply
186 a shortcut for calling L</qpos_to_rpos> in list context and discarding the
187 first return value.
188  
189 =cut
190  
191 # Consumption table based on
192 # https://github.com/samtools/htslib/blob/develop/htslib/sam.h#L81-L100
193 1 my %op_consumes = (
194 # op => [query, reference]
195 'M' => [1, 1],
196 'I' => [1, 0],
197 'D' => [0, 1],
198 'N' => [0, 1],
199 'S' => [1, 0],
200 'H' => [0, 0],
201 'P' => [0, 0],
202 '=' => [1, 1],
203 'X' => [1, 1],
204 );
205  
206 sub rpos_to_qpos {
207 38 ▶ my $self = shift;
Subroutine rpos_to_qpos: called CC=1 SCAR=0.0
Pod documented
208 38 return $self->_map_position( rpos => @_ );
209 }
210  
211 sub qpos_to_rpos {
212 39 ▶ my $self = shift;
Subroutine qpos_to_rpos: called CC=1 SCAR=0.0
Pod documented
213 39 return $self->_map_position( qpos => @_ );
214 }
215  
216 sub _map_position {
217 77 ▶ my $self = shift;
Subroutine _map_position: called CC=24 SCAR=31.9
218 77 my $from = shift;
219 77 my $target = shift;
220 77 ▶ my $target_len = $from eq "rpos" ? "reference_length" : "query_length";
Branch: 1 branch
BranchTrueFalseTotal
$from eq 'rpos' ? : 38 39 77
221  
222 77 my $rpos = 0;
223 77 my $qpos = 0;
224  
225 77 ▶ croak sprintf "$from = %d is < 1 or > $target_len (%d)", $target, $self->$target_len
Branch: 1 branch
BranchTrueFalseTotal
if $target < 1 or $target > $self->$target_len 5 72 77
Condition100%3/3
$target < 1 or $target > $self->$target_len
l!l&&r!l&&!r
2372
MC/DC100%2/2
$target < 1 or $target > $self->$target_len
$target < 1 $target > $self->$target_len
Truth table100%3/3
A or B
A= $target < 1B= $target > $self->$target_len
ABresult
1X1
011
000
226 if $target < 1 or $target > $self->$target_len;
227  
228 # Each cigar operation consumes the reference, query, or both. We keep
229 # track of both positions until the query/reference position hits the
230 # target. Then we return the equivalent other position.
231  
232 72 for my $op (@{ $self->ops }) {
233 222 my ($len, $type) = @$op;
234 222 my $consumes = $op_consumes{$type};
235 222 ▶ next unless $consumes->[0] or $consumes->[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $consumes->[0] or $consumes->[1] 0 222 222
Condition66%2/3
$consumes->[0] or $consumes->[1]
l!l&&r!l&&!r
195270
MC/DC0%0/2
$consumes->[0] or $consumes->[1]
$consumes->[0] $consumes->[1]
Truth table66%2/3
A or B
A= $consumes->[0]B= $consumes->[1]
ABresult
1X1
011
000
236  
237 222 ▶ $qpos += $len if $consumes->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $consumes->[0] 195 27 222
238 222 ▶ $rpos += $len if $consumes->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $consumes->[1] 150 72 222
239  
240 # The logic could be written with more variables to reduce the
241 # duplication in this if/else, but I think the clarity of the logic is
242 # enhanced by the similarity of the if/else branches. Adding more
243 # varibles to make the logic generic muddies the simplicity.
244 222 ▶ if ($from eq "rpos") {
Branch: 1 branch
BranchTrueFalseTotal
if ($from eq 'rpos') { } 114 108 222
245 114 ▶ if ($rpos > $target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rpos > $target) 21 93 114
246 21 my $overshoot = $rpos - $target;
247 21 $rpos -= $overshoot;
248 21 $qpos -= $overshoot;
249 }
250 114 ▶ if ($rpos == $target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rpos == $target) 36 78 114
251 36 ▶ if ($consumes->[1] and not $consumes->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($consumes->[1] and not $consumes->[0]) { } 3 33 36
Condition66%2/3
$consumes->[1] and not $consumes->[0]
!ll&&!rl&&r
0333
MC/DC50%1/2
$consumes->[1] and not $consumes->[0]
$consumes->[1] not $consumes->[0]
Truth table66%2/3
A and not(B)
A= $consumes->[1]B= not $consumes->[0]
ABresult
0X0
100
111
252 # Reference positions which are missing in the query sequence
253 # don't have a corresponding query position.
254 3 ▶ return wantarray ? (undef, $type) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
255 } else {
256 33 ▶ $qpos <= $self->query_length
Branch: 1 branch
BranchTrueFalseTotal
$qpos <= $self->query_length or croak(sprintf("Bug! Assertion failed: qpos <= qlen (target = %d, rpos = %d, qpos = %d, qlen = %d, cigar = %s)", $target, $rpos, $qpos, $self->query_length, $self->string)) 0 33 33
257 or croak sprintf "Bug! Assertion failed: qpos <= qlen"
258 . " (target = %d, rpos = %d, qpos = %d, qlen = %d, cigar = %s)",
259 $target, $rpos, $qpos, $self->query_length, $self->string;
260 33 ▶ return wantarray ? ($qpos, $type) : $qpos;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 22 11 33
261 }
262 }
263 } else {
264 108 ▶ if ($qpos > $target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qpos > $target) 18 90 108
265 18 my $overshoot = $qpos - $target;
266 18 $rpos -= $overshoot;
267 18 $qpos -= $overshoot;
268 }
269 108 ▶ if ($qpos == $target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qpos == $target) 36 72 108
270 36 ▶ if ($consumes->[0] and not $consumes->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($consumes->[0] and not $consumes->[1]) { } 12 24 36
Condition66%2/3
$consumes->[0] and not $consumes->[1]
!ll&&!rl&&r
02412
MC/DC50%1/2
$consumes->[0] and not $consumes->[1]
$consumes->[0] not $consumes->[1]
Truth table66%2/3
A and not(B)
A= $consumes->[0]B= not $consumes->[1]
ABresult
0X0
100
111
271 # Query positions which are insertions in the reference sequence
272 # don't have a corresponding reference position.
273 12 ▶ return wantarray ? (undef, $type) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 4 12
274 } else {
275 24 ▶ $rpos <= $self->reference_length
Branch: 1 branch
BranchTrueFalseTotal
$rpos <= $self->reference_length or croak(sprintf("Bug! Assertion failed: rpos <= rlen (target = %d, qpos = %d, rpos = %d, rlen = %d, cigar = %s)", $target, $qpos, $rpos, $self->reference_length, $self->string)) 0 24 24
276 or croak sprintf "Bug! Assertion failed: rpos <= rlen"
277 . " (target = %d, qpos = %d, rpos = %d, rlen = %d, cigar = %s)",
278 $target, $qpos, $rpos, $self->reference_length, $self->string;
279 24 ▶ return wantarray ? ($rpos, $type) : $rpos;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 16 8 24
280 }
281 }
282 }
283 }
284 0 croak sprintf "Bug! Ran out of ops but couldn't map %s %d"
285 . " (rpos = %d, qpos = %d, cigar = %s)",
286 $from, $target, $rpos, $qpos, $self->string;
287 }
288  
289 sub op_at_rpos {
290 12 ▶ my $self = shift;
Subroutine op_at_rpos: called CC=1 SCAR=0.0
Pod documented
291 12 my ($qpos, $type) = $self->rpos_to_qpos(@_);
292 12 return $type;
293 }
294  
295 sub op_at_qpos {
296 12 ▶ my $self = shift;
Subroutine op_at_qpos: called CC=1 SCAR=0.0
Pod documented
297 12 my ($rpos, $type) = $self->qpos_to_rpos(@_);
298 12 return $type;
299 }
300  
301 =head1 AUTHOR
302  
303 Thomas Sibley E<lt>trsibley@uw.eduE<gt>
304  
305 =head1 COPYRIGHT
306  
307 Copyright 2014- Mullins Lab, Department of Microbiology, University of Washington.
308  
309 =head1 LICENSE
310  
311 This library is free software; you can redistribute it and/or modify it under
312 the GNU General Public License, version 2.
313  
314 =head1 SEE ALSO
315  
316 L<SAMv1 spec|http://samtools.github.io/hts-specs/SAMv1.pdf>
317  
318 =cut
319  
320 1 1;
↑ Summary