Class: Ms::Calc::Pepmass

Inherits:
Molecules::Calc
  • Object
show all
Defined in:
lib/ms/calc/pepmass.rb

Overview

:startdoc::task a peptide mass calculator

Calculates the mass of a molecule or peptide. Molecules are entered as simple or compound formulae; polypeptides can be specified using the one-letter residue codes bracketed by semicolons. The options can be used to alter the output (precision, mass calculation method etc.)

% tap run -- pepmass H2O --: dump
18.0106 Da

% tap run -- pepmass :RPPGFSPFR: --: dump
1059.56 Da

Unimod modifcations may be specified by name at the polypeptide termini, provided a unimod database is available. Use ‘%’ signs as in a SQL query to shorten the name. The Unimod generator may be used to generate a unimod database if needed:

% tap generate unimod
% tap run -- pepmass Acetyl:RPPGFSPFR:Hydroxyl% -p 2 --: dump
1117.57 Da

Sequel and sqlite3-ruby must be installed for this feature to work.

* Sequel[http://sequel.rubyforge.org/rdoc/]
* sqlite3-ruby[http://rubyforge.org/projects/sqlite-ruby/]

Constant Summary collapse

EmpiricalFormula =
Molecules::EmpiricalFormula
WATER =
EmpiricalFormula.parse "H2O"

Instance Method Summary collapse

Instance Method Details

#find_mod(code_name) ⇒ Object

Attempts to find and instantiate an EmpiricalFormula for a unimod modification matching code_name.

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ms/calc/pepmass.rb', line 50

def find_mod(code_name)
  raise ArgumentError, "the unimod database does not exist" unless File.exists?(unimod)

  results = []
  db = SQLite3::Database.new(unimod)
  db.execute(mod_query(code_name)) do |row|
    results << row
  end
  db.close
  
  case results.length
  when 1 then EmpiricalFormula.parse_simple(results[0][1])
  when 0 then raise "could not find modification: #{code_name}"
  else raise ArgumentError, "multiple modifications found for: '#{code_name}' (#{results.collect {|result| result[0]}.join(', ')})"
  end
end

#mod_query(code_name) ⇒ Object

Formulates a query for a modification matching code_name for the unimod database. If the code_name contains a ‘%’ then the query will use a LIKE syntax, otherwise the code_name will be searced for exactly.



43
44
45
46
# File 'lib/ms/calc/pepmass.rb', line 43

def mod_query(code_name)
  # should do a rails-like escape on code_name
  "SELECT code_name, composition FROM modifications WHERE code_name #{code_name.include?('%') ? 'LIKE' : '='} '#{code_name}'"
end

#parse(formula) ⇒ Object

Parses the formula string into an EmpiricalFormula. Can be used as a hook for more complicated formulae in subclases.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ms/calc/pepmass.rb', line 70

def parse(formula)
  EmpiricalFormula.parse(formula) do |str|
    case str
    when /^(.*?):([A-Z]+):?(.*)$/
      peptide = Molecules::Libraries::Polypeptide.new($2) + WATER
      peptide += find_mod($1) unless $1.to_s.empty? 
      peptide += find_mod($3) unless $3.to_s.empty?
      peptide
    else nil
    end
  end
end