Class: Ms::Msrun::Plms1

Inherits:
Object
  • Object
show all
Defined in:
lib/ms/msrun/plms1.rb

Overview

Prince Lab MS 1: a simple format for reading and writing MS1 level mass spec data

see Ms::Msrun::Plms1::SPECIFICATION for the file specification

Constant Summary collapse

SPECIFICATION =
<<-HERE
  # The file format contains no newlines but is shown here broken into lines for
  # clarity.  Data should be little endian.  Comments begin with '#' but are not
  # part of the spec. Angled brackets '<>' indicate the data type and square
  # brackets '[]' the name of the data. An ellipsis '...' represents a
  # continuous array of data points.

  <uint32>[Number of scans]
  <uint32>[scan number] ...  # array of scan numbers as uint32
  <float64>[time point] ...  # array of time points as double precision floats (in seconds)
  # this is a repeating unit based on [Number of scans]:
  <uint32>[Number of data rows]  #  almost always == 2 (m/z, intensity)
  # this is a repeating unit based on [Number of data rows]
  <uint32>[Number of data points]
  <float64>[data point] ...  # array of data points as double precision floats
HERE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_scan_numbers = [], _times = [], _spectra = []) ⇒ Plms1

Returns a new instance of Plms1.



69
70
71
# File 'lib/ms/msrun/plms1.rb', line 69

def initialize(_scan_numbers=[], _times=[], _spectra=[])
  (@scan_numbers, @times, @spectra) = [_scan_numbers, _times, _spectra]
end

Instance Attribute Details

#scan_numbersObject

an array of scan numbers



62
63
64
# File 'lib/ms/msrun/plms1.rb', line 62

def scan_numbers
  @scan_numbers
end

#spectraObject

an array that contains parallel rows of arrays holding the actual data these are NOT bona fide spectra objects



67
68
69
# File 'lib/ms/msrun/plms1.rb', line 67

def spectra
  @spectra
end

#timesObject

an array of time data



64
65
66
# File 'lib/ms/msrun/plms1.rb', line 64

def times
  @times
end

Instance Method Details

#read(io_or_filename) ⇒ Object

returns self for chaining



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ms/msrun/plms1.rb', line 84

def read(io_or_filename)
  io = 
    if io_or_filename.is_a?(String)
      filename = io_or_filename
      File.open(io_or_filename,'rb')
    else ; io_or_filename end

  num_scans = read_uint32(io)[0]
  @scan_numbers = read_uint32(io, num_scans)
  @times = read_float64(io, num_scans)
  @spectra = num_scans.times.map do
   read_uint32(io)[0].times.map do
      read_float64(io, read_uint32(io)[0])
    end
  end
  io.close if filename
  self
end

#read_float64(io, cnt = 1) ⇒ Object

returns an array of Floats



79
80
81
# File 'lib/ms/msrun/plms1.rb', line 79

def read_float64(io, cnt=1)
  io.read(cnt*8).unpack("E*")
end

#read_uint32(io, cnt = 1) ⇒ Object

returns an array of Integers



74
75
76
# File 'lib/ms/msrun/plms1.rb', line 74

def read_uint32(io, cnt=1)
  io.read(cnt*4).unpack("V*")
end

#write(filename = nil) ⇒ Object

returns the string if no filename given



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ms/msrun/plms1.rb', line 114

def write(filename=nil)
  out = 
    if filename
      File.open(filename,'w')
    else
      StringIO.new
    end
  write_uint32(out, spectra.size)
  write_uint32(out, scan_numbers)
  write_float64(out, times)
  spectra.each do |spectrum|
    write_uint32(out, spectrum.size)  # number of rows
    spectrum.each do |row|
      write_uint32(out, row.size)
      write_float64(out, row)
    end
  end
  if filename
    out.close
    filename
  else
    out.string
  end
end

#write_float64(out, data) ⇒ Object



108
109
110
111
# File 'lib/ms/msrun/plms1.rb', line 108

def write_float64(out, data)
  to_pack = data.is_a?(Array) ? data : [data]
  out << to_pack.pack('E*')
end

#write_uint32(out, data) ⇒ Object



103
104
105
106
# File 'lib/ms/msrun/plms1.rb', line 103

def write_uint32(out, data)
  to_pack = data.is_a?(Array) ? data : [data]
  out << to_pack.pack('V*')
end