mspire 0.6.18 → 0.6.19

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.18
1
+ 0.6.19
@@ -64,6 +64,30 @@ module MS
64
64
  # currently being described, ordered.
65
65
  attr_accessor :products
66
66
 
67
+ # takes a Nokogiri node and sets relevant properties
68
+ def self.from_xml(xml)
69
+ spec = MS::Mzml::Spectrum.new(xml[:id])
70
+
71
+ params = {}
72
+ xml.xpath("./cvParam").each do |cvparam|
73
+ params[cvparam[:accession]] = cvparam[:value]
74
+ end
75
+ spec.ms_level = params['MS:1000511'].to_i
76
+ # TODO: need to slot in all the other info in reasonable ways
77
+
78
+ data_arrays = xml.xpath('./binaryDataArrayList/binaryDataArray').map do |binary_data_array_n|
79
+ accessions = binary_data_array_n.xpath('./cvParam').map {|node| node['accession'] }
80
+ base64 = binary_data_array_n.xpath('./binary').text
81
+ MS::Mzml.unpack_binary(base64, accessions)
82
+ end
83
+ # if there is no spectrum, we will still return a spectrum object, it
84
+ # just has no mzs or intensities
85
+ data_arrays = [[], []] if data_arrays.size == 0
86
+ spec.data_arrays = data_arrays
87
+ spec
88
+ end
89
+
90
+
67
91
  # the most common param to pass in would be ms level: 'MS:1000511'
68
92
  #
69
93
  # This would generate a spectrum of ms_level=2 :
@@ -76,6 +100,7 @@ module MS
76
100
  block.call(self) if block
77
101
  end
78
102
 
103
+
79
104
  # see SpectrumList for generating the entire list
80
105
  def to_xml(builder)
81
106
  atts = {}
data/lib/ms/mzml.rb CHANGED
@@ -4,7 +4,7 @@ require 'nokogiri'
4
4
  require 'io/bookmark'
5
5
  require 'zlib'
6
6
  require 'ms/mzml/index_list'
7
- require 'ms/spectrum'
7
+ require 'ms/mzml/spectrum'
8
8
  require 'ms/mzml/file_description'
9
9
  require 'ms/mzml/software'
10
10
  require 'ms/mzml/scan_list'
@@ -161,6 +161,7 @@ module MS
161
161
  end
162
162
 
163
163
  class << self
164
+
164
165
  # read-only right now
165
166
  def open(filename, &block)
166
167
  File.open(filename) do |io|
@@ -169,6 +170,7 @@ module MS
169
170
  end
170
171
 
171
172
  def foreach(filename, &block)
173
+ block or return enum_for(__method__, filename)
172
174
  open(filename) do |mzml|
173
175
  mzml.each(&block)
174
176
  end
@@ -208,9 +210,14 @@ module MS
208
210
  end
209
211
 
210
212
  def each_spectrum(&block)
213
+ block or return enum_for(__method__)
211
214
  (0...@index_list[:spectrum].size).each do |int|
212
- block.call spectrum(int)
215
+ block.call(spectrum(int))
213
216
  end
217
+ #block_given? or return enum_for(__method__)
218
+ #(0...@index_list[:spectrum].size).each do |int|
219
+ # yield spectrum(int)
220
+ #end
214
221
  end
215
222
 
216
223
  # returns the Nokogiri::XML::Node object associated with each spectrum
@@ -231,17 +238,9 @@ module MS
231
238
  # @param [Object] arg an index number (Integer) or id string (String)
232
239
  # @return [MS::Spectrum] a spectrum object
233
240
  def spectrum(arg)
234
- ################### trouble
235
241
  start_byte = index_list[0].start_byte(arg)
236
- data_arrays = spectrum_node_from_start_byte(start_byte).xpath('//binaryDataArray').map do |binary_data_array_n|
237
- accessions = binary_data_array_n.xpath('./cvParam').map {|node| node['accession'] }
238
- base64 = binary_data_array_n.xpath('./binary').text
239
- MS::Mzml.unpack_binary(base64, accessions)
240
- end
241
- # if there is no spectrum, we will still return a spectrum object, it
242
- # just has no mzs or intensities
243
- data_arrays = [[], []] if data_arrays.size == 0
244
- MS::Spectrum.new(data_arrays)
242
+ spec_n = spectrum_node_from_start_byte(start_byte)
243
+ MS::Mzml::Spectrum.from_xml(spec_n)
245
244
  end
246
245
 
247
246
  # returns the number of spectra
data/lib/ms/spectrum.rb CHANGED
@@ -156,7 +156,7 @@ module MS
156
156
 
