lib/MS/Mass.pm

filter: Statementstmt 100% 75 / 75 Branchbran 95.4% 21 / 22 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 20 / 20 Podpod 100% 11 / 11 totaltotal 99.2% 137 / 138 CC 16 SCAR 10.4
CC
16
Cov
99%
_check_type
6.0
formula_mass
5.0
atoms_mass
3.0
CRAP 2.8
« lib/MS/Reader/MSn.pm ↑ Summary lib/MS/Reader/MGF/Spectrum.pm »
1 package MS::Mass;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use File::ShareDir qw/dist_file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our @EXPORT_OK = qw/
12 aa_mass
13 atoms
14 atoms_mass
15 brick_mass
16 db_version
17 elem_mass
18 formula_mass
19 list_bricks
20 mod_data
21 mod_id_to_name
22 mod_mass
23 /;
24  
25 4 our %EXPORT_TAGS = (
26 all => \@EXPORT_OK,
27 );
28  
29 4 my $unimod;
30  
31 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
32  
33 4 my $fn_unimod = dist_file('MS' => 'unimod.stor');
34 4 ▶ $unimod = retrieve $fn_unimod
Branch: 1 branch
BranchTrueFalseTotal
$unimod = retrieve($fn_unimod) or croak("failed to read Unimod database from storage: $@") 4 0 4
35 or croak "failed to read Unimod database from storage: $@";
36  
37 } #end BEGIN
38  
39  
40  
41 2265 ▶sub _check_type {
Subroutine _check_type: called CC=6 SCAR=17.9
42  
43 2265 my ($type) = @_;
44  
45 2265 ▶ return 'mono_mass' if (! defined $type);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 32 2233 2265
46 2233 ▶ return ($type eq 'mono' || $type eq 'mono_mass') ? 'mono_mass'
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'mono' || $type eq 'mono_mass' ? : 1278 955 2233
$type eq 'average' || $type eq 'avge_mass' ? : 954 1 955
Condition100%6/6
$type eq "mono" or $type eq "mono_mass"
l!l&&r!l&&!r
967311955
$type eq "average" or $type eq "avge_mass"
l!l&&r!l&&!r
910441
MC/DC100%2/2
$type eq "mono" or $type eq "mono_mass"
$type eq "mono" $type eq "mono_mass"
MC/DC100%2/2
$type eq "average" or $type eq "avge_mass"
$type eq "average" $type eq "avge_mass"
Truth table100%3/3
A or B
A= $type eq "mono"B= $type eq "mono_mass"
ABresult
1X1
011
000
Truth table100%3/3
A or B
A= $type eq "average"B= $type eq "avge_mass"
ABresult
1X1
011
000
47 : ($type eq 'average' || $type eq 'avge_mass') ? 'avge_mass'
48 : croak "Unexpected mass type (allowed: 'mono','average')\n";
49  
50 }
51  
52  
53 1 ▶sub db_version {
Subroutine db_version: called CC=1 SCAR=0.0
Pod documented
54 return $unimod->{db_version}
55 1 }
56  
57 2173 ▶sub _mass {
Subroutine _mass: called CC=2 SCAR=6.9
58 2173 my ($group, $name, $type) = @_;
59 2173 ▶ if (! defined $unimod->{$group}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $unimod->{$group}{$name}) 2 2171 2173
60 2 carp "Undefined $group $name";
61 2 return;
62 }
63 2171 $type = _check_type( $type );
64 2170 return $unimod->{$group}->{$name}->{$type};
65 }
66  
67 1802 ▶sub aa_mass { return _mass( 'aa', @_ ) };
Subroutine aa_mass: called CC=1 SCAR=0.0
Pod documented
68 2 ▶sub mod_mass { return _mass( 'mod', @_ ) };
Subroutine mod_mass: called CC=1 SCAR=0.0
Pod documented
69 15 ▶sub elem_mass { return _mass( 'elem', @_ ) };
Subroutine elem_mass: called CC=1 SCAR=0.0
Pod documented
70 1 ▶sub brick_mass { return _mass( 'brick', @_ ) };
Subroutine brick_mass: called CC=1 SCAR=0.0
Pod documented
71  
72 1 ▶sub mod_data {
Subroutine mod_data: called CC=1 SCAR=0.0
Pod documented
73 1 my ($mod) = @_;
74 1 return { %{$unimod->{mod}->{$mod}->{hashref}} };
75 }
76  
77 12 ▶sub formula_mass {
Subroutine formula_mass: called CC=5 SCAR=16.1
Pod documented
78  
79 12 my ($formula, $type) = @_;
80 12 $type = _check_type( $type );
81  
82 12 ▶ croak "unsupported characters in formula $formula"
Branch: 1 branch
BranchTrueFalseTotal
if $formula =~ /[^0-9A-Za-z]/ 1 11 12
83 if ($formula =~ /[^0-9A-Za-z]/);
84  
85 11 my $mass;
86 11 ▶ while ($formula =~ /([A-Z][a-z]?)(\d*)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($formula =~ /([A-Z][a-z]?)(\d*)/g) 21 10 31
87 21 my $single_mass = _mass('elem', $1, $type);
88 21 ▶ croak "mass not found for element $1" if (! defined $single_mass);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $single_mass 1 20 21
89 20 ▶ my $multiplier = $2 ? $2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 10 10 20
90 20 $mass += $single_mass * $multiplier;
91 }
92  
93 10 return $mass;
94  
95 }
96  
97 82 ▶sub atoms_mass {
Subroutine atoms_mass: called CC=3 SCAR=11.0
Pod documented
98  
99 82 my ($ref, $type) = @_;
100 82 $type = _check_type( $type );
101  
102 82 my $mass;
103 82 for (keys %$ref) {
104 332 my $single_mass = _mass('elem', $_, $type);
105 332 ▶ croak "mass not found for element $_" if (! defined $single_mass);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $single_mass 1 331 332
106 331 $mass += $single_mass * $ref->{$_};
107 }
108  
109 81 return $mass;
110  
111 }
112  
113 5321 ▶sub atoms {
Subroutine atoms: called CC=2 SCAR=6.9
Pod documented
114  
115 5321 my ($type,$name) = @_;
116 5321 ▶ if (! defined $unimod->{$type}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $unimod->{$type}{$name}) 2 5319 5321
117 2 carp "Undefined $type $name";
118 2 return;
119 }
120 5319 return { %{$unimod->{$type}->{$name}->{atoms}} };
121  
122 }
123  
124 1 ▶sub mod_id_to_name {
Subroutine mod_id_to_name: called CC=1 SCAR=0.0
Pod documented
125  
126 1 my ($id) = @_;
127 1 return $unimod->{mod_index}->{$id};
128  
129 }
130  
131 1 ▶sub list_bricks {
Subroutine list_bricks: called CC=2 SCAR=6.9
Pod documented
132  
133 1 my $str = "name\tfull_name\tmono_mass\tavg_mass\n";
134 1 my @names = sort {$a cmp $b} keys %{ $unimod->{brick} };
135 1 for (@names) {
136 56 my $ref = $unimod->{brick}->{$_};
137 $str .= join( "\t",
138 $_,
139 $ref->{full_name},
140 $ref->{mono_mass},
141 $ref->{avge_mass},
142 56 ) . "\n";
143 }
144  
145 1 return $str;
146  
147 }
148  
149 4 1;
150  
151  
152 __END__
« lib/MS/Reader/MSn.pm ↑ Summary lib/MS/Reader/MGF/Spectrum.pm »