Some xmgrace tips

Xmgrace is a very nice piece of software to create publication quality figures. Even better is that it stores your data and graph layout options as plain text (which is especially nice in combination with subversion or scripts to edit your graph).

But some things are not very intuitive, which is why I keep a list of some useful possibilities below:

  • Subscript, superscript
    x-squared: x\S2\N
    subscript: 3\s10\N
  • Greek letters, example: theta
    \f{Symbol}q\f{} or shorter: \xq\0q to get a theta symbol followed by the letter q: \x switches to the Symbol font and \0 switches back to Times-Roman.
  • Special symbols, example: Angstrom symbol
    \cE\C
    For other characters, look at this list: ascii table with low and high characters. Just use the character from the left column between \c and \C to produce the one from the right column. I highlighted the most interesting characters (for a scientist). The \c and \C option are listed as deprecated in the xmgrace manual., but what is the new way?. The new method to insert special characters in xmgrace is:

    • Press ctrl-e while positioned in a text-edit field to bring up the font dialog box.
    • Select the desired font from the drop-down list. You probably want to use Symbol because it contains many of the commonly used special characters.
    • Click on the character you want to insert
  • Saving the default settings for new graphs:
    open xmgrace, make the desired settings, save them as:
    ~/.grace/templates/Default.agr
    Unfortunately, this does not save the "print" settings, but see below.
  • Setting the default printer to print to .png files with 300dpi:
    create the file ~/.grace/gracerc.user and enter the following text:
    HARDCOPY DEVICE "PNG"
    DEVICE "PNG" DPI 300
  • Changing the definition of the default colors:
    Just edit the lines that say
    @map color 7 to (220, 220, 220), "grey"
    in the saved file. Edit the default file (see above) if you wish to use the new colors everywhere from now on.

See also the Grace users guide and the grace forums.

If you have more helpful hints, please post them in the comments, so that this blog post will become an interesting collection of tips that can be turned into a useful "cheat sheet".

Posted in linux, science | 55 Comments

Splitting a pdf-file under Linux

The pdftk command (pdf toolkit) can perform many operations on pdf files. One that I found useful is extracting certain pages from a pdf file. For example, to extract page 115-142 from input.pdf and save it as chapter5.pdf, the command would become:

pdftk input.pdf cat 115-142 output chapter5.pdf
Posted in linux | Leave a comment

Free SMTP server using gmail

How to use a free SMTP server that is accessible from anywhere, using your gmail account?
This may be useful if you are at a location where there is internet, but no (known) SMTP server as is the case for many public wifi networks, or when you are using your neighbour's wifi signal.

gmail smtp server details:

  • server: smtp.gmail.com
  • username: your gmail user name
  • password: the one from your gmail account
  • encryption: TLS
  • port: 465 or 587

If you want the email to appear to be sent from a different e-mail address than the gmail one, you can change this as follows:

  • log in to gmail
  • go to settings->accounts
  • add a new email address
  • set it as default address
  • choose the option: "always reply from default address"
Posted in internet | Leave a comment

Draw a helical wheel plot from a pdb file

Many programs are available that draw helical wheels from a given peptide primary structure (sequence), assuming a perfect alpha helix. Sometimes it may be interesting to see what the helical-wheel plot of a non-ideal helix looks like. Because I could not find such a program, I created one myself.

This script needs the PDL and SVG modules, and makes some assumptions as to how your pdb file looks. Please read below.

It is a Perl script that uses the PDL (perl data language) module for the calculations and the SVG module to generate the image. You should have Perl and these two modules installed to use it. The modules can be downloaded from http://www.cpan.org. You also need the Getopt::Long and Pod::Usage modules, but you probably already have those. I only tested it on Linux, but it should work on windows XP (and other versions) as well.

I have made the script available under the creative commons by-nc-sa licence, which means you may use and distribute it as long as you say you got it from me. And not for commercial purposes. If you publish any results, I would appreciate it if you mention my name in your article. At the moment there is no scientific publication related to this software.

click here to download (wheel.tar.bz2)
(but please continue reading for more information)