157
157
  if opt[:normalize]
158
158
  sz = spectra.size
159
- spectrum.data[1].map! {|v| v.to_f / sz }
159
+ spectrum.intensities.map! {|v| v.to_f / sz }
160
160
  end
161
161
  if opt[:return_data]
162
162
  $stderr.puts "returning spectrum (#{spectrum.mzs.size}) and data" if $VERBOSE
@@ -10,24 +10,25 @@ module MS
10
10
  # boolean for if the spectrum represents centroided data or not
11
11
  attr_accessor :centroided
12
12
 
13
- # The underlying data store. methods are implemented so that data[0] is
14
- # the m/z's and data[1] is intensities
15
- attr_accessor :data
13
+ # The underlying data store. methods are implemented so that data_arrays[0] is
14
+ # the m/z's and data_arrays[1] is intensities
15
+ attr_accessor :data_arrays
16
+
16
17
 
17
18
  def centroided?() centroided end
18
19
 
19
- # data takes an array: [mzs, intensities]
20
20
  # @return [MS::Spectrum]
21
21
  # @param [Array] data two element array of mzs and intensities
22
- def initialize(data, centroided=true)
23
- @data = data
22
+ # @param [Boolean] centroided is the spectrum centroided or not
23
+ def initialize(data_arrays, centroided=true)
24
+ @data_arrays = data_arrays
24
25
  @centroided = centroided
25
26
  end
26
27
 
27
28
  # found by querying the size of the data store. This should almost always
28
29
  # be 2 (m/z and intensities)
29
30
  def size
30
- @data.size
31
+ @data_arrays.size
31
32
  end
32
33
 
33
34
  def ==(other)
@@ -36,26 +37,26 @@ module MS
36
37
 
37
38
  # An array of the mz data.
38
39
  def mzs
39
- @data[0]
40
+ @data_arrays[0]
40
41
  end
41
42
 
42
43
  # An array of the intensities data, corresponding to mzs.
43
44
  def intensities
44
- @data[1]
45
+ @data_arrays[1]
45
46
  end
46
47
 
47
48
  def mzs_and_intensities
48
- [@data[0], @data[1]]
49
+ [@data_arrays[0], @data_arrays[1]]
49
50
  end
50
51
 
51
52
  # retrieve an m/z and intensity doublet at that index
52
53
  def [](array_index)
53
- [@data[0][array_index], @data[1][array_index]]
54
+ [@data_arrays[0][array_index], @data_arrays[1][array_index]]
54
55
  end
55
56
 
56
57
  # yields(mz, inten) across the spectrum, or array of doublets if no block
57
58
  def points(&block)
58
- @data[0].zip(@data[1], &block)
59
+ @data_arrays[0].zip(@data_arrays[1], &block)
59
60
  end
60
61
 
61
62
  alias_method :each, :points
@@ -84,7 +85,7 @@ module MS
84
85
  def sort!
85
86
  _points = points.to_a
86
87
  _points.sort!
87
- _points.each_with_index {|(mz,int), i| @data[0][i] = mz ; @data[1][i] = int }
88
+ _points.each_with_index {|(mz,int), i| @data_arrays[0][i] = mz ; @data_arrays[1][i] = int }
88
89
  self
89
90
  end
90
91
 
@@ -23,7 +23,7 @@ describe 'converting mzml to plms1' do
23
23
  plms1.scan_numbers.should == scan_nums
24
24
  plms1.spectra.each do |spec|
25
25
  spec.size.should == 2
26
- spec.class.should == MS::Spectrum
26
+ spec.should be_a_kind_of(MS::SpectrumLike)
27
27
  spec.mzs.should == []
28
28
  spec.intensities.should == []
29
29
  end
@@ -51,7 +51,7 @@ describe 'converting mzml to plms1' do
51
51
  plms1.scan_numbers.should == [1, 2, 3]
52
52
  sizes = [20168, 315, 634]
53
53
  plms1.spectra.zip(sizes).each do |spec,exp_size|
54
- spec.class.should == MS::Spectrum
54
+ spec.should be_a_kind_of(MS::SpectrumLike)
55
55
  spec.size.should == 2
56
56
  spec.mzs.size.should == exp_size
57
57
  spec.intensities.size.should == exp_size
data/spec/ms/mzml_spec.rb CHANGED
@@ -19,9 +19,10 @@ describe MS::Mzml do
19
19
 
20
20
  it '#spectrum (or #[]) returns a spectrum when queried by index (Integer)' do
