Class: MzID::FilteredStreamingParser
- Inherits:
-
StreamingParser
- Object
- BaseParser
- BatchParser
- StreamingParser
- MzID::FilteredStreamingParser
- Defined in:
- lib/mzid/filtered_streaming_parser.rb
Overview
class to parse an mzIdentML file in a streaming (i.e., mem-efficient) manner performs multi-pass filtering so that can maintain smallest datastruct in memory 1) first collect counts of elements 2) get list of peptide evidence from PSMs that pass filter 3)
Instance Attribute Summary collapse
-
#pep_ev_h_dbseqRef ⇒ Object
Returns the value of attribute pep_ev_h_dbseqRef.
Instance Method Summary collapse
-
#cache_db_seq_entries(root) ⇒ Object
store database sequence entries (ids).
-
#cache_ids(use_pbar = @use_pbar) ⇒ Object
store peptide sequences in hash for lookup.
- #cache_ids2(use_pbar = @use_pbar) ⇒ Object
-
#cache_pep_ev(root) ⇒ Object
store peptide evidence sequences in hash for lookup.
-
#each_psm(use_pbar = @use_pbar) ⇒ Object
iterate through each psm.
-
#get_prot_id(pep_ev_id) ⇒ Object
def get_pep_ev_protID(pid) @pep_ev_h_protID end.
-
#get_psm(psm_node) ⇒ Object
given a xml node of a psm, return the PSM.
-
#initialize(file, sp_thresh = 10.0**-10,, use_pbar = nil) ⇒ FilteredStreamingParser
constructor
A new instance of FilteredStreamingParser.
-
#write_to_file(outfile, use_pbar = @use_pbar) ⇒ Object
load PSMs into memory, and go back to perform lookup for prot ids.
Methods inherited from BatchParser
Constructor Details
#initialize(file, sp_thresh = 10.0**-10,, use_pbar = nil) ⇒ FilteredStreamingParser
Returns a new instance of FilteredStreamingParser.
16 17 18 19 20 21 22 23 24 |
# File 'lib/mzid/filtered_streaming_parser.rb', line 16 def initialize(file, sp_thresh = 10.0**-10, = nil) @num_spec = 0 # @pep_ev_h_protID = Hash.new @pep_ev_h_startPos = Hash.new @pep_ev_h_endPos = Hash.new @pep_ev_h_dbseqRef = Hash.new super(file, ) end |
Instance Attribute Details
#pep_ev_h_dbseqRef ⇒ Object
Returns the value of attribute pep_ev_h_dbseqRef.
38 39 40 |
# File 'lib/mzid/filtered_streaming_parser.rb', line 38 def pep_ev_h_dbseqRef @pep_ev_h_dbseqRef end |
Instance Method Details
#cache_db_seq_entries(root) ⇒ Object
store database sequence entries (ids)
141 142 143 144 145 146 147 148 |
# File 'lib/mzid/filtered_streaming_parser.rb', line 141 def cache_db_seq_entries(root) dbseq_lst = root.xpath('//DBSequence') dbseq_lst.each do |dnode| id = dnode["id"].to_sym acc_id = dnode["accession"] @db_seq_h[id] = acc_id.to_sym end end |
#cache_ids(use_pbar = @use_pbar) ⇒ Object
store peptide sequences in hash for lookup
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 |
# File 'lib/mzid/filtered_streaming_parser.rb', line 43 def cache_ids( = @use_pbar) num_pep, num_db_seq, num_pep_ev = get_num_elements(nil) puts "SPEC:\t#{@num_spec}" puts "PEP:\t#{num_pep}" puts "DB:\t#{num_db_seq}" puts "PEPEV:\t#{num_pep_ev}" #pbar1 = ProgressBar.new("Caching psm", num_pep) if use_pbar #reader = Nokogiri::XML::Reader(File.open(@mzid_file)) #reader.each do |node| #end @pep_h = Hash.new @mod_h = Hash.new #pbar = ProgressBar.new("Caching", num_pep+num_db_seq+num_pep_ev) if use_pbar = ProgressBar.new("peptides", num_pep/2) if reader = Nokogiri::XML::Reader(File.open(@mzid_file)) reader.each do |node| # if node.name == "Peptide" then #pbar.inc if use_pbar # parse local peptide entry tmp_node = Nokogiri::XML.parse(node.outer_xml) tmp_node.remove_namespaces! root = tmp_node.root pep_id = root["id"].to_sym # skip if already handled PepID next if @pep_h.has_key?(pep_id) # parse sequence/mods if haven't seen it yet pep_seq = get_peptide_sequence(root) mod_line = get_modifications(root) @pep_h[pep_id] = pep_seq @mod_h[pep_id] = mod_line .inc if end end .finish if # = ProgressBar.new("db_seq", num_db_seq) if IO.foreach(@mzid_file) do |line| next if !line.match(/^\s+<DBSequence\s/) prot_id = line.match(/accession=\"([\w|\|]+)/)[1] db_id = line.match(/id=\"(\w+)/)[1] @db_seq_h[db_id.to_sym] = prot_id.to_sym .inc if end # reader2 = Nokogiri::XML::Reader(File.open(@mzid_file)) # reader2.each do |node| # # # if node.name == "DBSequence" then # # parse local DBSequence entry # tmp_node = Nokogiri::XML.parse(node.outer_xml) # tmp_node.remove_namespaces! # root = tmp_node.root # cache_db_seq_entries(root) # pbar2.inc if use_pbar # end # end .finish if # = ProgressBar.new("pep_ev", num_pep_ev) if IO.foreach(@mzid_file) do |line| next if !line.match(/^\s+<PeptideEvidence\s/) db_id = line.match(/dBSequence_ref=\"(\w+)/)[1] pep_ev = line.match(/id=\"(\w+)/)[1] @pep_ev_h_dbseqRef[pep_ev.to_sym] = db_id.to_sym .inc if end # reader3 = Nokogiri::XML::Reader(File.open(@mzid_file)) # reader3.each do |node| # if node.name == "PeptideEvidence" then # # parse local DBSequence entry # tmp_node = Nokogiri::XML.parse(node.outer_xml) # tmp_node.remove_namespaces! # root = tmp_node.root # cache_pep_ev(root) # pbar3.inc if use_pbar # end # # if node.name == "PeptideEvidence" then # # tmp_node = Nokogiri::XML.parse(node.outer_xml) # # root = tmp_node.root # # pep_ref = root.to_s.match(/peptide_ref=\"(\w+)\"/)[1] # # id_ref = root.to_s.match(/id=\"(\w+)\"/)[1] # # db_ref = root.to_s.match(/dBSequence_ref=\"(\w+)\"/)[1] # # @pep_ev_h_dbseqRef[id_ref.to_sym] = db_ref.to_sym # # end # end .finish if puts "PEP_H SIZE:\t#{@pep_h.size}" puts "DBSEQ_H SIZE:\t#{@db_seq_h.size}" puts "PEP_EV_H SIZE:\t#{@pep_ev_h_dbseqRef.size}" end |
#cache_ids2(use_pbar = @use_pbar) ⇒ Object
27 28 |
# File 'lib/mzid/filtered_streaming_parser.rb', line 27 def cache_ids2( = @use_pbar) end |
#cache_pep_ev(root) ⇒ Object
store peptide evidence sequences in hash for lookup
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/mzid/filtered_streaming_parser.rb', line 152 def cache_pep_ev(root) pep_ev_lst = root.xpath('//PeptideEvidence') pep_ev_lst.each do |pnode| id = pnode["id"].to_sym # @pep_ev_h[id] = # PeptideEvidence.new(#:id => pnode["id"], # :db_seq_ref => pnode["dBSequence_ref"], # #:pep_id => pnode["peptide_ref"], # :start_pos => pnode["start"].to_i, # :end_pos => pnode["end"].to_i, # #:pre => pnode["pre"], # #:post => pnode["post"], # :prot_id => @db_seq_h[pnode["dBSequence_ref"]].to_sym) # @pep_ev_h_protID[id.to_sym] = @db_seq_h[pnode["dBSequence_ref"]].to_sym # @pep_ev_h_startPos[id.to_sym] = pnode["start"].to_i, # @pep_ev_h_endPos[id.to_sym] = pnode["end"].to_i @pep_ev_h_dbseqRef[id.to_sym] = pnode["dBSequence_ref"].to_sym end end |
#each_psm(use_pbar = @use_pbar) ⇒ Object
iterate through each psm
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/mzid/filtered_streaming_parser.rb', line 175 def each_psm(=@use_pbar) hit_values = File.open(@mzid_file) do |io| doc = Nokogiri::XML.parse(io, nil, nil, Nokogiri::XML::ParseOptions::DEFAULT_XML | Nokogiri::XML::ParseOptions::NOBLANKS | Nokogiri::XML::ParseOptions::STRICT) doc.remove_namespaces! root = doc.root # get list of identifications spec_results = root.xpath('//SpectrumIdentificationResult') = ProgressBar.new("PSMs", spec_results.size) if spec_results.each do |sres| # psms_of_spec = sres.xpath('.//SpectrumIdentificationItem') # go over each PSM from the spectra psms_of_spec.each do |psm_node| psm = get_psm(psm_node) # yield psm object yield psm end .inc if end .finish if end end |
#get_prot_id(pep_ev_id) ⇒ Object
def get_pep_ev_protID(pid) @pep_ev_h_protID end
32 33 34 35 36 |
# File 'lib/mzid/filtered_streaming_parser.rb', line 32 def get_prot_id(pep_ev_id) dbref = @pep_ev_h_dbseqRef[pep_ev_id] prot_id = @db_seq_h[dbref] prot_id end |
#get_psm(psm_node) ⇒ Object
given a xml node of a psm, return the PSM
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/mzid/filtered_streaming_parser.rb', line 200 def get_psm(psm_node) # get peptide evidence list pep_ev_raw_lst = psm_node.xpath('.//PeptideEvidenceRef') pep_ev_lst = pep_ev_raw_lst.map{|penode| pep_ev_ref_id = penode["peptideEvidence_ref"].to_sym} # get cvparams cvlst = psm_node.xpath('.//cvParam') # find spectral prob tmp_lst = cvlst.select{|v| v['name'] == "MS-GF:SpecEValue"} spec_prob = tmp_lst[0]['value'] # get peptide pep_seq = @pep_h[psm_node['peptide_ref'].to_sym] # get spectrum id/ref number spec_id = psm_node['id'] spec_num = spec_id.split("_")[1].to_i spec_ref = spec_id.split("_")[-1].to_i # # store in object psm = PSM.new(:spec_num => spec_num, :spec_ref => spec_ref, :pep => pep_seq, :spec_prob => spec_prob.to_f, :mods => (@mod_h.has_key?(psm_node['peptide_ref']) ? @mod_h[psm_node['peptide_ref']] : nil), :pep_ev => pep_ev_lst) end |
#write_to_file(outfile, use_pbar = @use_pbar) ⇒ Object
load PSMs into memory, and go back to perform lookup for prot ids
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 |
# File 'lib/mzid/filtered_streaming_parser.rb', line 227 def write_to_file(outfile, =@use_pbar) = ProgressBar.new("Caching pep_ev", num_db_seq) if t1_db = Time.now reader3 = Nokogiri::XML::Reader(File.open(@mzid_file)) reader3.each do |node| if node.name == "PeptideEvidence" then # parse local DBSequence entry tmp_node = Nokogiri::XML.parse(node.outer_xml) tmp_node.remove_namespaces! root = tmp_node.root #cache_pep_ev(root) pep_ev_lst = root.xpath('//PeptideEvidence') pep_ev_lst.each do |pnode| id = pnode["id"] start_pos = pnode["start"].to_i, end_pos = pnode["end"].to_i db_seq_ref = pnode["dBSequence_ref"].to_sym end .inc if end end .finish if end |