November 22nd, 2008Organic Chemistry – Reaction Simulator
Organic Reaction Simulator
It is a tool that simulates the organic reactions and automatically generate the IUPAC names for the organic compounds drawn by you. This covers most of the syllabus of the Organic Chemistry for G.C.E. A/L examination.
Features
- Nice Canvas to draw your Organic compounds.
- Generates IUPAC names for the drawn component as possible. (It generates IUPAC names for almost all the compounds which G.C.E A/L examination expect students to know)
- You can simulate reaction for organic compounds with preferred inorganic/organic compounds and the selected conditions. (Currently supporting 168 reactions)
- Extensibility of adding IUPAC naming rules without the need of recompiling the code.
- Extensibility of adding custom reactions, again without compiling a single line of code.
Here is a screen shot of the main panel when ‘Aldole reaction’ is simulated for some simple reactants.
The Drawing Panel
The ‘Reaction Simulator’ app provides 2 views for the user. One is the main panel which shows and manages reactions which is shown in the above figure. The other view is the drawing panel. It will be not much different from your favorite drawing application.
Here you can select elements and bond types to draw your organic compound. Additionally It has tools to move or delete selected parts of your drawing.
The other thing you may have already noticed, you don’t need to complete all the bonds for a particular element. Rather if you keep one side of a single bond unconnected, it will be automatically connected to a ‘H’(Hydrogen) at the rendering, similarly for double bond the default connecting element would be ‘O’(Oxygen), and for triple bond it is ‘N’ (Nytrogen). And you don’t even need to bother about connecting bonds with an element, as if you connected only one bond to ‘C’ all the other 3 possible bonds will be considered as connected with ‘H’s.
Just check the drawing panel on your own and find its user friendliness. Now after you finish designing your compound, just press “RETURN”.
Yea it will nicely render our compound, and look at the bottom, it has generated the IUPAC name for the compound.
The Main Panel
This contain a canvas that you can add, edit and delete organic compounds (so you will be directed to the ‘Drawing Panel’), some sidebar panels to select Inorganic compounds and additional conditions requires for the reactions and a set of buttons to start reactions and manage the result set. At the end of the sidebar there is a ‘help’ button that directed you to the user guide.
Extending IUPAC Naming Rules
If you go to the “Data/IUPAC/” directory (you can view svn from here, http://svn.dimuthu.org/organic_chemistry/Organic_Reaction_Simulator/Data/IUPAC/) you can find there are set of .txt files (infact xmls) that defines the IUPAC rules.
If you open one of them (we will take al.txt), it would be something like this
<IUPAC basename="al" subname="oxo" level="6" nonumber="true" affectto="1"> <bond type="2"> <element type="7"> </element> </bond> <bond type="1"> <element type="1"> </element> </bond> </IUPAC>
It is the naming rules for aldehyde compounds. (H-C=O). First it identify the the aldehyde by checking double bond to ‘O’ (which is the element type ‘7′), and a single bond to ‘H’ (which is the element type ‘1′). If the element group found, it will give the name ‘al’ or ‘oxo’ depending on whether it defines the main group of elements of the compound or not.
If you take a look at all these rules, you can have a good idea what each of these syntax mean. And may be you can add your own rules, if you find something missing or incorrect there.
Extending the Reactions
The reaction rules are stored in the “Data/Reactions/” directory ( http://svn.dimuthu.org/organic_chemistry/Organic_Reaction_Simulator/Data/Reactions/).
I will take the first reaction we studied in the ‘Organic Chemistry’ Class.
CH4 + Cl2 + hv (dim light) —————-> CCl4 + H2
Here is the rule defining that reaction. (cl2hv.txt)
<reaction inorganics="Cl2" conditions="hv"> <check> <bond type="1"> <element type="H"> </element> </bond> </check> <reordering activity="remain"> <bond type="1"> <check> <element type="H"> </element> </check> <reordering activity="remain"> <element type="H"> <reordering activity="replace" to="Cl"> </reordering> </element> </reordering> </bond> </reordering> </reaction>
If you take a close look at, what it says is if there exist inorganic ‘Cl2′ and condition ‘hv’ (reactions inorganics=”Cl2″ conditions=”hv”), check for a ‘H’ (element type=”H”) elements that is connected to a ‘C’ element (which is the root of the XML) through a single bond (bond type=”1″), then don’t replace the ‘C’ (reordering activity=”remain”) and don’t replace the bond type (reordering activity=”remain”), just replace the ‘H’ with ‘Cl’ (reordering activity=”replace” to=”Cl”). It is so simple as that.
These rules are applied to all the C elements in reactants. Look at the following figure for this rule in application.
These reaction rules can be as complex as you want. Specially when two or more reactants are involved in a reaction, rules defining that reaction will be little complex. Look at how aldole reaction (which is applied in the first figure of this post) is written in aldole.txt.
There are all together 168 reactions currently defined in this way. If you found some reaction is missing, feel free to add a another rule file defining that reaction. And if you want to share that with others, just let me know
, so I can integrate it in to the distribution.
Download
This software application is not yet in a official release. I just thought use this post to do a pre release of the ‘Reaction Simulator’. You can download the windows binary of the pre release version (343KB) from http://downloads.dimuthu.org/bins/chemistry/reaction_simulator_pre_release/reaction_simulator_pre_release.zip
Source Code
SVN location for the source code of ‘Reaction Simulator’ is http://svn.dimuthu.org/organic_chemistry/Organic_Reaction_Simulator/
Known Limitations – Possible Improvements
- Generation of IUPAC names and simulations of reactions are not supported for compounds which involves Benzene ring.
- IUPAC names generation is not supported for cyclic compounds which are anyway not part of the G.C.E. A/L syllabus.
- The set of elements, available in drawing compounds, set of inorganic compounds and conditions, available in reactions are fixed and cannot be extended without changing the code and recompiling.
- You can’t start with an IUPAC name and derive the compound. Currently you always have to start with drawing the compound and then generate the IUPAC.
- Only for windows!
Little Background
4 years ago, When I was a level 2 student in the University of Moratuwa, I participated to a competition for making educational software tools organized by C.S.E and N.I.E (National Institute of Education). I submitted a software that teaches Organic Chemistry. It consisted of interactive tutorials targeting local G.C.E A/L exams with exercises and revisions in both Sinhala(My Mother tongue) and English languages(not in Unicode though). I got the second price for that.
After the award I decided to improve my software application by adding an ‘Organic Reaction Simulator’. In fact in the vacation of 2 weeks for the New Year, I could complete it.
Although the N.I.E supposed to distribute the applications submitted to the competition throughout the country, it didn’t happened. So I decided to publish at least the ‘Reaction Simulator’ application which I actually didn’t submitted to the competition.
Technologies Used
This is completely written in C++. It hasn’t use MFC, because I thought MFC is too heavy for such a small application. I used a lightweight, small image library code taken with some custom changes from some windows game programming book.
This is using XML to load data about reactions and IUPAC names which I have described in details in the early part of the blog. It uses a small inbuilt xml parser (just one recursive function) to parse these xmls.
So it has no depenedencies to third party libraries, You can just chekcout the souce code from the svn, open the visual studio project and compile it (press ‘F7′).
July 28th, 2008Perl XML Parsers And My Story
Last week I had an opportunity to write some CGI scripts in Perl. It is like going few years back in web development. And it gave me the answer why PHP become favorite over Perl among the web developers. It is not just PHP’s C-like friendly syntax, but also the ability to write inline script in html may have been a big factor.
In there we came across parsing following type of XML.
<ns1:person xmlns:ns1="http://dimuthu.org/example/perl_xml/xsd"> <ns1:name>PQR XYZ</ns1:name> <ns1:age>25</ns1:age> </ns1:person>
We started to use XML::Twig. And it is pretty straightforward. Here was our code.
use XML::Twig; my $xml_str = <<E; <ns1:person xmlns:ns1="http://dimuthu.org/example/perl_xml/xsd"> <ns1:name>PQR XYZ</ns1:name> <ns1:age>25</ns1:age> </ns1:person> E my $xt = XML::Twig->new(); $xt->parse($xml_str); my $txt = $xt->root->findvalue("//ns1:name"); print $txt;
OK. It was working.
Anyway in practice we found that this is not the only way we receive the xml. That is the namespace prefix can be different. When you write an XML to an XML schema you are free to have your own prefixes for the namespaces. And in fact in practice different people, programs and vendors uses different prefixes.
So our program should be able to parse following XML too. (see the namespace prefix is changed)
<ns0:person xmlns:ns0="http://dimuthu.org/example/perl_xml/xsd"> <ns0:name>PQR XYZ</ns0:name> <ns0:age>25</ns0:age> </ns0:person>
And with the default namespace.
<person xmlns="http://dimuthu.org/example/perl_xml/xsd"> <name>PQR XYZ</name> <age>25</age> </person>
Both these occasions our code failed. And normally whenever there is an API to parse XPath, you can register namespaces. But XML::Twit there was no something like that. That made us to jump to use XML::LibXML. It is apparently little complicated than the simple Twig API. But it did work. (Look later for the code using XML::LibXML)
The story is actually there is a way to register namespaces in Twit, in fact it is not in XML::Twit, it is in the XML::Twit:XPath module which is hardly any documented. It just duplicate the Twit API adding some functionalities to deal with namespaces. Apparently not the most elegant way of designing an API. Anyway Here is the code that works written using XML::Twit:XPath.
use XML::Twig; use XML::Twig::XPath; my $xml_str = <<E; <person xmlns="http://dimuthu.org/example/perl_xml/xsd"> <name>PQR XYZ</name> <age>25</age> </person> E my $xtp = XML::Twig::XPath->new(); $xtp->parse($xml_str); $xtp->set_namespace('ns', 'http://dimuthu.org/example/perl_xml/xsd'); my $txt = $xtp->root->findvalue('//ns:name'); print $txt;
Note that I have register the namespace to the prefix ‘ns’, so in xpath quires I can use this prefix to refer namespaces.
Anyway Perl is not bad, it has dozens of modules to do the same thing. So just for the reference I will note down it here,
Using XML:XPath,
use XML::XPath; my $xml_str = <<E; <person xmlns="http://dimuthu.org/example/perl_xml/xsd"> <name>PQR XYZ</name> <age>25</age> </person> E my $xp = XML::XPath->new(xml => $xml_str); $xp->set_namespace('ns', 'http://dimuthu.org/example/perl_xml/xsd'); my $txt = $xp->findvalue('//ns:name'); # get name print $txt;
Then Using XML::LibXML;
use XML::LibXML; my $xml_str = <<E; <person xmlns="http://dimuthu.org/example/perl_xml/xsd"> <name>PQR XYZ</name> <age>25</age> </person> E my $xl= XML::LibXML->new(); $xml = $xl->parse_string($xml_str); my $xpc = XML::LibXML::XPathContext->new($xml); $xpc->registerNs('ns', 'http://dimuthu.org/example/perl_xml/xsd'); my $txt = $xpc->findvalue('//ns:name'); # get name print $txt;
So it is all for this post, And one reason I didn’t tell you earlier and so obvious why PHP is popular over Perl. Look at http://php.net/dom or http://php.net/simplexml. It has a great documentation for every function to the every needle. Whenever Perl, Ruby thinking of going pass PHP, they have to consider this aspect too more seriously.
July 19th, 2008Welcome to dimuthu.org
I have been blogging in wordpress http://dimuthuc.wordpress.com and blogspot http://dimuthuc.blogspot.com for sometime now. And I have been blogging a lot around my working area from http://phpwebservices.blogspot.com.
Still I m not a regular blogger. And I dont think having my own blog with my own domain name will do any different
. But it s always worth do a try. Now I can install my favorite software and favorite plug-ins, themes and customize them to the full extend. Lacking of these features were the main reason I moved here and there before this. So I hope I will do more blogging in here.
So what will I blog? I have had some hits for my blogs on Axis2/C, WSDL2C codegen tool, WSF/PHP and web services areas. And definitely I will continue blogging more and more on that space. And I should update the blog with the interesting things I learn from the work and studies that worth to share with everyone.
Hope to see you through many blogs!





