molecules 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
- require 'molecules/empirical_formula'
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::manifest a mass calculator
14
- #
15
- # Calculates the mass of a molecule. Compound formulae are allowed and you may
16
- # specify a list of formulae. The options can be used to alter the output (precision,
17
- # mass calculation method etc.)
18
- #
19
- # % tap run -- molecules/calc H2O
20
- # I[17:08:00] 18.0105646863 Da H2O
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
- # % tap run -- molecules/calc H2O --units yg --precision 6
23
- # I[17:08:21] 29.907243 yg H2O
24
- #
25
- # % tap run -- molecules/calc 'C3H5NO + H2O' C50H73N15O11 -p 2
26
- # I[17:08:53] 89.05 Da C3H5NO + H2O
27
- # I[17:08:53] 1059.56 Da C50H73N15O11
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(*formulae)
45
- formulae.collect do |formula_str|
46
- mass = parse(formula_str).mass do |element|
47
- case type
48
- when :monoisotopic then element.mass
49
- when :average then element.std_atomic_weight.value
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
- mass = Unit.new(mass, "Da").convert_to(units)
55
- unless precision == nil
56
- mass = Unit.new( Utils.round(mass.scalar, precision), units)
57
- end
58
-
59
- log "#{mass.scalar} #{mass.units}", composition ? formula : formula_str
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.1.3
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: 2008-10-13 00:00:00 -06:00
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.10.8
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.2.0
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.