Importance of list comprehensions in Python

A beginner to python programming is usually taught to use for loops to do a lot things. The temptation  is to bust out a for loop whenever you need to modify a list or string object, but this quickly leads to complex “loops within loops” code architectures that are hard to read (by humans), even though they may work OK.

A simple example:

>>>test_list = [2,4,6,8]

>>>for x in test_list:

…     new_list.append(x  + 1)

>>>new_list

[3,5,7,9]

A better approach is to take advantage of Python’s built-in list comprehension expressions, with the form ‘x for x in y’.

Example:

>>>new_list = [x+2 for x in test_list]

>>>new_list

[4,6,8,10]

This can be expanded to include conditionals, for example:

>>>stripped_list = [line.strip() for line in line_list if line !=””]

You can also loop over multiple elements like this:

>>>seq1=’abc’

>>>seq2=(1,2,3)

>>>[(x,y) for x in seq1 for y in seq2]

[(‘a’,1),(‘a’,2),(‘a’,3),(‘b’,1),(‘b’,2)….(‘c’,3)]

Important to use deuterated buffers in small molecule NMR

One way to make your life massively easier if you are doing NMR of small molecules, especially at low concentrations (sub-1mM), is to simply work out what buffer you’d like to use and then order all of the components in deuterated form ahead of time.

For example, if you would like to study your molecule in a buffer like HEPES with 5% DMSO, you can order fully-deuterated HEPES and DMSO from companies like CIL and Sigma-ISOTEC.  Although expensive, the time it can save you at the spectrometer and the enhanced quality of the data are likely worthwhile tradeoffs.

You can also go a step further and prepare your buffers in 100% D2O, making water suppression vastly easier and improving the quality of your spectra. These steps work together in a synergistic manner to dramatically improve your data quality when acquiring on small molecules at low concentrations.

Using R to create a dotplot with jittered x values

If you need to create a plot where you have a several groups of data that you want to distribute along the ‘y’ axis, but bin into one of several categories in x then you can do the following:

1) create a .csv file with your data in columns (you can use headers)

2) import the .csv file into R with: TEST <- read.table(“yourfile.csv”, sep=’,’, header=TRUE)

3) do the dotplot: dotplot(values ~ ind, data=stack(TEST), jitter.x=TRUE)

The important point here is the use of the “stack” function.  This converts vectors into factors; it also lets you create the type of dotplot where the data is plotted along ‘y’ while having the same ‘x’ value.

Saving current shims for an automation run

Here is an important tip if you are setting up an automation run using Bruker’s ICON-NMR software.  Before the run, lock and shim on your first sample.  Once you have a very good shim, write your shim settings (‘wsh’) to a new shimset called “automation.”  If you specify the “automation” shimset in the gradshim menu as the one to be used by ICON,  it will use this shimset as a starting point for automated shimming before each sample rather than a default shim.