To learn how to use the program, you can take a look at the man-page with

./wheel.pl -m

The program goes through the following steps to draw the helical wheel:

  • Read the coordinates of the CA atoms from the pdb file
  • Find the principal axis of those atoms (the longest axis)
  • Move the centre of mass to the origin
  • Calculate the mass distribution tensor of the CA atoms
  • Calculate the eigenvectors and eigenvalues of this matrix (the matrix of eigenvectors is the rotation matrix)
  • Rotate the principal axis to coincide with the z-axis
  • Draw the xy-plane, which is the helical wheel plot

The numerical data for all steps is written to a log-file, in case you want to know what happened.

The output image will look similar to this (it is resized and converted to a png file here for web publishing):
helical wheel plot example
Because the program creates an SVG image, it may be edited and resized without losing quality. I recommend inkscape for this purpose.

It is important to realize that this program does not show the idealized helical wheel representation, but a projection of the CA atoms on the xy plane of the actual pdb coordinates. This is useful, but of course leads to images that "look less nice" than the idealized helical wheels. If a helix is curved, has a kink, or flexible termini, the output may not be what you expect. If this is the case, the principal helix axis that is calculated by the software does not lead to the "nice" representation you want to see, even though it is technically correct.

There are three ways I can think of to solve this problem:

  1. remove some amino acid residues (by using the -startres and -endres options of the program, or by removing them from the pdb file). But this may not be what you want.
  2. align (a part of) the helix to the z-axis manually (for example using molmol), and then use the -norotate option of the program. Although the helix will not be aligned to the principal axis, it may better represent the relative position of the amino acids as you want to show it (and still be correct).
  3. a combination of (1) and (2)

Please also note that the script makes several assumptions regarding the way your pdb file looks. If your pdb-file looks like the example below, it should work:

ATOM      1  N   LYS+    1       2.006  -6.797 -16.583  1.00  0.00
ATOM      2  H   LYS+    1       2.409  -5.881 -16.443  1.00  0.00
ATOM      3  CA  LYS+    1       1.934  -7.721 -15.428  1.00  0.00
ATOM      4  HA  LYS+    1       0.893  -7.948 -15.252  1.00  0.00
ATOM      5  CB  LYS+    1       2.689  -9.032 -15.717  1.00  0.00

At the moment, pdb-files that contain a chain identifier field cannot be read without making a small change to the source code of the script. The easiest way around this is probably to temporarily remove the chain identifier from your pdb file with your favorite editor (try a regexp in vi).

Feel free to contact me if you have any ideas, questions, problems or comments.

download (wheel.tar.bz2)

Posted in science | 6 Comments

Setting fuse-bits on Atmel Atmega8 AVR

The second thing most people do when they start programming microcontrollers is to try and use an external crystal as a clock source. And many beginners start by setting the wrong fuse bits, so that the AVR expects an external clock signal instead of a crystal, thereby locking themselves out. This may be a problem, because the fuse-bits cannot be changed back without actually connecting the external clock source the microcontroller expects.
I am no exception, and started out by setting the wrong fuse bits. It was however very educational, and I will describe below how I created the problem, and how I corrected it.

The first thing to do, is to read the current fuse bytes:

louic@carbon ~/myavr $ avrdude -c myavr -p m8 -P /dev/parport0 -U hfuse:r:high.bin:b -U lfuse:r:low.bin:b
 
avrdude: AVR device initialized and ready to accept instructions
 
Reading | ################################################## | 100% 0.00s
 
avrdude: Device signature = 0x1e9307
avrdude: reading hfuse memory:
 
Reading | ################################################## | 100% 0.00s
 
avrdude: writing output file "high.bin"
avrdude: reading lfuse memory:
 
Reading | ################################################## | 100% 0.00s
 
avrdude: writing output file "low.bin"
 
avrdude: safemode: Fuses OK
 
avrdude done.  Thank you.
 
louic@carbon ~/myavr $ cat high.bin low.bin
0b11011001
0b11100001

