Sunday, December 14, 2008

Introduction to OBDotNet: Part 1

This tutorial is intended for C# developers looking to use OBDotNet to develop chemically aware applications and libraries. It assumes that the reader is familiar with OpenBabel or similar cheminformatics toolkits.

What follows is an overview of the core classes in the C# bindings for OpenBabel with an eye toward things that may confuse C# programmers who are unfamiliar with SWIG and C++. If you are completely new to OpenBabel start by skimming the documentation and the examples in the project wiki.


To install OBDotNet:

1)Download the current release
2)Unzip into the directory of your choice
3)Set an environment variable named BABEL_DATADIR to point to the data folder.
3)Register OBDotNet.dll in the GAC using gacutil.exe or the .Net management console.(optional)


Components:

OBDotNet currently consists of a single namespace: OpenBabel. It contains C# wrappers for all the publicly exposed classes, functions, and constants defined in the Open Babel library. It also contains a number of classes defined by SWIG that wrap arrays and STL types exposed by Open Babel. There are a few things that users need to be aware of if they are not familiar with SWIG.

1) The openbabel class

SWIG creates a module class that contains global constants and functions. The module class for OBDotNet is the openbabel class. Important methods belonging to the openbabel class include:

CartesianToInternal(vectorpInternalCoord arg0, OBMol mol)
InternalToCartesian(vectorpInternalCoord arg0, OBMol mol)
dot(OBVector3 a, OBVector3 b)
cross(OBVector3 a, OBVector3 b)

This is also where SWIG maps the values of macros and most enumerated constants from OpenBabel. For example the values in the OBGenericDataType enumeration are mapped to static fields of the openbabel class.

openbabel.PairData;
openbabl.RingData;
...


2) std::vector wrappers

SWIG generates wrapper it creates a class for each type of vector and array found in OpenBabel.
For example:

std::vector<string> -> vectorString.

These wrappers implement a type safe but non-generic IEnumerable. This means that foreach loops work without a cast but you can't call extension methods that target generic IEnumerables or use the copy constuctors of generic collection types. Use the Cast<T>() extension method to convert to a strongly typed IEnumerable<T>.


OBConversion obc = new OBConversion();
vectorString inFormats = obc.GetSupportedInputFormat();
List molFormats = new List(inFormat.Cast<string>());

foreach(string frmt in molFormats.Where(s => s.ToLower().Contains("mdl")))
...



3) The double_array class

This is only one array wrapper in OBDotNet. It uses the basic SWIG array template which does not implement ICollection, has no indexer, and is not enumerable. Items are manipulated using the getitem(int n) and setitem(int m) methods.

4) SWIGTYPE

If you examine OBDotNet you'll see a lot of class names that look like this:

SWIGTYPE_p_istream.

These represent places where the SWIG type map was not able to create a C# proxy for a C++ type. These placeholder classes cannot be instantiated and have no methods. Any method that takes or returns one these types is currently unusable.


OBConversion:

The classes you will work with most often are the OBMol, OBAtom, OBBond, OBVector3, and OBConversion.

We'll start with OBConversion. Instances of the OBConversion class are used for reading and writing structure files, parsing miles and InChI strings, and converting between chemical file formats. Open Babel supports a large number of file formats. In C++ a instance of OBConversion can be created using the following constructor

OBConversion(std::istream *is=NULL, std::ostream *os=NULL)

Currently OBDotNet does not have support for wrapping std:istream and std:ostream. As a result we see our first SWIGTYPE:

OBConversion(SWIGTYPE_p_std__istream,SWIGTYPE_p_std__ostream)

So we'll have to use the default constuctor. This stream issue is common to most of the bindings. As a work around the OpenBabel developers added the Read/WriteFile and Read/WriteString methods to the class. These allow you to read and write data without exposing a stream. The Read/WriteString methods can be used with the "smi" or "inchi" formats to parse smiles or strings.

Here is a first test program to check your OBDotNet install. It creates an OBConversion and calls the GetSupportedInputFormat() and GetSupportedOutputFormat() to display the available file formats.

example 1: displaying the supported file formats



You should get a list of 93 input formats and 96 output formats.


The next example demonstrates reading structure data from an sdf file and writing out smiles strings.

example 2: reading an sdf file



This usage suggests definitely suggests a pattern, so our final example is a facade class to simplify reading structure files.

example 3: a facade class for reading files



Now we can just write

