Class: MS::Fragmenter

Inherits:
Object
  • Object
show all
Includes:
MS
Defined in:
lib/fragmenter.rb

Defined Under Namespace

Classes: TableEntry

Constant Summary collapse

Ion_Defaults =
{:b => true, :y => true}
Defaults =
{:charge_states => true, :avg => false}

Constants included from MS

AvgResidueMasses, CTerm, IonTypeMassDelta, MonoResidueMasses, NTerm, Proton

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MS

#charge_state, #precursor_mass

Constructor Details

#initialize(opts = {}, ion_opts = {}) ⇒ Fragmenter

Returns a new instance of Fragmenter.



11
12
13
14
# File 'lib/fragmenter.rb', line 11

def initialize(opts = {}, ion_opts = {})
  set_options(opts, ion_opts)
  self
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



7
8
9
# File 'lib/fragmenter.rb', line 7

def list
  @list
end

Instance Method Details

#calculate_fragments(sequence) ⇒ Object

set_options



45
46
47
48
49
50
51
52
53
# File 'lib/fragmenter.rb', line 45

def calculate_fragments(sequence)
  arr = sequence.upcase.split('')
  out = [[],[]]
  (0..arr.size-2).each do |i|
    out[0] << arr[0..i].join
    out[1] <<  arr[(i+1)..-1].join
  end
  out
end

#fragment(pep_seq, options = {}) ⇒ Object

Options may include a list of fragment classes as symbols (i.e. :b, :y)



56
57
58
59
60
# File 'lib/fragmenter.rb', line 56

def fragment(pep_seq, options={}) # TODO handle an intensity option to handle normalization and scaling...?
  set_options(options) unless options.empty?
  generate_fragment_masses(pep_seq)
  @list.map(&:mass)
end

#generate_fragment_masses(sequence) ⇒ Object

Returns the TableEntry object which should be easy to use for table generation



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
# File 'lib/fragmenter.rb', line 61

def generate_fragment_masses(sequence) # Returns the TableEntry object which should be easy to use for table generation
  @sequence = sequence
  @max_charge ||= MS::ChargeCalculator.charge_at_pH(MS::ChargeCalculator.identify_potential_charges(sequence), 2).ceil
  ### Calculate the base ion masses	
  n_terms, c_terms = calculate_fragments(sequence)
  n_terms.map! do |seq|
    mass = MS::NTerm
    seq.chars.map(&:to_sym).each do |residue|
      mass += @mass_list[residue]
    end
    [seq, mass]
  end
  c_terms.map! do |seq|
    mass = MS::CTerm
    seq.chars.map(&:to_sym).each do |residue|
      mass += @mass_list[residue]
    end
    [seq, mass]
  end
### Tablify and generate a comprehensive list of ions
  list = []
  send_to_list = lambda do |fragment_arr, iontypes_arr|
    fragment_arr.each do |n_terms|
      seq = n_terms.first
      mass = n_terms.last
      iontypes_arr.each do |iontype|
        (1..@max_charge).each do |charge|
          charge_legend = '+'*charge
          list << TableEntry.new("#{iontype}(#{seq.size})#{charge_legend}".to_sym, seq, charge_state(mass + IonTypeMassDelta[iontype], charge), charge)
        end # 1..max_charge
      end	# iontypes_arr
    end # fragment_arr
  end # lambda block
  send_to_list.call(n_terms, @n_term_search_ion_types)
  send_to_list.call(c_terms, @c_term_search_ion_types)		
  @list = list
end

#graph(list = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/fragmenter.rb', line 116

def graph(list = nil)
  list ? list : list = @list
  require 'rserve/simpler'
  robj = Rserve::Simpler.new
  hash = {}
  hash["mass"] = list.map(&:mass)
  hash["intensity"] = list.map{ 1000.0} # Hacky standard intensity value
  robj.converse( masses: hash.to_dataframe) do 
    "attach(masses)"
  end
  #robj.converse( data: Rserve::DataFrame.from_structs(list))
  robj.converse "setwd('#{Dir.pwd}')"
  output_file_name = "#{@sequence}_spectra.png"
  robj.converse do 
    %Q{png(file='#{output_file_name}')
      plot(masses$mass, masses$intensity, type='h')
      dev.off()
  }
  end	
  output_file_name
end

#set_options(opts, ion_opts) ⇒ Object



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
# File 'lib/fragmenter.rb', line 15

def set_options(opts, ion_opts)
  #@opts = Default_fragments.merge(opts)
  opts = Defaults.merge(opts)
  ion_opts = Ion_Defaults.merge(ion_opts)
  @n_term_search_ion_types = []
  @c_term_search_ion_types = []
  @max_charge = 1 unless opts[:charge_states]
  #puts "options :charge_states = #{opts[:charge_states]}"
  ion_opts.each do |key, v|
    if v
      case key 
        when :b
          @n_term_search_ion_types.push(:b, :b_star, :b_not)
        when :a
          @n_term_search_ion_types.push(:a, :a_star, :a_not)
        when :c
          @n_term_search_ion_types << :c
        when :x
          @c_term_search_ion_types << :x
        when :y
          @c_term_search_ion_types.push(:y, :y_star, :y_not)
        when :z
          @c_term_search_ion_types << :z
      end
    end
  end
  @mass_list = opts[:avg] ? MS::AvgResidueMasses : MS::MonoResidueMasses
  #putsv "@mass_list: #{@mass_list}"
end

#to_mgf(seq = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fragmenter.rb', line 98

def to_mgf(seq = nil)
  if seq.nil?
    seq = @sequence
    list = @list
  else
    list = generate_fragment_masses(seq)
  end
  intensity = 1000 # An arbitrary intensity value
  output_arr = []
  output_arr << %Q{COM=Project: In-silico Fragmenter\nBEGIN IONS\nPEPMASS=#{precursor_mass(seq, @max_charge)}\nCHARGE=#{@max_charge}+\nTITLE=Label: Sequence is #{seq}}
  list.sort_by{|a| a.mass}.each do |table_entry|
    #	TableEntry = Struct.new(:ion, :seq, :mass, :charge)
    output_arr << "#{"%.5f" % table_entry.mass }\t#{intensity}"
  end
  output_arr << "END IONS"
  File.open("#{seq}.mgf", 'w') {|o| o.print output_arr.join("\n") }
  output_arr.join("\n")
end