Friday, 8 June 2007

Minimizing an objective function using Python

Thanks largely to physicists, Python has very good support for efficient scientific computing. The following code shows how to use the brute-force optimization function of scipy to minimize the value of some objective function with 4 parameters. Since it is a grid-based method, it's likely that you may have to rerun the optimization with a smaller parameter space.

import numpy
import scipy.optimize

def my_objective_fn(params):
print params,
value_of_objectivefn = calc_objectivefn_value(params)
print value_of_objectivefn
return value_of_objectivefn

if __name__=="__main__":
# Method 1 (same number of steps for each parameter)
myranges = ((21,49), (0,0.1), (95,123), (1.6, 2.0))
scipy.optimize.brute(my_objective_fn, myranges, Ns=5)

# Method 2 (can have different number of steps for
# different parameters)

slice_obj = numpy.s_[21:56:7, 0:0.1:0.02,
95:130:7, 1.6:2.1:0.1]
scipy.optimize.brute(my_objective_fn, slice_obj)


A number of cleverer optimization functions are also available in scipy, including constrained optimization and stochastic methods. For more information, install scipy (on Windows or Linux), and type the following at the Python prompt:

>>> import scipy.optimize
>>> help(scipy.optimize)
DESCRIPTION
Optimization Tools
==================

A collection of general-purpose optimization routines.

fmin -- Nelder-Mead Simplex algorithm
(uses only function calls)
fmin_powell -- Powell's (modified) level set method
(uses only function calls)
...

Monday, 4 June 2007

Add to Connotea from Journal Pages

If you regularly read journal Table of Contents pages and want an easy way to keep track of papers you find interesting, you may want to try my "Add to Connotea" Greasemonkey script.



To use it, you first need to have a Connotea account. When you navigate to a journal web page for the first time after you have started your browser, you will need to enter your Connotea username and password.

The script adds an "Add to Connotea" link next to every DOI on journal pages. If you click on this, a dialog box appears that allows you to bookmark the paper on Connotea. A useful feature is the ability to quickly select from tags that you have already used. The "Add to Connotea" script also displays a count of the number of people who have already bookmarked a particular paper. If you click on this number, it will bring you to the Connotea page for that paper.

Why use Connotea inside of just bookmarking a paper, or saving it on your computer? Here's why I do: adding to Connotea is 'cheap'. I'm a lazy bookmarker; now that my bookmarks have reached the bottom of the my screen I've given up. Arranging into folders is too much hassle for my busy lifestyle. Now I can tag a paper with "Read me later", or "Journal club", and I will actually be able to find them later. Bookmarks are available from any browser; you can bookmark from home, and read the paper later from work. Your computer may explode (not due to my Greasemonkey script, I hope), but your bookmarks will still exist. Another nice thing is that Connotea is part of Web 2.0 - it provides an API that allows it to be used in mashups; like this Greasemonkey script!

There is also the 'social' side to 'social bookmarking'. I have already mentioned that the "Add to Connotea" script keeps a count of how many people have bookmarked a particular article; this may indicate that a particular article is worth reading. There are people who track all articles posted with particular tags, e.g. "evolution", to keep up to date with the latest articles, but I'm not sure how useful this is in chemistry. Also, you may want to keep up to date with what all the kool kids are reading, in which case you may want to track what articles your friends are bookmarking (note: all bookmarks are generally public, but you can opt to make them private). Connotea allows you to add a description and/or a comment to an article, but while this seems like a neat idea (e.g. for posting a reviews), it does not seem to be used that much and in fact, seems to suffer from link spam (see here).

I should point out that Connotea also provides a bookmarklet that you can click after selecting a DOI. While the bookmarklet is the only option for websites that my Greasemonkey script can't handle, I find my script a handier way to bookmark papers on journal websites, plus you have the added goodness of identifying popular articles.

Saturday, 2 June 2007

Open Babel Python module for Windows (1.2) released

Announcing the release of the OpenBabel module for Python, version 1.2, for Windows.

OpenBabel is a chemical toolbox designed to speak the many languages of chemical data. It's an open, collaborative project allowing anyone to search, convert, analyze, or store data from molecular modeling, chemistry, solid-state materials, biochemistry, or related areas. This Python module allows you to access this popular C++ library in your Python scripts.

New features:
(1) Data fields in file formats like MOL2 and SDF can now be accessed and edited
(2) Unit cell data in crystallographic file formats such as CIF can now be accessed
(3) A list of all detected input and output file formats can be accessed

Core features:
(1) Read and write over 80 molecular file formats
(2) Access molecular properties like molecular weight, formula, charge
(3) Daylight-type fingerprints and calculation of the Tanimoto coefficient
(4) SMARTS pattern matching
(5) Graph algorithms such as the Smallest Set of Smallest Rings (SSSR) and Depth-First Iteration over atoms

Documentation:
(1) The Python module
(2) The C++ toolkit
(3) The OpenBabel web site

Support:
(1) If you have any questions, send an email to openbabel-scripting@lists.sourceforge.net
(2) Report a bug