OBMol m = OBReader.ReadMol("capsaicin.mol);

or

IEnumerable<OBMol> dataSet = OBReader.ReadFiles("someMols.mol");

Returning the IEnumerable<OBMol> allows developers to use LINQ to Objects for filtering data sets.

foreach(OBMol mol in dataSet.Where(mol => mol.GetMW() < 500))
...

We'll look more at LINQ when we discuss descriptors in part 3.

That's it for part 1. After reading this you should be able to use OBDotNet to create OBMol objects from data in files or smiles/inchi strings. Part 2 will look at the OBAtom, OBMol, OBBond, and OBVector3 classes as well as the use of enumerators.

33 comments:

Unknown said...

I'm having some troubles with getting OBDotNet to work and am hoping for some help. I have OpenBable-2.2.0 installed and am using VS 2008 sp1. I made the simplest program

using System;
using OpenBabel;

namespace OpenBabelTesting
{
class Program
{
static void Main(string[] args)
{
OBConversion obc = new OBConversion();
}

}
}
and when I run it I get this exception:
System.TypeInitializationException was unhandled
Message="The type initializer for 'OpenBabel.openbabelPINVOKE' threw an exception."
Source="OBDotNet"
TypeName="OpenBabel.openbabelPINVOKE"
StackTrace:
at OpenBabel.openbabelPINVOKE.new_OBConversion__SWIG_2()
at OpenBabel.OBConversion..ctor()
at OpenBabelTesting.Program.Main(String[] args) in C:\Projects\Constellation.HTS\ChemRegistration\OpenBabelTesting\Program.cs:line 10
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.TypeInitializationException
Message="The type initializer for 'SWIGExceptionHelper' threw an exception."
Source="OBDotNet"
TypeName="SWIGExceptionHelper"
StackTrace:
at OpenBabel.openbabelPINVOKE.SWIGExceptionHelper..ctor()
at OpenBabel.openbabelPINVOKE..cctor()
InnerException: System.DllNotFoundException
Message="Unable to load DLL 'openbabel': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
Source="OBDotNet"
TypeName=""
StackTrace:
at OpenBabel.openbabelPINVOKE.SWIGExceptionHelper.SWIGRegisterExceptionCallbacks_openbabel(ExceptionDelegate applicationDelegate, ExceptionDelegate arithmeticDelegate, ExceptionDelegate divideByZeroDelegate, ExceptionDelegate indexOutOfRangeDelegate, ExceptionDelegate invalidCastDelegate, ExceptionDelegate invalidOperationDelegate, ExceptionDelegate ioDelegate, ExceptionDelegate nullReferenceDelegate, ExceptionDelegate outOfMemoryDelegate, ExceptionDelegate overflowDelegate, ExceptionDelegate systemExceptionDelegate)
at OpenBabel.openbabelPINVOKE.SWIGExceptionHelper..cctor()
InnerException:

I thought it had something to do with openbabel.dll but I could not add it as a reference to my project or the GAC. Any ideas?

Thanks in advance,
Bob

mesprague said...

What's happening is that the managed assembly (OBDotNet.dll) can't find the intermediate unmanaged C++ dll (openbabel.dll) that handles interop with the OpenBabel libraries.
This is common problem.

The usual culprit is one of two things:

1)The environment variables described in the IronPython instructions are set correctly.

2) OBDotNet.dll and all of it's supporting files installed in a directory other than the project output directory but the COPY LOCAL property for the assmebly is set to True in VS.

If you're still having trouble, email more details about the installation directory. My address is listed in the my user profile.

-Matt

Mounika said...

It is better to engaged ourselves in activities we like. I liked the post. Thanks for sharing.
python Training institute in Pune
python Training institute in Chennai
python Training institute in Bangalore

sheela said...

Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
Devops Training in Bangalore
Microsoft azure training in Bangalore
Power bi training in Chennai

riya said...

Resources like the one you mentioned here will be very useful to me ! I will post a link to this page on my blog. I am sure my visitors will find that very useful
Data Science training in rajaji nagar
Data Science with Python training in chennai
Data Science training in electronic city
Data Science training in USA
Data science training in bangalore

Darshana M said...
This comment has been removed by the author.
Priyanka said...

Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
python training in bangalore

Training for IT and Software Courses said...

Excellent post for the people who really need information for this technology.

best sap oracle cloud training

best sap oracle cloud education &cerification suppor

best sap oracle cloud training cost

best sap oracle cloud careers

best sap oracle cloud erp education

best sap oracle cloud courses

best sap oracle cloud training institute

