mspire-isotope 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,67 @@
1
+ require 'mspire/isotope'
2
+
3
+ class Mspire::Isotope::NIST::Updater
4
+ NIST_ISOTOPE_SITE = 'http://physics.nist.gov/cgi-bin/Compositions/stand_alone.pl?ele=&all=all&ascii=ascii2&isotype=some'
5
+
6
+ class << self
7
+
8
+ # Creates an isotope from a nist entry. Sets mono to false, which is not
9
+ # always correct (but needs to be corrected with additional info)
10
+ def isotope_from_nist_line(*args)
11
+ # atomic_number and mass_number are ints
12
+ [0,2].each {|i| args[i] = args[i].to_i }
13
+ # element is a downcase sym
14
+ args[1] = args[1].to_sym
15
+ # atomic_mass, relative_abundance, and average_mass as floats
16
+ [3, 4, 5].each {|i| args[i] = args[i][/([\w.]*)/].to_f }
17
+ # by default call every isotope the non-monoisotopic peak
18
+ # (will correct it as a group)
19
+ args << false
20
+ Mspire::Isotope.new(*args)
21
+ end
22
+
23
+ def write_nist_info(filename)
24
+ isotopes = isotopes_from_nist_site
25
+ File.write(filename, isotopes.map {|isotope| Mspire::Isotope::MEMBERS.map {|key| isotope.send(key) }}.to_yaml)
26
+ end
27
+
28
+ def isotopes_from_nist_site(deuterium_is_kind_of_hydrogen=true)
29
+ require 'mechanize'
30
+ body = Mechanize.new.get(NIST_ISOTOPE_SITE).body.split("\n")
31
+ body.delete_if {|l| l[/^(<|\/)/]}
32
+ body.shift(22)
33
+ isotopes = body.each_slice(8).map do |lines|
34
+ arr = (1..4).to_a.map {|i| match lines[i] }
35
+ rel, avg = match(lines[5]), match(lines[6])
36
+ next if rel.nil?
37
+ rel.size > 0 ? isotope_from_nist_line(*arr, rel, avg) : nil
38
+ end.compact!
39
+
40
+ # deuterium should be grouped with hydrogen, not as its own element!
41
+ isotopes.find {|iso| iso.element == :D }.element = :H if deuterium_is_kind_of_hydrogen
42
+
43
+ # update the mono boolean if this is the highest abundance peak
44
+ isotopes.group_by(&:element).values.each do |set|
45
+ set.max_by(&:relative_abundance).mono = true
46
+ end
47
+ isotopes
48
+ end
49
+
50
+ private
51
+
52
+ def match(string)
53
+ unless string.nil?
54
+ if string.empty?
55
+ nil
56
+ else
57
+ string[/= (.*)/,1]
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ if __FILE__ == $0
65
+ require 'mspire/isotope/nist'
66
+ Mspire::Isotope::NIST::Updater.write_nist_info(Mspire::Isotope::NIST::INFO_FILE_FULL_PATH)
67
+ end
@@ -0,0 +1,5 @@
1
+ module Mspire
2
+ class Isotope
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mspire/isotope/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mspire-isotope"
8
+ spec.version = Mspire::Isotope::VERSION
9
+ spec.authors = ["John T. Prince"]
10
+ spec.email = ["jtprince@gmail.com"]
11
+ spec.summary = %q{mspire library holding element isotope information}
12
+ spec.description = %q{mspire library holding element isotope information. Mostly just holds constants.}
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
+ #["nokogiri", "~> 1.6.1"],
23
+ #["bsearch", ">= 1.5.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
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ require 'mspire/isotope'
4
+
5
+ describe Mspire::Isotope do
6
+
7
+ specify 'Mspire::Isotope[] accesses isotopes by element' do
8
+ carbon12 = Mspire::Isotope[:C][0] # the lightest carbon isotope
9
+ carbon12.element.should == :C
10
+ carbon12.mass_number.should == 12
11
+ carbon12 = Mspire::Isotope[:C].find(&:mono) # the most abundant (i.e., monoisotopic isotope)
12
+ carbon12.element.should == :C
13
+ carbon12.mass_number.should == 12
14
+ carbon12.mono.should be_true
15
+ end
16
+
17
+ it 'can set the element_hash to change convenience method access' do
18
+ Mspire::Isotope[:C][0].relative_abundance.should == 0.9891
19
+ Mspire::Isotope.element_hash = Mspire::Isotope::NIST::BY_ELEMENT
20
+ Mspire::Isotope[:C][0].relative_abundance.should == 0.9893
21
+ end
22
+
23
+ specify 'Mspire::Isotope::ISOTOPES has all the common isotopes and (uses Neese by default)' do
24
+ # frozen
25
+ Mspire::Isotope::ISOTOPES.size.should == 288
26
+ hydrogen_isotopes = Mspire::Isotope::ISOTOPES.select {|iso| iso.element == :H }
27
+ hydrogen_isotopes.size.should == 2
28
+
29
+ {atomic_number: 1, element: :H, mass_number: 1, atomic_mass: 1.00782503207, relative_abundance: 0.999844, average_mass: 1.00794, mono: true}.each do |k,v|
30
+ hydrogen_isotopes.first.send(k).should == v
31
+ end
32
+ {atomic_number: 1, element: :H, mass_number: 2, atomic_mass: 2.0141017778, relative_abundance: 0.000156, average_mass: 1.00794, mono: false}.each do |k,v|
33
+ hydrogen_isotopes.last.send(k).should == v
34
+ end
35
+ u = Mspire::Isotope::ISOTOPES.last
36
+ {atomic_number: 92, element: :U, mass_number: 238, atomic_mass: 238.0507882, relative_abundance: 0.992742, average_mass: 238.02891, mono: true}.each do |k,v|
37
+ u.send(k).should == v
38
+ end
39
+ end
40
+
41
+ specify 'Mspire::Isotope::BY_ELEMENT has all common isotopes by element (uses Neese by default)' do
42
+ [{atomic_number: 6, element: :C, mass_number: 12, atomic_mass: 12.0, relative_abundance: 0.9891, average_mass: 12.0107, mono: true}, {atomic_number: 6, element: :C, mass_number: 13, atomic_mass: 13.0033548378, relative_abundance: 0.0109, average_mass: 12.0107, mono: false}].zip(Mspire::Isotope::BY_ELEMENT[:C]) do |hash, iso|
43
+ hash.each do |k,v|
44
+ iso.send(k).should == v
45
+ end
46
+ end
47
+ Mspire::Isotope::BY_ELEMENT[:H].size.should == 2
48
+ end
49
+ 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,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mspire-isotope
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-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.14.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.14.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 4.1.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.1.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.8.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.8.2
83
+ description: mspire library holding element isotope information. Mostly just holds
84
+ constants.
85
+ email:
86
+ - jtprince@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/mspire/isotope.rb
97
+ - lib/mspire/isotope/aa.rb
98
+ - lib/mspire/isotope/neese.rb
99
+ - lib/mspire/isotope/nist.rb
100
+ - lib/mspire/isotope/nist/isotope_info.yml
101
+ - lib/mspire/isotope/nist/updater.rb
102
+ - lib/mspire/isotope/version.rb
103
+ - mspire-isotope.gemspec
104
+ - spec/mspire/isotope_spec.rb
105
+ - spec/spec_helper.rb
106
+ homepage: ''
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.2.2
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: mspire library holding element isotope information
130
+ test_files:
131
+ - spec/mspire/isotope_spec.rb
132
+ - spec/spec_helper.rb