-
Notifications
You must be signed in to change notification settings - Fork 10
/
checkmod.rb
executable file
·312 lines (280 loc) · 11.6 KB
/
checkmod.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
#! /usr/bin/env ruby
# Copyright (c) 2010 Michael Specht
#
# This file is part of Proteomatic.
#
# Proteomatic is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Proteomatic is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Proteomatic. If not, see <http://www.gnu.org/licenses/>.
require './include/ruby/proteomatic'
require 'yaml'
require 'set'
class CheckMod < ProteomaticScript
def run()
peptides = @param[:peptides].split(/[\s,]+/)
peptides.collect! { |x| x.strip.upcase }
peptides.reject! { |x| x.empty? }
peptides.sort!
peptides.uniq!
modPeptides = Set.new
peptides.each do |peptide|
# create all possible modification states
places = []
(0...peptide.size).each do |i|
aa = peptide[i, 1]
if 'STY'.include?(aa)
places << i
end
end
possibilities = 1 << places.size
(0...possibilities).each do |pattern|
modPeptide = peptide.dup
(0...places.size).each do |i|
if ((pattern >> i) & 1) != 0
modPeptide[places[i], 1] = modPeptide[places[i], 1].downcase
end
end
modPeptides << modPeptide
end
end
ids = @param[:ids].split(/[\s,]+/)
ids.collect! { |x| x.strip }
ids.reject! { |x| x.empty? }
ids.sort!
ids.uniq!
puts "Matching #{peptides.size} peptide#{peptides.size == 1 ? '' : 's'} with a total of #{modPeptides.size} modification states against #{ids.size} #{ids.size == 1 ? 'spectrum' : 'spectra'}."
# check if there are spectra files that are not dta or mgf
lk_PreparedSpectraFiles = Array.new
lk_XmlFiles = Array.new
@input[:spectra].each do |ls_Path|
if @inputFormat[ls_Path] == 'mgf'
# it's MGF, use it directly
lk_PreparedSpectraFiles.push(ls_Path)
else
# it's something else, convert it first
lk_XmlFiles.push("\"" + ls_Path + "\"")
end
end
@ms_TempPath = tempFilename('checkmod')
FileUtils::mkpath(@ms_TempPath)
unless (lk_XmlFiles.empty?)
# convert spectra to MGF
puts 'Converting XML spectra to MGF format...'
ls_Command = "\"#{ExternalTools::binaryPath('ptb.xml2mgf')}\" -i \"#{ids.join(' ')}\" -o \"#{@ms_TempPath}/mgf-in\" #{lk_XmlFiles.join(' ')}"
runCommand(ls_Command)
lk_PreparedSpectraFiles = lk_PreparedSpectraFiles + Dir[@ms_TempPath + '/mgf-in*']
end
# read input MGF files, extract spectra and do the job
scans = Hash.new
lk_PreparedSpectraFiles.each do |path|
peaks = Array.new
charge = nil
scanid = nil
pepmass = nil
File::open(path, 'r') do |f|
inScan = false
f.each_line do |line|
line.strip!
next if line.empty?
if line == 'BEGIN IONS'
inScan = true
peaks = Array.new
charge = nil
scanid = nil
pepmass = nil
elsif line == 'END IONS'
scans[scanid] = {:charge => charge, :precursor => pepmass, :peaks => peaks}
inScan = false
elsif line =~ /[A-Z]+=.+/
# property
lineArray = line.split('=')
key = lineArray[0].upcase
value = lineArray[1]
if key == 'CHARGE'
charge = value.to_i
elsif key == 'TITLE'
scanid = value.split('.')[-2]
elsif key == 'PEPMASS'
pepmass = value.to_f
end
else
# m/z intensity pair
lineArray = line.split(/\s/)
mz = lineArray[0].to_f
intensity = lineArray[1].to_f
peaks << [mz, intensity]
end
end
end
end
iterationCount = modPeptides.size * ids.size
i = 0
results = Array.new
modPeptides.each do |modPeptide|
ids.each do |id|
print "\rMatching... #{sprintf('%d', i * 100 / iterationCount)}% done."
i += 1
score = calculateScore(scans[id], modPeptide)
if score[:precursorIonAccuracy] <= @param[:precursorIonMassAccuracy]
results << {:id => id, :modPeptide => modPeptide, :score => score[:score], :precursorIonAccuracy => score[:precursorIonAccuracy]}
end
end
end
print "\rMatching... 100% done."
puts
results.sort! do |a, b|
if a[:id] == b[:id]
b[:score] <=> a[:score]
else
a[:id] <=> b[:id]
end
end
oldId = nil
results.each do |x|
if x[:id] != oldId
puts
puts "Scan ##{x[:id]}:"
oldId = x[:id]
end
puts "#{sprintf('%9.4f', x[:score])}, #{x[:modPeptide]} (#{sprintf('%1.2f', x[:precursorIonAccuracy])} ppm)"
end
puts
FileUtils::rm_rf(@ms_TempPath)
end
def calculateScore(scan, peptide)
lk_Masses = {'G' => 57.021464, 'A' => 71.037114, 'S' => 87.032029,
'P' => 97.052764, 'V' => 99.068414, 'T' => 101.04768, 'C' => 103.00919,
'L' => 113.08406, 'I' => 113.08406, 'N' => 114.04293, 'D' => 115.02694,
'Q' => 128.05858, 'K' => 128.09496, 'E' => 129.04259, 'M' => 131.04048,
'H' => 137.05891, 'F' => 147.06841, 'R' => 156.10111, 'Y' => 163.06333,
'W' => 186.07931, '$' => 0.0, 'X' => 0.0}
ld_Water = 18.01057
# UNSURE ABOUT THIS VALUE BUT OK WITH BIANCAS EXAMPLES, should be 1.0078250
ld_Hydrogen = 1.0073250
ld_Phosphorylation = 79.966330408
ions = Hash.new
# collect b ions
fragmentMass = 0.0
(0...peptide.size).each do |i|
aa = peptide[i, 1]
fragmentMass += lk_Masses[aa.upcase]
if aa =~ /[a-z]/
fragmentMass += ld_Phosphorylation
end
ions[{:origin => "b#{i + 1}", :mods => Set.new, :score => 1.0}] = fragmentMass
end
# collect y ions
fragmentMass = ld_Water
(0...peptide.size).each do |i|
aa = peptide[peptide.size - i - 1, 1]
fragmentMass += lk_Masses[aa.upcase]
if aa =~ /[a-z]/
fragmentMass += ld_Phosphorylation
end
ions[{:origin => "y#{i + 1}", :mods => Set.new, :score => 1.0}] = fragmentMass
end
# add water loss
newIons = Hash.new
(0..5).each do |i|
ions.each do |key, mass|
newKey = YAML::load(key.to_yaml)
newKey[:mods] << "-#{i}H20" if i > 0
newKey[:score] *= 0.5 ** i
newIons[newKey] = mass - ld_Water * i
end
end
ions = YAML::load(newIons.to_yaml)
# add PA loss
newIons = Hash.new
(0..1).each do |i|
ions.each do |key, mass|
newKey = YAML::load(key.to_yaml)
newKey[:mods] << "-H3PO4" if i > 0
newKey[:score] *= 0.5 if i > 0
newIons[newKey] = mass - (ld_Phosphorylation + ld_Water) * i
end
end
ions = YAML::load(newIons.to_yaml)
# add charge states
oldIons = ions.dup
ions = Hash.new
(1..scan[:charge]).each do |i|
oldIons.each do |key, mass|
newKey = key.dup
newKey[:charge] = i
ions[newKey] = (mass + i * ld_Hydrogen) / i
end
end
# ions.keys.each do |key|
# puts "#{key[:origin]}#{key[:mods].to_a.sort.join(' ')} (#{key[:charge]}+): #{ions[key]} / #{key[:score]}"
# end
# puts peaks.to_yaml
peaks = scan[:peaks].dup
minMz = peaks.collect { |x| x.first}.min
maxMz = peaks.collect { |x| x.first}.max
maxIntensity = peaks.collect { |x| x[1]}.max
# puts "m/z range is #{minMz} - #{maxMz}, max intensity is #{maxIntensity}"
# puts "There are #{peaks.size} peaks in the scan."
peaks.reject! do |x|
x[1] / maxIntensity < @param[:noiseFilter] / 100.0
end
# puts "After filtering out the low peaks, there are #{peaks.size} peaks left."
errors = Array.new
intensities = Array.new
ids = Array.new
scores = Array.new
ions.each do |key, mz|
next unless (minMz..maxMz).include?(mz)
matches = Array.new
# :TODO: do a binary search here, it's faster
peaks.each do |peak|
error = ((mz - peak[0]) / mz * 1000000.0).abs
matches << peak if error <= @param[:productIonMassAccuracy]
end
next if matches.empty?
# select higher peak if multiple matches
if matches.size > 1
matches.sort! { |x, y| y[1] <=> x[1] }
end
match = matches.first
error = ((mz - match[0]) / mz * 1000000.0).abs
errors << error
intensities << match[1] / maxIntensity
ids << "#{key[:origin]}#{key[:mods].to_a.sort.join('')} (#{key[:charge]}+)"
scores << key[:score] * (match[1] / maxIntensity)
end
# puts "Matching peaks: #{errors.size}."
averageIntensity = intensities.inject { |x, y| x + y } / intensities.size
# puts "Average intensity is #{averageIntensity * 100.0}."
medianIntensity = intensities[intensities.size / 2]
# puts "Median intensity is #{medianIntensity * 100.0}."
averageError = errors.inject { |x, y| x + y } / errors.size
# puts "Average error is #{averageError} ppm."
medianError = errors[errors.size / 2]
# puts "Median error is #{medianError} ppm."
averageScore = scores.inject { |x, y| x + y }
# puts "Score is #{averageScore}."
precursorMz = scan[:precursor]
theoMz = ld_Water
(0...peptide.size).each do |i|
aa = peptide[i, 1]
theoMz += lk_Masses[aa.upcase]
if aa =~ /[a-z]/
theoMz += ld_Phosphorylation
end
end
theoMz = (theoMz + (ld_Hydrogen * scan[:charge])) / scan[:charge]
precursorIonAccuracy = ((theoMz - precursorMz) / theoMz).abs() * 1000000.0
return {:score => averageScore, :precursorIonAccuracy => precursorIonAccuracy}
end
end
script = CheckMod.new