Class: SpectrumHash::Splash

Inherits:
Object
  • Object
show all
Defined in:
lib/spectrum_hash/splash.rb

Constant Summary collapse

DEFAULT_SPLASH_API_URI =
'http://splash.fiehnlab.ucdavis.edu/splash/it'
DEFAULT_SPECTRUM_TYPE =
:ms
SPECTRUM_TYPES =
{ms: 1, nmr: 2, uv_vis: 3, ir: 4, raman: 5}.freeze
RETRIES =
3
RETRY_WAIT =
0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spectrum, options = {}) ⇒ Splash

Returns a new instance of Splash.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/spectrum_hash/splash.rb', line 15

def initialize(spectrum, options={})
  # options can be version or uri to use a different end point
  @spectrum      = spectrum
  @spectrum_type = options[:spectrum_type] || DEFAULT_SPECTRUM_TYPE
  @api_uri       = options[:uri]           || DEFAULT_SPLASH_API_URI

  # Load the splash if given
  @splash = options[:splash]

  fetch! if @splash.nil?
end

Instance Attribute Details

#api_uriObject (readonly)

Returns the value of attribute api_uri.



8
9
10
# File 'lib/spectrum_hash/splash.rb', line 8

def api_uri
  @api_uri
end

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/spectrum_hash/splash.rb', line 8

def response
  @response
end

#spectrumObject (readonly)

Returns the value of attribute spectrum.



8
9
10
# File 'lib/spectrum_hash/splash.rb', line 8

def spectrum
  @spectrum
end

#spectrum_typeObject (readonly)

Returns the value of attribute spectrum_type.



8
9
10
# File 'lib/spectrum_hash/splash.rb', line 8

def spectrum_type
  @spectrum_type
end

#splashObject (readonly)

Returns the value of attribute splash.



8
9
10
# File 'lib/spectrum_hash/splash.rb', line 8

def splash
  @splash
end

Instance Method Details

#distance_to(other) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/spectrum_hash/splash.rb', line 82

def distance_to(other)
  sum = 0
  other_histo = other.histogram_list
  histogram_list.each.with_index(0) do |my_value,i|
    sum += ( my_value - other_histo[i] ).abs
  end
  sum
end

#fetch!Object

TODO handle retries



31
32
33
34
35
# File 'lib/spectrum_hash/splash.rb', line 31

def fetch!
  return @splash unless @splash.nil?
  @response = fetch_splash
  @splash = @response.body
end

#hash_blockObject



70
71
72
# File 'lib/spectrum_hash/splash.rb', line 70

def hash_block
  split_splash[3]
end

#histogram_blockObject



66
67
68
# File 'lib/spectrum_hash/splash.rb', line 66

def histogram_block
  split_splash[2]
end

#histogram_listObject



74
75
76
# File 'lib/spectrum_hash/splash.rb', line 74

def histogram_list
  @histogram_list ||= histogram_block.chars.map{|v| v.to_i }
end

#payloadObject



43
44
45
46
47
48
# File 'lib/spectrum_hash/splash.rb', line 43

def payload
  {
    ions: peak_list,
    type: spectrum_type.to_s.upcase
  }
end

#peak_listObject



37
38
39
40
41
# File 'lib/spectrum_hash/splash.rb', line 37

def peak_list
  @peak_list ||= spectrum.map do |peak|
    {mass: peak.first, intensity: peak.last}
  end
end

#split_splashObject



50
51
52
# File 'lib/spectrum_hash/splash.rb', line 50

def split_splash
  @split_splash ||= splash.split(/-/)
end

#to_sObject



78
79
80
# File 'lib/spectrum_hash/splash.rb', line 78

def to_s
  splash
end

#top_ten_blockObject



62
63
64
# File 'lib/spectrum_hash/splash.rb', line 62

def top_ten_block
  split_splash[1]
end

#versionObject



54
55
56
# File 'lib/spectrum_hash/splash.rb', line 54

def version
  "1"
end

#version_blockObject



58
59
60
# File 'lib/spectrum_hash/splash.rb', line 58

def version_block
  split_splash[0]
end