From: richard a. <ric...@ya...> - 2006-10-28 06:08:53
|
Open Babel conversions work in Ruby! After studying the Python examples, I found that the Open Babel-Ruby interface handles SMILES and Molfile formats quite well (and presumably the others): $ irb irb(main):001:0> require 'openbabel_wrap' => true irb(main):002:0> obc=Openbabel_wrap::OBConversion.new => #<Openbabel_wrap::OBConversion:0x2b5d3ca6bf98> irb(main):003:0> mol=Openbabel_wrap::OBMol.new => #<Openbabel_wrap::OBMol:0x2b5d3ca670d8> irb(main):004:0> obc.SetInAndOutFormats("smi", "mdl") => true irb(main):005:0> obc.ReadString(mol, 'c1ccccc1') => true irb(main):006:0> mol.NumAtoms => 6 irb(main):007:0> mol.AddHydrogens => true irb(main):008:0> mol.NumAtoms => 12 irb(main):009:0> molfile=obc.WriteString mol => "\n OpenBabel10270622492D\n\n 12 12 0 0 0 0 0 0 0 0999 V2000\n 0.0000 0.0000 0.0000 C 0 0 0 0 0\n 0.0000 0.0000 0.0000 C 0 0 0 0 0\n 0.0000 0.0000 0.0000 C 0 0 0 0 0\n 0.0000 0.0000 0.0000 C 0 0 0 0 0\n 0.0000 0.0000 0.0000 C 0 0 0 0 0\n 0.0000 0.0000 0.0000 C 0 0 0 0 0\n 0.0000 0.0000 0.0000 H 0 0 0 0 0\n 0.0000 0.0000 0.0000 H 0 0 0 0 0\n 0.0000 0.0000 0.0000 H 0 0 0 0 0\n 0.0000 0.0000 0.0000 H 0 0 0 0 0\n 0.0000 0.0000 0.0000 H 0 0 0 0 0\n 0.0000 0.0000 0.0000 H 0 0 0 0 0\n 1 6 2 0 0 0\n 1 2 1 0 0 0\n 1 7 1 0 0 0\n 2 3 2 0 0 0\n 2 8 1 0 0 0\n 3 4 1 0 0 0\n 3 9 1 0 0 0\n 4 5 2 0 0 0\n 4 10 1 0 0 0\n 5 6 1 0 0 0\n 5 11 1 0 0 0\n 6 12 1 0 0 0\nM END\n" irb(main):010:0> I'm wondering about the best names for the library and its modules. For example, this looks ideal to me: require 'openbabel' # the name of the wrapper lib # is openbabel - assuming no # name clash w/ OB itself - I'll # check this out. mol = OpenBabel::OBMol.new obc = OpenBabel::OBConversion.new # etc. This is easy to set up by editing the wrapper generated by SWIG. With some simple Ruby metaprogramming techniques, it's even possible to give method names that follow Ruby convention: require 'openbabel' mol = OpenBabel::OBMol.new atom_count = mol.num_atoms mw = mol.mol_wt # ... etc. And then it might be worth offering a RubyGem. RMagick, which is the Ruby interface for ImageMagick followed this route. cheers, Rich --- richard apodaca <ric...@ya...> wrote: > Success! > > Geoff, I think this works now. I checked out OB-SVN. > With a couple of minor modifications (for example, > changing openbabel_ruby.cpp to openbabel_wrap.cpp, > adding #include <math.h>) - I'll detail them > shortly), > I got the scripts/ruby wrapper compiled. > > Behold: > > $ irb > irb(main):001:0> require 'openbabel_wrap' > => true > irb(main):002:0> m=Openbabel_wrap::OBMol.new > => #<Openbabel_wrap::OBMol:0x2ba75b5563c8> > irb(main):003:0> h=m.NewAtom > => #<Openbabel_wrap::OBAtom:0x2ba75b552778> > irb(main):004:0> h.SetAtomicNum(1) > => nil > irb(main):005:0> m.GetMolWt > => 1.00794 > > It looks like all of the format interconversion > utilities (smiles, mdl, etc) are missing, but we > should be able to fix that. The important thing is > now > we've got a good place to start from. > > It must have been the older version of OB that I was > using (2.0.2) that was the source of the problems. > > Could you verify that the above Ruby code works for > you too? > > BTW, I'll respond in more detail to you're > suggestions > below shortly. > > cheers, > Rich > > --- Geoffrey Hutchison <ge...@ge...> > wrote: > > > > > On Oct 14, 2006, at 4:47 PM, richard apodaca > wrote: > > > > > (3) edit openbabel.i: comment out line 27 (Swig > > can't > > > find std_list.i for Ruby - it works for Python, > > > though). Change line 5 to %module openbabel_wrap > > > (don't want to risk a collision w/ installed > > openbabel > > > library) > > > > Seems like it might soon be a good idea to have > > different openbabel.i > > files for the different scripting modules. The SVN > > version (for > > 2.1.0bX) has the std_list.i #ifdef'ed out > > automatically. > > > > > This generates lots of warnings that don't > appear > > when > > > the argument is -python instead of -ruby (maybe > > this > > > is the problem): > > > > Nah. I get these for Perl and Python too. The > > problem is that C++ has > > different naming restrictions than scripting > > languages. So some > > functions may not be available until remapped. Not > a > > problem for core > > functionality, as far as I've seen. > > > > > (8) save the following as "extconf.rb": > > > require 'mkmf' > > > > > > dir_config('openbabel') > > > dir_config('openbabel-2.0') > > > have_library('openbabel') > > > > > > create_makefile('openbabel_wrap') > > > > One bit which might be good is to have a path to > the > > libopenbabel > > too. On my system it was looking in the normal > > search path, which may > > or may not work for everyone (e.g., with a custom > > --prefix call to > > the Open Babel configure script.) > > > > > (9) create the makefile: > > > > > > ruby extconf.rb > > > > > > --with-openbabel-include='/usr/local/include/openbabel-2.0/openbabel' > > > > > > --with-openbabel-2.0-include='/usr/local/include/openbabel-2.0' > > > > Ideally could this be something that could go into > > the scripts/ > > Makefile.am as a shell script? The Makefiles > should > > have the correct > > paths already. > > > > > (13) require 'openbabel_wrap' > > > irb(main):001:0> require 'openbabel_wrap' > > > > Here (on OS X, Ruby 1.8.4), I get: > > > > irb(main):001:0> require 'openbabel_wrap' > > => true > > > > Not quite sure of enough ruby (or the binding) to > > try anything else > > yet... Maybe you should try this on the SVN > version? > > I'd obviously > > like to get a ruby module into 2.1.0 anyway. > > > > Cheers, > > -Geoff > > > > > > > ____________________________________________________________________________________ > We have the perfect Group for you. Check out the > handy changes to Yahoo! Groups > (http://groups.yahoo.com) > > > ------------------------------------------------------------------------- > Using Tomcat but need to do more? Need to support > web services, security? > Get stuff done quickly with pre-integrated > technology to make your job easier > Download IBM WebSphere Application Server v.1.0.1 > based on Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > OpenBabel-scripting mailing list > Ope...@li... > https://lists.sourceforge.net/lists/listinfo/openbabel-scripting > ____________________________________________________________________________________ Low, Low, Low Rates! Check out Yahoo! Messenger's cheap PC-to-Phone call rates (http://voice.yahoo.com) |