Class: MzMLParser

Inherits:
Object
  • Object
show all
Defined in:
lib/protk/mzml_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ MzMLParser

Returns a new instance of MzMLParser.



8
9
10
11
12
13
14
15
# File 'lib/protk/mzml_parser.rb', line 8

def initialize(path)
	@namespace=
	@mzml_ns_prefix="xmlns:"
	@mzml_ns="xmlns:http://psi.hupo.org/ms/mzml"

	doc=XML::Document.file(path)
	@file_reader=XML::Reader.document(doc)
end

Instance Method Details

#next_spectrumObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/protk/mzml_parser.rb', line 17

def next_spectrum()

	until @file_reader.name=="spectrum" 
		if !@file_reader.read()
			return nil
		end
	end

	this_spect=spectrum_as_hash(@file_reader.expand)

	@file_reader.next_sibling

	return this_spect
end

#spectrum_as_hash(spectrum) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/protk/mzml_parser.rb', line 32

def spectrum_as_hash(spectrum)
	index=spectrum.attributes['index']
	sid = spectrum.attributes['id']
	precursor_mz_param = spectrum.find(".//#{@mzml_ns_prefix}cvParam[@accession=\"MS:1000744\"]",@mzml_ns)[0]
	mslevel_param = spectrum.find("./#{@mzml_ns_prefix}cvParam[@accession=\"MS:1000511\"]",@mzml_ns)[0]

	title_param = spectrum.find("./#{@mzml_ns_prefix}cvParam[@accession=\"MS:1000796\"]",@mzml_ns)[0]

	# prec_mz = spectrum.find(".//#{@mz}")

	precursor_mz_mz = precursor_mz_param.attributes['value'] if precursor_mz_param
	mslevel = mslevel_param.attributes['value'] if mslevel_param
	spectrum_title = title_param['value'] if title_param

	data_arrays = spectrum.find("./#{@mzml_ns_prefix}binaryDataArrayList/#{@mzml_ns_prefix}binaryDataArray",@mzml_ns)

	data={}
	data_arrays.each do |arr|
		the_data = arr.find("./#{@mzml_ns_prefix}binary",@mzml_ns)[0].content
		mzaccession = arr.find("./#{@mzml_ns_prefix}cvParam[@accession=\"MS:1000514\"]",@mzml_ns)
		if ( mzaccession.length==1 )
			data[:mz] = the_data
		else 
			data[:intensity] = the_data
		end
	end
	data[:title]=spectrum_title
	data[:mzlevel]=mslevel
	data[:index]=index
	data[:precursormz]=precursor_mz_mz
	data[:id]=sid

	data
end