33
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.
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:
In this project these are shown like this:
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
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.123456789
.##
at 9
then going down.##
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
try
and except
, I will explain it to you in just a minute.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)
random.randit(1,4)
1 to 4if elif
conditions to check the program for us and values at the right place.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
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.
33