Class: Ms::Xcalibur::PeakFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ms/xcalibur/peak_file.rb

Overview

A simple representation of a peak file exported from Xcalibur Qual Browser (v 2.0). The expected format of a peak file is as shown below:

[peak_file.txt]
SPECTRUM - MS
GSE_T29K_080703143635.raw
ITMS + c ESI Full ms [300.00-2000.00]
Scan #: 11
RT: 0.07
Data points: 1490
Mass	Intensity
300.516479	2000.0
301.392487	1000.0
302.465759	3000.0
...

Any headers matching the pattern ‘key: value’ will be parsed as a header, while other lines (ex: SPECTRUM - MS) are parsed into the description.

Constant Summary collapse

HEADER_ORDER =

The order of headers observed in export files

[
  "Scan #",
  "RT",
  "Mass defect",
  "Data points"
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PeakFile.



70
71
72
73
74
# File 'lib/ms/xcalibur/peak_file.rb', line 70

def initialize(desc=[], headers={}, data=[])
  @desc = desc
  @headers = headers
  @data = data
end

Instance Attribute Details

#dataObject

An array of (mz, intensity) values



68
69
70
# File 'lib/ms/xcalibur/peak_file.rb', line 68

def data
  @data
end

#descObject

An array of description lines



62
63
64
# File 'lib/ms/xcalibur/peak_file.rb', line 62

def desc
  @desc
end

#headersObject

A hash of headers



65
66
67
# File 'lib/ms/xcalibur/peak_file.rb', line 65

def headers
  @headers
end

Class Method Details

.parse(str) ⇒ Object

Parses the input string into a PeakFile



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ms/xcalibur/peak_file.rb', line 28

def parse(str)
  peak_file = PeakFile.new
  mode = :header
  str.each_line do |line|
    case mode
    when :header

      case line
      when /^(.*?): (.*)$/
        peak_file.headers[$1] = $2.strip
      when /Mass\sIntensity/
        mode = :data
      else
        peak_file.desc << line.strip
      end

    when :data
      peak_file.data << line.split(/\s/).collect {|mz| mz.to_f }
    end
  end

  peak_file
end

Instance Method Details

#to_s(sep = "\r\n") ⇒ Object

Recreates the peak file



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ms/xcalibur/peak_file.rb', line 77

def to_s(sep="\r\n")
  lines = desc + 
  HEADER_ORDER.collect do |key|
    next nil unless headers.has_key?(key)
    "#{key}: #{headers[key]}"
  end.compact +
  ["Mass\tIntensity"] +
  data.collect do |point|
    point.join("\t")
  end

  lines.join(sep) + sep
end