bahuvrihi-molecules 0.1.1 → 0.1.2
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/lib/molecules/calc.rb +22 -83
- metadata +1 -1
data/lib/molecules/calc.rb
CHANGED
@@ -10,101 +10,40 @@ Unit.setup
|
|
10
10
|
|
11
11
|
module Molecules
|
12
12
|
|
13
|
-
# :startdoc::manifest a mass calculator
|
14
|
-
# Calculates the mass of a molecule or empirical formula. The
|
15
|
-
# options can be used to alter the output (precision, mass
|
16
|
-
# calculation method etc.) You may enter compound formulae, or
|
17
|
-
# a list of formulae. In addition, polypeptides can be specified
|
18
|
-
# using the one-letter residue codes:
|
13
|
+
# :startdoc::manifest a mass calculator
|
19
14
|
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
# % tap -- molecules/calc H2O -u kg
|
24
|
-
# I[13:35:59] 2.99072e-026 kg H2O
|
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.)
|
25
18
|
#
|
26
|
-
# % tap -- molecules/calc
|
27
|
-
# I[17:08:
|
28
|
-
# I[17:08:21] 1059.56 Da C50H73N15O11
|
29
|
-
#
|
30
|
-
# % tap -- molecules/calc :RPPGFSPFR
|
31
|
-
# I[13:35:02] 1059.56 Da :RPPGFSPFR
|
32
|
-
#
|
33
|
-
# Furthermore, if a unimod path is specified in the configurations,
|
34
|
-
# unimod modifcations may be specified by name as the polypeptide
|
35
|
-
# termini. Use '%' signs as in a SQL query to shorten the name:
|
36
|
-
#
|
37
|
-
# % tap -- molecules/calc 'Acetyl:RPPGFSPFR:Hydroxyl%' --unimod-path <...>
|
38
|
-
# I[13:33:25] 1059.56 Da Acetyl:RPPGFSPFR:Hydroxyl%
|
19
|
+
# % tap run -- molecules/calc H2O
|
20
|
+
# I[17:08:00] 18.0105646863 Da H2O
|
39
21
|
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
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
|
+
#
|
46
29
|
class Calc < Tap::Task
|
47
30
|
|
48
31
|
config :type, :monoisotopic # the mass type calculated
|
49
32
|
config :precision, nil, :short => 'p' # the precision of the mass
|
50
33
|
config :units, "Da", :short => 'u', &c.string # the mass unit reported
|
51
34
|
config :composition, false, :short => 'c', &c.flag # reports the composition, not the formula
|
52
|
-
config :unimod_path, nil do |path| # the path to the unimod database
|
53
|
-
case
|
54
|
-
when path == nil then nil
|
55
|
-
when File.exists?(path) then path
|
56
|
-
else raise "path to unimod db does not exist: #{path}"
|
57
|
-
end
|
58
|
-
end
|
59
35
|
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
|
64
|
-
|
65
|
-
# should do a rails-like escape on code_name
|
66
|
-
"SELECT code_name, composition FROM modifications WHERE code_name #{code_name.include?('%') ? 'LIKE' : '='} '#{code_name}'"
|
36
|
+
# Parses the formula string into an EmpiricalFormula.
|
37
|
+
# Can be used as a hook for more complicated formulae
|
38
|
+
# in subclases.
|
39
|
+
def parse(formula)
|
40
|
+
EmpiricalFormula.parse(formula)
|
67
41
|
end
|
68
42
|
|
69
|
-
# Attempts to find and instantiate an EmpiricalFormula for
|
70
|
-
# a unimod modification matching code_name.
|
71
|
-
def find_mod(code_name)
|
72
|
-
raise "no unimod_path was specified" if unimod_path == nil
|
73
|
-
require 'sqlite3' unless Object.const_defined?(:SQLite3)
|
74
|
-
|
75
|
-
results = []
|
76
|
-
db = SQLite3::Database.new(unimod_path)
|
77
|
-
db.execute(mod_query(code_name)) do |row|
|
78
|
-
results << row
|
79
|
-
end
|
80
|
-
db.close
|
81
|
-
|
82
|
-
case results.length
|
83
|
-
when 1 then EmpiricalFormula.parse_simple(results[0][1])
|
84
|
-
when 0 then raise "could not find modification: #{code_name}"
|
85
|
-
else raise "multiple modifications found for: #{code_name} (#{results.collect {|result| result[0]}.join(', ')})"
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
WATER = EmpiricalFormula.parse "H2O"
|
90
|
-
HYDROGEN = EmpiricalFormula.parse "H"
|
91
|
-
HYDROXIDE = EmpiricalFormula.parse "OH"
|
92
|
-
|
93
43
|
# Returns an array of the calculated masses, in the correct unit.
|
94
44
|
def process(*formulae)
|
95
|
-
formulae.collect do |formula_str|
|
96
|
-
|
97
|
-
case str
|
98
|
-
when /^(.*?):([A-Z]+):?(.*)$/
|
99
|
-
peptide = Libraries::Polypeptide.new($2) + WATER
|
100
|
-
peptide += find_mod($1) unless $1.to_s.empty?
|
101
|
-
peptide += find_mod($3) unless $3.to_s.empty?
|
102
|
-
peptide
|
103
|
-
else nil
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
mass = formula.mass do |element|
|
45
|
+
formulae.collect do |formula_str|
|
46
|
+
mass = parse(formula_str).mass do |element|
|
108
47
|
case type
|
109
48
|
when :monoisotopic then element.mass
|
110
49
|
when :average then element.std_atomic_weight.value
|
@@ -116,8 +55,8 @@ module Molecules
|
|
116
55
|
unless precision == nil
|
117
56
|
mass = Unit.new( Utils.round(mass.scalar, precision), units)
|
118
57
|
end
|
119
|
-
|
120
|
-
log mass, composition ? formula : formula_str
|
58
|
+
|
59
|
+
log "#{mass.scalar} #{mass.units}", composition ? formula : formula_str
|
121
60
|
|
122
61
|
mass
|
123
62
|
end
|