lib/MS/Peptide.pm

filter: Statementstmt 90.5% 144 / 159 Branchbran 70.0% 49 / 70 Conditioncond 52.5% 21 / 40 MC/DCmcdc 27.2% 6 / 22 Subroutinesub 100% 26 / 26 Podpod 100% 18 / 18 totaltotal 78.8% 264 / 335 CC 61 SCAR 22.4
CC
61
Cov
80%
as_string
21.2
range
15.4
has_mod
12.0
CRAP 9.4
« lib/MS/Reader.pm ↑ Summary lib/MS/Protein.pm »
1 package MS::Peptide;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 use overload
7 19 ▶ '""' => sub{ $_[0]->as_string(fmt => 'original') },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8 3 fallback => 1;
9  
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Storable qw/dclone/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use List::Util qw/sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use MS::Mass qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 my %light = (
17 '2H' => 'H',
18 '13C' => 'C',
19 '15N' => 'N',
20 '18O' => 'O',
21 );
22  
23 3 my %heavy = (
24 'H' => '2H',
25 'C' => '13C',
26 'N' => '15N',
27 'O' => '18O',
28 );
29  
30  
31 198 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32  
33 198 my ($class, $seq, %args) = @_;
34  
35 198 $seq = uc $seq;
36 198 my $self = bless {seq => $seq} => $class;
37  
38 198 my @atoms = map {atoms('aa', $_)} (split '', $seq);
39  
40 198 $self->{atoms} = \@atoms;
41 198 $self->{length} = CORE::length $seq;
42 198 $self->{n_mod} = 0;
43 198 $self->{c_mod} = 0;
44  
45 # these can be undefined if not known or specified
46 198 $self->{prev} = $args{prev};
47 198 $self->{next} = $args{next};
48 198 $self->{start} = $args{start};
49 198 $self->{end} = $args{end};
50  
51 198 return $self;
52  
53 }
54  
55 #----------------------------------------------------------------------------#
56 # accessors
57 #----------------------------------------------------------------------------#
58  
59 786 ▶sub length { return $_[0]->{length} }
Subroutine length: called CC=1 SCAR=0.0
Pod documented
60 589 ▶sub seq { return $_[0]->{seq} }
Subroutine seq: called CC=1 SCAR=0.0
Pod documented
61  
62 #----------------------------------------------------------------------------#
63 # accessors/modifiers
64 #----------------------------------------------------------------------------#
65  
66 2 ▶sub prev {
Subroutine prev: called CC=2 SCAR=6.9
Pod documented
67  
68 2 my ($self, $new_val) = @_;
69 2 ▶ $self->{prev} = $new_val if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 1 1 2
70 2 return $self->{prev};
71  
72 }
73  
74 2 ▶sub next {
Subroutine next: called CC=2 SCAR=6.9
Pod documented
75  
76 2 my ($self, $new_val) = @_;
77 2 ▶ $self->{next} = $new_val if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 1 1 2
78 2 return $self->{next};
79  
80 }
81  
82 3 ▶sub start {
Subroutine start: called CC=3 SCAR=11.0
Pod documented
83  
84 3 my ($self,$new_val) = @_;
85 3 ▶ if (defined $new_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_val) 2 1 3
86 2 ▶ croak "Value must be numeric\n"
Branch: 1 branch
BranchTrueFalseTotal
if $new_val =~ /\D/ 1 1 2
87 if ($new_val =~ /\D/);
88 1 $self->{start} = $new_val;
89 }
90 2 return $self->{start};
91  
92 }
93  
94 3 ▶sub end {
Subroutine end: called CC=3 SCAR=11.0
Pod documented
95  
96 3 my ($self,$new_val) = @_;
97 3 ▶ if (defined $new_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_val) 2 1 3
98 2 ▶ croak "Value must be numeric\n"
Branch: 1 branch
BranchTrueFalseTotal
if $new_val =~ /\D/ 1 1 2
99 if ($new_val =~ /\D/);
100 1 $self->{end} = $new_val;
101 }
102 2 return $self->{end};
103  
104 }
105  
106 #----------------------------------------------------------------------------#
107  
108 3 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
109  
110 3 return dclone($_[0]);
111  
112 }
113  
114 1 ▶sub residue_positions {
Subroutine residue_positions: called CC=2 SCAR=6.9
Pod documented
115  
116 1 my ($self, @res) = @_;
117  
118 1 my $res_str = join '', @res;
119 1 my @pos = ();
120 1 my $s = $self->seq;
121 1 ▶ while ($s =~ /[$res_str]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /[$res_str]/g) 3 1 4
122 3 push @pos, $-[0]+1;
123 }
124  
125 1 return \@pos;
126  
127 }
128  
129 196 ▶sub range {
Subroutine range: called CC=12 SCAR=27.3
Pod documented
130  
131 196 my ($self, $start, $end) = @_;
132  
133 196 ▶ croak "start must be less than or equal to end"
Branch: 1 branch
BranchTrueFalseTotal
if $start > $end 0 196 196
134 if ($start > $end);
135 196 ▶ croak "coordinate(s) out of range"
Branch: 1 branch
BranchTrueFalseTotal
if $start < 1 or $end < 1 or $start > $self->length or $end > $self->length 0 196 196
Condition33%3/9
$start < 1 or $end < 1 or $start > $self->length or $end > $self->length
l!l&&r!l&&!r
00196
$start < 1 or $end < 1 or $start > $self->length
l!l&&r!l&&!r
00196
$start < 1 or $end < 1
l!l&&r!l&&!r
00196
MC/DC0%0/4
$start < 1 or $end < 1 or $start > $self->length or $end > $self->length
$start < 1 $end < 1 $start > $self->length $end > $self->length
Truth table20%1/5
A or B or C or D
A= $start < 1B= $end < 1C= $start > $self->lengthD= $end > $self->length
ABCDresult
1XXX1
01XX1
001X1
00011
00000
136 if ($start < 1 || $end < 1
137 || $start > $self->length || $end > $self->length);
138  
139 196 my $seq = substr $self->seq, $start-1, $end-$start+1;
140 196 ▶ my $pep = MS::Peptide->new( $seq,
Branch: 2 branches
BranchTrueFalseTotal
$start == 1 ? : 3 193 196
$start == $self->length ? : 0 196 196
141 prev => ( $start == 1
142 ? ''
143 : substr $self->seq, $start-2, 1
144 ),
145 next => ( $start == $self->length
146 ? ''
147 : substr $self->seq, $end, 1
148 ),
149 start => $start,
150 end => $end,
151 );
152 196 ▶ if ($start == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start == 1) 3 193 196
153 3 $pep->{n_mod} = $self->{n_mod};
154 }
155 196 ▶ if ($end == $self->length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end == $self->length) 2 194 196
156 2 $pep->{c_mod} = $self->{c_mod};
157 }
158 196 for my $i ($start..$end) {
159 3504 my $i2 = $i - $start + 1;
160 3504 for my $mod ($self->get_mods($i)) {
161 0 $pep->add_mod( $i2, $mod );
162 }
163 }
164  
165 196 return $pep;
166  
167 }
168  
169  
170 1 ▶sub make_heavy {
Subroutine make_heavy: called CC=8 SCAR=22.8
Pod documented
171  
172 1 my ($self, $loc, $atom) = @_;
173  
174 # $loc and $atom can be scalar or arrayref
175 1 ▶ my @locs = ref($loc) ? @$loc : ($loc);
Branch: 1 branch
BranchTrueFalseTotal
ref $loc ? : 1 0 1
176 1 ▶ my @atoms = ref($atom) ? @$atom : ($atom);
Branch: 1 branch
BranchTrueFalseTotal
ref $atom ? : 0 1 1
177  
178 1 for my $i (@locs) { # 1-based residue
179  
180 croak "Residue index out of range\n"
181 10 ▶ if ( $i < 1 && $i > $self->{length});
Branch: 1 branch
BranchTrueFalseTotal
if $i < 1 and $i > $self->{"length"} 0 10 10
Condition33%1/3
$i < 1 and $i > $self->{"length"}
!ll&&!rl&&r
1000
MC/DC0%0/2
$i < 1 and $i > $self->{"length"}
$i < 1 $i > $self->{"length"}
Truth table33%1/3
A and B
A= $i < 1B= $i > $self->{"length"}
ABresult
0X0
100
111
182  
183 10 for my $a (@atoms) {
184 10 ▶ my $heavy = $heavy{$a} or croak "No heavy atom defined for $a\n";
Branch: 1 branch
BranchTrueFalseTotal
my $heavy = $heavy{$a} or croak("No heavy atom defined for $a\n") 10 0 10
185 $self->{atoms}->[$i-1]->{$heavy}
186 10 = delete $self->{atoms}->[$i-1]->{$a};
187 }
188 }
189  
190 1 return 1;
191  
192 }
193  
194 4 ▶sub mz {
Subroutine mz: called CC=3 SCAR=11.0
Pod documented
195  
196 4 my ($self, %args) = @_;
197  
198 4 ▶ my $type = $args{type} // 'mono';
Condition100%2/2
$args{"type"} // "mono"
l!l
22
MC/DC100%1/1
$args{"type"} // "mono"
$args{"type"}
Truth table100%2/2
A
A= $args{"type"}
Aresult
11
00
199 4 ▶ my $z = $args{charge} // 1;
Condition100%2/2
$args{"charge"} // 1
l!l
22
MC/DC100%1/1
$args{"charge"} // 1
$args{"charge"}
Truth table100%2/2
A
A= $args{"charge"}
Aresult
11
00
200  
201 my $M = formula_mass('H2O', $type)
202 4 + sum map {atoms_mass($_, $type)} @{ $self->{atoms} };
203  
204 4 return ($M + $z * elem_mass('H', $type))/$z;
205  
206 }
207  
208 3 ▶sub neutral_mass {
Subroutine neutral_mass: called CC=2 SCAR=6.9
Pod documented
209  
210 3 my ($self, %args) = @_;
211  
212 3 ▶ my $type = $args{type} // 'mono';
Condition100%2/2
$args{"type"} // "mono"
l!l
21
MC/DC100%1/1
$args{"type"} // "mono"
$args{"type"}
Truth table100%2/2
A
A= $args{"type"}
Aresult
11
00
213  
214 return formula_mass('H2O', $type)
215 3 + sum map {atoms_mass($_, $type)} @{ $self->{atoms} };
216  
217 }
218  
219 5 ▶sub add_mod {
Subroutine add_mod: called CC=10 SCAR=24.3
Pod documented
220  
221 5 my ($self, $loc, $mod) = @_;
222  
223 # $loc and $atom can be scalar or arrayref
224 5 ▶ my @locs = ref($loc) ? @$loc : ($loc);
Branch: 1 branch
BranchTrueFalseTotal
ref $loc ? : 1 4 5
225 5 ▶ my $atoms = atoms('mod' => $mod)
Condition100%2/2
atoms("mod", $mod) // croak("Bad modification\n")
l!l
41
MC/DC100%1/1
atoms("mod", $mod) // croak("Bad modification\n")
atoms("mod", $mod)
Truth table100%2/2
A
A= atoms("mod", $mod)
Aresult
11
00
226 // croak "Bad modification\n";
227  
228 4 for my $i (@locs) { # 1-based residue
229  
230 croak "Residue index out of range\n"
231 7 ▶ if ( $i < 1 || $i > $self->{length});
Branch: 1 branch
BranchTrueFalseTotal
if $i < 1 or $i > $self->{"length"} 1 6 7
Condition66%2/3
$i < 1 or $i > $self->{"length"}
l!l&&r!l&&!r
016
MC/DC50%1/2
$i < 1 or $i > $self->{"length"}
$i < 1 $i > $self->{"length"}
Truth table66%2/3
A or B
A= $i < 1B= $i > $self->{"length"}
ABresult
1X1
011
000
232  
233 6 for my $a (keys %$atoms) {
234
235 18 my $delta = $atoms->{$a};
236  
237 # removal of heavy atoms is a special case
238 18 ▶ if ( $delta < 0
Branch: 1 branch
BranchTrueFalseTotal
if ($delta < 0 and not exists $self->{"atoms"}[$i - 1]{$a} and exists $self->{"atoms"}[$i - 1]{$heavy{$a}}) 0 18 18
Condition33%2/6
$delta < 0 and not exists $self->{"atoms"}[$i - 1]{$a} and exists $self->{"atoms"}[$i - 1]{$heavy{$a}}
!ll&&!rl&&r
1800
$delta < 0 and not exists $self->{"atoms"}[$i - 1]{$a}
!ll&&!rl&&r
1800
MC/DC0%0/3
$delta < 0 and not exists $self->{"atoms"}[$i - 1]{$a} and exists $self->{"atoms"}[$i - 1]{$heavy{$a}}
$delta < 0 not exists $self->{"atoms"}[$i - 1]{$a} exists $self->{"atoms"}[$i - 1]{$heavy{$a}}
Truth table25%1/4
A and not(B) and C
A= $delta < 0B= not exists $self->{"atoms"}[$i - 1]{$a}C= exists $self->{"atoms"}[$i - 1]{$heavy{$a}}
ABCresult
0XX0
10X0
1100
1111
239 && ! exists $self->{atoms}->[$i-1]->{ $a }
240 && exists $self->{atoms}->[$i-1]->{ $heavy{$a} }
241 ) {
242 0 $a = $heavy{$a};
243 }
244  
245 18 $self->{atoms}->[$i-1]->{$a} += $delta;
246  
247 }
248  
249 6 ++$self->{mods}->[$i-1]->{$mod};
250
251 }
252  
253 3 return 1;
254  
255 }
256  
257 3504 ▶sub get_mods {
Subroutine get_mods: called CC=4 SCAR=17.9
Pod documented
258  
259 3504 my ($self, $loc) = @_;
260  
261 croak "Residue index out of range\n"
262 3504 ▶ if ( $loc < 1 && $loc > $self->{length});
Branch: 1 branch
BranchTrueFalseTotal
if $loc < 1 and $loc > $self->{"length"} 0 3504 3504
Condition33%1/3
$loc < 1 and $loc > $self->{"length"}
!ll&&!rl&&r
350400
MC/DC0%0/2
$loc < 1 and $loc > $self->{"length"}
$loc < 1 $loc > $self->{"length"}
Truth table33%1/3
A and B
A= $loc < 1B= $loc > $self->{"length"}
ABresult
0X0
100
111
263  
264 3504 ▶ return () if ! defined $self->{mods}->[$loc-1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"mods"}[$loc - 1] 3504 0 3504
265 0 return keys %{ $self->{mods}->[$loc-1] };
266  
267 }
268  
269 50 ▶sub has_mod {
Subroutine has_mod: called CC=9 SCAR=24.8
Pod documented
270  
271 50 my ($self, $loc, $mod) = @_;
272  
273 my @locs = ref($loc) ? @$loc
274 : defined $loc ? ($loc)
275 50 ▶ : (1..$self->{length});
Branch: 2 branches
BranchTrueFalseTotal
ref $loc ? : 0 50 50
defined $loc ? : 50 0 50
276  
277 50 ▶ my @mods = ref($mod) ? @$mod : ($mod);
Branch: 1 branch
BranchTrueFalseTotal
ref $mod ? : 0 50 50
278
279 50 my $ret_val = 0;
280  
281 50 for my $i (@locs) {
282  
283 croak "Residue index out of range\n"
284 50 ▶ if ( $i < 1 && $i > $self->{length});
Branch: 1 branch
BranchTrueFalseTotal
if $i < 1 and $i > $self->{"length"} 0 50 50
Condition33%1/3
$i < 1 and $i > $self->{"length"}
!ll&&!rl&&r
5000
MC/DC0%0/2
$i < 1 and $i > $self->{"length"}
$i < 1 $i > $self->{"length"}
Truth table33%1/3
A and B
A= $i < 1B= $i > $self->{"length"}
ABresult
0X0
100
111
285  
286 50 for my $m (@mods) {
287  
288 50 ▶ if (! defined $m) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $m) { } 50 0 50
289 50 $ret_val += defined $self->{mods}->[$i-1];
290 }
291 else {
292 0 $ret_val += defined $self->{mods}->[$i-1]->{$m};
293 }
294  
295 }
296  
297 }
298  
299 50 return $ret_val;
300  
301 }
302  
303  
304 25 ▶sub as_string {
Subroutine as_string: called CC=11 SCAR=30.6
Pod documented
305  
306 25 my ($self, %args) = @_;
307  
308 25 ▶ my $fmt = $args{fmt} // 'case';
Condition100%2/2
$args{"fmt"} // "case"
l!l
223
MC/DC100%1/1
$args{"fmt"} // "case"
$args{"fmt"}
Truth table100%2/2
A
A= $args{"fmt"}
Aresult
11
00
309 25 my $str;
310  
311 #------------------------------------------------------------------------#
312  
313 25 ▶ if ($fmt eq 'original') {
Branch: 3 branches
BranchTrueFalseTotal
if ($fmt eq 'original') { } 20 5 25
elsif ($fmt eq 'case') { } 5 0 5
elsif ($fmt eq 'deltas') { } 0 0 0
314  
315 20 $str = $self->{seq};
316  
317 }
318  
319 #------------------------------------------------------------------------#
320  
321 elsif ($fmt eq 'case') {
322  
323 5 my @aa = split '', $self->{seq};
324 $str = join '', map {
325 5 ▶ $self->has_mod($_+1) ? lc($aa[$_]) : $aa[$_]
Branch: 1 branch
BranchTrueFalseTotal
$self->has_mod($_ + 1) ? : 5 45 50
326 } 0..$#aa;
327  
328 }
329  
330 #------------------------------------------------------------------------#
331  
332 elsif ($fmt eq 'deltas') {
333  
334 0 my @aa = split '', $self->{seq};
335 0 my @mods = $self->mod_array;
336  
337 # move terminal mods to residues
338 0 my $n_mod = shift @mods;
339 0 my $c_mod = pop @mods;
340 0 $mods[0] += $n_mod;
341 0 $mods[-1] += $c_mod;
342  
343 0 for my $i (0..$#aa) {
344 0 $str .= $aa[$i];
345 0 my $delta = sprintf('%.0f', $mods[$i]);
346 0 ▶ $str .= "[$delta]" if ($delta != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $delta != 0 0 0 0
347 }
348  
349 }
350  
351 #------------------------------------------------------------------------#
352  
353 25 ▶ if ($args{adjacent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"adjacent"}) 3 22 25
354 croak "Can't form string - adjacent residues not defined"
355 3 ▶ if (! defined $self->{prev} || ! defined $self->{next});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"prev"} and defined $self->{"next"} 0 3 3
Condition33%1/3
defined $self->{"prev"} and defined $self->{"next"}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $self->{"prev"} and defined $self->{"next"}
defined $self->{"prev"} defined $self->{"next"}
Truth table33%1/3
A and B
A= defined $self->{"prev"}B= defined $self->{"next"}
ABresult
0X0
100
111
356 3 $str = "$self->{prev}.$str.$self->{next}";
357 }
358  
359 25 return $str;
360  
361 }
362  
363 1 ▶sub mod_array {
Subroutine mod_array: called CC=3 SCAR=11.0
Pod documented
364  
365 1 my ($self, $type) = @_;
366  
367 1 my @aa = split '', $self->{seq};
368 1 my @delta = (0) x $self->{length};
369 1 for my $i (0..$#aa) {
370 10 my $d = sprintf '%4f', atoms_mass($self->{atoms}->[$i], $type)
371 - aa_mass($aa[$i], $type);
372 10 ▶ $delta[$i] = $d == 0 ? 0 : $d;
Branch: 1 branch
BranchTrueFalseTotal
$d == 0 ? : 6 4 10
373 }
374 1 push @delta, $self->{n_mod};
375 1 unshift @delta, $self->{c_mod};
376 1 return @delta;
377  
378 }
379  
380  
381 3 1;
382  
383 __END__
« lib/MS/Reader.pm ↑ Summary lib/MS/Protein.pm »