Module: Ms::Msrun::Search

Included in:
Ms::Msrun
Defined in:
lib/ms/msrun/search.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(format, file, opts = {}) ⇒ Object

convenience method to convert a file to a search format

Examples:

Ms::Msrun::Search.convert(:mgf, "myfilename.mzXML", :run_id => 25, :run_id_cat => '__')
# writes to the file: "myfilename__25.mgf"

Parameters:

  • format (Symbol)

    valid format symbol

  • file (String)

    the filename (relative or absolute)

  • opts (Hash) (defaults to: {})

    other options taken by Search:to_search

Options Hash (opts):

  • :run_id (Object)

    concatenated to output filename with :run_id_cat

  • :run_id_cat (String) — default: '.'

    concatenate run_id



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ms/msrun/search.rb', line 18

def self.convert(format, file, opts={})
  opts[:run_id_cat] ||= '.'
  new_filename = file.chomp(File.extname(file))
  if opts[:run_id]
    new_filename << opts[:run_id_cat].to_s << opts[:run_id].to_s
  end
  new_filename << '.' << format.to_s

  search_opts = {:output => new_filename}.merge(opts)
  [:run_id, :run_id_cat].each {|s| search_opts.delete(s) }

  Ms::Msrun.open(file) do |ms|
    ms.to_search(format, search_opts)
  end
end

Instance Method Details

#get_vals(opts, scan) ⇒ Object

Factored out method. Simply serves to reduce the size of to_search



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ms/msrun/search.rb', line 159

def get_vals(opts, scan)
  if opts[:determine_plus_ones]
    # tic under precursor > 95% and true = save the spectrum info
    if scan.plus1?(0.95)
      opts[:charge_states] = [1]
    end
  end
  
  chrg_sts = scan.precursor.charge_states
  if chrg_sts.nil? || !chrg_sts.first.is_a?(Integer)
    chrg_sts = opts[:charge_states_for_unknowns]
  end
  
  pmz = scan.precursor && scan.precursor.mz
  
  [opts, chrg_sts, pmz]
end

#mgf_header(out, scan, sn, z, prec_string, pmz, rtinseconds = nil) ⇒ Object

Creates the mgf-type spectrum header



107
108
109
110
111
112
113
114
# File 'lib/ms/msrun/search.rb', line 107

def mgf_header(out, scan, sn, z, prec_string, pmz, rtinseconds=nil)
  out.puts "BEGIN IONS"
  out.puts "TITLE=#{self.parent_basename_noext}.#{sn}.#{sn}.#{z}"
  out.puts "CHARGE=#{z}+"
  # our current mzML parser doesn't have scan.time implemented...
  out.puts "RTINSECONDS=#{rtinseconds}" if rtinseconds
  out.printf(prec_string, pmz, scan.precursor.intensity)
end

#ms2_header(out, scan, sn, z, mh, pmz) ⇒ Object

Creates the ms2-type spectrum header



117
118
119
120
121
# File 'lib/ms/msrun/search.rb', line 117

def ms2_header(out, scan, sn, z, mh, pmz)
  [['S', sn, sn, pmz], ['I', 'RTime', scan.time], ['Z', z, mh]].each do |ar|
    out.puts ar.join("\t")
  end
end

#set_opts(opts) ⇒ Object

Sets options and other variables to be used by the to_* methods.

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :included_scans (Array)

    only include a subset of scans in the output (:ms_levels precedes :included_scans)



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
# File 'lib/ms/msrun/search.rb', line 125

def set_opts(opts)
  opts = {
    :retention_times => true,  # includes retention time if applicable
    :filter_zero_intensity => true,
    :output => nil,  # an output file or io object
    :bottom_mh => 0.0,
    :top_mh => nil,
    :ms_levels => (2..-1),  # range or intger, -1 at end will be substituted for last level
    :min_peaks => 0,
    :prec_mz_precision => 6,
    :prec_int_precision => 6,
    :frag_mz_precision => 5,
    :frag_int_precision => 1,
    :charge_states_for_unknowns => [2,3],
    :determine_plus_ones => false,
    :included_scans => nil 
  }.merge(opts)
  
  if opts[:top_mh].nil? || opts[:top_mh] == -1
    opts[:top_mh] = nil
  end
  
  if opts[:last_scan].nil? or opts[:last_scan] == -1
    opts[:last_scan] = self.scan_nums.last
  end
  
  if !opts[:ms_levels].is_a?(Integer) && opts[:ms_levels].last == -1
    opts[:ms_levels] = ((opts[:ms_levels].first)..(scan_counts.size-1))
  end
  
  opts
end

#to_mgf(opts = {}) ⇒ Object

Returns a string unless :output given (may be a String (filename) or a writeable IO object in which case the data is written to file or io and the number of spectra written is returned



37
38
39
# File 'lib/ms/msrun/search.rb', line 37

def to_mgf(opts={})
  to_search(:mgf, opts)
end

#to_ms2(opts = {}) ⇒ Object

same as to_mgf, but for the ms2 format



42
43
44
# File 'lib/ms/msrun/search.rb', line 42

def to_ms2(opts={})
  to_search(:ms2, opts)
end

#to_search(format, opts) ⇒ Object

performs the common actions for the different formats (currently :mgf and :ms2), and calls the command for the given format recognizes:

:output => file to write to
:included_scans => Array of scan numbers
:filter_zero_intensity => remove peaks whose intensity is zero
:bottom_mh => lowest mh value to consider
:top_mh => highest mh value to consider


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
# File 'lib/ms/msrun/search.rb', line 54

def to_search(format, opts)

  # set up included_scans for fast access
  if opts[:included_scans]
    included_scans = []
    opts[:included_scans].each {|num| included_scans[num] = true }
  end

  opts = set_opts(opts)
  
  sep = ' '
  frag_string = "%0.#{opts[:frag_mz_precision]}f%s%0.#{opts[:frag_int_precision]}f\n"
  mgf_prec_string = "PEPMASS=%0.#{opts[:prec_mz_precision]}f %0.#{opts[:prec_int_precision]}f\n"
  retention_times = opts[:retention_times]
  
  any_output(opts[:output]) do |out, out_type|
    each_scan(:ms_level => opts[:ms_levels]) do |scan|
      sn = scan.num

      next unless included_scans[sn] if included_scans
      next unless scan.num_peaks >= opts[:min_peaks]
      
      opts, chrg_sts, pmz = get_vals(opts, scan)
      
      chrg_sts.each do |z|
        mh = (pmz * z) - (z - 1)*Ms::Mass::PROTON
        next unless (mh >= opts[:bottom_mh]) 
        next unless (mh <= opts[:top_mh]) if opts[:top_mh]
        
        case format
        when :mgf ; mgf_header(out, scan, sn, z, mgf_prec_string, pmz, (retention_times ? scan.time : nil))
        when :ms2 ; ms2_header(out, scan, sn, z, mh, pmz)
        end
        
        scan.spectrum.peaks do |mz,int|
          unless opts[:filter_zero_intensity] && (int == 0.0)
            out.printf(frag_string, mz, sep, int)
          end
        end
        
        out.puts "END IONS\n\n" if format == :mgf
      end
    end
    
    if out_type == :string_io
      out.string
    else
      count
    end
  end
end