In hex, these values are 0xD9 for the high fuse byte, and 0xE1 for the low byte, the factory settings of my brand new atmaga8. My programmer board has a 3.6864MHz quartz crystal that is connected to the XTAL inputs, and the atmega8 datasheet will tell us the appropriate fuse bit settings.

Now that I write this I do not see why I made a mistake setting the fuse bits, but a fact is that I told the AVR that I have an external clock source by setting the low byte to 0b11100111. The following error message was the result when I tried to upload a program to the microcontroller:

louic@carbon ~/myavr $ avrdude -p m8 -c myavr -P /dev/parport0 -U flash:w:prog.elf:r
avr-objcopy: --change-section-lma .eeprom=0x0000000000000000 never used
 
avrdude: AVR device not responding
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.
 
avrdude done.  Thank you.

You're welcome. After a long search on the internet (and some experimenting) I succesfully used a second microcontroller as an external clock source, so that the controller with the wrong fuse-bits could be reprogrammed.

First, I removed the external crystal and the two capacitors. Then I connected one of the output pins of a second microcontroller to the XTAL1 pin of the "broken" atmega8. Nothing should be connected to XTAL2. Both of the microcontrollers have to be connected with their GND pin to the same ground, and to a power source of course. The working microcontroller should be programmed to send pulses with regular intervals on its output pin. The program shown below is fine:

#include <avr/io.h>           //Required to use assembler commands
#define F_CPU 1000000UL  // set the internal clock speed of 1 MHz
#include <util/delay.h>
 
int main()
{
 
    DDRC = 0xFF;            //Make Port C output values
    PORTC = 0x00;           //Turn all output pins on port c off
 
    while(1) {
        PORTC = 0x01;
        _delay_ms(5);     //This delay is probably not necessary
        PORTC = 0x00;
        _delay_ms(5);     //This delay is probably not necessary either
    }
 
}

With this setup, the Atmega8 with the wrong fuse bits may be programmed as usual, because the other AVR now serves as external clock source. I started out by setting the default fusebits back:

louic@carbon ~/myavr $ avrdude -c myavr -p m8 -P /dev/parport0 -U lfuse:w:0xE1:m -U hfuse:w:0xD9:m
 
avrdude: AVR device initialized and ready to accept instructions
 
Reading | ################################################## | 100% 0.00s
 
avrdude: Device signature = 0x1e9307
avrdude: reading input file "0xE1"
avrdude: writing lfuse (1 bytes):
 
Writing |                                                    | 0% 0.00s ***failed;
Writing | ################################################## | 100% 0.02s
 
avrdude: 1 bytes of lfuse written
avrdude: verifying lfuse memory against 0xE1:
avrdude: load data lfuse data from input file 0xE1:
avrdude: input file 0xE1 contains 1 bytes
avrdude: reading on-chip lfuse data:
 
Reading | ################################################## | 100% 0.00s
 
avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
0xe1 != 0x00
avrdude: verification error; content mismatch
 
avrdude: safemode: lfuse changed! Was e1, and is now 0
Would you like this fuse to be changed back? [y/n] y
avrdude: safemode: and is now rescued
avrdude: safemode: hfuse changed! Was d9, and is now 0
Would you like this fuse to be changed back? [y/n] y
avrdude: safemode: and is now rescued
avrdude: safemode: Fuses OK
 
avrdude done.  Thank you.

Because of all the error messages, I tried again, just to check if the fuse-bits would be unchanged this time (they should be, because I just set them):

louic@carbon ~/myavr $ avrdude -c myavr -p m8 -P /dev/parport0 -U lfuse:w:0xE1:m -U hfuse:w:0xD9:m
 
avrdude: AVR device initialized and ready to accept instructions
 
Reading | ################################################## | 100% 0.00s
 
avrdude: Device signature = 0x1e9307
avrdude: reading input file "0xE1"
avrdude: writing lfuse (1 bytes):
 
Writing | ################################################## | 100% 0.00s
 
avrdude: 1 bytes of lfuse written
avrdude: verifying lfuse memory against 0xE1:
avrdude: load data lfuse data from input file 0xE1:
avrdude: input file 0xE1 contains 1 bytes
avrdude: reading on-chip lfuse data:
 
