require 'spec/msrun' require 'spec/mzdata' begin require 'xmlparser' rescue LoadError puts "*******************************************************************" puts "WARNING: 'xmlparser' library not installed:" puts "Defaulting to REXML (slower, but guaranteed to parse correct xml)" puts "Use :parse_type => 'regex' for faster (but not guaranteed) parsing" puts "Or install 'xmlparser'!" puts "*******************************************************************" end begin $XMLParserClass = XMLParser rescue NameError $XMLParserClass = Object end module Spec::MzData::PrecMzByNum; end module Spec::MzData::XMLParser; end class Spec::MzData::XMLParser::PrecMzByNum < $XMLParserClass ## This is an example from mzXML =begin @@scan_num = nil @@get_data = false attr_accessor :prec_mz def initialize @prec_mz = [] end def startElement(name,attrs) if name == "scan" @@scan_num = attrs["num"].to_i elsif name == "precursorMz" @prec_mz[@@scan_num] = "" @@get_data = true end end def endElement(name) if name == "precursorMz" @@get_data = false end end def character(data) if @@get_data @prec_mz[@@scan_num] << data end end =end end class Spec::MzData::Parser def default_parser xmlparser = false $".each do |lib| if lib =~ /xmlparser/ xmlparser = true end end if xmlparser return "xmlparser" else return "rexml" end end # Returns array where array[scan_num] = precursorMz # Parent scans are not arrayed # Values are strings. Array index likely starts at 1! # parse_type = "regex" | "rexml" | "xmlparser" # (only xmlparser works for now!) def precursor_mz_by_scan(file, parse_type=nil) puts "NO PARSERS AVAIALABLE YET!!" throw Exception # unless parse_type then parse_type = default_parser end # case parse_type # when "xmlparser" # ##XMLParser: # parser = Spec::MzData::XMLParser::PrecMzByNum.new # File.open(file) do |fh| # parser.parse(fh.read) # end # parser.prec_mz # when "regex" # Spec::MzData::Regexp.precursor_mz_by_scan(file) # when "rexml" # listener = Spec::MzData::REXMLStreamListener::PrecMzByNum.new # REXML::Document.parse_stream(File.new(file), listener) # listener.prec_mz # else # puts "Don't recognize parse_type: #{parse_type}" # end end end