MS
view release on metacpan or search on metacpan
bin/unimod2storable.pl view on Meta::CPAN
close $in;
my $ua = HTTP::Tiny->new();
ELEM:
for my $el (keys %elements) {
say STDERR "Fetching $el";
my $url = sprintf
"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/fastformula/%s/JSON?MaxRecords=1",
$elements{$el},
;
my $res = $ua->get($url);
if (! $res->{success}) {
warn "Failed to fetch data for $el: $res->{reason}\n";
next ELEM;
}
lib/MS/Mass.pm view on Meta::CPAN
use File::ShareDir qw/dist_file/;
use Exporter qw/import/;
our @EXPORT_OK = qw/
aa_mass
atoms
atoms_mass
brick_mass
db_version
elem_mass
formula_mass
list_bricks
mod_data
mod_id_to_name
mod_mass
/;
our %EXPORT_TAGS = (
all => \@EXPORT_OK,
);
lib/MS/Mass.pm view on Meta::CPAN
sub aa_mass { return _mass( 'aa', @_ ) };
sub mod_mass { return _mass( 'mod', @_ ) };
sub elem_mass { return _mass( 'elem', @_ ) };
sub brick_mass { return _mass( 'brick', @_ ) };
sub mod_data {
my ($mod) = @_;
return { %{$unimod->{mod}->{$mod}->{hashref}} };
}
sub formula_mass {
my ($formula, $type) = @_;
$type = _check_type( $type );
croak "unsupported characters in formula $formula"
if ($formula =~ /[^0-9A-Za-z]/);
my $mass;
while ($formula =~ /([A-Z][a-z]?)(\d*)/g) {
my $single_mass = _mass('elem', $1, $type);
croak "mass not found for element $1" if (! defined $single_mass);
my $multiplier = $2 ? $2 : 1;
$mass += $single_mass * $multiplier;
}
return $mass;
}
lib/MS/Mass.pm view on Meta::CPAN
=head1 NAME
MS::Mass - core functions for molecular mass calculations
=head1 SYNOPSIS
use MS::Mass qw/:all/;
use constant PROTON => elem_mass('H'); # 1.007825035
use constant WATER => formula_mass('H2O'); # 18.0105647
my $add_NQ = mod_mass('Deamidated', 'average'); # 0.9848
my $C_proline = atoms('aa' => 'P')->{C}; # 5
=head1 DESCRIPTION
C<MS::Mass> provides a set of core functions for use in calculating and
working with molecular mass values common in mass spectrometry. It is expected
lib/MS/Mass.pm view on Meta::CPAN
=item B<brick_mass> I<name> [I<type>]
use constant WATER => brick_mass('Water');
use constant WATER_AVG => brick_mass('Water', average');
Takes one required argument (the brick name) and one optional argument (the
mass value to return, either 'mono' or 'average') and returns the associated
mass value. By default, the monoisotopic mass is returned. See C<list_bricks>
for more details.
=item B<formula_mass> I<formula> [I<type>]
Takes one required argument (a string containing a chemical formula) and one
optional argument (the mass value to return, either 'mono' or 'average') and
returns the associated mass value. By default, the monoisotopic mass is
returned.
Formulas are case-sensitive, for obvious reasons. Currently grouping is not
supported. For example, to get the formula for Al2(SO4)3 you must flatten it
out:
my $al_sulf = formula_mass('Al2S3O12');
and not:
my $al_sulf = formula_mass('Al2(SO4)3'); # this gives an error
Support for more complicated formulas may be added in the future.
=item B<atoms_mass> I<hashref> [I<type>]
Takes one required argument (a hashref containing element counts) and one
optional argument (the mass value to return, either 'mono' or 'average') and
returns the associated mass value. By default, the monoisotopic mass is
returned.
This function was added to make it easy to recalculate masses based on
modifying the return reference from C<atoms>.
lib/MS/PepInfo.pm view on Meta::CPAN
}
sub calc_fragments {
my ($peptide,$mod_ref,$max_charge,$incl_nloss) = @_;
my $PROTON = elem_mass('H');
$max_charge = $max_charge // 1;
my @masses = map {aa_mass($_)} split('',$peptide);
unshift @masses, 0;
push @masses, formula_mass('HOH');
# check that mod array length equals mass array length
my $n_mass = scalar(@masses);
my $n_mods = scalar(@$mod_ref);
die "mod array len mismatch ($n_mass mass vs $n_mods mods)"
if ($n_mass ne $n_mods);
for (0..$#{$mod_ref}) {
my $mod = $mod_ref->[$_];
if (defined $mod) {
lib/MS/PepInfo.pm view on Meta::CPAN
my $term = $type =~ /^[y]$/ ? 'C' : 'N';
# calculate standard series
for my $i (1..$#masses-1) {
#for my $i ($charge..$#masses-1) {
#for my $i ($charge..$#masses-2) {
my ($start,$end)
= $term eq 'N' ? (0, $i) : ($#masses-$i, $#masses);
my $adjust
= $type eq 'a' ? - formula_mass('CO')
: 0;
my $mz = (sum(@masses[$start..$end]) + $adjust
+ $PROTON*$charge)/$charge;
push @series, [$mz,$type,$i,$charge, $tag];
}
# neutral ammonia loss
while ($peptide =~ /[RKQN]/g) {
my $i = $-[0]+1;
$i = $#masses - $i if ($term eq 'C');
next if ($i < $charge);
next if ($i >= $#masses -1);
my ($start,$end)
= $term eq 'N' ? (0, $i) : ($#masses-$i, $#masses);
my $adjust
= $type eq 'a' ? - formula_mass('CO')
: 0;
my $mz = (sum(@masses[$start..$end]) + $adjust - formula_mass('NH3')
+ $PROTON*$charge)/$charge;
push @series, [$mz,$type,$i,$charge,"-NH3$tag"];
}
pos($peptide) = 0;
# neutral water loss
if ($type ne 'a') {
while ($peptide =~ /[STED]/g) {
my $i = $-[0]+1;
$i = $#masses - $i if ($term eq 'C');
next if ($i < $charge);
next if ($i >= $#masses -1);
my ($start,$end)
= $term eq 'N' ? (0, $i) : ($#masses-$i, $#masses);
my $adjust
= $type eq 'a' ? - formula_mass('CO')
: 0;
my $mz = (sum(@masses[$start..$end]) + $adjust - formula_mass('H2O')
+ $PROTON*$charge)/$charge + $adjust;
push @series, [$mz,$type,$i,$charge,"-H2O$tag"];
}
}
return @series;
}
lib/MS/Peptide.pm view on Meta::CPAN
}
sub mz {
my ($self, %args) = @_;
my $type = $args{type} // 'mono';
my $z = $args{charge} // 1;
my $M = formula_mass('H2O', $type)
+ sum map {atoms_mass($_, $type)} @{ $self->{atoms} };
return ($M + $z * elem_mass('H', $type))/$z;
}
sub neutral_mass {
my ($self, %args) = @_;
my $type = $args{type} // 'mono';
return formula_mass('H2O', $type)
+ sum map {atoms_mass($_, $type)} @{ $self->{atoms} };
}
sub add_mod {
my ($self, $loc, $mod) = @_;
# $loc and $atom can be scalar or arrayref
my @locs = ref($loc) ? @$loc : ($loc);
lib/MS/Protein.pm view on Meta::CPAN
# build lookup tables
my $kyte_doolittle = _kyte_doolittle();
my $pK = _pK();
my $pKt = _pKt();
sub molecular_weight {
my ($seq, $type) = @_;
return sum( map {aa_mass($_, $type) // return undef}
split('', $seq) ) + formula_mass('H2O');
}
sub n_atoms {
my ($seq) = @_;
my %counts;
++$counts{$_} for (split '', $seq);
my %atoms = (H => 2, O => 1);
for my $aa (keys %counts) {
# test mod_mass()
my $name = mod_id_to_name(21);
ok ($name eq 'Phospho', "mod_id_to_name");
ok( are_equal( mod_mass($name), 79.9663 , 3), "mod_mass(mono)" );
ok( are_equal( mod_mass($name, 'average'), 79.9799 , 2), "mod_mass(avg)" );
# test brick_mass()
ok( are_equal( brick_mass('Water'), 18.010565, 3), "brick_mass()" );
# test formula_mass()
ok( are_equal( formula_mass('H2O'), 18.010565, 3), "formula_mass()" );
like( exception {formula_mass('foo!')}, qr/unsupported characters/,
"atoms_mass() bad atoms" );
like( exception {formula_mass('Qq')}, qr/mass not found/,
"atoms_mass() bad atoms" );
# test atoms()
ok( my $atoms = atoms('brick' => 'Water'), "atoms()" );
ok( ! defined atoms('brick' => 'FooBar'), "atoms() bad name" );
ok( are_equal( atoms_mass($atoms), 18.010565, 3), "atoms_mass()" );
like( exception {atoms_mass({'Foo'=>2})}, qr/mass not found/,
"atoms_mass() bad atoms" );
# test list_bricks()
( run in 0.739 second using v1.01-cache-2.11-cpan-26ccb49234f )