forked from specht/proteomatic-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match-peptides.rb
executable file
·283 lines (276 loc) · 11.3 KB
/
match-peptides.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
#! /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 './include/ruby/externaltools'
require './include/ruby/fasta'
require 'set'
class MatchPeptides < ProteomaticScript
def run()
# load peptides
peptides = Set.new
@input[:peptides].each do |path|
File::open(path, 'r') do |f|
f.each_line do |peptide|
peptide.strip!
next if peptide.empty?
peptides << peptide
end
end
end
puts "Matching #{peptides.size} peptides to #{@input[:databases].size} FASTA databases."
peptidesPath = tempFilename('match-peptides-peptides-');
File::open(peptidesPath, 'w') do |f|
f.puts peptides.to_a.join("\n")
end
resultsPath = tempFilename('match-peptides-results-')
results = Hash.new
@input[:databases].each do |path|
print "Matching to #{File::basename(path)}..."
command = "\"#{ExternalTools::binaryPath('ptb.matchpeptides')}\" --output \"#{resultsPath}\" --peptideFiles \"#{peptidesPath}\" --modelFiles \"#{path}\""
runCommand(command, true)
results[path] = YAML::load_file(resultsPath)
puts ' done.'
end
inputKeys = @input[:databases].collect { |x| File::basename(x) }
if @output[:htmlResults]
File::open(@output[:htmlResults], 'w') do |f|
f.puts "<html>"
f.puts "<head><title>Matched peptides</title>"
f.puts DATA.read
f.puts "</head>"
f.puts "<body>"
f.puts "<table>"
f.puts "<thead>"
f.puts "<tr>"
f.puts "<th>Peptide</th>#{inputKeys.collect { |x| '<th>' + x + '</th>' }.join('')}"
f.puts "</tr>"
f.puts "</thead>"
f.puts "<tbody>"
peptides.to_a.sort.each do |peptide|
f.puts "<tr style='vertical-align: top;'>"
f.puts "<td>#{peptide}</td>"
@input[:databases].each do |path|
if results[path][peptide]
f.puts "<td><ul>#{results[path][peptide].keys.collect { |x| '<li>' + x + '</li>' }.join('')}</ul></td>"
else
f.puts "<td>–</td>"
end
end
f.puts "</tr>"
end
f.puts "</tbody>"
f.puts "</table>"
f.puts "</body>"
f.puts "</html>"
end
end
if @output[:csvResults]
File::open(@output[:csvResults], 'w') do |f|
f.puts "Peptide,Protein,Start,Length"
peptides.to_a.sort.each do |peptide|
@input[:databases].each do |path|
if results[path][peptide]
results[path][peptide].keys.each do |protein|
results[path][peptide][protein].each do |x|
f.puts "\"#{peptide}\",\"#{protein}\",#{x['start']},#{x['length']}"
end
end
end
end
end
end
end
if @output[:yamlResults]
File::open(@output[:yamlResults], 'w') do |f|
peptideList = []
proteinList = []
peptideIndex = {}
proteinIndex = {}
data = {}
peptides.to_a.sort.each do |peptide|
peptideIndex[peptide] = peptideList.size
peptideList << peptide
@input[:databases].each do |path|
if results[path][peptide]
results[path][peptide].keys.each do |protein|
unless proteinIndex.include?(protein)
proteinIndex[protein] = proteinList.size
proteinList << protein
end
data[protein] ||= Set.new
data[protein] << peptide
end
end
end
end
result = {}
result['peptides'] = peptideList
result['proteins'] = proteinList
result['peptidesForProtein'] = Hash.new
proteinList.each do |protein|
result['peptidesForProtein'][proteinIndex[protein]] = []
data[protein].each do |peptide|
result['peptidesForProtein'][proteinIndex[protein]] << peptideIndex[peptide]
end
end
f.puts result.to_yaml
end
end
if @output[:sequenceCoverage]
File::open(@output[:sequenceCoverage], 'w') do |f|
f.puts "<html>"
f.puts "<head><title>Sequence coverage</title>"
f.puts DATA.read
f.puts "</head>"
f.puts "<body>"
@input[:databases].each do |key|
sequences = {}
fastaIterator(File::open(key)) do |id, seq|
sequences[id] = seq
end
f.puts "<h1>#{File::basename(key)}</h1>"
f.puts "<table>"
f.puts "<thead>"
f.puts "<tr style='vertical-align: top;'>"
f.puts "<th>Protein</th><th>Sequence</th><th>Coverage</th><th>Peptide count</th><th>Peptides</th>"
f.puts "</tr>"
f.puts "</thead>"
f.puts "<tbody>"
proteinHash = {}
# peptide:
# protein A:
# - left, right, start, length, proteinLength
# - left, right, start, length, proteinLength
# protein B:
# - left, right, start, length, proteinLength
# - left, right, start, length, proteinLength
results[key].each_pair do |peptide, x|
if x != nil # this is necessary for peptides which are not found in the database
x.each_pair do |protein, matches|
proteinHash[protein] ||= Hash.new
proteinHash[protein][peptide] = matches
end
end
end
proteinCoverage = {}
proteinHash.keys.each do |protein|
proteinLength = proteinHash[protein].values.first.first['proteinLength']
covered = []
proteinLength.times { covered << false }
proteinHash[protein].each_pair do |peptide, infoList|
infoList.each do |info|
p = info['start']
info['length'].times do
covered[p] = true
p += 1
end
end
end
proteinCoverage[protein] = covered.count(true).to_f / proteinLength
end
proteinHash.keys.sort { |a, b| proteinCoverage[b] <=> proteinCoverage[a] }. each do |protein|
f.puts "<tr style='vertical-align: top;'>"
f.puts "<td>#{protein}</td>"
proteinLength = proteinHash[protein].values.first.first['proteinLength']
covered = []
proteinLength.times { covered << false }
proteinHash[protein].each_pair do |peptide, infoList|
infoList.each do |info|
p = info['start']
info['length'].times do
covered[p] = true
p += 1
end
end
end
coverage = covered.count(true).to_f / proteinLength
printSequence = sequences[protein]
f.puts "<td style='font-family: monospace;'>"
i = 0
inSpan = false
while i < printSequence.size
c = printSequence[i, 1]
if ((i > 0) && (covered[i] && !covered[i - 1])) || (i == 0 && covered[i])
f.print("<span style='background-color: #fce94f;'>")
inSpan = true
end
f.print(c)
if ((i < proteinLength - 1) && (covered[i] && !covered[i + 1]))
f.print("</span>")
inSpan = false
end
f.print("<br />") if (i + 1) % 70 == 0
i += 1
end
f.print("</span>") if inSpan
f.puts "</td>"
f.puts "<td>#{sprintf('%1.2f', coverage * 100)}%</td>"
f.puts "<td>#{proteinHash[protein].values.size}</td>"
f.puts "<td>#{proteinHash[protein].keys.join("<br />")}</td>"
f.puts "</tr>"
end
f.puts "</tbody>"
f.puts "</table>"
end
f.puts "</body>"
f.puts "</html>"
end
end
end
end
script = MatchPeptides.new
__END__
<style type='text/css'>
body {
font-family: Verdana;
font-size: 9pt;
}
th {
text-align: left;
border: 1px solid #000;
background-color: #ddd;
padding-left: 0.2em;
padding-right: 0.2em;
}
td {
text-align: left;
border: 1px solid #000;
padding-left: 0.2em;
padding-right: 0.2em;
}
tr.protein {
background-color: #eee;
}
table {
border-collapse: collapse;
font-size: 9pt;
}
.number {
width: 3em;
text-align: right;
}
.unmodpep {
background-color: #fff;
}
.modpep {
background-color: #fce94f;
}
.protein {
background-color: #fff;
}
</style>