My first object oriented code:A set of python classes to create dispense lists for the Formulatrix liquid handler robot
Jan 28th, 2009 by harijay
Over my few years writing scripts in Python or Perl , I always told myself that the next time I had to solve a problem I would make my code more object oriented and re-useable. Like an addict trying to break a habit , I always failed miserably. It was just too easy to just write one giant script to get the job done. So much so that when I started writing this blog almost two years ago , I once again stated my desire to break this habit. Also along these lines, I always felt I should be using some form of version control. Even for a script it made sense to be able to experiment with code and go back and forth on a source tree. But again I just did not have the time to do any of that!
I am quite happy to say that I have finally managed to write an object oriented set of python classes that create dispense lists for a liquid handling robot . Also I went ahead and used git for version control all along the way.
The problem I was solving was fairly routine . I had to create an easy software routine to create dispense lists for the Formulatrix liquid handling robot in the lab. Each dispense list was a list of volumes of the robot had to dispense to each well of a 96 well plate.
Crystal screens typically keep some components constant across a 96 well plate while varying some others systematically. Therefore the dispense list creator code had to deal with creating gradients of components along the two axes of the plate and also handle component staying constant . Regardless the final output is a tab delimited set of volumes that the robot then uses to carry out the dispense. Imagine a 96 character array with volume for each well and one array for each component.
Like all object oriented approaches tell you , it helps to model the problem space appropriately. This involve defining all the actors in the problem you are trying to solve and writing appropriate classes. For me that was the hardest part. Object oriented analysis and design is Hard! But I persisted in the good faith that the object orientation would pay off , and once I modeled my classes appropriately the code would just write itself .
As my code grew . Adding new features became easier than it ever had been when I was writing imperative linear scripts. Also thanks to some timely and excellent help from Michael Foord and Chris Lasher delivered over twitter and a blogpost , I could use the object oriented nature of the code to quite some advantage . Incidentally Michael Foord writes an excellent pythonic blog at voidspace,is one of the main developers at Resolver Systems and the co-author of IronPython in action and Chris Lasher is on the Bio-python team and an avid Bioinformatician who I got introduced to thanks to Atom.
The code that creates these dispense lists and the examples are posted on github . Take a look and let me know how I did with the design aspect of my classes . The most exciting moments of writing object oriented code is when you realize that adding functionality comes so much easier than procedural code . I am thinking in terms of writing a GUI to wrap the functionality of the code in additon to a web app/form based CGI frontend. Any help and suggestions are welcome.
The icing on the cake was when the code actually worked and I now have a few custom screens with multiple gradients hopefully growing giant membrane protein crystals.
Now onto writing some GUI code.
[...] decided to start playing with git. The fun part was when I could see myself coding a lot more and writing code that even I reused. Another conversation had us talking about Living code , about how Paul Buchheit built gmail and [...]