-
Notifications
You must be signed in to change notification settings - Fork 10
/
isotope-envelope.rb
executable file
·186 lines (166 loc) · 6.8 KB
/
isotope-envelope.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
#! /usr/bin/env ruby
# Copyright (c) 2007-2008 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 IsotopeEnvelope < ProteomaticScript
def loadData()
@isotopes = Hash.new
File::open('include/proteomics-knowledge-base/isotopes.csv', 'r') do |f|
firstIsotope = Hash.new
header = mapCsvHeader(f.readline)
f.each_line do |line|
lineArray = line.parse_csv()
element = lineArray[header['element']]
isotope = lineArray[header['isotope']].to_i
firstIsotope[element] ||= isotope
mass = lineArray[header['monoisotopicmass']].to_f
abundance = lineArray[header['naturalabundance']].to_f
if (@param[:label] == 'N15')
if element == 'N'
if isotope == 15
abundance = @param[:labelingEfficiency] / 100.0
else
abundance = 1.0 - @param[:labelingEfficiency] / 100.0
end
end
end
@isotopes[element] ||= Array.new
lastAccumulatedAbundance = 0
lastAccumulatedAbundance = @isotopes[element].last[:accumulatedAbundance] unless @isotopes[element].empty?
@isotopes[element] ||= Array.new
@isotopes[element] <<
{:isotope => isotope, :mass => mass,
:accumulatedAbundance => lastAccumulatedAbundance + abundance,
:abundance => abundance, :relativeIsotope => isotope - firstIsotope[element]}
end
end
@aminoAcidComposition = Hash.new
File::open('include/proteomics-knowledge-base/amino-acids.csv', 'r') do |f|
header = mapCsvHeader(f.readline)
f.each_line do |line|
lineArray = line.parse_csv()
aaLetter = lineArray[header['singlelettercode']]
composition = lineArray[header['composition']]
next if (!composition) || composition.empty?
result = Hash.new
i = 0
while (i < composition.size)
letter = composition[i, 1]
i += 1
numberString = ''
while (i < composition.size && composition[i, 1] =~ /\d/)
numberString += composition[i, 1]
i += 1
end
numberString = '1' if numberString.empty?
count = numberString.to_i
result[letter] = count
end
@aminoAcidComposition[aaLetter] = result
end
end
end
def pickAtomFast(element, random = true)
result = Hash.new
@isotopes[element].each do |isotope|
next unless random || isotope[:relativeIsotope] == 0
result[isotope[:relativeIsotope]] = isotope[:abundance]
end
return result
end
def pickAtom(element, random = true)
r = rand()
i = 0
if random
while @isotopes[element][i][:accumulatedAbundance] < r
i += 1
end
else
i = 1 if @param[:label] == 'N15' && element == 'N'
end
return [@isotopes[element][i][:isotope], @isotopes[element][i][:mass]]
end
def pickPeptideFast(peptide, random = true)
result = Hash.new
atom = pickAtomFast('H', random)
atom.keys.each { |i| result[i] ||= 0.0; result[i] += atom[i] }
atom = pickAtomFast('H', random)
atom.keys.each { |i| result[i] ||= 0.0; result[i] += atom[i] }
atom = pickAtomFast('H', random)
atom.keys.each { |i| result[i] ||= 0.0; result[i] += atom[i] }
(0...peptide.size).each do |i|
aa = peptide[i, 1]
@aminoAcidComposition[aa].keys.each do |element|
@aminoAcidComposition[aa][element].times do
atom = pickAtomFast(element, random)
atom.keys.each { |i| result[i] ||= 0.0; result[i] += atom[i] }
end
end
end
return result
end
def pickPeptide(peptide, random = true)
mass = [0, 0.0]
atom = pickAtom('H', random)
(0...atom.size).each { |i| mass[i] += atom[i] }
atom = pickAtom('H', random)
(0...atom.size).each { |i| mass[i] += atom[i] }
atom = pickAtom('O', random)
(0...atom.size).each { |i| mass[i] += atom[i] }
(0...peptide.size).each do |i|
aa = peptide[i, 1]
@aminoAcidComposition[aa].keys.each do |element|
@aminoAcidComposition[aa][element].times do
atom = pickAtom(element, random)
(0...atom.size).each { |i| mass[i] += atom[i] }
end
end
end
return mass
end
def run()
loadData()
# puts pickPeptideFast(@param[:peptide]).to_yaml
# exit
lowest = pickPeptide(@param[:peptide], false)
histogram = Hash.new
minMass = Hash.new
maxMass = Hash.new
@param[:count].times do
pick = pickPeptide(@param[:peptide])
pick[0] -= lowest[0]
histogram[pick[0]] ||= 0
histogram[pick[0]] += 1
minMass[pick[0]] ||= pick[1]
minMass[pick[0]] = pick[1] if pick[1] < minMass[pick[0]]
maxMass[pick[0]] ||= pick[1]
maxMass[pick[0]] = pick[1] if pick[1] > maxMass[pick[0]]
end
max = 0.0
histogram.values.each { |x| max = x if x > max }
histogram.keys.sort.each do |isotope|
m0 = minMass[isotope]
m1 = maxMass[isotope]
average = (m0 + m1) * 0.5
error = (m0 - average).abs() / average * 1000000.0
puts "A#{@param[:label] != 'none' ? '*' : ''}#{sprintf('%+d', isotope)}: #{sprintf('%1.6f', histogram[isotope].to_f / max)} (#{sprintf('%1.2f', error)} ppm)"
end
end
end
script = IsotopeEnvelope.new