ms-spectral_summing 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.rdoc +19 -0
  3. data/lib/spectral_summing.rb +157 -0
  4. metadata +56 -0
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ryan Taylor
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = spectral_summing
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to spectral_summing
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Ryan Taylor. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,157 @@
1
+ Spectrum = Struct.new(:spectrum, :scan_num, :scan_time, :scan_range, :precursor_mass, :charge_states, :intensities, :mz_values)
2
+
3
+ class Parser
4
+ attr_accessor :spectra
5
+ def initialize(file)
6
+ @file = file
7
+ end
8
+ def parse(file = nil)
9
+ file ||= @file
10
+ require 'ms/msrun'
11
+ @spectra = []
12
+ Ms::Msrun.open(file) do |ms|
13
+ ms.each(:ms_level => 2) do |scan|
14
+ @spectra << Spectrum.new(scan, scan.num, scan.time, (scan.start_mz..scan.end_mz), scan.precursor.mz, scan.precursor.charge_states, scan.spectrum.intensities, scan.spectrum.mzs)
15
+ end
16
+ end
17
+ end
18
+ def parse_by_scan_num(scan_nums, file = nil)
19
+ file ||= @file
20
+ require 'ms/msrun'
21
+ @spectra = []
22
+ Ms::Msrun.open(file) do |ms|
23
+ ms.each(:ms_level => 2) do |scan|
24
+ if scan_nums.include?(scan.num)
25
+ @spectra << Spectrum.new(scan, scan.num, scan.time, (scan.start_mz..scan.end_mz), scan.precursor.mz, scan.precursor.charge_states, scan.spectrum.intensities, scan.spectrum.mzs)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ class Combiner
33
+ Defaults = {:bin_window => 0.1, :window_size => 4, :precursor_mass_tolerance_in_ppm => 10, :tolerant => false}
34
+ attr_accessor :output_spectra
35
+ def initialize(spectra = nil, opts = {})
36
+ @spectra = spectra
37
+ @opts = Defaults.merge(opts)
38
+ end
39
+ def combine(spectrum1, spectrum2)
40
+ tolerance = calculate_daltons_from_ppm(spectrum1.precursor_mass, @opts[:precursor_mass_tolerance_in_ppm] )
41
+ if tolerance.include?(spectrum2.precursor_mass) or @opts[:tolerant]
42
+ data_arr = summer(spectrum1.mz_values, spectrum1.intensities, spectrum2.mz_values, spectrum2.intensities)
43
+ end
44
+ data_arr
45
+ end
46
+ def summer(x1,y1,x2,y2) # What should this return?
47
+ endpoints = (x1+x2).each.minmax
48
+ bin_width = @opts[:bin_window]
49
+ num_bins = ((endpoints.last - endpoints.first)/bin_width).ceil
50
+ data_x = [endpoints.first+bin_width/2.0]
51
+ data_y = Array.new(num_bins, 0)
52
+ j, k = 0,0
53
+ one = [x1,y1]; two = [x2,y2]
54
+ if x1.first == endpoints.first
55
+ data_x[0] = x1.first
56
+ data_y[0] += y1.first
57
+ y1[0] = 0
58
+ elsif x2.first == endpoints.first
59
+ data_x[0] = x2.first
60
+ data_y[0] += y2.first
61
+ y2[0] = 0
62
+ end
63
+ (1..num_bins-1).each do |i|
64
+ data_x[i] = data_x[i-1] + bin_width
65
+ check = data_x[i] + bin_width/2.0
66
+ #puts "check= #{check}"
67
+ if one.first[j]
68
+ while one.first[j] < check
69
+ data_y[i] += one.last[j]
70
+ j += 1
71
+ break if one.first[j].nil?
72
+ end
73
+ end
74
+ if two.first[k]
75
+ while two.first[k] < check
76
+ data_y[i] += two.last[k]
77
+ k += 1
78
+ break if two.first[k].nil?
79
+ end
80
+ end
81
+ end
82
+ [data_x, data_y]
83
+ end
84
+ def combine_for_more_combining(spectrum1, spectrum2)
85
+ arr = combine(spectrum1, spectrum2)
86
+ joined_spectrum = Spectrum.new()
87
+ joined_spectrum.precursor_mass = (spectrum1.precursor_mass + spectrum2.precursor_mass)/2.0
88
+ joined_spectrum.mz_values = arr.first
89
+ joined_spectrum.intensities = arr.last
90
+ # Spectrum = Struct.new(:spectrum, :scan_num, :scan_time, :scan_range, :precursor_mass, :charge_states, :intensities, :mz_values)
91
+ joined_spectrum
92
+ end
93
+ def calculate_daltons_from_ppm(mass, ppm)
94
+ diff = ppm*mass/1e6
95
+ (mass-diff)..(mass+diff)
96
+ end
97
+ def to_mgf(spectrum, filename)
98
+ File.open(filename,'w') do |out|
99
+ out.puts "BEGIN IONS"
100
+ out.puts "TITLE=Spec1:#{spectrum.precursor_mass}_#{spectrum.charge_states.first}"
101
+ out.puts "CHARGE=#{spectrum.charge_states.to_s}+"
102
+ # our current mzML parser doesn't have scan.time implemented...
103
+ spectrum.mz_values.each_with_index do |mz, i|
104
+ intensity = spectrum.intensities[i]
105
+ out.puts "#{"%.5f" % mz}/t#{"%.5f" % intensity}" unless intensity == 0
106
+ end
107
+ out.puts "END IONS"
108
+ end
109
+ end
110
+ def combine_to_mgf(spectrum1, spectrum2, filename) # Thanks JOHN!!! Ms-Msrun 0.3.6
111
+ results = combine(spectrum1, spectrum2)
112
+ File.open(filename, 'w') do |out|
113
+ out.puts "BEGIN IONS"
114
+ out.puts "TITLE=Spec1:#{spectrum1.precursor_mass}_Spec2:#{spectrum2.precursor_mass}.#{spectrum1.scan_num}_#{spectrum2.scan_num}_#{spectrum1.charge_states.first}"
115
+ out.puts "CHARGE=#{spectrum1.charge_states.to_s}+"
116
+ # our current mzML parser doesn't have scan.time implemented...
117
+ results.first.each_with_index do |mz, i|
118
+ intensity = results.last[i]
119
+ out.puts "#{"%.5f" % mz}/t#{"%.5f" % intensity}" unless intensity == 0
120
+ end
121
+ out.puts "END IONS"
122
+ end
123
+ end
124
+ end
125
+
126
+ if ARGV.size == 0 or ARGV.size % 2 != 0
127
+ puts "Usage: #{__FILE__} input_file.mzXML scan_nums.txt input_file2.mzXML scan_nums2.txt ... "
128
+ puts "NOTE: scan_nums.txt files must have a new line break between each integer value."
129
+ puts 'Returns input_file_input_file2_..._input_file(n).mgf'
130
+ exit
131
+ else
132
+ mzXMLs = []
133
+ scan_nums = []
134
+ while ARGV.size > 0
135
+ mzXMLs << ARGV.shift
136
+ scan_nums << ARGV.shift
137
+ end
138
+ scan_nums.map {|file| IO.readlines(file) }
139
+ # Parse the files and put the data into spectra objects, held within the list of all spectra to combine.
140
+ spectras = []
141
+ mzXML.each_with_index do |file, i|
142
+ @parse_object = Parser.new(file)
143
+ @parse_object.parse_by_scan_num(scan_nums[i])
144
+ spectras << @parse_object.spectra
145
+ end
146
+ combined_spectrum = spectras.shift
147
+ combiner = Combiner.new(combined_spectrum)
148
+ spectras.each do |spectrum|
149
+ combined_spectrum = combiner.combine_for_more_combining(combined_spectrum, spectrum)
150
+ end
151
+ combiner.to_mgf(combined_spectrum, 'combined_multiple_files.mgf')
152
+ end
153
+
154
+
155
+
156
+
157
+
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ms-spectral_summing
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Ryan M Taylor
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-27 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: This is the utility built for summing of individual MS(n) spectra in order to bolster the signal of weak fragmentation. It provides an API and a command-line interface for general use.
17
+ email: ryanmt@byu.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.rdoc
26
+ - LICENSE.txt
27
+ - lib/spectral_summing.rb
28
+ homepage: https://github.com/princelab/spectral_summing
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.7.2
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Ms-spectral_summing provides a utility for the combination of multiple scans from mzXML files into a single MGF file. It provides both an API and a generic use via the command line. This relies upon the proven utility of the ms-msrun library.
55
+ test_files: []
56
+