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
# 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
  Ms::Msrun.open(file) do |ms|
    ms.to_search(format, :output => new_filename)
  end
end

Instance Method Details

#get_vals(opts, scan) ⇒ Object

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ms/msrun/search.rb', line 141

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) ⇒ Object

Creates the mgf-type spectrum header



93
94
95
96
97
98
# File 'lib/ms/msrun/search.rb', line 93

def mgf_header(out, scan, sn, z, prec_string, pmz)
  out.puts "BEGIN IONS"
  out.puts "TITLE=#{self.parent_basename_noext}.#{sn}.#{sn}.#{z}"
  out.puts "CHARGE=#{z}+"
  out.printf(prec_string, pmz, scan.precursor.intensity)
end

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

Creates the ms2-type spectrum header



101
102
103
104
105
# File 'lib/ms/msrun/search.rb', line 101

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)



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

def set_opts(opts)
  opts = {
    :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



33
34
35
# File 'lib/ms/msrun/search.rb', line 33

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

#to_ms2(opts = {}) ⇒ Object

same as to_mgf, but for the ms2 format



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

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

#to_search(format, opts) ⇒ Object

performs the common actions for the different formats, and calls the command for the given format



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

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"
  
  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)
        when :ms2 ; ms2_header(out, scan, sn, z, mh, pmz)
        end
        
        scan.spectrum.peaks do |mz,int|
          out.printf(frag_string, mz, sep, int)
        end
        
        out.puts "END IONS\n\n" if format == :mgf
      end
    end
    
    if out_type == :string_io
      out.string
    else
      count
    end
  end
end