Class: Ms::Mascot::Mgf::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/ms/mascot/mgf/entry.rb

Overview

Represents a mascot generic file (mgf) formatted entry.

BEGIN IONS
TITLE=7100401blank.190.190.2.dta
CHARGE=2+
PEPMASS=321.571138
100.266 2.0
111.323 2.5
...
496.110 3.3
601.206 1.3
END IONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers = {}, data = []) ⇒ Entry

Initialized a new Entry using the headers and data. Set charge and pepmass using the CHARGE and PEPMASS headers.



63
64
65
66
67
68
69
70
71
72
# File 'lib/ms/mascot/mgf/entry.rb', line 63

def initialize(headers={}, data=[])
  @headers = {}
  @pepmass = nil
  @charge = nil
  @data = data

  headers.each_pair do |key, value|
    self[key] = value
  end
end

Instance Attribute Details

#chargeObject

The charge of the entry



53
54
55
# File 'lib/ms/mascot/mgf/entry.rb', line 53

def charge
  @charge
end

#dataObject

The data (mz/intensity) for the entry



59
60
61
# File 'lib/ms/mascot/mgf/entry.rb', line 59

def data
  @data
end

#headersObject (readonly)

A hash of mgf headers, not including CHARGE and PEPMASS



50
51
52
# File 'lib/ms/mascot/mgf/entry.rb', line 50

def headers
  @headers
end

#pepmassObject

The peptide mass of the entry



56
57
58
# File 'lib/ms/mascot/mgf/entry.rb', line 56

def pepmass
  @pepmass
end

Class Method Details

.parse(str) ⇒ Object

Parses the entry string into an Mgf::Entry. The entry must be complete and properly formatted, ie it must begin with a ‘BEGIN IONS’ line and end with an ‘END IONS’ line.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ms/mascot/mgf/entry.rb', line 24

def parse(str)
  entry = Entry.new

  lines = str.strip.split(/\s*\r?\n\s*/)

  unless lines.shift == "BEGIN IONS"
    raise ArgumentError, "input should begin with 'BEGIN IONS'"
  end

  unless lines.pop == "END IONS"
    raise ArgumentError, "input should end with 'END IONS'"
  end

  lines.each do |line|
    if line =~ /^(.*?)=(.*)$/
      entry[$1] = $2
    else
      entry.data << line.split(/\s+/, 2).collect {|i| i.to_f }
    end
  end

  entry
end

Instance Method Details

#[](key) ⇒ Object

Retrieve a header using an mgf header string. CHARGE and PEPMASS headers can be retrieved using [], and will reflect the current values of charge and pepmass. Keys are stringified and upcased.



77
78
79
80
81
82
83
84
# File 'lib/ms/mascot/mgf/entry.rb', line 77

def [](key)
  key = key.to_s.upcase
  case key
  when "PEPMASS" then pepmass.to_s
  when "CHARGE" then charge_to_s
  else headers[key]
  end
end

#[]=(key, value) ⇒ Object

Set a header using an mgf header string. CHARGE and PEPMASS headers may be set using using []=, and will modify the current values of charge and pepmass. Keys are stringified and upcased.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ms/mascot/mgf/entry.rb', line 89

def []=(key, value)
  key = key.to_s.upcase
  case key
  when "PEPMASS" 
    self.pepmass = value.to_f
  when "CHARGE" 
    value = case value
    when Fixnum then value
    when /^(\d+)([+-])$/ then $1.to_i * ($2 == "+" ? 1 : -1) 
    else raise "charge should be an number, or a string formatted like '1+' or '1-'"
    end
    
    self.charge = value
  else
    headers[key] = value
  end
end

#dump(target = "", options = {}) ⇒ Object

Formats and puts self to the target. Use the options to modify the output:

headers

an array of headers to include (by default all headers will be included; pepmass and charge will always be included)

pepmass_precision

integer value specifying precision of pepmass

mz_precision

integer value specifying precision of mz values

intensity_precision

integer value specifying precision of intensity values



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ms/mascot/mgf/entry.rb', line 117

def dump(target="", options={})
  options = {
    :mz_precision => nil,
    :intensity_precision => nil,
    :pepmass_precision => nil,
    :headers => nil
  }.merge(options)

  target << "BEGIN IONS\n"
  (options[:headers] || headers.keys).each do |key|
    target << "#{key.upcase}=#{headers[key]}\n"
  end
    
  target << "CHARGE=#{charge_to_s}\n"
  target << "PEPMASS=#{format options[:pepmass_precision]}\n" % pepmass

  data_format = "#{format options[:mz_precision]} #{format options[:intensity_precision]}\n"
  data.each do |data_point|
    target << (data_format % data_point)
  end
  
  target << "END IONS\n"
  target
end

#to_sObject

Returns self formatted as a string



143
144
145
# File 'lib/ms/mascot/mgf/entry.rb', line 143

def to_s
  dump
end