P.S. The Python module is also available for Linux and MacOSX. It is also possible to use OpenBabel from C++, Perl, Ruby and Java, although getting these to work on Windows may be more difficult.

Thursday, 17 May 2007

Stack It! - Popularity voting for papers

Prompted by a blog post by Ψ*Ψ which referred to a reddit-style voting system for papers, and based on the excellent "Stack It!" feature of Ohloh, I've implemented a Stack It for journal papers using Greasemonkey:


All you need is an account at Connotea. The first time it runs on a journal web page, it asks for your Connotea username and password - this will be remembered until you close the browser. To stack your favourite papers, just click "Stack It!". To unstack, you'll have to log into Connotea. The tag "stackit" is used for all stacked papers.

It should be possible to identify other people with similar stacks to yourself, and see what else they have stacked (Ohloh can do this). Also, you could have an RSS feed to what people are stacking recently, or you could have a list of the most popular JACS articles in the last month.

Wednesday, 16 May 2007

Framework for a commenting service

Over at Carbon-Based Curiosities there have been some interesting discussions on a social commenting service for journal papers. In addition, PMR has been discussing the merits and best methods to do standoff annotation of PubChem.

Here's a mashup method that would be suitable for both, and reuses existing technology so that only the glue needs to be written:
(1) Create a blog called http://commentsonpapers.blogspot.com
(2) Write a greasemonkey script (or Firefox extension) that adds a link "Comment on this" beside the title or DOI of the paper of interest
(3) When you click on the link, it creates a new blog entry for that paper or else brings you to the existing blog entry.
(4) You add your comments like in a normal blog, and click Save.
(5) The script in Step 2 should also flag any papers that already have comments

Advantages:
(1) Commenting is already well developed for blogs. There is anti-spam system in place. You can subscribe to comments on your own papers, etc.
(2) Moderation of comments is possible, although the entire chemical blogosphere would have to help out on this - no moderating of comments on your own papers, of course. :-)
(3) The Greasemonkey script for displaying comments from existing blogs does many of the same steps, and has shown that it *is* possible (not just pie in the sky). The chemical blogosphere is fewer than 100 people at the moment, whereas the potential commenters on papers is much larger.

Disadvantages:
(1) Users need Firefox, Greasemonkey and the Greasemonkey script.
(2) I think that it may require a standoff site (similar to Chemical Blogspace) to gather the list of existing comments and make it accessible to the Greasemonkey script via an API. However, I could be wrong. In any case, that is no big deal.
(3) The blog username and password would be contained in the Greasemonkey script for all to see. Can varying levels of permissions be set for different users of a blog? Alternatively, the blog entries could be created automatically based on RSS feeds from the journals, and so only the comments would be possible using the Greasemonkey script.
(4) Would probably need to develop a standoff backup service for comments, in the event of people deleting them. Not a big deal - possibly has already been done.

The main thing I'm worried about though is that this has already been done by Connotea. I'm not very familiar with Connotea - is that a better route for social commenting? I know that there are already some Greasemonkey scripts for Connotea - can you comment on a paper from a journal web page, and see existing comments?

Tuesday, 8 May 2007

Supporting information available as text

"Supporting information is available for this article as text files. See below for the links."

Providing datasets as text files is much more useful than making them available as PDFfiles (hamburger, anyone?). Recently a colleague of mine, Dave Palmer, published a QSAR model of the aqueous solubility of organic compounds. I'm not sure whether at the time it was possible to provide the supporting information as a text file. In any case, the test and training sets are available on the ACS website as two PDF files rather than as text.

Thanks to the magic of the Chemical Blogspace Greasemonkey Script I can alert anyone who visits the journal website that the supporting information is now available as text files.



Supporting Information:
Training Set
Test Set

Tuesday, 1 May 2007

Add quotes from PostGenomic and Chemical Blogspace to journal

Update 18 July 07: Updated address of Blue Obelisk wiki

Greasemonkey is a Firefox extension that allows you to rewrite the HTML of a webpage on-the-fly. Pedro Beltrão was the first to think of adding a link to journal Table of Contents pages whenever a particular paper had been reviewed on PostGenomic.com. I extended Pedro's script to include a clickable pop up of the actual blog post as described by Egon.

I have just released a new version, described on the Blue Obelisk wiki and available from User scripts. This incorporates comments from both Postgenomic and Chemical Blogspace, although you can use the menu to choose just one or the other.

Feedback is welcome. In particular, what journals would people like to see added? Currently, only the following websites are included, although others may work if you add them (please let me know if they do):
  • http://pubs*.acs.org/*
  • http://www.rsc.org/*
  • http://www*.interscience.wiley.com/*
  • http://www.nature.com/*
  • http://*.oxfordjournals.org/* (Added 01/May/07)
  • http://*.plosjournals.org/*
  • http://www.pnas.org/*
Here's the obligatory screenshot showing a recent issue of Nature containing quotes from both Chemical Blogspace and Postgenomic: