Class: Merger

Inherits:
Object
  • Object
show all
Defined in:
lib/ms/merger.rb

Class Method Summary collapse

Class Method Details

.int_value(arr) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/ms/merger.rb', line 12

def self.int_value(arr)
  if arr.class == Array
    return arr.last + int_value(arr.first)
  else
    return arr
  end
end

.merge(half_range, db) ⇒ Object



37
38
39
40
41
42
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
# File 'lib/ms/merger.rb', line 37

def self.merge(half_range,db)
  prog = Progress.new("Merging Overlaps:")
  db.execute "CREATE TABLE IF NOT EXISTS merged(merge_id INTEGER PRIMARY KEY, merged_vals TEXT, a_vals TEXT, b_vals TEXT)"
  spectra = db.execute "SELECT * FROM spectra"
  spectra = spectra.group_by{|spec| spec[2]}
  total = spectra.size
  merge_id = 0
  k = 0
  spectra.each do |rt,peaks|
    if k.even?
      num = (((k/total)*100).to_i)
      prog.update(num)
    end
    peaks.sort_by!{|a| a[2]} #mz
    peaks_t = peaks.transpose
    pep_ids = peaks_t[1]
    cent_ids = peaks_t[0]
    mzs = peaks_t[3]
    ints = peaks_t[4]
    mzs.each_with_index do |mz,i|
      o_mz = mz
      range = (mz..mz+half_range)
      if range.include?(mzs[i+1])
        metaA_mz = [o_mz, mzs[i+1]]
        meta_int = [ints[i],ints[i+1]]
        sum = ints[i] + ints[i+1]
        new_mz = w_avg(metaA_mz,meta_int)
        db.execute "DELETE FROM spectra WHERE cent_id=#{cent_ids[i]}"
        db.execute "DELETE FROM spectra WHERE cent_id=#{cent_ids[i+1]}"
        db.execute "INSERT INTO spectra VALUES(#{cent_ids[i]},#{pep_ids[i]},#{rt},#{new_mz},#{sum},#{merge_id})"
        db.execute "INSERT INTO merged VALUES(#{merge_id}, '#{cent_ids[i]},#{pep_ids[i]},#{rt},#{new_mz},#{sum}', '#{peaks[i]}', '#{peaks[i+1]}')"
        merge_id += 1
      end
    end
    k += 1
  end
  prog.finish!
end

.mz_value(arr) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/ms/merger.rb', line 4

def self.mz_value(arr)
  if arr.class == Hash
    return arr.keys[0][0]
  else
    return arr
  end
end

.w_avg(values, weights) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ms/merger.rb', line 20

def self.w_avg(values,weights)
  if values.class == hash
    values = values.values.flatten
  end
  a = []
  int = 0
  mz = 0
  values.each_with_index do |v,i| 
    mz = mz_value(v)
    int = int_value(weights[i])
    a<<mz*int
  end
  a = a.inject(:+)
  b = weights.flatten.inject(:+)
  return a/b
end