Class: Ms::Msrun::Regexp::Mzxml

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msrun_object, io, version) ⇒ Mzxml

Returns a new instance of Mzxml.



20
21
22
23
24
# File 'lib/ms/msrun/regexp/mzxml.rb', line 20

def initialize(msrun_object, io, version)
  @msrun = msrun_object
  @io = io
  @version = version
end

Instance Attribute Details

#ioObject

Returns the value of attribute io.



18
19
20
# File 'lib/ms/msrun/regexp/mzxml.rb', line 18

def io
  @io
end

#msrunObject

Returns the value of attribute msrun.



18
19
20
# File 'lib/ms/msrun/regexp/mzxml.rb', line 18

def msrun
  @msrun
end

#versionObject

Returns the value of attribute version.



18
19
20
# File 'lib/ms/msrun/regexp/mzxml.rb', line 18

def version
  @version
end

Class Method Details

.parse_peaksObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ms/msrun/regexp/mzxml.rb', line 67

def self.parse_peaks
  precision = 32
  byte_order = 'network'
  while line = @io.gets
    if line =~ /(precision|byteOrder)=["'](\w+)["']/
      case $1
      when 'precision'
        $2.to_i
      when 'byteOrder'
        byte_order = $2
      end
    end
    if line =~ %r{</peaks>}
      first_pos = line.index('>')
      last_pos = @io.pos + line.rindex("</peaks>")
      Ms::Spectrum
      break
    end
  end
end

.parse_precursor(line) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ms/msrun/regexp/mzxml.rb', line 50

def self.parse_precursor(line)
  prec = Ms::Precursor.new
  loop do
    if line =~ /precursorIntensity=['"]([\d\.]+)['"]/
      prec[1] = $1.to_f
    end
    if line =~ /precursorCharge=["'](\d+)["']/
      prec[3] = [$1.to_i]
    end
    if line =~ %r{>([\d\.]+)</precursorMz>}
      prec[0] = $1.to_f
      break
    end
    line = io.gets
  end
end

.parse_scan(start_byte, length) ⇒ Object

assumes that the io object has been set to the beginning of the scan element. Returns an Ms::Scan object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ms/msrun/regexp/mzxml.rb', line 91

def self.parse_scan(start_byte, length)
  @io.pos = start_byte
  hash = {}
  while line = @io.gets do
    if line =~ /^\s*<precursorMz/
      self.parse_precursor(line)
      self.parse_peaks
      break
    end
    if line =~ /(\w+)=["'](\w+)["']/
      hash[$1] = $2
    end
  end
  new_scan_from_hash(hash)
end

Instance Method Details

#new_scan_from_hash(hash) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ms/msrun/regexp/mzxml.rb', line 107

def new_scan_from_hash(hash)
  scan = Ms::Scan.new  # array class creates one with 9 positions
  scan[0] = hash['num'].to_i
  scan[1] = hash['msLevel'].to_i
  if x = hash['retentionTime']
    scan[2] = x[2...-1].to_f
  end
  if x = hash['startMz']
    scan[3] = x.to_f
    scan[4] = hash['endMz'].to_f
    scan[5] = hash['peaksCount'].to_i
    scan[6] = hash['totIonCurrent'].to_f
  end
  scan
end

#parse_headerObject

returns the msrun



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ms/msrun/regexp/mzxml.rb', line 27

def parse_header
  while line = @io.gets
    if line =~ %r{\s+fileName=['"](.*?)['"]}
      (bn, dn) = Ms::Mzxml.parent_basename_and_dir($1)
      @msrun.parent_basename = bn
      @msrun.parent_location = dn
    end
    if line =~ /\s+scanCount=['"](\w+)['"]/
      @msrun.scan_count = $1.to_i
    end
    if line =~ /startTime=['"]([\w\.]+)['"]/
      @msrun.start_time = $1[2...-1].to_f
    end
    if line =~ /endTime=['"]([\w\.]+)['"]/
      @msrun.end_time = $1[2...-1].to_f
    end
    if @io =~ /^\s*<scan/
      break
    end
  end
  @msrun
end