Learn how to create a flashing LED box that reads a DNA sequence and flashes a different colour according to one of the four DNA nucleotides. You’ll need a Raspberry Pi (any model will do), an RGB led, three resistors and jumper wires (and breadboard) to wire it all up together!
How the idea came about…
I’ve been meaning to do a science oriented project with my Raspberry Pi for a while now, but with the existing components at hand, I was rather limited with what I could do. Most of the sensors in the starter kits are not very accurate, and the Raspberry Pi itself isn’t considered the most ideal device for logging accurate sensor data. The more purely electronics oriented Arduino boards are considered a better option for such projects. The idea for this project, surprisingly, came about whilst shopping at the home accessory store Tiger. Tiger does really nice trendy looking home furniture and accessories, sort of like IKEA, but cheaper and in more brightly coloured varieties. That’s where I saw one of there storage boxes which was made from a frosted, transparent plastic material. I brought it home and assembled it, and that’s when I had the idea of hooking it up to my Raspberry Pi and making a DNA flashing LED box!
What you’ll need
- Raspberry Pi (any model will do)
- Breadboard with jumper wires
- RGB LED
- 3x 470 Ω resistors
- Transparent and frosted storage box
- Alternatively, the Monk Makes Starter Kit provides all the components
Difficulty level: Intermediate
Once you have all the components, you’ll need to wire them as shown below. If you are using the Monk Makes Starter Kit, the positive leg of the LED is one of the middle leads. If you plug it into the breadboard as I’ve shown below, the lead on the right most is for red colours, the middle lead for green colour and the last for the blue colour. Please take note of which number GPIO pins you connect the wires to, as the code is set up for the pin numbers shown in the diagram. If you’re using a different LED, you’re wiring may differ. If you need some help with understanding what a breadboard is or how the GPIO pins are laid out, consult my first post on the alarm clock with a Raspberry Pi.
How will it work?
Let’s talk about how this is going to work! Our code, written in Python, will read a DNA sequence from a file and instruct the LED to flash a different colour according to the specific nucleotide it reads. Here’s where the science comes in! If you’ve ever done any basic bioinformatics work, you may have come across the .FASTA file type. This is essentially a text file, in which DNA or amino acid sequences are stored. Another popular format for storing biological sequences is the Genbank file format; however, we’ll be using the FASTA format as it’s much easier to parse (read) with a Python script. The structure of the FASTA file is really simple: the first line starts with a right pointing chevron followed by the name of the sequence as so:
>DNA. The next line will contain the DNA sequence, preferably in capitals. The idea of writing such a script that reads FASTA files means that you can simply download a DNA sequence for any organism, protein, etc. from sites such as the NCBI Gene website. Make sure your FASTA file has a DNA sequence in it, and not just amino acid sequences, as it won’t work for amino acid sequences. The reason is quite simply because there are 20 amino acids, and it would take much longer to code 20 different colours, not considering the fact that it may not be possible to make 20 distinct colours from the LED.
I’ve provided the code as two separate Python scripts. The first script,
bioseq.py, provides a function for parsing the FASTA file to extract the DNA sequence. The second file,
nu_blink.py, is the actual script which you need to run from the terminal. The code can be downloaded from my Github page here. The script for flashing the LED is coded to flash a bright purple for the adenine (A) nucleotide, blue for thymidine (T), green for cytosine (C), and cyan for guanosine (G). You can of course code in any colour for the nucleotides, provided you know their RGB value. To search for some nice colours, try Adobe Color CC or Color Hex. Color Hex is a pretty cool site to find nice shades of colours, particularly as they have the exact colours for popular brands such as the Twitter bird icon! Adobe Color, on the other hand, has an excellent selection of matching colour themes. If you find a nice colour you’d like to use, simply find the RGB value for the color. Replace the corresponding values in the code within the
colour.start() functions as follows for the color RGB (0,168,222)
elif nucleotide == 'A': red.start(0); blue.start(168); green.start(222);
If you’ve ever used an Arduino board, you’ll know that these boards can only take one script at a time and so launch the script immediately upon start up. Unfortunately with the Raspberry Pi, we’ll need a bit of extra coding for this! Here’s how you can get your scripts to launch on startup (code adapted from this Instructable):
Making the script run automatically
Firstly, create a folder on your RPi and place all three files within, those being
nu_blink.py and the .FASTA file (
RTT103.fasta). In my case, I’ve placed them in the following folder which I made
home/pi/python. Launch terminal from your RPi (through whichever method you wish, .e.g. over SSH or directly on the RPi) and type the following:
cd python nano launcher.sh cd / cd home/pi/python sudo python nu_blink.py RTT103.fasta cd /
nano launcher.sh will create a new file called
launcher.sh using the terminal program nano. To save the file once you’ve written the code, press
Ctrl-X, and then type
y followed by Enter to save the file.
The next step is to make the file an executable script. This is simply done with the command from terminal
chmod 775 launcher.sh. We will now make this script run at launch using crontab. At the terminal prompt, type
sudo crontab -e, which will open a new program. Use the arrow keys to navigate to the end of the file and add the following file:
@reboot sh /home/pi/python/launcher.sh
This line will ensure the script runs once upon bootup. Depending on the set up of your Raspberry Pi image, the script may take a while to start up. For example, if you SSH in to your RPi over the network, your RPi will wait up to 120 seconds until it can establish a network connection before booting up.
Putting it all together
If you can get yourself the Tiger storage box I mentioned, you can place the RPi inside the box, and provide a power source through a long USB cable and pass it through the small holes at the corners of the box where the side walls meet. Turn off the lights in your room, and you should get something like this:
Cool, isn’t it? Of course you might say that you can easily make this much easier using a looping script without the need of a DNA file. But then what cool story will you have to tell your friends when they come over and they see your unsuspecting flashing box???