molecules 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/molecules/calc.rb +48 -43
- data/lib/molecules/empirical_formula.rb +1 -1
- metadata +4 -4
data/lib/molecules/calc.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require '
|
1
|
+
require 'tap/task'
|
2
|
+
require 'molecules/empirical_formula'
|
2
3
|
require 'molecules/libraries/polypeptide'
|
3
4
|
|
4
5
|
# patch for ruby units
|
@@ -7,29 +8,37 @@ class Unit < Numeric # :nodoc:
|
|
7
8
|
UNIT_DEFINITIONS['<dalton>'] = [%w{Da Dalton Daltons dalton daltons}, 1/6.0221415e26, :mass, %w{<kilogram>}]
|
8
9
|
end
|
9
10
|
Unit.setup
|
10
|
-
|
11
|
-
module Molecules
|
12
|
-
|
13
|
-
# :startdoc::
|
14
|
-
#
|
15
|
-
# Calculates the mass of a molecule.
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
11
|
+
|
12
|
+
module Molecules
|
13
|
+
|
14
|
+
# :startdoc::task a mass calculator
|
15
|
+
#
|
16
|
+
# Calculates the mass of a molecule or formula. The options can be used to
|
17
|
+
# alter the output (precision, mass calculation method etc.)
|
18
|
+
#
|
19
|
+
# % tap run -- molecules/calc H2O --: dump
|
20
|
+
# 18.0106 Da
|
21
|
+
#
|
22
|
+
# % tap run -- molecules/calc "NH3 + H2O" --precision 2 --: dump
|
23
|
+
# 35.04 Da
|
24
|
+
#
|
25
|
+
# Units can carry prefixes (ex 'mm', 'kg'). See {Ruby Units}[http://ruby-units.rubyforge.org/ruby-units/]
|
26
|
+
# for more information.
|
27
|
+
#
|
28
|
+
# % tap run -- molecules/calc H2O --units yg --precision 2 --: dump
|
29
|
+
# 29.91 yg
|
21
30
|
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
class Calc < Tap::Task
|
30
|
-
|
31
|
-
config :type, :monoisotopic # the mass type calculated
|
32
|
-
config :precision, nil, :short => 'p' # the precision of the mass
|
31
|
+
# Note that Calc returns instances of Unit, which by default prints itself
|
32
|
+
# with a precison of 4. To view the full-precision value, inspect the scalar
|
33
|
+
# value of the result.
|
34
|
+
#
|
35
|
+
# % tap run -- molecules/calc H2O --: inspect -m scalar
|
36
|
+
# 18.0105646863
|
37
|
+
#
|
38
|
+
class Calc < Tap::Task
|
39
|
+
|
40
|
+
config :type, :monoisotopic # the mass type calculated
|
41
|
+
config :precision, nil, :short => 'p' # the precision of the mass
|
33
42
|
config :units, "Da", :short => 'u', &c.string # the mass unit reported
|
34
43
|
config :composition, false, :short => 'c', &c.flag # reports the composition, not the formula
|
35
44
|
|
@@ -40,27 +49,23 @@ module Molecules
|
|
40
49
|
EmpiricalFormula.parse(formula)
|
41
50
|
end
|
42
51
|
|
43
|
-
# Returns an array of the calculated masses, in the correct unit.
|
44
|
-
def process(
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
else raise "unknown mass type: #{type}"
|
51
|
-
end
|
52
|
+
# Returns an array of the calculated masses, in the correct unit.
|
53
|
+
def process(formula)
|
54
|
+
mass = parse(formula).mass do |element|
|
55
|
+
case type
|
56
|
+
when :monoisotopic then element.mass
|
57
|
+
when :average then element.std_atomic_weight.value
|
58
|
+
else raise "unknown mass type: #{type}"
|
52
59
|
end
|
60
|
+
end
|
53
61
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
mass
|
62
|
-
end
|
62
|
+
mass = Unit.new(mass, "Da").convert_to(units)
|
63
|
+
unless precision == nil
|
64
|
+
mass = Unit.new( Utils.round(mass.scalar, precision), units)
|
65
|
+
end
|
66
|
+
|
67
|
+
mass
|
63
68
|
end
|
64
|
-
|
65
|
-
end
|
69
|
+
|
70
|
+
end
|
66
71
|
end
|
@@ -299,7 +299,7 @@ module Molecules
|
|
299
299
|
# - Masses are calculated such that mathematical operations
|
300
300
|
# are performed on the return of the block.
|
301
301
|
#
|
302
|
-
def mass(&block)
|
302
|
+
def mass(&block) # :yields: element
|
303
303
|
if block_given?
|
304
304
|
mass = 0
|
305
305
|
each {|e, n| mass = (yield(e) * n) + mass }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: molecules
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Chiang
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-05-06 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 0.17.0
|
34
34
|
version:
|
35
35
|
description:
|
36
36
|
email: simon.a.chiang@gmail.com
|
@@ -73,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
73
|
requirements: []
|
74
74
|
|
75
75
|
rubyforge_project: bioactive
|
76
|
-
rubygems_version: 1.
|
76
|
+
rubygems_version: 1.3.1
|
77
77
|
signing_key:
|
78
78
|
specification_version: 2
|
79
79
|
summary: A library of molecules for scientific calculations in Ruby.
|