-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathion_extractor.rb
executable file
·346 lines (297 loc) · 9.19 KB
/
ion_extractor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env ruby
progname = File.basename(__FILE__)
require 'optparse'
require 'ostruct'
require 'csv'
require 'stringio'
begin
require 'andand'
require 'mspire/mzml'
require 'gnuplot/multiplot'
rescue
puts "to run #{progname} you need the following gems: "
puts " gnuplot-multiplot (also install gnuplot the program), andand, and mspire!"
puts "to install the gems:"
puts " gem install gnuplot-multiplot andand mspire"
exit
end
def putsv(*args)
puts *args if $VERBOSE
end
module Gnuplot
INFINITY = 1e30
end
class Range
def to_gplot
"[#{self.begin}:#{self.end}]"
end
end
Window = Struct.new(:mz_range, :time_range, :orig_string) do
def to_s
"#{mz_range.begin}:#{mz_range.end}mz,#{time_range.begin}:#{time_range.end}(s)"
end
end
Centroid = Struct.new(:scan_num, :time, :mz, :intensity) do
def to_s
"<#{scan_num}:#{time}(s) #{mz}m/z #{intensity}>"
end
end
# holds ExtractedTimePoint objects
class ExtractedChromatogram
attr_accessor :filename
attr_accessor :window
attr_accessor :centroids
attr_accessor :mz_range
attr_accessor :time_range
# if set to a value, transform the color by that log base
attr_accessor :color_log_base
def initialize(filename, window, centroids=[])
@color_log_base = nil
@filename, @window, @centroids = filename, window, centroids
end
def time_range
window.time_range
end
def mz_range
window.mz_range
end
def mz_start
mz_range.begin
end
def mz_end
mz_range.end
end
def time_start
time_range.begin
end
def time_end
time_range.end
end
def min_time
centroids.first.andand.time
end
def max_time
centroids.last.andand.time
end
def times
centroids.map(&:time)
end
def scan_numbers
centroids.map(&:scan_num)
end
# the total ion counts
def ion_count
centroids.inject(0.0) {|sum,centroid| sum + centroid.intensity }
end
def intensities
centroids.group_by(&:scan_num).map do |scan_num, cents|
cents.inject(0.0) {|sum,obj| sum + obj.intensity }
end
end
def mzs
centroids.map(&:mz)
end
def ion_chromatogram
[times.uniq, intensities]
end
def mz_view_chromatogram
[times, mzs]
end
def mz_view_chromatogram_with_colors
_ints = centroids.map do |centroid|
intensity = centroid.intensity
@color_log_base ? Math.log(intensity, @color_log_base) : intensity
end
[times, mzs, _ints]
end
def gplot_fname_base
base = filename.chomp(File.extname(filename))
[base, window.orig_string.sub(':','-')].join("__")
end
end
# returns xics
def sequential(mzml, xics)
searching = xics.dup
mzml.each do |spectrum|
next unless spectrum.ms_level == 1
scan_num = spectrum.id[/scan=(\d+)/,1].to_i
rt = spectrum.retention_time
finished = []
matching = searching.select do |xic|
if xic.time_range === rt
true
else
finished << xic if rt > xic.time_range.end
false
end
end
matching.each do |xic|
indices = spectrum.select_indices(xic.mz_range)
centroids = spectrum.mzs.values_at(*indices).
zip(spectrum.intensities.values_at(*indices)).
map do |mz, int|
Centroid.new(scan_num, rt, mz, int)
end
xic.centroids.push(*centroids)
end
searching -= finished
if searching.size == 0
putsv "no more windows to match at rt(s): #{rt}"
break
end
end
xics
end
opt = OpenStruct.new(
windows: [],
plot_size: "600,300",
sequential: true,
)
parser = OptionParser.new do |op|
op.banner = "usage: #{progname} [OPTS] <file>.mzML ..."
op.separator "KEY OPTIONS:"
op.on("-q", "--quantfile <filename>", "write quantitation to given csv filename") {|v| opt.quantfile = v }
op.on("-m", "--multiplot <filename>", "plot windows to filename as svg") {|v| opt.multiplot = v }
op.on("-p", "--plot", "plot each window to svg") {|v| opt.plot = v }
op.on("-w", "--window <m:m,t:t>", "mz_start:mz_end[,time_st:time_end]", "call multiple times for many windows", "<m,t:t> to apply --mz-window", "leave off times (or end time)", "to get entire chromatogram") do |v|
opt.windows << v
end
op.on("-g", "--global-mz-window <m/z>", Float, "a global m/z window", "applied to every window w/ single m/z") {|v| opt.global_mz_window = v }
op.separator ""
op.separator "OTHER OPTIONS:"
op.on("--plot-size <W,H>", "width, height of each plot (#{opt.plot_size})") {|v| opt.plot_size = v }
op.on("--color-log <Float>", Float, "use log transformed values for color") {|v| opt.color_log = v }
op.on("--gnuplot-file <filename>", "write gnuplot commands to specified file") {|v| opt.gnuplot_file = v }
op.on("-v", "--verbose", "talk about it") {|v| $VERBOSE = 5 }
end
parser.parse!
if ARGV.size == 0 || opt.windows.size == 0 || (!opt.quantfile && !opt.multiplot && !opt.plot)
puts parser
exit
end
opt.windows.map! do |string|
(mz_string, time_string) = string.split(',')
mz_range_vals =
if mz_string[':']
mz_string.split(':').map(&:to_f)
else
raise ArgumentError, '!!!! --global-mz-window required if range of mz values not given!' unless opt.global_mz_window
mz_val = mz_string.to_f
[mz_val - opt.global_mz_window, mz_val + opt.global_mz_window]
end
time_vals =
if time_string
(start_time, end_time) = time_string.split(':').map(&:to_f)
[start_time, end_time || Float::INFINITY]
else
[0, Float::INFINITY]
end
Window.new(Range.new(*mz_range_vals), Range.new(*time_vals), string )
end
opt.plot_size = opt.plot_size.split(',').map(&:to_i)
filenames = ARGV.dup
xic_set = filenames.map do |filename|
xics = opt.windows.map {|window| ExtractedChromatogram.new( filename, window ) }
Mspire::Mzml.open(filename) do |mzml|
if opt.sequential
sequential(mzml, xics)
else
abort "haven't implemented binary search yet, use --sequential for now"
end
end
end
width = filenames.size * opt.plot_size.first
height = opt.windows.size * opt.plot_size.last
xics = xic_set.flatten(1)
xics.each {|xic| xic.color_log_base = opt.color_log } if opt.color_log
opt.term_cmd = "svg noenhanced size #{width},#{height}"
if opt.multiplot || opt.plot
putsv "plotting"
plot_xics = ->(gp) do
xics.group_by(&:window).each do |window, xics_by_window|
max_group_intensity = xics_by_window.map {|xic| xic.intensities.max }.max.andand.ceil || 1.0
min_time = window.time_range.begin
max_time = window.time_range.end
if max_time == Float::INFINITY
# take the highest available time from a real data point
# will be nil if no data points
max_time = xics_by_window.map {|xic| xic.max_time }.max
end
color_range_max =
if opt.color_log
Math.log(max_group_intensity, opt.color_log)
else
max_group_intensity
end
filenames.map {|filename| xics_by_window.find {|xic| xic.filename == filename } }.each do |xic|
Gnuplot::Plot.new(gp) do |plot|
if opt.plot
plot.term opt.term_cmd
plot.output xic.gplot_fname_base + ".svg"
end
#plot.palette "model XYZ rgbformulae -7,-22,-23"
plot.palette "model XYZ rgbformulae -10,-22,-23"
plot.cbrange "[0:#{color_range_max}]"
plot.unset "colorbox"
plot.title "#{xic.window} #{xic.filename}"
plot.xlabel "time (s)"
plot.ylabel "intensity"
plot.yrange "[0:#{max_group_intensity}]"
plot.y2label "m/z"
plot.ytics "nomirror"
plot.y2tics
plot.y2range window.mz_range.to_gplot
plot.xrange (min_time..(max_time || Gnuplot::INFINITY)).to_gplot
unless max_time
plot.xtics %Q{#{min_time},#{Gnuplot::INFINITY}}
plot.xtics %Q{add ("#{min_time}" #{min_time})}
plot.xtics %Q{add ("Infinity" #{Gnuplot::INFINITY})}
end
ion_chrmt = xic.ion_chromatogram
no_data = ion_chrmt.first.size == 0
ion_chrmt = [[min_time],[Gnuplot::INFINITY]] if no_data
plot.data << Gnuplot::DataSet.new( ion_chrmt ) do |ds|
ds.axes = "x1y1"
ds.with = "lines"
ds.title = "ion intensity"
end
unless no_data
plot.data << Gnuplot::DataSet.new( xic.mz_view_chromatogram_with_colors) do |ds|
ds.axes = "x1y2"
ds.title = "m/z"
ds.with = "points pt 7 ps 0.3 palette"
end
end
end
end
end
end
open_io = ->(io) do
if opt.multiplot
io << "set term #{opt.term_cmd}\n"
io << %Q{set output "#{opt.multiplot}"\n}
end
if opt.multiplot
Gnuplot::Multiplot.new(io, layout: [opt.windows.size, filenames.size], &plot_xics)
else
plot_xics[io]
end
end
if opt.gnuplot_file
gp = StringIO.new
open_io[gp]
gp.rewind
File.write(opt.gnuplot_file, gp.string)
else
Gnuplot.open {|gp| open_io[gp] }
end
end
if opt.quantfile
headers = %w(filename mz_start mz_end time_start time_end ion_count).map(&:to_sym)
CSV.open(opt.quantfile, 'wb') do |csv|
csv << headers
xics.each do |xic|
csv << headers.map {|key| xic.send(key) }
end
end
end