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.



56
57
58
# File 'lib/ms/msrun/plms1.rb', line 56

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



49
50
51
# File 'lib/ms/msrun/plms1.rb', line 49

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



54
55
56
# File 'lib/ms/msrun/plms1.rb', line 54

def spectra
  @spectra
end

#timesObject

an array of time data



51
52
53
# File 'lib/ms/msrun/plms1.rb', line 51

def times
  @times
end

Instance Method Details

#read(io_or_filename) ⇒ Object

returns self for chaining



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ms/msrun/plms1.rb', line 71

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



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

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

#read_uint32(io, cnt = 1) ⇒ Object

returns an array of Integers



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

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

#write(filename = nil) ⇒ Object

returns the string if no filename given



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ms/msrun/plms1.rb', line 101

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



95
96
97
98
# File 'lib/ms/msrun/plms1.rb', line 95

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

#write_uint32(out, data) ⇒ Object



90
91
92
93
# File 'lib/ms/msrun/plms1.rb', line 90

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