Class: Mspire::Plms1

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

Overview

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

see Mspire::Plms1::SPECIFICATION for the file specification

Constant Summary

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



71
72
73
# File 'lib/mspire/plms1.rb', line 71

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



65
66
67
# File 'lib/mspire/plms1.rb', line 65

def scan_numbers
  @scan_numbers
end

#spectraObject

an array that contains spectrum objects



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

def spectra
  @spectra
end

#timesObject

an array of time data



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

def times
  @times
end

Instance Method Details

#read(io_or_filename) ⇒ Object

returns self for chaining



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mspire/plms1.rb', line 86

def read(io_or_filename)
  openany(io_or_filename) do |io|
    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
      data = read_uint32(io)[0].times.map do
        read_float64(io, read_uint32(io)[0])
      end
      Mspire::Spectrum.new(data)
    end
  end
  self
end

#read_float64(io, cnt = 1) ⇒ Object

returns an array of Floats



81
82
83
# File 'lib/mspire/plms1.rb', line 81

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

#read_uint32(io, cnt = 1) ⇒ Object

returns an array of Integers



76
77
78
# File 'lib/mspire/plms1.rb', line 76

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

#write(filename = nil, ascii = false) ⇒ Object

returns the string if no filename given



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mspire/plms1.rb', line 136

def write(filename=nil, ascii=false)
  if ascii
    write_ascii(filename)
  else
    write_file_or_string(filename) do |out|
      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
        if spectrum.size > 0
          mzs = spectrum.mzs
          write_uint32(out, mzs.size)
          write_float64(out, mzs)
          intensities = spectrum.intensities
          write_uint32(out, intensities.size)
          write_float64(out, intensities)
        end
      end
    end
  end
end

#write_ascii(filename = nil) ⇒ Object

writes an ascii version of the format It is the same as the binary format, except a newline follows each length indicator or array of data. An empty line represents an empty array.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mspire/plms1.rb', line 115

def write_ascii(filename=nil)
  write_file_or_string(filename) do |out|
    out.puts scan_numbers.size
    out.puts scan_numbers.join(' ')
    out.puts times.join(' ')
    spectra.each do |spectrum|
      puts "HIAYDSFA DSF"
      puts spectrum.class
      p spectrum
      out.puts spectrum.size
      if spectrum.size > 0
        out.puts spectrum.mzs.size
        out.puts spectrum.mzs.join(' ')
        out.puts spectrum.intensities.size
        out.puts spectrum.intensities.join(' ')
      end
    end
  end
end

#write_float64(out, data) ⇒ Object



106
107
108
109
# File 'lib/mspire/plms1.rb', line 106

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

#write_uint32(out, data) ⇒ Object



101
102
103
104
# File 'lib/mspire/plms1.rb', line 101

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