mspire-mass 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 031bab08ab5b939d6cf65abe8f610bc08206b2ee
4
+ data.tar.gz: f11cfb2f633ea73d2435bb8fcac67f94859b507a
5
+ SHA512:
6
+ metadata.gz: 6635c2dc17e376012df5b938c65b1ea225bd222d1f287b8763e8aaa72df2069ced8daa835f6d686643f9481e0165fc6142dd96cfc951b94c22875686ee127ee2
7
+ data.tar.gz: b6220a5181ccca4604ad0f9b9c7e41ef773873d37b072b5bc5746b5f9c379ed2c05a55291cecf54d186baee41bb1f7c4e851f1ec77fc59f2d2985ca223c605ba
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mspire-mass.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2014 Brigham Young University
2
+ author: John T. Prince
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # Mspire::Mass
2
+
3
+ mspire library holding constants for mass lookup. Uses NIST masses (where applicable). See [mspire-isotope](https://github.com/princelab/mspire-isotope) for complete isotope information (including masses). See [mspire-molecular_formula](https://github.com/princelab/mspire-molecular_formula) for mass or m/z methods for larger molecular formulas.
4
+
5
+ ## Installation
6
+
7
+ gem install mspire-mass
8
+
9
+ ## Examples
10
+
11
+ ### MONO\_STRING and AVG\_STRING
12
+
13
+ A simple hash may be accessed directly from the various modules:
14
+
15
+ ```ruby
16
+ require 'mspire/mass'
17
+
18
+ Mspire::Mass::Element::MONO_STRING['C'] # => 12.0
19
+ Mspire::Mass::AA::MONO_STRING['C'] # => 103.0091844778 (residue)
20
+ Mspire::Mass::Subatomic::MONO_STRING['NEUTRON'] # => 1.0086649156
21
+ Mspire::Mass::Common::MONO_STRING['H2O'] # => 18.0105646837
22
+
23
+ # similar for average masses
24
+ Mspire::Mass::Element::AVG_STRING['C'] # => 12.0107
25
+
26
+ # note that elements are accessed in mixed case by default
27
+ Mspire::Mass::Element::MONO_STRING['Se'] # => 79.9165213
28
+ ```
29
+
30
+ A hash where everything but the amino acids is downcased can also be accessed
31
+
32
+ ```ruby
33
+ require 'mspire/mass/merged' # <- must be required explicitly
34
+ Mspire::Mass::Merged::MONO_STRING['C'] # (cysteine) => 103.0091844778
35
+ Mspire::Mass::Merged::MONO_STRING['c'] # (carbon) => 12.0
36
+ Mspire::Mass::Merged::MONO_STRING['h2o'] # (water) => 18.0105646837
37
+ Mspire::Mass::Merged::MONO_STRING['e'] # (or 'electron') => 0.0005486
38
+ # Mspire::Mass::Merged::AVG_STRING also available
39
+ ```
40
+
41
+ MONO\_STRING and AVG\_STRING are only for quick, low-level access. They are
42
+ purposefully ugly to encourage you to use the beautiful interface shown
43
+ directly below!
44
+
45
+ ### <Module>.masses
46
+
47
+ A mass hash can be created in a variety of forms depending on your
48
+ needs/preferences.
49
+
50
+ ---------------------------------
51
+ <default>
52
+ ---------------------------------
53
+ type = :mono | :avg
54
+ by = :symbol | :string | :both
55
+ case = :up | :down | :both
56
+
57
+ **NOTE:** it defaults to symbols, not strings.
58
+
59
+ ```ruby
60
+ el_masses = Mspire::Mass::Element.masses
61
+ # which defaults to this
62
+ el_masses = Mspire::Mass::Element.masses(type: :mono, by: :symbol, case: :up)
63
+ el_masses[:C] # (carbon) => 12.0
64
+
65
+ aas = Mspire::Mass::AA.masses(type: :avg, by: :string)
66
+ aas['C'] # (cysteine) => 103.1429
67
+
68
+ subatomic = Mspire::Mass::Subatomic.masses
69
+ subatomic[:PROTON] # (same as subatomic[:'H+'] or subatomic[:H_PLUS]) # => 1.00727643207
70
+ ```
71
+
72
+ A merged hash can be created in a similar way, but :up and :down aren't
73
+ respected since it merges by upcasing amino acids and downcasing all else.
74
+
75
+ ```ruby
76
+ merged = Mspire::Mass::Merged.masses(type: :mono)
77
+ merged[:C] # (cysteine) => 103.0091844778
78
+ merged[:c] # (carbon) => 12.0
79
+ ```
80
+
81
+ ### Subatomic constants on Mspire::Mass
82
+
83
+ The strings that play nice are defined as constants on Mspire::Mass so you can
84
+ do this:
85
+
86
+ ```ruby
87
+ Mspire::Mass::ELECTRON #=> 0.0005486
88
+ Mspire::Mass::PROTON #=> 1.00727643207
89
+ Mspire::Mass::NEUTRON #=> 1.0086649156
90
+ ```
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ @module_name = Mspire::Mass
4
+ @gem_name = 'mspire-mass'
5
+ @gem_path_name = @gem_name.gsub('-','/')
6
+
7
+ require "#{@gem_path_name}/version"
8
+
9
+ require 'rspec/core'
10
+ require 'rspec/core/rake_task'
11
+ RSpec::Core::RakeTask.new(:spec) do |spec|
12
+ spec.pattern = FileList['spec/**/*_spec.rb']
13
+ end
14
+
15
+ task :default => :spec
16
+
17
+ require 'rdoc/task'
18
+ Rake::RDocTask.new do |rdoc|
19
+ version = @module_name.const_get('VERSION')
20
+ rdoc.rdoc_dir = 'rdoc'
21
+ rdoc.title = "#{@gem_name} #{version}"
22
+ rdoc.rdoc_files.include('README*')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
@@ -0,0 +1,39 @@
1
+ require 'mspire/mass/version'
2
+ require 'mspire/mass/element'
3
+ require 'mspire/mass/subatomic'
4
+ require 'mspire/mass/common'
5
+ require 'mspire/mass/aa'
6
+
7
+ module Mspire
8
+ # includes the Subatomic entities that are acceptable contants (e.g., H+
9
+ # won't work) as uppercase module constants (e.g. Mspire::Mass::ELECTRON
10
+ # and Mspire::Mass::PROTON)
11
+ module Mass
12
+ Subatomic::MONO_STRING.each do |key, val|
13
+ unless key == 'H+'
14
+ self.const_set(key, val)
15
+ end
16
+ end
17
+
18
+ #class << self
19
+ ## takes a molecular formula as a string, hash or MolecularFormula object
20
+ ## and returns the exact mass.
21
+ #def formula_to_exact_mass(formula)
22
+ #Mspire::MolecularFormula.from_any(formula).mass
23
+ #end
24
+ #alias_method :formula, :formula_to_exact_mass
25
+
26
+ #def aa_to_exact_mass(aa_seq)
27
+ #chain_mass = aa_seq.each_char.inject(0.0) do |sum, aa_char|
28
+ #sum + AA[aa_char]
29
+ #end
30
+ #chain_mass + formula_to_exact_mass('H2O')
31
+ #end
32
+ #alias_method :aa, :aa_to_exact_mass
33
+
34
+ #end
35
+
36
+ end
37
+ end
38
+
39
+
@@ -0,0 +1,70 @@
1
+ require 'mspire/mass/mass_provider'
2
+
3
+ module Mspire
4
+ module Mass
5
+ module AA
6
+ extend MassProvider
7
+
8
+ # amino_acids keys as strings, average masses
9
+ AVG_STRING = {
10
+ "*"=>118.88603,
11
+ "A"=>71.0779,
12
+ "B"=>172.1405,
13
+ "C"=>103.1429,
14
+ "D"=>115.0874,
15
+ "E"=>129.11398,
16
+ "F"=>147.17386,
17
+ "G"=>57.05132,
18
+ "H"=>137.13928,
19
+ "I"=>113.15764,
20
+ "K"=>128.17228,
21
+ "L"=>113.15764,
22
+ "M"=>131.19606,
23
+ "N"=>114.10264,
24
+ "O"=>211.28076,
25
+ "P"=>97.11518,
26
+ "Q"=>128.12922,
27
+ "R"=>156.18568,
28
+ "S"=>87.0773,
29
+ "T"=>101.10388,
30
+ "U"=>150.0379,
31
+ "V"=>99.13106,
32
+ "W"=>186.2099,
33
+ "X"=>118.88603,
34
+ "Y"=>163.17326,
35
+ "Z"=>128.6231
36
+ }
37
+
38
+ # amino_acids keys as strings, monoisotopic masses
39
+ MONO_STRING = {
40
+ "*"=>118.805716,
41
+ "A"=>71.0371137878,
42
+ "B"=>172.048405,
43
+ "C"=>103.0091844778,
44
+ "D"=>115.026943032,
45
+ "E"=>129.0425930962,
46
+ "F"=>147.0684139162,
47
+ "G"=>57.0214637236,
48
+ "H"=>137.0589118624,
49
+ "I"=>113.0840639804,
50
+ "K"=>128.0949630177,
51
+ "L"=>113.0840639804,
52
+ "M"=>131.0404846062,
53
+ "N"=>114.0429274472,
54
+ "O"=>211.1446528645,
55
+ "P"=>97.052763852,
56
+ "Q"=>128.0585775114,
57
+ "R"=>156.1011110281,
58
+ "S"=>87.0320284099,
59
+ "T"=>101.0476784741,
60
+ "U"=>150.9536355878,
61
+ "V"=>99.0684139162,
62
+ "W"=>186.0793129535,
63
+ "X"=>118.805716,
64
+ "Y"=>163.0633285383,
65
+ "Z"=>128.550585
66
+ }
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,21 @@
1
+ require 'mspire/mass/mass_provider'
2
+ require 'mspire/mass/element'
3
+
4
+ module Mspire
5
+ module Mass
6
+ module Common
7
+ extend MassProvider
8
+
9
+ MONO_STRING = {
10
+ 'H2O' => %w(H H O).map {|el| Element::MONO_STRING[el] }.reduce(:+),
11
+ 'OH' => %w(O H).map {|el| Element::MONO_STRING[el] }.reduce(:+),
12
+ }
13
+
14
+ AVG_STRING = {
15
+ 'H2O' => %w(H H O).map {|el| Element::AVG_STRING[el] }.reduce(:+),
16
+ 'OH' => %w(O H).map {|el| Element::AVG_STRING[el] }.reduce(:+),
17
+ }
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ require 'mspire/isotope'
2
+ require 'mspire/mass/mass_provider'
3
+
4
+ module Mspire
5
+ module Mass
6
+ module Element
7
+ extend MassProvider
8
+
9
+ AVG_STRING = {}
10
+ MONO_STRING = {}
11
+ Mspire::Isotope::BY_ELEMENT.each do |el, isotopes|
12
+ AVG_STRING[el.to_s] = isotopes.first.average_mass
13
+ MONO_STRING[el.to_s] = isotopes.find(&:mono).atomic_mass
14
+ end
15
+
16
+ MONO_STRING['D'] = Mspire::Isotope::BY_ELEMENT[:H].find {|iso| iso.element == :H && iso.mass_number == 2 }.atomic_mass
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,59 @@
1
+ module Mspire
2
+ module Mass
3
+ module MassProvider
4
+ DEFAULTS = {
5
+ type: :mono,
6
+ by: :symbol,
7
+ case: :up,
8
+ }
9
+
10
+ def prepare_hash(hash, opts={})
11
+ opt = Mspire::Mass::MassProvider::DEFAULTS.merge(opts)
12
+ newhash = {}
13
+
14
+ (upcase, downcase, symbol, string) =
15
+ ['case'.to_sym, :up, 'case'.to_sym, :down, :by, :symbol, :by, :string].each_slice(2).map do |opt_key, unique|
16
+ [:both, unique].include?(opt[opt_key])
17
+ end
18
+
19
+ hash.each do |k,v|
20
+ keys = []
21
+ if upcase
22
+ keys << k.to_s # <= assumes they start in proper up or mixed case
23
+ end
24
+ if downcase
25
+ keys << k.to_s.downcase
26
+ end
27
+ if symbol
28
+ final_keys = keys.map(&:to_sym)
29
+ end
30
+ if string
31
+ string_keys = keys.map(&:to_s)
32
+ final_keys ||= []
33
+ final_keys.push(*string_keys)
34
+ end
35
+ final_keys.each {|key| newhash[key] = v }
36
+ end
37
+ newhash
38
+ end
39
+
40
+ # options:
41
+ #
42
+ # <default>
43
+ # type: :mono | :avg
44
+ # case: :up | :down | :both
45
+ # by: :symbol | :string | :both
46
+ #
47
+ # By default will use the module's <type>_STRING constant but this can
48
+ # be overridden with the :hash option which accetps a hash with symbol
49
+ # or string keys in uppercase or mixed case form (the :up merely
50
+ # respects the given case, while :down actively downcases. Accepts a
51
+ # hash with symbol or string keys. The keys should be in uppercase or
52
+ # mixed case to begin with.
53
+ def masses(opts={})
54
+ opt = DEFAULTS.merge(opts)
55
+ prepare_hash(opt[:hash] || self.const_get(opt[:type].to_s.upcase << "_STRING"), opt)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,38 @@
1
+ require 'mspire/mass/mass_provider'
2
+ require 'mspire/mass/element'
3
+ require 'mspire/mass/common'
4
+ require 'mspire/mass/aa'
5
+ require 'mspire/mass/subatomic'
6
+ require 'mspire/mass'
7
+
8
+ module Mspire
9
+ module Mass
10
+ # provides hashes with both Amino Acids (uppercase letters) and elements
11
+ # (lowercased) along with common abbreviations
12
+ module Merged
13
+ extend MassProvider
14
+
15
+ class << self
16
+ def downcase_keys(hash)
17
+ Hash[ hash.map {|key,val| [key.to_s.downcase, val] } ]
18
+ end
19
+
20
+ def masses(opts={})
21
+ # force upper case (which merely respects the case given)
22
+ super(opts.merge(case: :up))
23
+ end
24
+ end
25
+
26
+ MONO_STRING = downcase_keys( Element::MONO_STRING )
27
+ .merge( downcase_keys( Common::MONO_STRING ) )
28
+ .merge( AA::MONO_STRING )
29
+ .merge( downcase_keys( Subatomic::MONO_STRING ) )
30
+
31
+ AVG_STRING = downcase_keys( Element::AVG_STRING )
32
+ .merge( downcase_keys( Common::AVG_STRING ) )
33
+ .merge( AA::AVG_STRING )
34
+ .merge( downcase_keys( Subatomic::AVG_STRING ) )
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,20 @@
1
+ require 'mspire/mass/mass_provider'
2
+ require 'mspire/mass/element'
3
+
4
+ module Mspire
5
+ module Mass
6
+ module Subatomic
7
+ extend MassProvider
8
+
9
+ MONO_STRING = {
10
+ 'ELECTRON' => 0.0005486, # www.mikeblaber.org/oldwine/chm1045/notes/Atoms/.../Atoms03.htm
11
+ 'NEUTRON' => 1.0086649156,
12
+ }
13
+ # 'h+' => 1.00727646677,
14
+ MONO_STRING['PROTON'] = MONO_STRING['H_PLUS'] = MONO_STRING['H+'] = Mspire::Mass::Element::MONO_STRING['H'] - MONO_STRING['ELECTRON']
15
+ MONO_STRING['E'] = MONO_STRING['ELECTRON']
16
+
17
+ AVG_STRING = MONO_STRING.dup
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module Mspire
2
+ module Mass
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mspire/mass/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mspire-mass"
8
+ spec.version = Mspire::Mass::VERSION
9
+ spec.authors = ["John T. Prince"]
10
+ spec.email = ["jtprince@gmail.com"]
11
+ spec.summary = %q{mspire library for mass calculations.}
12
+ spec.description = %q{mspire library for mass calculations. Mainly holds constants for simple lookup.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ [
22
+ # should probably be its only dependency
23
+ ["mspire-isotope", "~> 0.1.0"],
24
+ ].each do |args|
25
+ spec.add_dependency(*args)
26
+ end
27
+
28
+ [
29
+ ["bundler", "~> 1.6.2"],
30
+ ["rake"],
31
+ ["rspec", "~> 2.14.1"],
32
+ ["rdoc", "~> 4.1.1"],
33
+ ["simplecov", "~> 0.8.2"],
34
+ ].each do |args|
35
+ spec.add_development_dependency(*args)
36
+ end
37
+
38
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ require 'mspire/mass/aa'
4
+
5
+ describe Mspire::Mass::AA do
6
+ specify 'Mspire::Mass::AA::MONO_STRING provides direct access to amino acid masses (precalculated)' do
7
+ mono = Mspire::Mass::AA::MONO_STRING
8
+ mono['C'].should == 103.0091844778
9
+ end
10
+
11
+ specify 'Mspire::Mass::AA::AVG_STRING provides direct access to avg amino acid masses (precalculated)' do
12
+ avg = Mspire::Mass::AA::AVG_STRING
13
+ avg['C'].should == 103.1429
14
+ end
15
+
16
+ specify 'also gives access to: "*", "B", "O", "U", "X" (averagine), and "Z"' do
17
+ mono = Mspire::Mass::AA::MONO_STRING
18
+ avg = Mspire::Mass::AA::AVG_STRING
19
+ [mono, avg].each do |hash|
20
+ ["*", "B", "O", "U", "X", "Z"].each do |key|
21
+ hash.key?(key).should be_true
22
+ end
23
+ end
24
+ end
25
+
26
+ specify '#masses can return hash of mono|avg, symbols|strings|both, up|down|both' do
27
+ masses = Mspire::Mass::AA.masses
28
+ masses[:C].should == 103.0091844778
29
+
30
+ masses = Mspire::Mass::AA.masses(type: :mono)
31
+ masses[:C].should ==103.0091844778
32
+
33
+ masses = Mspire::Mass::AA.masses(by: :symbol)
34
+ masses[:C].should ==103.0091844778
35
+
36
+ masses = Mspire::Mass::AA.masses(by: :symbol, case: :down)
37
+ masses[:c].should ==103.0091844778
38
+
39
+ masses = Mspire::Mass::AA.masses(type: :avg, by: :string)
40
+ masses['C'].should == 103.1429
41
+
42
+ masses = Mspire::Mass::AA.masses(type: :avg, by: :string, case: :down)
43
+ masses['c'].should == 103.1429
44
+
45
+ masses = Mspire::Mass::AA.masses(type: :avg, by: :symbol, case: :down)
46
+ masses[:c].should == 103.1429
47
+ end
48
+
49
+ specify 'can pass in own lookup hash' do
50
+ masses = Mspire::Mass::AA.masses(hash: {C: 66}, case: :down)
51
+ masses[:c].should == 66
52
+ end
53
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ require 'mspire/mass/common'
4
+
5
+ describe Mspire::Mass::Common do
6
+ specify 'Mspire::Mass::Common::MONO_STRING provides string direct access to a few common masses' do
7
+ masses = Mspire::Mass::Common::MONO_STRING
8
+ masses['OH'].should be_within(0.0000001).of( 17.002739651629998 )
9
+ end
10
+
11
+ specify 'Mspire::Mass::Common::AVG_STRING provides string direct access to a few common avg masses' do
12
+ avg = Mspire::Mass::Common::AVG_STRING
13
+ avg['OH'].should == 17.00734
14
+ end
15
+
16
+ specify 'gives mass of H2O and OH' do
17
+ masses = Mspire::Mass::Common.masses
18
+ masses[:H2O].should == 18.0105646837
19
+ masses[:OH].should == 17.002739651629998
20
+ end
21
+
22
+ specify '#masses can return hash of mono|avg, symbols|strings|both, up|down|both' do
23
+ masses = Mspire::Mass::Common.masses
24
+ masses[:H2O].should == 18.0105646837
25
+
26
+ masses = Mspire::Mass::Common.masses(type: :mono)
27
+ masses[:H2O].should == 18.0105646837
28
+
29
+ masses = Mspire::Mass::Common.masses(by: :symbol)
30
+ masses[:H2O].should == 18.0105646837
31
+
32
+ masses = Mspire::Mass::Common.masses(by: :symbol, case: :down)
33
+ masses[:h2o].should == 18.0105646837
34
+
35
+ masses = Mspire::Mass::Common.masses(type: :avg, by: :string)
36
+ masses['H2O'].should == 18.01528
37
+
38
+ masses = Mspire::Mass::Common.masses(type: :avg, by: :string, case: :down)
39
+ masses['h2o'].should == 18.01528
40
+
41
+ masses = Mspire::Mass::Common.masses(type: :avg, by: :symbol, case: :down)
42
+ masses[:h2o].should == 18.01528
43
+ end
44
+
45
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ require 'mspire/mass/element'
4
+
5
+ describe Mspire::Mass::Element do
6
+
7
+ specify 'Mspire::Mass::Element::MONO_STRING provides direct access to element masses' do
8
+ mono_str = Mspire::Mass::Element::MONO_STRING
9
+ mono_str['Se'].should == 79.9165213
10
+ end
11
+
12
+ specify 'Mspire::Mass::Element::AVG_STRING provides direct access to avg element masses' do
13
+ avg = Mspire::Mass::Element::AVG_STRING
14
+ avg['Se'].should == 78.96
15
+ end
16
+
17
+ specify '#masses can return hash of mono|avg, symbols|strings|both, up|down|both' do
18
+ masses = Mspire::Mass::Element.masses
19
+ masses[:Se].should == 79.9165213
20
+
21
+ masses = Mspire::Mass::Element.masses(type: :mono)
22
+ masses[:Se].should == 79.9165213
23
+
24
+ masses = Mspire::Mass::Element.masses(by: :symbol)
25
+ masses[:Se].should == 79.9165213
26
+
27
+ masses = Mspire::Mass::Element.masses(by: :symbol, case: :down)
28
+ masses[:se].should == 79.9165213
29
+
30
+ masses = Mspire::Mass::Element.masses(type: :avg, by: :string)
31
+ masses['Se'].should == 78.96
32
+
33
+ masses = Mspire::Mass::Element.masses(type: :avg, by: :string, case: :down)
34
+ masses['se'].should == 78.96
35
+
36
+ masses = Mspire::Mass::Element.masses(type: :avg, by: :symbol, case: :down)
37
+ masses[:se].should == 78.96
38
+ end
39
+
40
+ specify 'can pass in own lookup hash' do
41
+ masses = Mspire::Mass::Element.masses(hash: {Se: 77})
42
+ masses[:Se].should == 77
43
+ end
44
+
45
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ require 'mspire/mass/mass_provider'
4
+
5
+ module MockModule
6
+ extend Mspire::Mass::MassProvider
7
+ end
8
+
9
+ describe Mspire::Mass::MassProvider do
10
+ subject do
11
+ { :C => 3, :E => 5 }
12
+ end
13
+ it 'can prepare hashes' do
14
+ new_hash = MockModule.prepare_hash(subject)
15
+ new_hash.should == {:C=>3, :E=>5}
16
+ end
17
+
18
+ it 'can prepare hashes with downcase keys' do
19
+ new_hash = MockModule.prepare_hash(subject, case: :down)
20
+ new_hash.should == {:c=>3, :e=>5}
21
+ end
22
+
23
+ it 'can prepare hashes with upcase keys' do
24
+ new_hash = MockModule.prepare_hash(subject, case: :up)
25
+ new_hash.should == {:C=>3, :E=>5}
26
+ end
27
+
28
+ it 'can prepare hashes with either case keys' do
29
+ new_hash = MockModule.prepare_hash(subject, case: :both)
30
+ new_hash.sort.should == {:c=>3, :e=>5, :C=>3, :E=>5}.sort
31
+ end
32
+
33
+ it 'can prepare hashes with symbol keys' do
34
+ new_hash = MockModule.prepare_hash(subject, by: :symbol)
35
+ new_hash.should == {:C=>3, :E=>5}
36
+ end
37
+
38
+ it 'can prepare hashes with symbol keys even if string keys were used' do
39
+ yikes = subject.map {|k,v| [k.to_s, v]}.to_h
40
+ yikes.should == {"C"=>3, "E"=>5}
41
+ new_hash = MockModule.prepare_hash(yikes, by: :symbol)
42
+ new_hash.should == {:C=>3, :E=>5}
43
+ end
44
+
45
+ it 'can prepare hashes with string keys' do
46
+ new_hash = MockModule.prepare_hash(subject, by: :string)
47
+ new_hash.should == {'C'=>3, 'E'=>5}
48
+ end
49
+
50
+ it 'can prepare hashes with both string and symbol keys' do
51
+ new_hash = MockModule.prepare_hash(subject, by: :both)
52
+ # could fail if sort order is done differently
53
+ new_hash.should == {:C=>3, :E=>5, 'C'=>3, 'E'=>5}
54
+ end
55
+
56
+ it 'can prepare hashes with both string and symbol keys and upper and lower case' do
57
+ new_hash = MockModule.prepare_hash(subject, by: :both, case: :both)
58
+ # could fail if sort order is done differently
59
+ new_hash.should == {:C=>3, :c=>3, "C"=>3, "c"=>3, :E=>5, :e=>5, "E"=>5, "e"=>5}
60
+ end
61
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ require 'mspire/mass/merged'
3
+
4
+ describe Mspire::Mass::Merged do
5
+ it 'accesses elements by lower case and amino acids by upper case' do
6
+ {
7
+ 'c' => 12.0, # carbon
8
+ 'C' => 103.00918, # cysteine
9
+ 'br' => 78.9183361, # bromine
10
+ 'd' => 2.014101779, # deuterium
11
+ 'D' => 115.0269430, # aspartic acid
12
+ 'h+' => 1.00727646677, # proton
13
+ 'h' => 1.007825035, # hydrogen
14
+ 'h2o' => 18.0105647, # water
15
+ 'oh' => 17.002739665, # oh
16
+ 'e' => 0.0005486, # electron
17
+ }.each do |el, mass|
18
+ Mspire::Mass::Merged::MONO_STRING[el].should_not be_nil
19
+ Mspire::Mass::Merged::MONO_STRING[el].should be_within(0.00001).of(mass)
20
+ end
21
+
22
+ { 'h' => 1.00794, 'he' => 4.002602, 'ni' => 58.6934, 'H' => 137.13928 }.each do |el, mass|
23
+ Mspire::Mass::Merged::AVG_STRING[el].should_not be_nil
24
+ Mspire::Mass::Merged::AVG_STRING[el].should be_within(0.00001).of(mass)
25
+ end
26
+ end
27
+
28
+ specify '#masses can return hash of mono|avg, symbols|strings|both' do
29
+ masses = Mspire::Mass::Merged.masses
30
+ masses[:D].should == 115.026943032
31
+ masses[:d].should == 2.0141017778
32
+
33
+ masses = Mspire::Mass::Merged.masses(type: :mono)
34
+ masses[:D].should == 115.026943032
35
+ masses[:d].should == 2.0141017778
36
+
37
+ masses = Mspire::Mass::Merged.masses(by: :symbol)
38
+ masses[:D].should == 115.026943032
39
+ masses[:d].should == 2.0141017778
40
+
41
+ masses = Mspire::Mass::Merged.masses(type: :avg, by: :string)
42
+ masses['D'].should == 115.0874
43
+ masses['d'].should be_nil # there is no deuterium as an average mass!
44
+ masses['c'].should == 12.0107
45
+ end
46
+
47
+ specify ':case => :up and => :down are meaningless' do
48
+ masses = Mspire::Mass::Merged.masses(type: :avg, by: :string, case: :down)
49
+ #masses = Mspire::Mass::Merged.masses(type: :avg, by: :string)
50
+ masses['D'].should == 115.0874
51
+ masses['c'].should == 12.0107
52
+ end
53
+
54
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ require 'mspire/mass/subatomic'
4
+
5
+ describe Mspire::Mass::Subatomic do
6
+
7
+ specify 'Mspire::Mass::Subatomic::MONO_STRING provides direct access to subatomic masses' do
8
+ masses = Mspire::Mass::Subatomic::MONO_STRING
9
+ masses['NEUTRON'].should == 1.0086649156
10
+ end
11
+
12
+ specify 'Mspire::Mass::Subatomic::AVG_STRING provides direct access to subatomic masses (same values as mono for subatomic)' do
13
+ masses = Mspire::Mass::Subatomic::AVG_STRING
14
+ masses['NEUTRON'].should == 1.0086649156
15
+ end
16
+
17
+ specify 'gives mass of :ELECTRON (and :E), :NEUTRON, :PROTON (:H+ and :H_PLUS)' do
18
+ masses = Mspire::Mass::Subatomic.masses
19
+ masses[:ELECTRON].should == 0.0005486
20
+ masses[:E].should == 0.0005486
21
+ masses[:NEUTRON].should == 1.0086649156
22
+ masses[:PROTON].should == 1.00727643207
23
+ masses['H+'.to_sym].should == 1.00727643207
24
+ masses[:H_PLUS].should == 1.00727643207
25
+ end
26
+
27
+ specify '#masses can return hash of mono|avg, symbols|strings|both, up|down|both' do
28
+ masses = Mspire::Mass::Subatomic.masses
29
+ masses[:ELECTRON].should == 0.0005486
30
+
31
+ masses = Mspire::Mass::Subatomic.masses(type: :mono)
32
+ masses[:ELECTRON].should == 0.0005486
33
+
34
+ masses = Mspire::Mass::Subatomic.masses(by: :symbol)
35
+ masses[:ELECTRON].should == 0.0005486
36
+
37
+ masses = Mspire::Mass::Subatomic.masses(by: :symbol, case: :down)
38
+ masses[:electron].should == 0.0005486
39
+
40
+ masses = Mspire::Mass::Subatomic.masses(type: :avg, by: :string)
41
+ masses['ELECTRON'].should == 0.0005486
42
+
43
+ masses = Mspire::Mass::Subatomic.masses(type: :avg, by: :string, case: :down)
44
+ masses['electron'].should == 0.0005486
45
+
46
+ masses = Mspire::Mass::Subatomic.masses(type: :avg, by: :symbol, case: :down)
47
+ masses[:electron].should == 0.0005486
48
+ end
49
+
50
+ specify 'can pass in own lookup hash' do
51
+ masses = Mspire::Mass::Subatomic.masses(hash: {ELECTRON: 8.77})
52
+ masses[:ELECTRON].should == 8.77
53
+ end
54
+
55
+ end
56
+
57
+
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ require 'mspire/mass'
4
+
5
+ describe Mspire::Mass do
6
+ specify 'require "mspire/mass" => requires Element, Subatomic, Common, and AA, but not "merged"' do
7
+ %w(Element Subatomic Common AA).each do |mod|
8
+ Mspire::Mass.const_get(mod).class.should == Module
9
+ end
10
+ expect { Mspire::Mass.const_get(Merged) }.to raise_error(NameError)
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'rspec'
5
+
6
+ TESTFILES = File.dirname(__FILE__) + '/testfiles'
7
+
8
+ # Requires supporting files with custom matchers and macros, etc,
9
+ # in ./support/ and its subdirectories.
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.color_enabled = true
15
+ config.tty = true
16
+ config.formatter = :documentation # :progress, :html, :textmate
17
+ #config.formatter = :progress # :progress, :html, :textmate
18
+ end
19
+
20
+
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mspire-mass
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John T. Prince
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mspire-isotope
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.14.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.14.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: rdoc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 4.1.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 4.1.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.8.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.8.2
97
+ description: mspire library for mass calculations. Mainly holds constants for simple
98
+ lookup.
99
+ email:
100
+ - jtprince@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/mspire/mass.rb
111
+ - lib/mspire/mass/aa.rb
112
+ - lib/mspire/mass/common.rb
113
+ - lib/mspire/mass/element.rb
114
+ - lib/mspire/mass/mass_provider.rb
115
+ - lib/mspire/mass/merged.rb
116
+ - lib/mspire/mass/subatomic.rb
117
+ - lib/mspire/mass/version.rb
118
+ - mspire-mass.gemspec
119
+ - spec/mspire/mass/aa_spec.rb
120
+ - spec/mspire/mass/common_spec.rb
121
+ - spec/mspire/mass/element_spec.rb
122
+ - spec/mspire/mass/mass_provider_spec.rb
123
+ - spec/mspire/mass/merged_spec.rb
124
+ - spec/mspire/mass/subatomic_spec.rb
125
+ - spec/mspire/mass_spec.rb
126
+ - spec/spec_helper.rb
127
+ homepage: ''
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.2.2
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: mspire library for mass calculations.
151
+ test_files:
152
+ - spec/mspire/mass/aa_spec.rb
153
+ - spec/mspire/mass/common_spec.rb
154
+ - spec/mspire/mass/element_spec.rb
155
+ - spec/mspire/mass/mass_provider_spec.rb
156
+ - spec/mspire/mass/merged_spec.rb
157
+ - spec/mspire/mass/subatomic_spec.rb
158
+ - spec/mspire/mass_spec.rb
159
+ - spec/spec_helper.rb