Reading | ################################################## | 100% 0.00s
 
avrdude: verifying ...
avrdude: 1 bytes of lfuse verified
avrdude: reading input file "0xD9"
avrdude: writing hfuse (1 bytes):
 
Writing | ################################################## | 100% 0.00s
 
avrdude: 1 bytes of hfuse written
avrdude: verifying hfuse memory against 0xD9:
avrdude: load data hfuse data from input file 0xD9:
avrdude: input file 0xD9 contains 1 bytes
avrdude: reading on-chip hfuse data:
 
Reading | ################################################## | 100% 0.00s
 
avrdude: verifying ...
avrdude: 1 bytes of hfuse verified
 
avrdude: safemode: Fuses OK
 
avrdude done.  Thank you.

To set the correct fuse bits, I referred to the atmega8 datasheet again.

  • For an external crystal, CKSEL3..0 should be set to a value between 1111 and 1010
  • For a 3-8MHz crystal, CKSEL3..1 has to be set to 111
  • And for any crystal, CKSEL0 = 1
  • To make sure it would work, I chose the longest startup time (65ms): SUT1..0 = 11
  • Because the crystal has a speed lower than 8MHz, CKOPT=0 and CKOPT=1 should both work, see the comments on this blog post. I chose to set CKOPT=1

Knowing this, one wonders what the values of the high and low fusebytes have to be. The answer is again found in the datasheet:

hfuse:
bit             7     6     5     4      3       2       1       0
name     RSTDISBL WDTON SPIEN CKOPT EESAVE BOOTSZ1 BOOTSZ0 BOOTRST
 
lfuse:
bit             7     6     5     4      3       2       1       0
name     BODLEVEL BODEN  SUT1  SUT0 CKSEL3  CKSEL2  CKSEL1  CKSEL0

Combining this with the information above, we now know that we want to program the following bits:

hfuse: 0b11011001 (0xD9), so no change here
lfuse: 0b11111111 (0xFF)

And the avrdude command becomes:

avrdude -c myavr -p m8 -P /dev/parport0 -U lfuse:w:0xFF:m

After having put back the crystal and capacitors, I set the fuse-bits to their correct values, and the microcontroller worked perfectly again, now using the speed of the external crystal.

Posted in atmega | 34 Comments

Installing avrdude on a Gentoo box

This post describes how to install avrdude on Gentoo Linux. Avrdude is software to "burn" a program on a microcontroller. I am using a MyAVR programmer board that is connected to the parallel port. I started out by following the (gentoo specific) instructions on this website, but got some error messages.

Fortunately, the solution to this problem is described on the gentoo-forums. So I looked up my gcc-version and used it as a command line parameter for crossdev:

louic@carbon ~ $ gcc-config -l
 [1] i686-pc-linux-gnu-3.3.6
 [2] i686-pc-linux-gnu-4.1.2 *
 [3] i686-pc-linux-gnu-4.2.4
 [4] i686-pc-linux-gnu-4.3.1
 
louic@carbon ~ $ sudo crossdev -t avr --g 4.1.2

There were no more error messages. Crossdev creates a portage overlay and installs it right away, so now everything should be ok.

louic@carbon ~ $ sudo emerge -va avrdude

To set up avrdude to work with my microcontroller programmer (The MyAVR programmer), I needed to add an entry to /etc/avrdude.conf

programmer
  id    = "myavr";
  desc  = "MyAVR.de programmer";
  type  = par;
  sck   = 2;
  reset = 3;
  mosi  = 9;
  miso  = 11;
;

This entry describes the pin-layout, which you should be able to find in the manual that comes with the programmer. The "type = par" directive is used to tell avrdude that the programmer is connected to the parallel port. Time to test!

louic@carbon ~ $ avrdude -p m8 -c myavr
 
avrdude: AVR device initialized and ready to accept instructions
 
Reading | ################################################## | 100% 0.00s
 
avrdude: Device signature = 0x1e9307
 
