mspire 0.7.10 → 0.7.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/mspire/molecular_formula.rb +26 -0
- data/spec/mspire/molecular_formula_spec.rb +34 -4
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.11
|
@@ -23,6 +23,15 @@ module Mspire
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
# returns a new formula object where all the atoms have been added up
|
27
|
+
def +(*others)
|
28
|
+
new_form = self.dup
|
29
|
+
others.each do |form|
|
30
|
+
new_form.merge!(form) {|key, oldval, newval| new_form[key] = oldval+newval }
|
31
|
+
end
|
32
|
+
new_form
|
33
|
+
end
|
34
|
+
|
26
35
|
def self.from_aaseq(aaseq)
|
27
36
|
hash = aaseq.each_char.inject({}) do |hash,aa|
|
28
37
|
hash.merge(Mspire::Isotope::AA::FORMULAS[aa]) {|h,o,n| (o ? o : 0) +n }
|
@@ -38,5 +47,22 @@ module Mspire
|
|
38
47
|
mss - (Mspire::Mass::ELECTRON * charge)
|
39
48
|
end
|
40
49
|
|
50
|
+
def to_s(alphabetize=true)
|
51
|
+
h = alphabetize ? self.sort : self
|
52
|
+
h.flat_map {|k,v|
|
53
|
+
[k.capitalize, v > 1 ? v : '']
|
54
|
+
}.join
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_hash
|
58
|
+
Hash[ self ]
|
59
|
+
end
|
60
|
+
|
61
|
+
alias_method :old_equal, '=='.to_sym
|
62
|
+
|
63
|
+
def ==(other)
|
64
|
+
old_equal(other) && self.charge == other.charge
|
65
|
+
end
|
66
|
+
|
41
67
|
end
|
42
68
|
end
|
@@ -7,17 +7,47 @@ describe Mspire::MolecularFormula do
|
|
7
7
|
it 'can be initialized with a String or Hash' do
|
8
8
|
data = {h: 22, c: 12, n: 1, o: 3, s: 2}
|
9
9
|
mf = Mspire::MolecularFormula.new "H22BeC12N1O3S2Li2"
|
10
|
-
mf.should == {:h=>22, :be=>1, :c=>12, :n=>1, :o=>3, :s=>2, :li=>2}
|
10
|
+
mf.to_hash.should == {:h=>22, :be=>1, :c=>12, :n=>1, :o=>3, :s=>2, :li=>2}
|
11
11
|
mf = Mspire::MolecularFormula.new(data)
|
12
|
-
mf.should == data
|
12
|
+
mf.to_hash.should == data
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'can be initialized with charge, too' do
|
16
|
+
mf = Mspire::MolecularFormula.new "H22BeC12N1O3S2Li2", 2
|
17
|
+
mf.to_hash.should == {:h=>22, :be=>1, :c=>12, :n=>1, :o=>3, :s=>2, :li=>2}
|
18
|
+
mf.charge.should == 2
|
13
19
|
end
|
14
20
|
|
15
21
|
it 'expects properly capitalized abbreviations' do
|
16
|
-
Mspire::MolecularFormula.new('Ni7Se3').should == {:ni=>7, :se=>3}
|
22
|
+
Mspire::MolecularFormula.new('Ni7Se3').to_hash.should == {:ni=>7, :se=>3}
|
17
23
|
# there is no such thing as the E element, so this is going to get the
|
18
24
|
# user in trouble. However, this is the proper interpretation of the
|
19
25
|
# formula.
|
20
|
-
Mspire::MolecularFormula.new('Ni7SE3').should == {:ni=>7, :s=>1, :e=>3}
|
26
|
+
Mspire::MolecularFormula.new('Ni7SE3').to_hash.should == {:ni=>7, :s=>1, :e=>3}
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'an object' do
|
30
|
+
|
31
|
+
subject {
|
32
|
+
data = {h: 22, c: 12, n: 1, o: 3, s: 2, be: 1}
|
33
|
+
Mspire::MolecularFormula.new(data)
|
34
|
+
}
|
35
|
+
|
36
|
+
it 'the string output is a standard molecular formula' do
|
37
|
+
subject.to_s.should == "BeC12H22NO3S2"
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'can be converted to a hash' do
|
41
|
+
subject.to_hash.should == {h: 22, c: 12, n: 1, o: 3, s: 2, be: 1}
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'is only equal if the charge is equal' do
|
45
|
+
another = subject.dup
|
46
|
+
another.should == subject
|
47
|
+
another.charge = 2
|
48
|
+
another.should_not == subject
|
49
|
+
end
|
50
|
+
|
21
51
|
end
|
22
52
|
|
23
53
|
end
|