Class: Rubidium::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubidium/lang.rb

Overview

A general-purpose molecular language converter.

Usage:

require 'rubidium/lang'

c = Rubidium::Converter.new # Defaults to toolkit = 'CDK'

c.set_formats 'smi', 'inchi'
c.convert 'c1ccccc1' # => "InChI=1/C6H6/c1-2-4-6-5-3-1/h1-6H"
c.molecule # returns the molecule representing benzene

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(toolkit = 'CDK') ⇒ Converter

Constructs a CDK converter by default.



39
40
41
42
43
# File 'lib/rubidium/lang.rb', line 39

def initialize(toolkit = 'CDK')
  @toolkit = toolkit
  @readers = create_readers
  @writers = create_writers
end

Instance Attribute Details

#in_formatObject (readonly)

Returns the value of attribute in_format.



34
35
36
# File 'lib/rubidium/lang.rb', line 34

def in_format
  @in_format
end

#moleculeObject

Returns the value of attribute molecule.



33
34
35
# File 'lib/rubidium/lang.rb', line 33

def molecule
  @molecule
end

#out_formatObject (readonly)

Returns the value of attribute out_format.



35
36
37
# File 'lib/rubidium/lang.rb', line 35

def out_format
  @out_format
end

#toolkitObject (readonly)

Returns the value of attribute toolkit.



36
37
38
# File 'lib/rubidium/lang.rb', line 36

def toolkit
  @toolkit
end

Instance Method Details

#convert(input) ⇒ Object

Converts given input based on the formats defined in #set_formats.

call-seq:

convert(input) => string


63
64
65
66
# File 'lib/rubidium/lang.rb', line 63

def convert input
  read input
  write
end

#read(input) ⇒ Object

Reads the molecule encoded by input in the format defined by #in_format.

call-seq:

input(input) => boolean


90
91
92
93
94
95
96
# File 'lib/rubidium/lang.rb', line 90

def read input
  raise "No input format" unless @in_format

  @molecule = @readers[@in_format].read input

  @molecule != nil
end

#set_formats(in_format, out_format) ⇒ Object

Sets the input and output formats for this Converter. CDK converters recognize the input formats: ‘mol’ (MDL V2000 molfile); ‘inchi’, and ‘smi’ (SMILES). Recognized output formats are: ‘mol’, ‘inchi’, and ‘smi’.



50
51
52
53
54
55
# File 'lib/rubidium/lang.rb', line 50

def set_formats in_format, out_format
  set_in_format in_format
  set_out_format out_format

  true
end

#set_in_format(format) ⇒ Object

Sets the input format to one of the formats defined in #set_formats.



70
71
72
73
74
# File 'lib/rubidium/lang.rb', line 70

def set_in_format format
  raise "No input format matching #{format}" unless @readers[format]

  @in_format = format
end

#set_out_format(format) ⇒ Object

Sets the output format to one of the formats defined in #set_formats.



78
79
80
81
82
# File 'lib/rubidium/lang.rb', line 78

def set_out_format format
  raise "No output format matching #{format}" unless @writers[format]

  @out_format = format
end

#writeObject

Writes the molecule encoded by the previous call to #read in the format defined by #out_format.

call-seq:

write => string


104
105
106
107
108
# File 'lib/rubidium/lang.rb', line 104

def write
  raise "No output format" unless @out_format

  @writers[@out_format].write @molecule
end