avrdude: safemode: Fuses OK
 
avrdude done.  Thank you.

Cool! Let's program! How to compile programs and my first experiments with the Atmega8 AVR, will be described in different posts. Have fun!

Posted in atmega, gentoo, linux | Leave a comment

Installing DSL on an old (16Mb RAM) laptop

A couple of days ago, I removed the dust from my old laptop, and installed Linux on it. I chose to install the damn small linux (dsl) distro and encountered some (small) problems during installation, which I describe below.
Since my laptop does not have a cd-rom drive, I used a bootdisk I still had lying around from a previous version of dsl. I put the dsl image (dsl-4.2.5.iso) on a (FAT16) usb stick. Note that my old laptop cannot boot from usb, hence the bootdisk. After booting I got a segmentation fault, which was solved by booting with the following kernel options:

dsl lowram mem=16M vga=771

The vga=771 line was needed to get the display resolution right. After having booted successfully, I followed the harddisk installation instructions, but the dsl-hdinstall script did not run because it could not write to the /tmp directory. The /tmp appered to be a symlink to a ramdisk, but the ramdisk did not exist. This was easily solved:

rm /tmp; mkdir /tmp; dsl-hdinstall

DSL is running from my harddisk now (still using the lowram mem=16M boot options). I still need to configure a couple of things and check if all the hardware works. If anything interesting comes up, I will add it to this message.

Posted in linux | 4 Comments

Useful things in (gentoo) linux

This is just a collection of some commands and tricks that I keep forgetting

  • Search and replace in multiple files, using perl
    perl -p -i -e 's/search/replace/g *.txt
  • Change the color of a (gentoo) bash prompt
    export PS1="

    \033[36;1m

    \u@\h

    \033[01;34m

    \w \$

    \033[00m

    "

    \033[36;1m

    \u@\h

    \033[01;34m

    \w \$!\033[00m "

  • Operations on multiple files using bash (in this example: something interesting is done with every tex file in the current directory)
    for f in *.tex
    do
      echo "doing something interesting to file $f"
    done
  • Redirect both standard error and stdout to /dev/null
    2>&1 >/dev/null
  • Syntax highlighting in openoffice (for presentations, for example)
    Athough it seems a bit inefficient, this is the way to do it:

    • open the file in kate and export it as html
    • open the file in a web-browser and copy the text
    • paste the text in an openoffice writer document
    • save the file als .odt or export it as .rtf for use in impress
  • Change the display of the "top" command, and save it for next time
    • change the settings, for example like this: start top, press i, 1, d 1
    • press W to save the current settings to ~/.toprc
  • to use an end-of-line character to a vim search query: ctrl-V ctrl-M
Posted in gentoo, linux | Leave a comment

Convert a LaTeX-created .pdf file to MS-Word .doc

Unfortunately, sometimes people want me to convert some beautiful LaTeX to MS-Word. I first tried to convert the .tex to .html and open the html file in MS-Word, but the result was usually a big mess. The best way I found so far is described here:

  • Produce a pdf with pdflatex, omitting all figures, page numbers, line numbers, etc. Fortunately, it is easy in LaTeX to turn this all off.
  • Generate a pdf file from your *.tex sources as usual.
  • Use pdf2text to convert the pdf file to a UTF-8 encoded text file:
     pdftotext -enc UTF8 vermeer2008.pdf
  • Open this text file in MS-Word

Not everything is converted correctly:

  • You probably want to replace the ligatures ("ff", "fi" and "ffi") with normal letters (use search & replace).
  • The lay-out and font-settings are all lost (subscript; superscript, etc.).
  • Math will probably have to be re-written in the MS-Word equation editor. Although the equations could be added to the document as images, that is not the best solution if you want a "standard" MS-Word document as a final product.

Despite the many drawbacks, I found that this is the most reliable way to convert the document. It is a lot of work, but for me it worked better than any other method. The method described here is easier than using conversion programs that try to preserve formatting, because in my experience they only make a bigger mess. I am always willing to learn though, so if you know a better method please let me know!

Posted in latex, linux | Leave a comment