ms-msrun 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,118 @@
1
+
2
+ require 'ms/spectrum'
3
+
4
+ module Kernel
5
+ private
6
+ def this_method
7
+ caller[0] =~ /`([^']*)'/ and $1
8
+ end
9
+ end
10
+
11
+ class Array
12
+ def sum
13
+ self.inject(0.0) {|_sum, v| _sum += v }
14
+ end
15
+ end
16
+
17
+ module Ms
18
+ class Spectrum
19
+ module Compare
20
+
21
+ # (Σ (Ii*Ij)^½) / (ΣIi * ΣIj)^½
22
+ def sim_score(other, opts={})
23
+ numer = 0.0
24
+ compare(other, {:yield_diff => false}) do |sint, oint|
25
+ numer += Math.sqrt(sint * oint)
26
+ end
27
+ numer / Math.sqrt( self.ints.sum * other.ints.sum )
28
+ end
29
+
30
+ # opts[:type] == :mutual_best
31
+ # will only return intensities on mutual best matches within radius.
32
+ # yields [self_intensity, other_intensity] for each match within
33
+ # the radius.
34
+ #
35
+ # opts[:type] == :best
36
+ # yields the best match in the radius peak. If one peak is already
37
+ # spoken for, a different peak may still match a close peak if it is
38
+ # its best match (how to explain this?).
39
+ #
40
+ # if opts[:yield_diff] == true then returns [..., diff] (d: true)
41
+ # if opts[:yield_mz] == true then returns [mz, mz, int, int] (d: false)
42
+ # if opts[:yield_indices] == true then returns [i,j] (d: false)
43
+ # assumes mzs are increasing
44
+ def compare(other, opts={})
45
+ defaults = {:radius => 1.0, :type=>:mutual_best, :yield_diff => true, :yield_mz => false, :yield_indices => false}
46
+ (radius, type, yield_diff, yield_mz, yield_indices) = defaults.merge(opts).values_at(:radius, :type, :yield_diff, :yield_mz, :yield_indices)
47
+ blk = block_given?
48
+ output = [] if !blk
49
+ s_ints = self.intensities
50
+ s_size = self.mzs.size
51
+ o_mzs = other.mzs
52
+ o_size = o_mzs.size
53
+ o_ints = other.intensities
54
+ oi = 0
55
+ start_j = 0
56
+ save = []
57
+ self.mzs.each_with_index do |mz,i|
58
+ break if start_j >= o_size
59
+ hi = mz + radius
60
+ lo = mz - radius
61
+ start_j.upto(o_size-1) do |j|
62
+ diff = mz - o_mzs[j]
63
+ absdiff = diff.abs
64
+ if absdiff <= radius # a potential hit!
65
+ save << [absdiff, i, j]
66
+ elsif diff < 0 # we overshot
67
+ break
68
+ else # undershot
69
+ start_j = j + 1 # this is for the benefit of the next search
70
+ end
71
+ end
72
+ end
73
+
74
+ #puts "BEFORE SORT: "
75
+ #p save
76
+ #save.sort!
77
+ #puts "AFTER SORT: "
78
+ #p save
79
+ iset = Array.new(s_size)
80
+ jset = Array.new(o_size)
81
+ save.each do |diff, i, j|
82
+ unless iset[i] || jset[j]
83
+ if type == :best
84
+ iset[i] = true
85
+ jset[j] = true
86
+ end
87
+ to_ret =
88
+ if yield_indices
89
+ [i,j]
90
+ else
91
+ [s_ints[i], o_ints[j]]
92
+ end
93
+ if yield_mz
94
+ to_ret.unshift( s_mzs[i], o_mzs[j] )
95
+ end
96
+ if yield_diff
97
+ to_ret.push( diff )
98
+ end
99
+ if blk
100
+ yield to_ret
101
+ else
102
+ output << to_ret
103
+ end
104
+ end
105
+ if type == :mutual_best
106
+ iset[i] = true
107
+ jset[j] = true
108
+ end
109
+ end
110
+ output
111
+ end
112
+ end # Compare
113
+
114
+ include Compare
115
+ end # Spectrum
116
+ end # Ms
117
+
118
+
@@ -0,0 +1,46 @@
1
+ require 'ms/spectrum'
2
+
3
+ module Ms
4
+ class Spectrum
5
+ module Filter
6
+
7
+ def filter(by=:bins, opts={})
8
+ send("filter_by_#{by}".to_sym, opts)
9
+ end
10
+
11
+ # filters by binning the mz field
12
+ # bin_width is in m/z units
13
+ # num_peaks is the top peaks to include per bin
14
+ def filter_by_bins(opts={})
15
+ (bw, np) = {:bin_width => 100, :num_peaks => 7 }.merge(opts).values_at(:bin_width, :num_peaks)
16
+
17
+ stop = bw
18
+ track = []
19
+ track_all = [track]
20
+ ints = self.intensities
21
+ self.each do |mz,int|
22
+ if mz > stop
23
+ stop += bw
24
+ track = []
25
+ track_all << track
26
+ end
27
+ track << [int, mz]
28
+ end
29
+
30
+ include = []
31
+ track_all.each do |track|
32
+ track.sort!
33
+ start = (track.size < np) ? track.size : np
34
+ include.push( *( track[-start,np]) )
35
+ end
36
+
37
+ ret_ints = []
38
+ ret_mzs = include.map {|int, mz| [mz, int] }.sort.map {|mz,int| ret_ints << int ; mz }
39
+
40
+ return Spectrum.new(ret_mzs, ret_ints)
41
+ end
42
+
43
+ end
44
+ include Filter
45
+ end
46
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path( File.dirname(__FILE__) + '/spec_helper' )
2
+
3
+ require 'lmat'
4
+
5
+ class LmatUnitSpec < MiniTest::Spec
6
+
7
+ def initialize(*args)
8
+ @klass = Lmat
9
+ super(*args)
10
+ end
11
+
12
+ it 'can be created with no arguments' do
13
+ obj1 = @klass.new
14
+ obj1.class.must_equal @klass
15
+ end
16
+
17
+ xit 'can be created with arrays' do
18
+ obj = @klass[[1,2,3],[4,5,6]]
19
+ obj[0,0].must_equal 1
20
+ obj[1,0].must_equal 4
21
+ obj[1,2].must_equal 6
22
+ end
23
+
24
+ xit 'can find the max value' do
25
+ obj = @klass[[1,2,3],[1,8,3]]
26
+ obj.max.must_equal 8
27
+ end
28
+
29
+ end
@@ -0,0 +1,56 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'ms/msrun'
4
+
5
+ class SearchSpec < MiniTest::Spec
6
+
7
+ xit 'works' do
8
+ @file = '/home/jtprince/ms-msrun/spec/files/opd1/000.v1.mzXML'
9
+ params = {
10
+ :bottom_mh => 300.0,
11
+ :top_mh => 4500.0,
12
+ :ms_levels => (2..-1),
13
+ :min_peaks => 10,
14
+ }
15
+
16
+ # no scans:
17
+ no_scans = [
18
+ [:min_peaks, 1000],
19
+ [:first_scan, 21],
20
+ [:first_scan, 10000],
21
+ [:last_scan, 0],
22
+ [:ms_levels, (3..4)],
23
+ [:ms_levels, 0],
24
+ [:ms_levels, 3],
25
+ [:top_mh, 0.0],
26
+ [:bottom_mh, 5000],
27
+ ]
28
+ Ms::Msrun.open(@file) do |ms|
29
+ no_scans.each do |k,v|
30
+ ms.to_mgf(nil, k => v).must_equal ""
31
+ end
32
+ end
33
+
34
+ some_scans = [
35
+ [:min_peaks, 0],
36
+ [:first_scan, 1],
37
+ [:first_scan, 9],
38
+ [:last_scan, 8],
39
+ [:ms_levels, 2],
40
+ [:ms_levels, (2..2)],
41
+ [:ms_levels, (2...3)],
42
+ [:top_mh, 1500],
43
+ [:bottom_mh, 500],
44
+ ]
45
+ Ms::Msrun.open(@file) do |ms|
46
+ no_scans.each do |k,v|
47
+ reply = ms.to_mgf(nil, k => v)
48
+ puts reply
49
+ reply.must_match(/BEGIN.IONS/)
50
+ reply.must_match(/END.IONS/)
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,50 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
2
+
3
+ require 'ms/msrun'
4
+
5
+ module MsrunSpec
6
+ extend Shareable
7
+
8
+ before do
9
+ @file = nil # you need to define this!
10
+ @key = nil # <- do nothing with this.
11
+ end
12
+
13
+ def key
14
+ @key || @key = YAML.load_file(@file + '.key.yml')
15
+ end
16
+
17
+ it 'reads header information' do
18
+ Ms::Msrun.open(@file) do |ms|
19
+ key['header'].each do |k,v|
20
+ ms.send(k.to_sym).must_equal v
21
+ end
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ class Mzxml_v1 < MiniTest::Spec
28
+ include MsrunSpec
29
+ before do
30
+ super
31
+ @file = '/home/jtprince/ms-msrun/spec/files/opd1/000.v1.mzXML'
32
+ end
33
+ end
34
+
35
+ class Mzxml_v2_0 < MiniTest::Spec
36
+ include MsrunSpec
37
+ before do
38
+ super
39
+ @file = '/home/jtprince/ms-msrun/spec/files/opd1/020.v2.0.readw.mzXML'
40
+ end
41
+ end
42
+
43
+ class Mzxml_v2_1 < MiniTest::Spec
44
+ include MsrunSpec
45
+ before do
46
+ super
47
+ @file = '/home/jtprince/ms-msrun/spec/files/opd1/000.v2.1.mzXML'
48
+ end
49
+ end
50
+
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ require 'ms/msrun'
4
+
5
+ class ScanUnitSpec < MiniTest::Spec
6
+
7
+ before do
8
+ @scan = Ms::Scan.new
9
+ @scan.precursor = Ms::Precursor.new
10
+ @scan.spectrum = Ms::Spectrum.new([1,2,3,4], [2,4,4,2])
11
+ end
12
+
13
+
14
+ it 'determines if its +1 or not' do
15
+ # these have not been checked for accuracy, just sanity
16
+ reply = [0.1,2.5, 3.5, 5].map do |prec_mz|
17
+ @scan.precursor.mz = prec_mz
18
+ @scan.plus1?(-0.1)
19
+ end
20
+ reply.all? {|v| v == true }.must_equal true
21
+
22
+ reply = [0.1,2.5, 3.5, 5].map do |prec_mz|
23
+ @scan.precursor.mz = prec_mz
24
+ @scan.plus1?(1.0)
25
+ end
26
+ reply.all? {|v| v == false }.must_equal true
27
+
28
+ reply = [0.1,2.5, 3.5, 5].map do |prec_mz|
29
+ @scan.precursor.mz = prec_mz
30
+ @scan.plus1?(0.5)
31
+ end
32
+ reply.must_equal [false, false, true, true]
33
+ end
34
+ end
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
2
+
3
+ require 'ms/spectrum/compare'
4
+
5
+ # when you switch order the return intensities are swapped:
6
+ class Array
7
+ def rev_ab
8
+ each {|v| (v[0], v[1]) = v[1], v[0] }
9
+ end
10
+ end
11
+
12
+ class CompareSpec < MiniTest::Spec
13
+ include Ms
14
+
15
+ before do
16
+ @a = Spectrum.new([0,2,3,4], [5,6,7,8])
17
+ @b = Spectrum.new([0, 1.5, 3.5, 5.5], [9,10,11,12])
18
+
19
+ @c = Spectrum.new([0, 1], [8,9])
20
+ @d = Spectrum.new([0.6, 0.75], [10,11])
21
+ end
22
+
23
+ it 'compares spectra' do
24
+ # array form:
25
+ @c.compare(@d).rev_ab.must_equal( @d.compare(@c) )
26
+ @c.compare(@d).must_equal [[8, 10, 0.6]]
27
+
28
+ # block form
29
+ exp = [[5,9], [6,10], [7,11]]
30
+ # default radius 1.0
31
+ @a.compare(@b) do |int_a, int_b|
32
+ exp.delete([int_a, int_b])
33
+ end
34
+ exp.size.must_equal 0
35
+ end
36
+
37
+ it 'computes similarity score' do
38
+ p @a.sim_score(@b)
39
+ p @a.sim_score(@a)
40
+ end
41
+
42
+ end
43
+
44
+
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
2
+
3
+ require 'ms/spectrum/filter'
4
+
5
+
6
+ # when you switch order the return intensities are swapped:
7
+ class Array
8
+ def rev_ab
9
+ each {|v| (v[0], v[1]) = v[1], v[0] }
10
+ end
11
+ end
12
+
13
+ class FilterSpec < MiniTest::Spec
14
+ include Ms
15
+
16
+ before do
17
+ @a = Spectrum.new([0,5,10, 15,16,17,18, 20.1], [0,1,2, 3,8,10,4, 0])
18
+ @null = Spectrum.new
19
+ end
20
+
21
+ it 'filters spectra' do
22
+ spec = @a.filter(:bins, :bin_width => 10, :num_peaks => 2)
23
+ spec.mzs.must_equal [5,10,16,17,20.1]
24
+ spec.ints.must_equal [1,2,8,10,0]
25
+
26
+ @a.filter(:bins, :bin_width => 100, :num_peaks => 8).must_equal @a
27
+ @a.filter(:bins, :bin_width => 1, :num_peaks => 1).must_equal @a
28
+ @a.filter(:bins, :bin_width => 1, :num_peaks => 0).must_equal @null
29
+ end
30
+
31
+ end
32
+
33
+
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ms-msrun
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Prince
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-17 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: axml
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.5
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: runarray
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: A library for working with LC/MS runs. Part of mspire. Has parsers for mzXML v1, 2, and 3, mzData and mzML. Can convert to commonly desired search output (such as mgf)
36
+ email: jtprince@gmail.com
37
+ executables:
38
+ - ms_to_search.rb
39
+ - base64_to_array.rb
40
+ - ms_to_obiwarp.rb
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README
45
+ - LICENSE
46
+ files:
47
+ - lib/ms/spectrum.rb
48
+ - lib/ms/msrun/axml/mzxml.rb
49
+ - lib/ms/msrun/search.rb
50
+ - lib/ms/msrun.rb
51
+ - lib/ms/scan.rb
52
+ - lib/ms/spectrum/compare.rb
53
+ - lib/ms/spectrum/filter.rb
54
+ - lib/ms/precursor.rb
55
+ - lib/bsearch.rb
56
+ - lib/lmat.rb
57
+ - README
58
+ - LICENSE
59
+ - Rakefile
60
+ has_rdoc: true
61
+ homepage: http://mspire.rubyforge.org/projects/ms-msrun
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --main
65
+ - README
66
+ - --title
67
+ - ms-msrun
68
+ - --line-numbers
69
+ - --inline-source
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ requirements:
85
+ - xmlparser (preferrably) or libxml
86
+ rubyforge_project: mspire
87
+ rubygems_version: 1.3.1
88
+ signing_key:
89
+ specification_version: 2
90
+ summary: A library for working with LC/MS runs
91
+ test_files:
92
+ - spec/ms/msrun_spec.rb
93
+ - spec/ms/msrun/search_spec.rb
94
+ - spec/ms/spectrum/compare_spec.rb
95
+ - spec/ms/spectrum/filter_spec.rb
96
+ - spec/ms/scan_spec.rb
97
+ - spec/lmat_spec.rb