rubabel 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -16,9 +16,19 @@ interface attempts to be a ruby-ish analogue of
16
16
 
17
17
  == Installing
18
18
 
19
- === Install openbabel
19
+ First, many thanks to Andreas Maunz for packaging openbabel as a gem which makes this install quite painless.
20
20
 
21
- First, make sure you have the ruby openbabel bindings properly installed. You can do this by {properly installing the gem][https://github.com/amaunz/openbabel-gem]. Alternatively, you can following these instructions to build from source:
21
+ === Quick Install
22
+
23
+ On a POSIX system, make sure you have openbabel (including header files), cmake, curl, tar, sed and make {see openbabel instructions}[https://github.com/amaunz/openbabel-gem]. On ubuntu/debian:
24
+
25
+ sudo apt-get install openbabel libopenbabel-dev cmake make curl
26
+
27
+ Then install the gem (which should install the openbabel gem, too):
28
+
29
+ gem install rubabel
30
+
31
+ === Building from Source
22
32
 
23
33
  1. download openbabel
24
34
  2. swap out Init_OpenBabel for Init_openbabel in scripts/ruby/openbabel-ruby.cpp (see here[http://forums.openbabel.org/Ruby-Open-Babel-in-2-1-1-td957640.html]). Some versions have this fixed already, apparently.
@@ -39,13 +49,7 @@ Here's a complete example of compiling for a single user on Ubuntu 11.10 and pro
39
49
  cmake ../openbabel-2.3.1 -DRUBY_BINDINGS=ON -DCMAKE_INSTALL_PREFIX=~/tools/openbabel-rvmruby1.9.3
40
50
  make && make install
41
51
 
42
- === Install the gem
43
-
44
- gem install rubabel
45
-
46
- Assuming you have the {dependencies for the openbabel gem}[https://github.com/amaunz/openbabel-gem], you could install in one go like this:
47
-
48
- gem install openbabel rubabel
52
+ [I still need to make directions to install the gem on top of a build from source]
49
53
 
50
54
  == Copyright
51
55
 
data/Rakefile CHANGED
@@ -14,12 +14,20 @@ Jeweler::Tasks.new do |gem|
14
14
  interface attempts to be a ruby-ish analogue of pybel.}
15
15
  gem.email = "jtprince@gmail.com"
16
16
  gem.authors = ["John T. Prince"]
17
+ [
18
+ ["openbabel", "~> 2.3.1.2"],
19
+ ["commander", "~> 4.1.2"],
20
+ ["andand", "~> 1.3.3"]
21
+ ].each do |args|
22
+ gem.add_dependency(*args)
23
+ end
24
+
17
25
  [
18
26
  ["rspec", "~> 2.8.0"],
19
27
  ["rdoc", "~> 3.12"],
20
28
  ["jeweler", "~> 1.8.3"]
21
- ].each do |name, version_string|
22
- gem.add_development_dependency(name, version_string)
29
+ ].each do |args|
30
+ gem.add_development_dependency(*args)
23
31
  end
24
32
  end
25
33
  Jeweler::RubygemsDotOrgTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/bin/rubabel ADDED
@@ -0,0 +1,229 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'commander/import'
5
+ require 'rubabel'
6
+
7
+ program :version, Rubabel::VERSION
8
+ program :description, 'tools for manipulating molecules with rubabel'
9
+
10
+ =begin
11
+ filename = filename.first unless options[:string_format]
12
+ mol_iter =
13
+ if str_f=options[:string_format]
14
+ # assumes you aren't passing massive numbers of molecules as strings
15
+ # on the commandline
16
+ filename.map {|str| Rubabel.read_string(str, str_f.to_sym) }.each
17
+ else
18
+ Rubabel.foreach(ARGV.first)
19
+ end
20
+ mol_iter.each do |mol|
21
+ mol.add_h! unless mol.ob.has_hydrogens_added
22
+ mol.ob.get_title ||
23
+
24
+ end
25
+ =end
26
+
27
+ def mol_iterator(args, strings)
28
+
29
+ end
30
+
31
+
32
+ command :babel do |c|
33
+ c.syntax = 'rubabel babel [options]'
34
+ c.summary = ''
35
+ c.description = ''
36
+ c.example 'description', 'command example'
37
+ c.option '--some-switch', 'Some switch that does something'
38
+ c.action do |args, options|
39
+ # Do something or c.when_called Rubabel::Commands::Babel
40
+ end
41
+ end
42
+
43
+ command :obabel do |c|
44
+ c.syntax = 'rubabel obabel [options]'
45
+ c.summary = ''
46
+ c.description = ''
47
+ c.example 'description', 'command example'
48
+ c.option '--some-switch', 'Some switch that does something'
49
+ c.action do |args, options|
50
+ # Do something or c.when_called Rubabel::Commands::Obabel
51
+ end
52
+ end
53
+
54
+ command :chiral do |c|
55
+ c.syntax = 'rubabel chiral [options]'
56
+ c.summary = ''
57
+ c.description = ''
58
+ c.example 'description', 'command example'
59
+ c.option '--some-switch', 'Some switch that does something'
60
+ c.action do |args, options|
61
+ # Do something or c.when_called Rubabel::Commands::Chiral
62
+ end
63
+ end
64
+
65
+ command :conformer do |c|
66
+ c.syntax = 'rubabel conformer [options]'
67
+ c.summary = ''
68
+ c.description = ''
69
+ c.example 'description', 'command example'
70
+ c.option '--some-switch', 'Some switch that does something'
71
+ c.action do |args, options|
72
+ # Do something or c.when_called Rubabel::Commands::Conformer
73
+ end
74
+ end
75
+
76
+ command :energy do |c|
77
+ c.syntax = 'rubabel energy [options]'
78
+ c.summary = ''
79
+ c.description = ''
80
+ c.example 'description', 'command example'
81
+ c.option '--some-switch', 'Some switch that does something'
82
+ c.action do |args, options|
83
+ # Do something or c.when_called Rubabel::Commands::Energy
84
+ end
85
+ end
86
+
87
+ command :fit do |c|
88
+ c.syntax = 'rubabel fit [options]'
89
+ c.summary = ''
90
+ c.description = ''
91
+ c.example 'description', 'command example'
92
+ c.option '--some-switch', 'Some switch that does something'
93
+ c.action do |args, options|
94
+ # Do something or c.when_called Rubabel::Commands::Fit
95
+ end
96
+ end
97
+
98
+ command :gen do |c|
99
+ c.syntax = 'rubabel gen [options]'
100
+ c.summary = ''
101
+ c.description = ''
102
+ c.example 'description', 'command example'
103
+ c.option '--some-switch', 'Some switch that does something'
104
+ c.action do |args, options|
105
+ # Do something or c.when_called Rubabel::Commands::Gen
106
+ end
107
+ end
108
+
109
+ command :grep do |c|
110
+ c.syntax = 'rubabel grep [options]'
111
+ c.summary = ''
112
+ c.description = ''
113
+ c.example 'description', 'command example'
114
+ c.option '--some-switch', 'Some switch that does something'
115
+ c.action do |args, options|
116
+ # Do something or c.when_called Rubabel::Commands::Grep
117
+ end
118
+ end
119
+
120
+ command :gui do |c|
121
+ c.syntax = 'rubabel gui [options]'
122
+ c.summary = ''
123
+ c.description = ''
124
+ c.example 'description', 'command example'
125
+ c.option '--some-switch', 'Some switch that does something'
126
+ c.action do |args, options|
127
+ # Do something or c.when_called Rubabel::Commands::Gui
128
+ end
129
+ end
130
+
131
+ command :minimize do |c|
132
+ c.syntax = 'rubabel minimize [options]'
133
+ c.summary = ''
134
+ c.description = ''
135
+ c.example 'description', 'command example'
136
+ c.option '--some-switch', 'Some switch that does something'
137
+ c.action do |args, options|
138
+ # Do something or c.when_called Rubabel::Commands::Minimize
139
+ end
140
+ end
141
+
142
+ command :probe do |c|
143
+ c.syntax = 'rubabel probe [options]'
144
+ c.summary = ''
145
+ c.description = ''
146
+ c.example 'description', 'command example'
147
+ c.option '--some-switch', 'Some switch that does something'
148
+ c.action do |args, options|
149
+ # Do something or c.when_called Rubabel::Commands::Probe
150
+ end
151
+ end
152
+
153
+ command :prop do |c|
154
+ c.syntax = 'rubabel prop [options]'
155
+ c.summary = ''
156
+ c.description = ''
157
+ c.example 'description', 'command example'
158
+ c.option '--some-switch', 'Some switch that does something'
159
+ c.action do |args, options|
160
+ # Do something or c.when_called Rubabel::Commands::Prop
161
+ end
162
+ end
163
+
164
+ command :probe do |c|
165
+ c.syntax = 'rubabel probe [options]'
166
+ c.summary = ''
167
+ c.description = ''
168
+ c.example 'description', 'command example'
169
+ c.option '--some-switch', 'Some switch that does something'
170
+ c.action do |args, options|
171
+ # Do something or c.when_called Rubabel::Commands::Probe
172
+ end
173
+ end
174
+
175
+ command :prop do |c|
176
+ c.syntax = 'rubabel prop [options]'
177
+ c.summary = ''
178
+ c.description = ''
179
+ c.example 'description', 'command example'
180
+ c.option '--some-switch', 'Some switch that does something'
181
+ c.action do |args, options|
182
+ # Do something or c.when_called Rubabel::Commands::Prop
183
+ end
184
+ end
185
+
186
+ command :rotamer do |c|
187
+ c.syntax = 'rubabel rotamer [options]'
188
+ c.summary = ''
189
+ c.description = ''
190
+ c.example 'description', 'command example'
191
+ c.option '--some-switch', 'Some switch that does something'
192
+ c.action do |args, options|
193
+ # Do something or c.when_called Rubabel::Commands::Rotamer
194
+ end
195
+ end
196
+
197
+ command :rotate do |c|
198
+ c.syntax = 'rubabel rotate [options]'
199
+ c.summary = ''
200
+ c.description = ''
201
+ c.example 'description', 'command example'
202
+ c.option '--some-switch', 'Some switch that does something'
203
+ c.action do |args, options|
204
+ # Do something or c.when_called Rubabel::Commands::Rotate
205
+ end
206
+ end
207
+
208
+ command :spectrophore do |c|
209
+ c.syntax = 'rubabel spectrophore [options]'
210
+ c.summary = ''
211
+ c.description = ''
212
+ c.example 'description', 'command example'
213
+ c.option '--some-switch', 'Some switch that does something'
214
+ c.action do |args, options|
215
+ # Do something or c.when_called Rubabel::Commands::Spectrophore
216
+ end
217
+ end
218
+
219
+ command :roundtrip do |c|
220
+ c.syntax = 'rubabel roundtrip [options]'
221
+ c.summary = ''
222
+ c.description = ''
223
+ c.example 'description', 'command example'
224
+ c.option '--some-switch', 'Some switch that does something'
225
+ c.action do |args, options|
226
+ # Do something or c.when_called Rubabel::Commands::Roundtrip
227
+ end
228
+ end
229
+
data/lib/rubabel.rb CHANGED
@@ -1,10 +1,21 @@
1
1
  require 'openbabel'
2
+ require 'rubabel/molecule'
2
3
 
3
4
  %w(atom molecule fingerprint smarts molecule_data).each do |klass|
4
5
  require "rubabel/#{klass}"
5
6
  end
6
7
 
7
8
  module Rubabel
9
+ # the mass of an electron
10
+ MASS_E = 0.0005486
11
+ # www.mikeblaber.org/oldwine/chm1045/notes/Atoms/.../Atoms03.htm
12
+
13
+ # available force-fields (would like to generate this with introspection)
14
+ AVAILABLE_FORCEFIELDS = [:mmff94, :ghemical, :mm2, :uff]
15
+ DEFAULT_FORCEFIELD = AVAILABLE_FORCEFIELDS.first
16
+
17
+ BUILDER = OpenBabel::OBBuilder.new
18
+
8
19
  # the command to execute the utility. They are initialized to be eponymous.
9
20
  CMD = {
10
21
  babel: 'babel',
@@ -13,6 +24,14 @@ module Rubabel
13
24
 
14
25
  class << self
15
26
 
27
+ def [](string, type=Rubabel::Molecule::DEFAULT_IN_TYPE)
28
+ Rubabel::Molecule.from_string(string, type)
29
+ end
30
+
31
+ def force_field(type=DEFAULT_FORCEFIELD)
32
+ OpenBabel::OBForceField.find_force_field(type.to_s)
33
+ end
34
+
16
35
  # returns a hash keyed by type (Symbol) pointing to a description of the
17
36
  # format
18
37
  def in_formats
@@ -25,13 +44,29 @@ module Rubabel
25
44
  @out_formats ||= formats_to_hash(OpenBabel::OBConversion.new.get_supported_output_format)
26
45
  end
27
46
 
47
+ # returns the format Symbol that can be used for conversion, or nil if
48
+ # the extension is not recognized.
49
+ def format_from_ext(filename)
50
+ obformat = OpenBabel::OBConversion.format_from_ext(filename)
51
+ obformat.get_id.to_sym if obformat
52
+ end
53
+
54
+ alias_method :filetype, :format_from_ext
55
+
56
+ # returns a format Symbol that can be used for conversion, or nil if the
57
+ # mime-type is not recognized
58
+ def format_from_mime(mime_type)
59
+ obformat = OpenBabel::OBConversion.format_from_mime(mime_type)
60
+ obformat.get_id.to_sym if obformat
61
+ end
62
+
28
63
  # determines the extension from filename if type is nil
29
64
  def foreach(filename, type=nil, &block)
30
65
  block or return enum_for(__method__, filename, type)
31
66
  (obmol, obconv, not_at_end) = read_first_obmol(filename, type)
32
67
  # the obmol is not valid if we are already at the end!
33
68
  while not_at_end
34
- block.call Rubabel::Molecule.new(obmol)
69
+ block.call Rubabel::Molecule.new(obmol, obconv)
35
70
  obmol = OpenBabel::OBMol.new
36
71
  not_at_end = obconv.read(obmol)
37
72
  end
@@ -40,24 +75,18 @@ module Rubabel
40
75
  # returns a Rubabel::Molecule (the first in the file if there are
41
76
  # multiples). See ::foreach for accessing all molecules in a file
42
77
  # determines the type from the extension if type is nil.
43
- def read_file(filename, type=nil)
44
- obmol = read_first_obmol(filename, type).first
45
- Rubabel::Molecule.new(obmol)
78
+ def molecule_from_file(filename, type=nil)
79
+ (obmol, obconv, not_at_end) = read_first_obmol(filename, type).first
80
+ Rubabel::Molecule.new(obmol, obconv)
46
81
  end
47
82
 
48
83
  # reads one molecule from the string
49
- def read_string(string, type=:smi)
84
+ def molecule_from_string(string, type=:smi)
50
85
  obmol = OpenBabel::OBMol.new
51
86
  obconv = OpenBabel::OBConversion.new
52
87
  obconv.set_in_format(type.to_s) || raise(ArgumentError, "invalid format #{type}")
53
- success = obconv.read_string(obmol, string)
54
- Rubabel::Molecule.new(obmol)
55
- end
56
-
57
- # returns a filetype symbol based on the extension
58
- def filetype(filename)
59
- # should use the openbabel method in the future
60
- File.extname(filename)[1..-1].to_sym
88
+ obconv.read_string(obmol, string) || raise(ArgumentError, "invalid string" )
89
+ Rubabel::Molecule.new(obmol, obconv)
61
90
  end
62
91
 
63
92
  private
@@ -66,7 +95,7 @@ module Rubabel
66
95
  # but is necessary based on discrepancies between accessing the first
67
96
  # molecule and subsequent molecules.
68
97
  def read_first_obmol(filename, type=nil)
69
- type ||= filetype(filename)
98
+ type ||= format_from_ext(filename)
70
99
  obconv = OpenBabel::OBConversion.new
71
100
  obconv.set_in_format(type.to_s) || raise(ArgumentError, "invalid format #{type}")
72
101
  obmol = OpenBabel::OBMol.new
@@ -97,6 +126,9 @@ module Rubabel
97
126
 
98
127
  # atomic number to lowercase symbol abbreviation
99
128
  NUM_TO_EL = Hash[ ELEMENTS.each_with_index.map {|el,i| [i+1,el.downcase.to_sym] } ]
129
+
130
+ EL_TO_NUM = NUM_TO_EL.invert
131
+ ELEMENT_TO_NUM = NUM_TO_ELEMENT.invert
100
132
  end
101
133
 
102
134
  =begin
data/lib/rubabel/atom.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'matrix'
2
+ require 'andand'
2
3
 
3
4
  require 'rubabel/bond'
4
5
 
@@ -12,6 +13,21 @@ module Rubabel
12
13
  class Atom
13
14
  include Enumerable
14
15
 
16
+ class << self
17
+ # takes an element symbol and creates that atom
18
+ def [](el_sym=:h, id=0)
19
+ ob_atom = OpenBabel::OBAtom.new
20
+ ob_atom.set_id(id)
21
+ ob_atom.set_atomic_num(Rubabel::EL_TO_NUM[el_sym])
22
+ self.new(ob_atom)
23
+ end
24
+ end
25
+
26
+ # returns the molecule that is parent of this atom
27
+ def mol
28
+ @ob.get_parent.andand.upcast
29
+ end
30
+
15
31
  # the OpenBabel::OBAtom object
16
32
  attr_accessor :ob
17
33
 
@@ -42,6 +58,16 @@ module Rubabel
42
58
  NUM_TO_ELEMENT[atomic_num]
43
59
  end
44
60
 
61
+ # creates a bond and adds it to both atoms
62
+ def add_atom!(other)
63
+ obbond = OpenBabel::OBBond.new
64
+ obbond.set_begin(self.ob)
65
+ obbond.set_end(other.ob)
66
+ @ob.add_bond(obbond)
67
+ other.ob.add_bond(obbond)
68
+ self
69
+ end
70
+
45
71
  def each_bond(&block)
46
72
  block or return enum_for(__method__)
47
73
  iter = @ob.begin_bonds
@@ -54,6 +80,11 @@ module Rubabel
54
80
 
55
81
  alias_method :each, :each_bond
56
82
 
83
+ # retrieves the bond
84
+ def get_bond(atom)
85
+ @ob.get_bond(atom.ob).andand.upcast
86
+ end
87
+
57
88
  # returns the bonds. Consider using each_bond.
58
89
  def bonds
59
90
  each_bond.map.to_a
@@ -92,6 +123,11 @@ module Rubabel
92
123
  end
93
124
  alias_method :charge, :formal_charge
94
125
 
126
+ def formal_charge=(val)
127
+ @ob.set_formal_charge(val)
128
+ end
129
+ alias_method :charge=, :formal_charge=
130
+
95
131
  def heavy_valence
96
132
  @ob.get_heavy_valence
97
133
  end
@@ -132,6 +168,48 @@ module Rubabel
132
168
  @ob.get_vector
133
169
  end
134
170
 
171
+ def hydrogen?() @ob.is_hydrogen end
172
+ def carbon?() @ob.is_carbon end
173
+ def nitrogen?() @ob.is_nitrogen end
174
+ def oxygen?() @ob.is_oxygen end
175
+ def sulfur?() @ob.is_sulfur end
176
+ def phosphorus?() @ob.is_phosphorus end
177
+ def aromatic?() @ob.is_aromatic end
178
+ def in_ring?() @ob.is_in_ring end
179
+ def in_ring_size?() @ob.is_in_ring_size end
180
+ def heteroatom?() @ob.is_heteroatom end
181
+ def not_c_or_h?() @ob.is_not_cor_h end
182
+ def connected?() @ob.is_connected end
183
+ def one_three?() @ob.is_one_three end
184
+ def one_four?() @ob.is_one_four end
185
+ def carboxyl_oxygen?() @ob.is_carboxyl_oxygen end
186
+ def phosphate_oxygen?() @ob.is_phosphate_oxygen end
187
+ def sulfate_oxygen?() @ob.is_sulfate_oxygen end
188
+ def nitro_oxygen?() @ob.is_nitro_oxygen end
189
+ def amide_nitrogen?() @ob.is_amide_nitrogen end
190
+ def polar_hydrogen?() @ob.is_polar_hydrogen end
191
+ def non_polar_hydrogen?() @ob.is_non_polar_hydrogen end
192
+ def aromatic_noxide?() @ob.is_aromatic_noxide end
193
+ def chiral?() @ob.is_chiral end
194
+ def axial?() @ob.is_axial end
195
+ def clockwise?() @ob.is_clockwise end
196
+ def anti_clockwise?() @ob.is_anti_clockwise end
197
+ def positive_stereo?() @ob.is_positive_stereo end
198
+ def negative_stereo?() @ob.is_negative_stereo end
199
+ def chirality_specified?() @ob.has_chirality_specified end
200
+ def chiral_volume?() @ob.has_chiral_volume end
201
+ def hbond_acceptor?() @ob.is_hbond_acceptor end
202
+ def hbond_donor?() @ob.is_hbond_donor end
203
+ def hbond_donor_h?() @ob.is_hbond_donor_h end
204
+
205
+ def carboxyl_carbon?
206
+ atoms.any?(&:carboxyl_oxygen?)
207
+ end
208
+
209
+ # # does this carbon hold a primary alcohol
210
+ # def primary_alcohol_carbon?
211
+ # end
212
+
135
213
  def coords
136
214
  Vector[@ob.x, @ob.y, @ob.z]
137
215
  end