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.



73
74
75
76
77
78
79
80
81
82
# File 'lib/ms/mascot/mgf/entry.rb', line 73

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



69
70
71
# File 'lib/ms/mascot/mgf/entry.rb', line 69

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.



87
88
89
90
91
92
93
94
# File 'lib/ms/mascot/mgf/entry.rb', line 87

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.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ms/mascot/mgf/entry.rb', line 99

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ms/mascot/mgf/entry.rb', line 127

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
  
  if charge
    target << "CHARGE=#{charge_to_s}\n"
  end
  
  if pepmass
    target << "PEPMASS=#{format options[:pepmass_precision]}\n" % pepmass
  end
  
  entry = data[0]
  data_format = case
  when entry == nil then nil
  when entry.kind_of?(Array) && entry.length == 2
    "#{format options[:mz_precision]} #{format options[:intensity_precision]}\n"
  else
    "#{format options[:mz_precision]}\n"
  end
  
  data.each do |data_point|
    target << (data_format % data_point)
  end
  
  target << "END IONS\n"
  target
end

#titleObject

returns the title of the entry (or nil if none)



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

def title
  @headers['TITLE']
end

#title=(string) ⇒ Object

sets the title



64
65
66
# File 'lib/ms/mascot/mgf/entry.rb', line 64

def title=(string)
  @headers['TITLE'] = string
end

#to_sObject

Returns self formatted as a string



166
167
168
# File 'lib/ms/mascot/mgf/entry.rb', line 166

def to_s
  dump
end