GenOO/Data/File/SAM/Record.pm

filter: Statementstmt 91.0% 92 / 101 Branchbran 65.0% 13 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 27 / 30 Podpod 0.0% 0 / 20 totaltotal 77.1% 132 / 171 CC 12 SCAR 8.1
CC
12
Cov
87%
query_seq
6.1
is_secondary
6.0
_calculate_strand
3.0
CRAP 2.3
« GenOO/RegionCollection/Type/DoubleHashArray.pm ↑ Summary GenOO.pm »
1 # POD documentation - main docs before the code
2  
3 =head1 NAME
4  
5 GenOO::Data::File::SAM::Record - Object representing a record of a sam file
6  
7 =head1 SYNOPSIS
8  
9 # Object representing a record of a sam file
10  
11 # To initialize
12 my $sam_record = GenOO::Data::File::SAM::Record->new(
13 fields => [qname,flag, rname, pos, mapq, cigar,
14 rnext, pnext, tlen, seq, qual, tags]
15 );
16  
17  
18 =head1 DESCRIPTION
19  
20 This object represents a record of a sam file and offers methods for accessing the different attributes.
21 It implements several additional methods that transform original attributes in more manageable attributes.
22 eg. from the FLAG attribute the actual strand is extracted etc.
23  
24 =head1 EXAMPLES
25  
26 # Check if the record corresponds to a match
27 my $mapped = $sam_record->is_mapped;
28
29 # Check if the record corresponds to a non match
30 my $unmapped = $sam_record->is_unmapped;
31
32 # Parse the FLAG attribute and return 1 or -1 for the strand
33 my $strand = $sam_record->strand;
34  
35 =cut
36  
37 # Let the code begin...
38  
39 package GenOO::Data::File::SAM::Record;
40 1 $GenOO::Data::File::SAM::Record::VERSION = '1.4.9';
41  
42 #######################################################################
43 ####################### Load External modules #####################
44 #######################################################################
45 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50  
51 #######################################################################
52 ####################### Interface attributes ######################
53 #######################################################################
54 has 'fields' => (
55 traits => ['Array'],
56 is => 'ro',
57 isa => 'ArrayRef[Str]',
58 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 1 handles => {
60 all_fields => 'elements',
61 add_field => 'push',
62 map_fields => 'map',
63 filter_fields => 'grep',
64 find_field => 'first',
65 get_field => 'get',
66 join_fields => 'join',
67 count_fields => 'count',
68 has_fields => 'count',
69 has_no_fields => 'is_empty',
70 sorted_fields => 'sort',
71 },
72 required => 1,
73 );
74  
75 1 has 'tags' => (
76 is => 'ro',
77 builder => '_read_tags',
78 init_arg => undef,
79 lazy => 1,
80 );
81  
82 1 has 'alignment_length' => (
83 is => 'ro',
84 builder => '_calculate_alignment_length',
85 init_arg => undef,
86 lazy => 1,
87 );
88  
89 1 has 'start' => (
90 is => 'ro',
91 builder => '_calculate_start',
92 init_arg => undef,
93 lazy => 1,
94 );
95  
96 1 has 'stop' => (
97 is => 'ro',
98 builder => '_calculate_stop',
99 init_arg => undef,
100 lazy => 1,
101 );
102  
103 1 has 'strand' => (
104 is => 'ro',
105 builder => '_calculate_strand',
106 init_arg => undef,
107 lazy => 1,
108 );
109  
110 1 has 'copy_number' => (
111 is => 'ro',
112 default => 1,
113 lazy => 1
114 );
115  
116 1 has 'extra' => (
117 is => 'rw',
118 init_arg => undef,
119 );
120  
121  
122 #######################################################################
123 ########################## Consumed Roles #########################
124 #######################################################################
125 1 with
126 'GenOO::Region' => {
127 -alias => { mid_position => 'region_mid_position' },
128 -excludes => 'mid_position',
129 },
130 'GenOO::Data::File::SAM::CigarAndMDZ' => {
131 };
132  
133  
134  
135 #######################################################################
136 ######################## Interface Methods ########################
137 #######################################################################
138 sub qname { # String [!-?A-~]f1,255g Query template NAME
139 2 ▶ my ($self) = @_;
Subroutine qname: called CC=1 SCAR=0.0
Pod undocumented
140
141 2 return $self->fields->[0];
142 }
143  
144 sub flag { # Int [0,216-1] bitwise FLAG
145 2041 ▶ my ($self) = @_;
Subroutine flag: called CC=1 SCAR=0.0
Pod undocumented
146
147 2041 return $self->fields->[1];
148 }
149  
150 sub rname { # String \*|[!-()+-<>-~][!-~]* Reference sequence NAME
151 647 ▶ my ($self) = @_;
Subroutine rname: called CC=1 SCAR=0.0
Pod undocumented
152
153 647 return $self->fields->[2];
154 }
155  
156 sub pos { # Int [0,229-1] 1-based leftmost mapping POSition
157 48 ▶ my ($self) = @_;
Subroutine pos: called CC=1 SCAR=0.0
Pod undocumented
158
159 48 return $self->fields->[3];
160 }
161  
162 sub mapq { # Int [0,28-1] MAPping Quality
163 2 ▶ my ($self) = @_;
Subroutine mapq: called CC=1 SCAR=0.0
Pod undocumented
164
165 2 return $self->fields->[4];
166 }
167  
168 sub cigar { # String \*|([0-9]+[MIDNSHPX=])+ CIGAR string
169 196 ▶ my ($self) = @_;
Subroutine cigar: called CC=1 SCAR=0.0
Pod undocumented
170
171 196 return $self->fields->[5];
172 }
173  
174 sub rnext { # String \*|=|[!-()+-<>-~][!-~]* Ref. name of the mate/next segment
175 2 ▶ my ($self) = @_;
Subroutine rnext: called CC=1 SCAR=0.0
Pod undocumented
176
177 2 return $self->fields->[6];
178 }
179  
180 sub pnext { # Int [0,229-1] Position of the mate/next segment
181 2 ▶ my ($self) = @_;
Subroutine pnext: called CC=1 SCAR=0.0
Pod undocumented
182
183 2 return $self->fields->[7];
184 }
185  
186 sub tlen { # Int [-229+1,229-1] observed Template LENgth
187 2 ▶ my ($self) = @_;
Subroutine tlen: called CC=1 SCAR=0.0
Pod undocumented
188
189 2 return $self->fields->[8];
190 }
191  
192 sub seq { # String \*|[A-Za-z=.]+ segment SEQuence
193 29 ▶ my ($self) = @_;
Subroutine seq: called CC=1 SCAR=0.0
Pod undocumented
194
195 29 return $self->fields->[9];
196 }
197  
198 sub qual { # String [!-~]+ ASCII of Phred-scaled base QUALity+33
199 2 ▶ my ($self) = @_;
Subroutine qual: called CC=1 SCAR=0.0
Pod undocumented
200
201 2 return $self->fields->[10];
202 }
203  
204 sub sequence {
205 0 ▶ my ($self) = @_;
Subroutine sequence: not called CC=1 SCAR=6.9
Pod undocumented
206  
207 0 return $self->seq;
208 }
209  
210 sub query_seq {
211 8 ▶ my ($self) = @_;
Subroutine query_seq: called CC=5 SCAR=18.1
Pod undocumented
212
213 8 ▶ if (defined $self->strand) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->strand) { } 8 0 8
elsif ($self->is_unmapped) { } 0 0 0
214 8 ▶ if ($self->strand == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->strand == 1) { } 5 3 8
elsif ($self->strand == -1) { } 3 0 3
215 5 return $self->seq;
216 }
217 elsif ($self->strand == -1) {
218 3 my $seq = reverse($self->seq);
219 3 $seq =~ tr/ATGCUatgcu/TACGAtacga/;
220 3 return $seq;
221 }
222 }
223 elsif ($self->is_unmapped) {
224 0 return $self->seq;
225 }
226 else {
227 0 return undef;
228 }
229 }
230  
231 sub query_length {
232 19 ▶ my ($self) = @_;
Subroutine query_length: called CC=1 SCAR=0.0
Pod undocumented
233
234 19 return CORE::length($self->seq); # using seq to avoid costs of query_seq
235 }
236  
237 sub tag {
238 43 ▶ my ($self, $tag_id) = @_;
Subroutine tag: called CC=2 SCAR=7.1
Pod undocumented
239
240 43 ▶ if (defined $self->tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->tags) 43 0 43
241 43 return $self->tags->{$tag_id};
242 }
243 }
244  
245 sub mdz {
246 32 ▶ my ($self) = @_;
Subroutine mdz: called CC=1 SCAR=0.0
Pod undocumented
247
248 32 return $self->tag('MD:Z');
249 }
250  
251 sub to_string {
252 1 ▶ my ($self) = @_;
Subroutine to_string: called CC=1 SCAR=0.0
Pod undocumented
253
254 1 return $self->join_fields("\t");
255 }
256  
257 sub is_mapped {
258 1349 ▶ my ($self) = @_;
Subroutine is_mapped: called CC=2 SCAR=6.9
Pod undocumented
259
260 1349 ▶ if ($self->flag & 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->flag & 4) { } 337 1012 1349
261 337 return 0;
262 }
263 else {
264 1012 return 1;
265 }
266 }
267  
268 sub is_unmapped {
269 3 ▶ my ($self) = @_;
Subroutine is_unmapped: called CC=2 SCAR=6.9
Pod undocumented
270
271 3 ▶ if ($self->flag & 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->flag & 4) { } 1 2 3
272 1 return 1;
273 }
274 else {
275 2 return 0;
276 }
277 }
278  
279 sub is_secondary {
280 0 ▶ my ($self) = @_;
Subroutine is_secondary: not called CC=2 SCAR=17.9
Pod undocumented
281
282 0 ▶ if ($self->flag & 256) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->flag & 256) { } 0 0 0
283 0 return 1;
284 }
285 else {
286 0 return 0;
287 }
288 }
289  
290  
291 #######################################################################
292 ######################### Private methods ##########################
293 #######################################################################
294 sub _calculate_alignment_length {
295 6 ▶ my ($self) = @_;
Subroutine _calculate_alignment_length: called CC=1 SCAR=0.0
296
297 6 return $self->length;
298 }
299  
300 sub _calculate_start {
301 46 ▶ my ($self) = @_;
Subroutine _calculate_start: called CC=1 SCAR=0.0
302
303 46 return $self->pos - 1;
304 }
305  
306 sub _calculate_stop {
307 13 ▶ my ($self) = @_;
Subroutine _calculate_stop: called CC=1 SCAR=0.0
308
309 13 return $self->start + $self->M_count + $self->D_count + $self->N_count + $self->EQ_count + $self->X_count + $self->P_count - 1;
310 }
311  
312 sub _calculate_strand {
313 687 ▶ my ($self) = @_;
Subroutine _calculate_strand: called CC=3 SCAR=11.0
314
315 687 ▶ if ($self->flag & 16) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->flag & 16) { } 319 368 687
elsif ($self->is_mapped) { } 365 3 368
316 319 return -1;
317 }
318 elsif ($self->is_mapped) {
319 365 return 1;
320 }
321 else {
322 3 return undef;
323 }
324 }
325  
326 sub _read_tags {
327 28 ▶ my ($self) = @_;
Subroutine _read_tags: called CC=2 SCAR=6.9
328
329 28 my %tags;
330
331 28 my @tags_array = @{$self->fields}[11..$self->count_fields-1];
332 28 foreach my $tag_var (@tags_array) {
333 63 $tag_var =~ /([A-Za-z][A-Za-z0-9]):([AifZHB]):(.+)/;
334 63 my $tag = $1;
335 63 my $tag_type = $2;
336 63 my $tag_value = $3;
337 63 $tags{"$tag:$tag_type"} = $tag_value;
338 }
339
340 28 return \%tags;
341 }
342  
343  
344 #######################################################################
345 ############################ Finalize #############################
346 #######################################################################
347 1 __PACKAGE__->meta->make_immutable;
348  
349 1 1;
« GenOO/RegionCollection/Type/DoubleHashArray.pm ↑ Summary GenOO.pm »