25
Python Project: DNA Visualization (with TUTORIAL)
Hey, do you want to make projects that make you stand out in your career and to make your craft to the next level!. This tutorial will help to achieve what you want.
Today, we're making a DNA (Deoxyribonucleic Acid) visualization which will look like this!.
Before you jump into the project read this!
DNA is a tiny molecule that exists in every cell of our bodies and contains the blueprint for how our bodies grow.
It looks like a double helix(a twisted ladder a like) of pairs of nucleotide molecules:
- guanine
- cytosine
- adenine
- and thymine
In this project these are shown like this:
- Guanine = G
- Cytosine = C
- Adenine = A
- Thymine = T
This program is a simple animation of DNA
This program create a scrolling animation by printing the strings from the ROWS list. The AT and CG pairs are inserted into each string with the format() string method.
First code then the "how" part
import random
import sys
import time
PAUSE = 0.15 # Change it 0.0 and see what happen
- Imported random, sys and time for the use. You can also write these in shorthand like
import random, sys, time
it will also work. -
PAUSE
!Try to change it 0.0 and see what happens when change it. First use 0.15 as default then change it.
# below are the rows of DNA animation
ROWS = [
' ##',
' #{}-{}#',
' #{}---{}#',
' #{}-----{}#',
' #{}------{}#',
' #{}------{}#',
' #{}-----{}#',
' #{}---{}#',
' #{}-{}#',
' ##',
' #{}-{}#',
' #{}---{}#',
' #{}-----{}#',
' #{}------{}#',
' #{}------{}#',
' #{}-----{}#',
' #{}---{}#',
' #{}-{}#',]
#123456789 use this to measure the number of spaces
-
ROWS
contain a list and in this a all the structure of the DNA is in a string. - Use this formula for better result
123456789
. - The first
##
at9
then going down. - At the tenth number
##
again starts but now going to right side rather left side.
try:
print('DNA Visualization || Ihtesham Haider')
print('Press CTRL-C on Keyboard to quit....')
time.sleep(2)
rowIndex = 0
#Main loop of the program || Started
while True:
#incrementing for to draw a next row:
rowIndex = rowIndex +1
if rowIndex == len(ROWS):
rowIndex = 0
# Row indexes 0 and 9 don't have nucleotides:
if rowIndex == 0 or rowIndex ==9:
print(ROWS[rowIndex])
continue
- If you're not familiar with
try
andexcept
, I will explain it to you in just a minute. - It is simply used for Error handling. The try block lets you test a block of code for errors.The except block lets you handle the error.
- while loop is the main loop which will be used for incrementing the string one after the other consecutively.
randomSelection = random.randint(1,4)
if randomSelection ==1:
leftNucleotide, rightNucleotide = 'A', 'T'
elif randomSelection ==2:
leftNucleotide, rightNucleotide = 'T', 'A'
elif randomSelection ==3:
leftNucleotide, rightNucleotide = 'C', 'G'
elif randomSelection ==4:
leftNucleotide, rightNucleotide = 'G', 'C'
# priting the row
print(ROWS[rowIndex].format(leftNucleotide, rightNucleotide))
time.sleep(PAUSE)
- This program will randomly select random nucleotide pairs, guanine-cytosine and adenine-thymine.
- For this purpose we have used
random.randit(1,4)
1 to 4 - Then we used
if elif
conditions to check the program for us and values at the right place. - After
conditions = if -elif
we printed out the row and then a time module is used which we have imported and used pause for it. # The time.sleep will add a slight pause.
except KeyboardInterrupt:
sys.exit() #This will end the program when click CTRL-C
- At last we use except for checking and executing the code.
- sys.exit() is used to end the program for us... whenever someone click CTRL-C on keyboard.
You can download or check the code by click on this DOWNLOAD
If you have any problem regarding programming and management I free for you to help you!.
Thanks! wish you Best Of Luck.
25