best sap oracle cloud course in bangalore marathalli

btm layout india

Anu said...

Thanks for sharing this information! I totally agree with you. Your information is very interesting and important. I really like this information.
devops training in chennai | devops training in anna nagar | devops training in omr | devops training in porur | devops training in tambaram | devops training in velachery

rocky said...

It's blog content is very nice. I'm really impressed and informative.
Python Training in Chennai | Certification | Online Training Course | Python Training in Bangalore | Certification | Online Training Course | Python Training in Hyderabad | Certification | Online Training Course | Python Training in Coimbatore | Certification | Online Training Course | Python Training in Online | Python Certification Training Course


deiva said...

Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info...
java training in chennai

java training in omr

aws training in chennai

aws training in omr

python training in chennai

python training in omr

selenium training in chennai

selenium training in omr

EXCELR said...

Thanks for sharing great information. I like your blog and highly recommendData Science Training in Hyderabad

praveen said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging
hadoop training in chennai

hadoop training in porur

salesforce training in chennai

salesforce training in porur

c and c plus plus course in chennai

c and c plus plus course in porur

machine learning training in chennai

machine learning training in porur

Jayalakshmi said...

Thanks for sharing this wonderful content.its very useful to us.I gained many unknown information, the way you have clearly explained is really fantastic.
hadoop training in chennai

hadoop training in tambaram

salesforce training in chennai

salesforce training in tambaram

c and c plus plus course in chennai

c and c plus plus course in tambaram

machine learning training in chennai

machine learning training in tambaram

jeni said...

Wow, amazing weblog format! How lengthy have you been running a blog for? you make running a blog look easy. The total glance of your website is wonderful, let alone the content!

angular js training in chennai

angular js training in velachery

full stack training in chennai

full stack training in velachery

php training in chennai

php training in velachery

photoshop training in chennai

photoshop training in velachery

shiny said...

Thanks for this helpful information I agree with all points you have given to us



hadoop training in chennai

hadoop training in annanagar

salesforce training in chennai

salesforce training in annanagar

c and c plus plus course in chennai

c and c plus plus course in annanagar

machine learning training in chennai

machine learning training in annanagar

Rohini said...

You actually make it look so easy with your performance but I find this matter to be actually something which I think I would never comprehend. It seems too complicated and extremely broad for me. I'm looking forward for your next post, I’ll try to get the hang of it!
data science course in hyderabad with placements

lavanya said...

Thanks for sharing.salesforce training in chennai

software testing training in chennai

robotic process automation rpa training in chennai

blockchain training in chennai

devops training in chennai

saketh321 said...

I have checked this link this is really important for the people to get benefit from. ExcelR Business Analytics Courses

Prwatech said...

Excellent content! Thanks for sharing such a useful information.

Big Data Training Institute in Pune
Big Data Analytics Training in Pune

Maneesha said...

I really like reading a post that can make people think. Also, thank you for permitting me to comment!|
data scientist training in hyderabad

Technogeekscs said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

Selenium Training in Pune

SixD Engineering Solutions said...

This is most informative and also this post most user-friendly and super navigation to all posts.

Point cloud to 3D Model Reading
Scan to BIM in Epsom
3d Laser Scanning Services in Massachusetts
Plant Engineering Services in California

traininginstitute said...

this is really nice to read..informative post is very good to read..thanks a lot!
data scientist training in malaysia

traininginstitute said...

Your work is very good and I appreciate you and hopping for some more informative posts
data science training

Maneesha said...

It was a wonderful chance to visit this kind of site and I am happy to know. Thank you so much for giving us a chance to have this opportunity..
data scientist course in hyderabad

Mallela said...

Thanks for posting the best information and the blog is very important.business analytics course in rajkot

360DigiTMG said...

Such a helpful article. Interesting to peruse this article.I might want to thank you for the endeavors you had made for composing this wonderful article.
data science classes in hyderabad

Pavithra Devi said...

This post is so interactive and informative.keep update more information...
Salesforce Training in Tambaram
Salesforce Training in Chennai

360DigiTMG said...

Very nice article, I enjoyed reading your post, very nice share, I want to twit this to my followers. Thanks!.
data analytics course in hyderabad

traininginstitute said...

Your work is very good and I appreciate you and hopping for some more informative posts.
data scientist course

Anonymous said...

great post , keep sharing valuable information Software Testing Classes in Pune

MeenaKK said...

Looking forward to reading more from your blog in the future. Keep up the excellent work!" Visit to Selenium Classes in Pune