The Automatic Labelling Machine

Version 2.5.3 introduced the console command text. With it you can create hundreds of labels with a single script, saving a considerable amount of time. Here we describe a real life example (courtesy of Prof. Antonio Randazzo, University of Naples) that makes the concept clear. You are invited to download the accompanying material which includes an array of 78 1H spectra, a Lua program (that you can adapt to your personal needs) and a table.

The spectra have already been combined into a pseudo-2D matrix (with the command Process/Process Folder). The file “labels.doc” is a Word document that contains the names of the sample corresponding to each spectrum (row). Such a list is organized in two columns: at left a progressive number (the position of the row, starting from the bottom of the matrix), at right the label to attach.

I manually selected the whole content of the table, copied into the clipboard and pasted into a text editor (I prefer TextWrangler, but Smultron is also great). This is how I created the file “AttachLabels.lua”. Before the labels I then inserted:

NumberedList = [[

another couple of ]] closes the string at the bottom. This is one of the ways in which you can define a string in Lua. Yes, you can define strings of any length! “AttachLabels.lua” is the Lua program that writes the labels on the spectrum. It is divided into three parts. The first one, already seen, defines the text as a string. The second part extracts 78 labels and stores them into a table called “Label”. The third module does the actual work. if you want to recycle the script for your own purposes, each of the modules will need re-editing, but splitting the task in three makes recycling easier anyway. For example you can generate tha table of labels in a completely different way, then jump to the last module. If your own list of labels does not contain the progressive numbers, then less work is required to parse it (the second module can be greatly simplified).

The usual way to run a script is to put it into your own special Scripts folder, before launching iNMR. From that moment on, the name of the script will appear under the Scripts menu of iNMR. That's all. To change the font of all the labels, first use the “Select All” command, then open the Font Panel. Under the same “Format” menu you'll find the command “Levels & Colors” to change the color. The first operation is already included into our script, with the instruction: font().

The program “AttachLabels” is commented in detail, so there's no need to comment it here again. The advantage of programming with an interpreter is that you don't pay particular attention to planning and form. Our script is a quick and dirty, real-life, way to have the job done, with little elegance but in the shortest amount of time. A more elegant solution is provided below. The most pleasant way to learn programming is by doing it. Edit the script and try to change the position of the labels along the x axes; then run the altered script to see the effect (possibly after removing the old labels!).

A more elegant solution

Instead of storing the data into the program itself, we should have stored them into its own file residing, for example into your home directory “/Users/Tom/”. It is a good idea to define HOME into your own “Lua.init” file. In this way there is no need to edit the program. We also suppose that the variable filename holds the file name. The first two modules would be greatly simplified:

spectral = getf("y")
Label = {}
for i = 1,spectral.size do "*line"			-- we skip the numbers
	Label[i] = "*line"	-- and store the labels

Another case

Using the same ingredients, you can start from two tables (one with chemical shifts and another with labels) and write each label at the corresponding chemical shift, on the spectrum. Here is an example of how it can be done:

fontsize = 12				-- you can choose a different size
font( "Geneva", fontsize )		-- you can also choose a different font
room = (fontsize + 8) * amp()		-- room for the label
-- amp() returns the equivalent of 1 pixel in the unit that we are going to use

for i = 1, NumberOfLabels do 
	x = ppm[i]			-- ppm is the list of frequencies
	y = hat( x )			-- peak height
	text( Label[i], x, y + room )

Copyright © 2005-2021 nucleomatica
Valid XHTML and CSS. UTF-8 encoding.