forked from specht/proteomatic-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter-qe-require-ms2.rb
executable file
·106 lines (100 loc) · 4.72 KB
/
filter-qe-require-ms2.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
#! /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/evaluate-omssa-helper'
require './include/ruby/ext/fastercsv'
require './include/ruby/misc'
require 'set'
require 'yaml'
class RequireMs2Event < ProteomaticScript
def run()
# test whether QE CSV headers are all the same
ls_AllHeader = nil
lk_AllHeader = nil
@input[:quantitationEvents].each do |ls_InPath|
File::open(ls_InPath, 'r') do |lk_In|
ls_Header = lk_In.readline
lk_Header = Set.new(mapCsvHeader(ls_Header).keys())
ls_AllHeader ||= ls_Header
lk_AllHeader = lk_Header
if lk_Header != lk_AllHeader
puts "Error: The CSV header was not consistent throughout all quantitation event input files. The offending header line was #{ls_Header}."
exit 1
end
end
end
# load all PSM and record every unmodified identification event
# lk_Ms2Events[band][peptide] = list of retention times (unsorted)
lk_Ms2Events = Hash.new
@input[:psmList].each do |ls_PsmPath|
ls_RunName = File::basename(ls_PsmPath).sub('.csv', '')
print "Loading #{File::basename(ls_PsmPath)}..."
lk_Results = loadPsm(ls_PsmPath, :silent => true)
lk_Results[:peptideHash].each_pair do |ls_Peptide, lk_Peptide|
lk_Peptide[:scans].each do |ls_Scan|
if lk_Results[:scanHash][ls_Scan][:retentionTime]
lf_RetentionTime = lk_Results[:scanHash][ls_Scan][:retentionTime]
ls_Band = ls_Scan.split('.').first
lk_Ms2Events[ls_Band] ||= Hash.new
lk_Ms2Events[ls_Band][ls_Peptide] ||= Array.new
lk_Ms2Events[ls_Band][ls_Peptide].push(lf_RetentionTime)
end
end
end
puts ''
end
if @output[:results]
File::open(@output[:results], 'w') do |lk_Out|
print 'Writing filtered results...'
li_InCount = 0
li_OutCount = 0
lk_Out.puts ls_AllHeader
@input[:quantitationEvents].each do |ls_InPath|
File::open(ls_InPath, 'r') do |lk_In|
ls_Header = lk_In.readline
lk_Header = mapCsvHeader(ls_Header)
lk_In.each_line do |ls_Line|
li_InCount += 1
lk_Line = ls_Line.parse_csv()
ls_Band = lk_Line[lk_Header['filename']].split('.').first
ls_Peptide = lk_Line[lk_Header['peptide']]
lf_RetentionTime = lk_Line[lk_Header['retentiontime']].to_f
if lk_Ms2Events[ls_Band]
if lk_Ms2Events[ls_Band][ls_Peptide]
lf_Minimum = nil
lk_Ms2Events[ls_Band][ls_Peptide].each do |lf_Ms2Time|
lf_Difference = (lf_Ms2Time - lf_RetentionTime).abs()
lf_Minimum ||= lf_Difference
lf_Minimum = lf_Difference if (lf_Difference < lf_Minimum)
end
if (lf_Minimum <= @param[:maxTimeDifference])
lk_Out.puts ls_Line
li_OutCount += 1
end
end
end
end
end
end
puts
puts "Discarded #{li_InCount - li_OutCount} of #{li_InCount} hits (#{sprintf('%1.1f', (li_InCount - li_OutCount).to_f / li_InCount.to_f * 100.0)}%)."
end
end
end
end
script = RequireMs2Event.new