21
21
  spectrum = @mzml.spectrum(1) # getting the second spectrum
22
- spectrum1 = @mzml[1]
22
+ spectrum1 = @mzml[1] # can get with brackets
23
+ spectrum.ms_level.should == 2
23
24
  spectrum.should == spectrum1
24
- spectrum.should be_a(MS::Spectrum)
25
+ spectrum.should be_a(MS::Mzml::Spectrum)
25
26
  spectrum.should respond_to(:mzs)
26
27
  spectrum.should respond_to(:intensities)
27
28
  spectrum.mzs.size.should == 315
@@ -32,8 +33,9 @@ describe MS::Mzml do
32
33
  it '#spectrum (or #[]) returns a spectrum when queried by id (String)' do
33
34
  spectrum = @mzml.spectrum("controllerType=0 controllerNumber=1 scan=2")
34
35
  spectrum1 = @mzml["controllerType=0 controllerNumber=1 scan=2"]
36
+ spectrum.ms_level.should == 2
35
37
  spectrum.should == spectrum1
36
- spectrum.should be_a(MS::Spectrum)
38
+ spectrum.should be_a(MS::Mzml::Spectrum)
37
39
  end
38
40
 
39
41
  it 'goes through spectrum with #each or #each_spectrum' do
@@ -42,6 +44,27 @@ describe MS::Mzml do
42
44
  spec.mzs.size.should == mz_sizes.shift
43
45
  end
44
46
  end
47
+
48
+ it 'gets an enumerator if called without a block' do
49
+ mz_sizes = [20168, 315, 634]
50
+ iter = @mzml.each
51
+ 3.times { iter.next.mzs.size.should == mz_sizes.shift }
52
+ lambda {iter.next}.should raise_error
53
+ end
54
+
55
+ it 'iterates with foreach' do
56
+ mz_sizes = [20168, 315, 634]
57
+ iter = MS::Mzml.foreach(@file)
58
+ 3.times { iter.next.mzs.size.should == mz_sizes.shift }
59
+ lambda {iter.next}.should raise_error
60
+ end
61
+
62
+ it 'can gracefully determine the m/z with highest peak in select scans' do
63
+ highest_mzs = MS::Mzml.foreach(@file).select {|v| v.ms_level > 1 }.map do |spec|
64
+ spec.points.sort_by(&:last).first.first
65
+ end
66
+ highest_mzs.map(&:round).should == [453, 866]
67
+ end
45
68
  end
46
69
  end
47
70
 
@@ -51,7 +74,7 @@ describe MS::Mzml do
51
74
  string.gsub(/"mspire" version="([\.\d]+)"/, %Q{"mspire" version="X.X.X"})
52
75
  end
53
76
 
54
- it 'reads MS1 spectra and retention times' do
77
+ it 'writes MS1 spectra and retention times' do
55
78
 
56
79
  spec_params = ['MS:1000127', ['MS:1000511', 1]]
57
80
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mspire
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.18
4
+ version: 0.6.19
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-02-23 00:00:00.000000000 Z
13
+ date: 2012-02-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: nokogiri
17
- requirement: &19554840 !ruby/object:Gem::Requirement
17
+ requirement: &10271800 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '1.5'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *19554840
25
+ version_requirements: *10271800
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: bsearch
28
- requirement: &19553820 !ruby/object:Gem::Requirement
28
+ requirement: &10656900 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 1.5.0
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *19553820
36
+ version_requirements: *10656900
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: andand
39
- requirement: &19552180 !ruby/object:Gem::Requirement
39
+ requirement: &10739280 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 1.3.1
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *19552180
47
+ version_requirements: *10739280
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: obo
50
- requirement: &19550640 !ruby/object:Gem::Requirement
50
+ requirement: &10814460 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 0.1.0
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *19550640
58
+ version_requirements: *10814460
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: rspec
61
- requirement: &19565640 !ruby/object:Gem::Requirement
61
+ requirement: &10811160 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '2.6'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *19565640
69
+ version_requirements: *10811160
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: jeweler
72
- requirement: &19564260 !ruby/object:Gem::Requirement
72
+ requirement: &10888880 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ~>
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: 1.5.2
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *19564260
80
+ version_requirements: *10888880
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rcov
83
- requirement: &19563540 !ruby/object:Gem::Requirement
83
+ requirement: &10902860 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,7 +88,7 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *19563540
91
+ version_requirements: *10902860
92
92
  description: mass spectrometry proteomics, lipidomics, and tools, a rewrite of mspire,
93
93
  merging of ms-* gems
94
94
  email: jtprince@gmail.com