-
Notifications
You must be signed in to change notification settings - Fork 10
/
run-gpf.rb
executable file
·139 lines (121 loc) · 5.43 KB
/
run-gpf.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
#! /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/formats'
require 'net/http'
require 'net/ftp'
require 'yaml'
require 'set'
class RunGpf < ProteomaticScript
def run()
@mk_Peptides = Set.new
ls_GenomePath = ''
ls_GenomePath = $gb_FixedGenomes ? @param[:genome] : @input[:genome].first
def handlePeptide(as_Peptide, af_Mass = nil)
ls_Id = "peptide=#{as_Peptide}"
ls_Id += ";precursorMass=#{af_Mass}" if af_Mass
@mk_Peptides.add(ls_Id)
end
@input[:predictions].each do |ls_Path|
if (fileMatchesFormat(ls_Path, 'gpf-queries'))
File.open(ls_Path, 'r') do |lk_File|
lk_File.each do |ls_Line|
ls_Line.strip!
next if ls_Line.empty?
handlePeptide(ls_Line)
end
end
elsif (fileMatchesFormat(ls_Path, 'txt'))
File.open(ls_Path, 'r') do |lk_File|
lk_File.each do |ls_Line|
ls_Line.strip!
next if ls_Line.empty?
handlePeptide(ls_Line)
end
end
else
File.open(ls_Path, 'r') do |lk_File|
lf_Mass = nil
ls_Peptide = ''
lk_File.each do |ls_Line|
ls_Line.strip!
if (ls_Line[0, 1] == '>')
handlePeptide(ls_Peptide, lf_Mass) unless ls_Peptide.empty?
lk_Line = ls_Line.split(' ')
lf_Mass = nil
if lk_Line.size > 3
lf_Mass = Float(lk_Line[lk_Line.size - 3].strip)
li_Charge = Integer(lk_Line[lk_Line.size - 2].strip)
lf_Mass = lf_Mass * li_Charge - 1.007825 * (li_Charge - 1)
end
ls_Peptide = ''
else
ls_Peptide += ls_Line
end
end
handlePeptide(ls_Peptide, lf_Mass) unless ls_Peptide.empty?
end
end
end
ls_Query = @mk_Peptides.to_a.sort.join("\n")
ls_GpfOptions = "masses #{@param[:masses]} protease #{@param[:protease]} massError #{@param[:massError]} searchSimilar #{@param[:searchSimilar]} searchIntrons #{@param[:searchIntrons]} maxIntronLength #{@param[:maxIntronLength]} minChainLength #{@param[:minChainLength]} fullDetails yes"
ls_QueryFile = tempFilename("gpf-queries-")
File::open(ls_QueryFile, 'w') { |lk_File| lk_File.write(ls_Query) }
ls_ResultFile = tempFilename("gpf-results-");
ls_CsvPathSwitch = ''
if @output[:csvResults]
ls_CsvPathSwitch = " --csvResultsPath \"#{@output[:csvResults]}\" "
end
ls_Command = "#{ExternalTools::binaryPath('gpf.gpfbatch')} #{ls_GpfOptions} --yamlResultsPath \"#{ls_ResultFile}\" #{ls_CsvPathSwitch} \"#{ls_GenomePath}\" \"#{ls_QueryFile}\""
runCommand(ls_Command)
FileUtils::cp(ls_ResultFile, @output[:yamlResults]) if @output[:yamlResults]
lk_Hits = Set.new
ls_LineBatch = ''
File::open(ls_ResultFile, 'r').each_line do |ls_Line|
if (ls_Line[0, 1] != ' ' && !ls_LineBatch.empty?)
# handle line batch
lk_ResultPart = YAML::load(ls_LineBatch)
lk_ResultPart.each do |ls_Key, lk_Result|
next unless lk_Result.class == Array
next if lk_Result.empty?
lk_Result.each { |lk_Hit| lk_Hits.add(lk_Hit['peptide']) }
end
ls_LineBatch = ''
end
ls_LineBatch += ls_Line
end
if (!ls_LineBatch.empty?)
# handle line batch
lk_ResultPart = YAML::load(ls_LineBatch)
lk_ResultPart.each do |ls_Key, lk_Result|
next unless lk_Result.class == Array
next if lk_Result.empty?
lk_Result.each { |lk_Hit| lk_Hits.add(lk_Hit['peptide']) }
end
ls_LineBatch = ''
end
puts "GPF found #{lk_Hits.size} hits."
if @output[:gpfPeptides]
File.open(@output[:gpfPeptides], 'w') do |lk_Out|
lk_Hits.to_a.sort.each { |ls_Peptide| lk_Out.print ">gpf__#{ls_Peptide}\n#{ls_Peptide}\n" }
end
end
end
end
script = RunGpf.new